Select Page

I recently had to migrate a Silverstripe installation’s comments to use the Disqus comment system. Here’s the script I wrote to grab and format the Silverstripe comments into the WXR xml structure Disqus can import. No doubt there’s an easier way. You’ll have to append this code to something else like a working Silverstripe task so you can run it from the commandline.

 

$site = 'http://example.com/blog/'; //url prefix
$output_file = '/tmp/rss.xml'; //where to put the xml

$dom = new DOMDocument("1.0");
$top = $dom->appendChild($dom->createElement("rss"));
$top->setAttribute("version","2.0");
$top->setAttribute("xmlns:content","http://purl.org/rss/1.0/modules/content/");
$top->setAttribute("xmlns:dsq","http://www.disqus.com/");
$top->setAttribute("xmlns:dc","http://purl.org/dc/elements/1.1/");
$top->setAttribute("xmlns:wp","http://wordpress.org/export/1.0/");
$channel = $top->appendChild($dom->createElement("channel"));

$comments = Comment::get()->sort(array('ParentID'=>'ASC', 'Created'=>'ASC'));
$cnt = 0;
$currblog = 0;
foreach($comments as $comment)
{
    $cnt++;
    $page = SiteTree::get()->byID($comment->ParentID);

    if($comment->ParentID!=$currblog)
    {
        $currblog = $comment->ParentID;

        $item = $channel->appendChild($dom->createElement("item"));
        $title = $item->appendChild($dom->createElement("title"));
        $title->appendChild($dom->createTextNode($page->Title));
        $link = $item->appendChild($dom->createElement("link"));
        $link->appendChild($dom->createTextNode($site.$page->URLSegment."/"));
        $content = $item->appendChild($dom->createElement("content:encoded"));
        $content->appendChild($dom->createCDATASection($page->Content));
        $content = $item->appendChild($dom->createElement("dsq:thread_identifier"));
        $content->appendChild($dom->createTextNode($page->ID));
        $date = $item->appendChild($dom->createElement("wp:post_date_gmt"));
        $date->appendChild($dom->createTextNode($page->Created));
        $open = $item->appendChild($dom->createElement("wp:comment_status"));
        $open->appendChild($dom->createTextNode('open'));
    }

    $icomment = $item->appendChild($dom->createElement("wp:comment"));
    $node = $icomment->appendChild($dom->createElement("wp:comment_id"));
    $node->appendChild($dom->createTextNode($comment->ID));
    $node = $icomment->appendChild($dom->createElement("wp:comment_author"));
    $node->appendChild($dom->createTextNode($comment->Name));
    $node = $icomment->appendChild($dom->createElement("wp:comment_author_email"));
    $node->appendChild($dom->createTextNode($comment->Email));
    $node = $icomment->appendChild($dom->createElement("wp:comment_author_url"));
    $node->appendChild($dom->createTextNode($comment->URL));
    $node = $icomment->appendChild($dom->createElement("wp:comment_author_IP"));
    $node->appendChild($dom->createTextNode(''));
    $node = $icomment->appendChild($dom->createElement("wp:comment_date_gmt"));
    $node->appendChild($dom->createTextNode($comment->Created));
    $node = $icomment->appendChild($dom->createElement("wp:comment_content"));
    $node->appendChild($dom->createCDATASection($comment->Comment));
    $node = $icomment->appendChild($dom->createElement("wp:comment_approved"));
    $node->appendChild($dom->createTextNode($comment->Moderated));
    $node = $icomment->appendChild($dom->createElement("wp:comment_parent"));
    $node->appendChild($dom->createTextNode(0));
}

$xml = $dom->saveXML();

file_put_contents($output_file, $xml);