Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, Swift, BASIC, and whatever else I happen to feel like hacking at.

42 Astoundingly Useful Scripts and Automations for the Macintosh

Work faster and more reliably. Add actions to the services menu and the menu bar, create drag-and-drop apps to make your Macintosh play music, roll dice, and talk. Create ASCII art from photos. There’s a script for that in 42 Astounding Scripts for the Macintosh.

Parsing JSKit/Echo XML using PHP

Jerry Stratton, October 29, 2012

According to dpusa in the comments, you can manually insert comments into WordPress using something like:

[toggle code]

  • $data = array(
    • 'comment_post_ID' => 256,
    • 'comment_author' => 'Dave',
    • 'comment_author_email' => 'dave@example.com',
    • 'comment_author_url' => 'http://hal.example.com',
    • 'comment_content' => 'Lorem ipsum dolor sit amet...',
    • 'comment_author_IP' => '',
    • 'comment_agent' => 'manual insertion',
    • 'comment_date' => date('Y-m-d H:i:s'),
    • 'comment_date_gmt' => date('Y-m-d H:i:s'),
    • 'comment_approved' => 1,
  • );
  • $comment_id = wp_insert_comment($data);

In PHP, you should be able to loop through a jskit XML file using something like:

[toggle code]

  • $comments = simplexml_load_file("/path/to/comments.xml");
  • function getJSKitAttribute($item, $key) {
    • $attribute = $item->xpath('./jskit:attribute[@key="' . $key . '"]/@value');
    • $attribute = $attribute[0];
    • return $attribute;
  • }
  • foreach ($comments as $page) {
    • if ($page->item) {
      • $pageURL = $page->link;
      • echo $pageURL, "\n";
      • foreach ($page->item as $comment) {
        • $date = $comment->pubDate;
        • $text = $comment->description;
        • $IP = getJSKitAttribute($comment, 'IP');
        • echo "\t", substr($text, 0, 80), "\n";
        • echo "\t\t", $date, "\n";
        • echo "\t\t", $IP, "\n";
      • }
      • echo "\n";
    • }
  • }

You could then fill out the $data array with the values of $date, $text, $IP, etc., or hard-code them to default values if they don’t exist. Do this in place of (or in addition to) the three “echo” lines.

[toggle code]

  • $data = array(
    • 'comment_post_ID' => $comment->guid,
    • 'comment_author' => $comment->author,
    • 'comment_content' => $text,
    • 'comment_author_IP' => $IP,
    • 'comment_agent' => 'manual insertion',
    • 'comment_date_gmt' => strtotime($date),
    • 'comment_approved' => 1,
  • );
  • $comment_id = wp_insert_comment($data);

Since I don’t know WordPress, I’m not going to guess at the exact code. For example, I suspect that WordPress’s ID field is numeric, which means you won’t be able to use JSKit’s guid, since it is a string. You’ll need to generate your own unique numeric ID if that’s the case. But if WordPress allows you to manually insert comments through the code, then you should be able to import your JSKit XML files into it relatively easily.

In response to Parsing JSKit/Echo XML comments files: While I’m not a big fan of remote comment systems for privacy reasons, I was willing to use JSKit as a temporary solution because they provide an easy XML dump of posted comments. This weekend, I finally moved my main blog to custom comments; here’s how I parsed JSKit’s XML file.