How to fix your local trackbacks ?

Monday, 16 March, Year 7 d.Tr. | Author: Mircea Popescu

If you're anything like me, you keep a blog like the sum total of human knowledge, and use newer articles to build upon older articles. In this approach to writing, the fact that newer articles send trackbacks to older articles is very useful, because the list of trackbacks on an older article can for instance indicate the more important nodes to the reader, among other benefits. As the blog grows this accumulated set of notations becomes quite invaluable.

Obviously Wordpress does a shit job of this task as it does a shit job of any other task. In this case, it will simply omit, for no apparent reason, to send some trackbacks. Notwithstanding that they're going from your own blog on your own server to your own blog on your own server, a good chunk get lost en route (imagine what happens when there's an actual internet between source and destination!). Pretty much the only guarantee Automattic offers is that if you write an article F with links to articles A, B, C, D and E, only some of A, B, C, D and E will receive the trackback. Certainly not all, and there's no good way to tell which. Talk about code that's poetry!

To fix this, you have to delve into the perl :

  • Step 1. Traverse the database to produce a list of pingbacks that weren't properly sent.
    • <?
      
      // Index of post at which script last ran. Script won't look
      // through earlier posts. You'll have to update the value yourself.
      $last_run = 0; 
      
      // Db connect data.
      
      $db_name = '';
      $db_user = '';
      $db_pass = '';
      
      $table_prefix  = '';
      
      $nconnection = mysql_connect("localhost", $db_user, $db_pass );
      mysql_select_db($db_name, $nconnection);
      
      // Part one : select all the posts that contain a link to your own blog.
      // Replace the url with your own.
      $local = "http://trilema.com/";
      
      $query = 'SELECT YEAR(post_date), post_name, post_content FROM '.
      $table_prefix.'posts WHERE post_type ="post" AND post_content LIKE
      "%<a href=%" AND ID > '.$last_run;
      $record = mysql_query($query);
      
      while (	$row = mysql_fetch_array($record, MYSQL_NUM)) {
        $post_url = $local.$row[0]."/".$row[1];i
      
        $dom = new DOMDocument();
        @$dom->loadHTML($row[2]);
      
        $xpath = new DOMXPath($dom);
        $hrefs = $xpath->evaluate("/html/body//a");
      
        for ($i = 0; $i < $hrefs->length; $i++) {
          $href = $hrefs->item($i);
          $url = $href->getAttribute('href');
      
          $parse = parse_url($url);
          echo 'curl -A "Mozilla/5.0" -r 0-4096 --connect-timeout 30 ';
          echo '--max-time 10 "http://';
          echo $parse['host'];
          echo '/xmlrpc.php" --header "Content-Type: text/xml" --data ';
          echo '"<?xmlversion="1.0"?>'
          echo '<methodCall><methodName>pingback.ping</methodName>';
          echo '<params><param><value><string>';
          echo $post_url;
          echo '</string></value></param><param><value><string>';
          echo $url;
          echo '</string></value></param></params></methodCall>"'."\n";
        }
      }
      ?>

    This will output a lengthy list of curl commands. You probably want to save it as a file on your server, say fix_trackbacks.php, after which you can call it from command line, perhaps with something like

      $curl http://your.domain/fix_trackbacks.php >> fix_trackbacks.sh

    (Make sure you edit the values of $db_name, $db_user, $db_pass and $local.)

  • Step 2. Connect to the server (via ssh), set the executable bit on the .sh script you just created ($chmod +x fix_trackbacks.sh) and then execute it (./ fix_trackbacks.sh >> result.txt).
  • Step 3. Once you're done, don't forget to set the $last_run variable to the ID of your latest post, so you don't have to go hunting for the correct value whenever you remember you need to run this again next. Or, alternatively, you could simply automate its function.

And in this manner, a very short half hour to an hour later, you'll be exactly in the position you should have been from the ver ybeginning. If only Computer Science weren't afflicted with the terrible curse of playing social security net for the kids that aren't smart enough to sling dope.

PS. There's probably a less... kludgy way to do this same thing, but I'll be damned before I'm paying any "Wordpress expert" a bent nickle, and I'll be triple damned and twice hexed before I'm paying any actual engineer to delve into the depths of that pile of encoded idiocy and figure out how the data structures work. It's a whole world of duct tape and chewing gum out there!

———
  1. Note that if your url scheme isn't domain/year/article like Trilema's, you'll have to fiddle with this string a little. []
Category: Meta psihoza
Comments feed : RSS 2.0. Leave your own comment below, or send a trackback.
Add your cents! »
    If this is your first comment, it will wait to be approved. This usually takes a few hours. Subsequent comments are not delayed.