You are here

function _disqus_migrate_export_api in Disqus 6

Calls function to gather comments to export, then attempts to create the comments in Disqus using the API.

1 string reference to '_disqus_migrate_export_api'
disqus_migrate_admin_export in include/disqus_migrate.export.inc
Menu callback for the export actions

File

include/disqus_migrate.export.inc, line 98

Code

function _disqus_migrate_export_api($form, &$form_state) {

  // Get API secret
  $api_secret = variable_get('disqus_secretkey', '');

  // Load API
  include_once 'sites/all/libraries/disqusapi/disqusapi.php';

  // Initialize the API
  $dapi = new DisqusAPI($api_secret);

  // What should we do to a local comment after processing
  $status_change = variable_get('disqus_migrate_export_status_alter', '--');

  // Gather thread data
  $thread_data = _disqus_migrate_export_gather();
  $comments_exported = 0;
  foreach ($thread_data as $nid => $thread) {

    // Check to see if this thread exists already in Disqus
    $thread_id = db_result(db_query("SELECT dtid FROM {disqus_migrate} WHERE nid=%d", $nid));
    if ($thread_id === FALSE) {
      $parameters = array(
        'forum' => variable_get('disqus_domain', ''),
        'thread:ident' => 'node/' . $nid,
        'thread:link' => url('node/' . $nid, array(
          'absolute' => TRUE,
          'alias' => TRUE,
        )),
        'api_key' => $api_key,
        'forum' => variable_get('disqus_domain', ''),
      );
      try {
        $response = $dapi->threads
          ->details($parameters);
        $thread_id = $response->id;
      } catch (Exception $e) {

        // Thread doesn't exist, don't do anything
      }
    }

    // No thread exists for this node, so we attempt to create one and obtain it's ID
    if ($thread_id === FALSE) {
      $parameters = array(
        'title' => $thread['title'],
        'url' => $thread['link'],
        'date' => $thread['post_date_gmt_unix'],
        'identifier' => $thread['identifier'],
        'api_key' => $api_key,
        'api_secret' => $api_secret,
        'forum' => variable_get('disqus_domain', ''),
      );
      try {
        $response = $dapi->threads
          ->create($parameters);
        $thread_id = $response->id;
      } catch (Exception $e) {

        // Log whatever error was returned
        drupal_set_message(t('Error when attempting to create Disqus thread for node id @nid (export has stopped): @error', array(
          '@nid' => $nid,
          '@error' => $e,
        )), 'error');
        break;
      }
    }
    else {
      foreach ($thread['comments'] as $cid => $comment) {

        // Get the Disqus parent ID
        $parent_did = db_result(db_query("SELECT did FROM {disqus_migrate} WHERE cid=%d", $comment['parent']));
        $parent_did = $parent_did === FALSE ? null : $parent_did;
        $parameters = array(
          'message' => substr($comment['content'], 0, 5000),
          // this seems to be the threshold fo how long a request can be
          'parent' => $parent_did,
          'thread' => $thread_id,
          'author_email' => $comment['author_email'],
          'author_name' => $comment['author'],
          'author_url' => $comment['author_url'],
          'state' => 'approved',
          'date' => $comment['date_gmt_unix'],
          'ip_address' => $comment['author_IP'],
          'api_secret' => $api_secret,
        );
        try {
          $response = $dapi->posts
            ->create($parameters);

          // Comment successfully exported... let's make a record of it
          $new_record = new stdClass();
          $new_record->did = $response->id;
          $new_record->dtid = $thread_id;
          $new_record->nid = $nid;
          $new_record->cid = $cid;
          drupal_write_record('disqus_migrate', $new_record);
          $comments_exported++;
        } catch (Exception $e) {

          // Log whatever error was returned
          drupal_set_message(t('Error when attempting to create Disqus post for comment id @cid (export has stopped): @error', array(
            '@cid' => $cid,
            '@error' => $e,
          )), 'error');
          break 2;
        }
      }

      // foreach comments
      // Should change the comment status for the node?
      if ($status_change != '--') {
        $update = db_query("UPDATE {node} SET comment = %d WHERE nid = %d", $status_change, $nid);
      }
    }
  }

  //foreach threads (nodes)
  drupal_set_message(t("@exported comments have been exported from Drupal into Disqus.", array(
    '@exported' => $comments_exported,
  )));
}