Importing Drupal 7 Comments into Disqus

 

Importing Drupal 7 Comments into Disqus

I needed to import some comments into Disqus and wrote a quick module to do it. There are things in here that are hard coded that I should fix, but thought it was worth posting for today. Share and enjoy!

<?php
function exportfordiscus_comments(){
  
$nodes = node_load_multiple(array(), array('status' => 1));
   global
$base_root;
 
  
$ourFileName = "sites/new/files/testFile.xml";
  
$fh= fopen($ourFileName, 'w') or die("can't open file");
  
  
  
//write headers
  
fwriteln($fh,'<?xml version="1.0" encoding="UTF-8"?>
');
   fwriteln($fh,'<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dsq="http://www.disqus.com/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wp="http://wordpress.org/export/1.0/">');
    fwriteln($fh,'<channel>');
 
  foreach($nodes as &$node){
    if ($node->comment_count > 0) {
      fwriteln($fh,'<item>');
      fwriteln($fh,'<title>'.$node->title.'</title>');
      fwriteln($fh,'<link>'.$base_root.url('node/'.$node->nid).'</link>');
      fwriteln($fh,'<content:encoded><![CDATA['.$node->body['und'][0]['safe_value'].']]></content:encoded>');
      fwriteln($fh,'<dsq:thread_identifier>node/'.$node->nid.'</dsq:thread_identifier>');
      fwriteln($fh,'<wp:post_date_gmt>'.gmdate('Y-m-d H:i:s', $node->created).'</wp:post_date_gmt>');
      fwriteln($fh,'<wp:comment_status>open</wp:comment_status>');
     
      $cids = comment_get_thread($node,  COMMENT_MODE_THREADED  , 100);
      $comments = comment_load_multiple($cids);
      $node->comments = $comments;
     
      foreach($comments as $comment){
        fwriteln($fh,'<wp:comment>');
        fwriteln($fh,'<wp:comment_id>'.$comment->cid.'</wp:comment_id>');
        fwriteln($fh,'<wp:comment_approved>1</wp:comment_approved>');
        fwriteln($fh,'<wp:comment_parent>0</wp:comment_parent>');
        fwriteln($fh,'<wp:comment_author>'.$comment->name.'</wp:comment_author>');
        if ($comment->name == "seanreiser"){
          fwriteln($fh,'<wp:comment_author_email>sean@seanreiser.com</wp:comment_author_email>');        
        }
        elseif ($comment->mail){
          fwriteln($fh,'<wp:comment_author_email>'.$comment->mail.'</wp:comment_author_email>');
        }
        else{
          fwriteln($fh,'<wp:comment_author_email>'.$comment->name.'</wp:comment_author_email>');
        }
        fwriteln($fh,'<wp:comment_author_url>'.$comment->homepage.'</wp:comment_author_url>');
        fwriteln($fh,'<wp:comment_author_IP>'.$comment->hostname.'</wp:comment_author_IP>');
        fwriteln($fh,'<wp:comment_date_gmt>'.gmdate('Y-m-d H:i:s', $comment->created).'</wp:comment_date_gmt>');
        fwriteln($fh,'<wp:comment_content><![CDATA['.$comment->comment_body['und'][0]['safe_value'].']]></wp:comment_content>');
       
       
       
        fwriteln($fh,'</wp:comment>');
      }
     
     
      fwriteln($fh,'</item>');    
      $node_comment[] = $node;
    }
   

  }
  fwriteln($fh,'</channel>');
  fwriteln($fh,'</rss>');
  fclose($fh);
 
 
  dpm($node_comment);
 
  return '123';
 
}

function fwriteln ($fh,$string){
  fwrite($fh,$string."\n");

}
?>

Project Files: 

Top