<?php
// Part of my ugly hack for making trackbacks to TopicExchange easier to do and more visible on the weblog
// 18.07.04 Stephanie Booth aka bunnywabbit_ on #wordpress -- http://climbtothestars.org/ for questions or fixes
// A patch to add meta fields for already existing Topic Exchange trackbacks -- stick in wp-admin and load in browser.
// run it only once or it will insert duplicate meta keys. If you're in a mess, try this query to start fresh: DELETE FROM `wp_postmeta` WHERE meta_key = 'ite_topic'
// see debugging notes below. Will spew out warnings but still do what we want it to do: add meta keys for existing trackbacks to topic exchange.

require('admin-functions.php');
require(
'../wp-config.php');
include(
ABSPATH.'wp-blog-header.php');
update_ite_meta();




// check all trackbacked uris and make sure they exist as meta info
function update_ite_meta()
{
global 
$wpdb;
global 
$tableposts;
  
// pull out all posts (rude, but we will only be running this once, normally
  
$posts $wpdb->get_results("SELECT * FROM $tableposts");
  
$posts=array_reverse($posts);
  
// go through the posts to insert all our TE trackbacks as meta
 
foreach ($posts as $post)
 {      
start_wp();  
        
$post_ID=$post->ID;
        
        
// the channels we have already recorded *** for some reason this doesn't retrieve different values -- only "wordpress" for me
        
$channels=get_post_custom_values("ite_topic");    
        
$channels=array_unique($channels);
        
// debugging
        
var_dump($channels);
        
// the trackbacks we have pinged
        
$pinged $post->pinged;
        
$pinged explode("\n"$pinged);
        foreach (
$pinged as $tb_pinged
        {
                
// does this match a TE channel?
                
if(preg_match("|topicexchange\.com\/t\/(.*)\/?|"$tb_pinged$topic_array))
                {
                    
$topic=$topic_array[1];
                    
$topic=str_replace('/'''$topic);
                    
// check it is not already present in post2meta
                    // for some reason, this check is not working. values will be inserted multiple times if the patch is run multiple times.
                    // debug
                    // print_r($channels[0]);
                    // make sure $channels is an array
                    
if(!is_array($channels))
                    { 
                        
$channels[]=$channels;
                    }    
                    if(!
in_array($topic$channels))
                    {
                        
$_POST['metakeyselect'] = 'ite_topic';
                          
$_POST['metavalue'] = $topic;
                        
add_meta($post_ID);
                        print(
"Adding meta for $topic on post $post_ID\n");
                    }
                } 
// end if preg match
    
        
// end foreach pinged
    
// end foreach post

// end function

?>