You are here

function _disqus_migrate_export_gather in Disqus 6

Helper function to gather comments to export. The boolean parameter determines whether or not to select all the comments or all comments since last export

2 calls to _disqus_migrate_export_gather()
_disqus_migrate_export_api in include/disqus_migrate.export.inc
Calls function to gather comments to export, then attempts to create the comments in Disqus using the API.
_disqus_migrate_export_wxr in include/disqus_migrate.export.inc
Calls function to gather comments to export, then builds the appropriate XML file and presents it to the user for download.

File

include/disqus_migrate.export.inc, line 306

Code

function _disqus_migrate_export_gather($select_all = FALSE) {

  // Select all the nodes with comments
  $thread_data = array();

  // Make sure the dates are exported in GMT time as expected by Disqus
  date_default_timezone_set('GMT');

  // Get max amount of comments to gather
  $export_threshold = variable_get('disqus_migrate_export_api_limit', 100);
  $comments_gathered = 0;

  // Storage for looked up user emails
  $user_emails = array();

  // Gather distinct node IDs from comments
  if ($select_all) {
    $node_results = db_query("SELECT DISTINCT c.nid FROM {comments} c");
  }
  else {

    // Find all comments that haven't already been imported/exported via this module. Let's also limit the result to prevent
    // any gathering an unnecessary amount of comments.
    $node_results = db_query("SELECT DISTINCT c.nid FROM {comments} c LEFT JOIN {disqus_migrate} dm ON c.cid = dm.cid WHERE dm.cid IS NULL LIMIT 0, %d", $export_threshold);
  }
  $max_comment_id = 0;
  while ($nid = db_result($node_results)) {

    // get some bits of info from the node. This is much more efficient than doing a node_load
    $node_data_query = db_query("SELECT title, created, status FROM {node} WHERE nid = %d", $nid);
    $node_data = db_fetch_object($node_data_query);

    // Load up the thread data array for this node
    $thread_data[$nid] = array(
      'title' => $node_data->title,
      'link' => url("node/" . $nid, array(
        'absolute' => TRUE,
        'alias' => TRUE,
      )),
      'identifier' => 'node/' . $nid,
      'post_date_gmt' => date("Y-m-d H:i:s", $node_data->created),
      'post_date_gmt_unix' => $node_data->created,
    );

    // Find all comments attached to this node
    if ($select_all) {
      $comment_query = "SELECT * FROM {comments} c WHERE c.nid = %d ORDER BY c.cid ASC";
    }
    else {
      $comment_query = "SELECT c.* FROM {comments} c LEFT JOIN {disqus_migrate} dm ON c.cid = dm.cid WHERE dm.cid IS NULL AND c.nid = %d ORDER BY c.cid ASC";
    }
    $comments_results = db_query($comment_query, $nid);
    while ($comment = db_fetch_object($comments_results)) {

      // Set max comment ID
      $max_comment_id = $comment->cid > $max_comment_id ? $comment->cid : $max_comment_id;
      if ($comment->uid != 0) {

        // Logged in user left comment, obtain email from user account
        if (isset($user_emails[$comment->uid])) {
          $comment_mail = $user_emails[$comment->uid];
        }
        else {
          $user_mail = db_result(db_query("SELECT mail FROM {users} WHERE uid=%d", $comment->uid));
          $comment_mail = $user_mail;

          // Save into storage so we don't need another query
          $user_emails[$comment->uid] == $user_mail;
        }
      }
      elseif ($comment->mail) {

        // Anon commenter left email, use that
        $comment_mail = $comment->mail;
      }
      else {

        // Anon commenter did NOT leave email, but it's required by Disqus, so
        // generate a random one (to make each unique)
        $random = rand(1000, 100000);
        $comment_mail = "anon" . $random . "@anonymous.com";
      }

      // Use name "Anonymous" for anonymous users
      if ($comment->name) {
        $comment_name = $comment->name;
      }
      else {
        $comment_name = "Anonymous";
      }

      // Load up the comment data for this comment
      $thread_data[$nid]['comments'][$comment->cid] = array(
        'id' => $comment->cid,
        'author' => $comment_name,
        'author_email' => $comment_mail,
        'author_url' => $comment->homepage,
        'author_IP' => $comment->hostname,
        'date_gmt' => date("Y-m-d H:i:s", $comment->timestamp),
        'date_gmt_unix' => $comment->timestamp,
        'content' => $comment->comment,
        'approved' => $comment->status == 1 ? 0 : 1,
        'parent' => $comment->pid,
      );
      $comments_gathered++;

      // Check to see if we reached limit of comments we shoudl export
      if ($comments_gathered >= $export_threshold && !$select_all) {
        break 2;
      }
    }
  }
  return $thread_data;
}