Index: disqus_migrate.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/disqus/Attic/disqus_migrate.module,v retrieving revision 1.1.2.1 diff -u -r1.1.2.1 disqus_migrate.module --- disqus_migrate.module 29 Nov 2008 09:59:51 -0000 1.1.2.1 +++ disqus_migrate.module 24 Mar 2010 09:04:23 -0000 @@ -1,10 +1,15 @@ '. t('The tool below allows you to import comments from Disqus to the Drupal comment system.', array('@disqus' => 'http://disqus.com')) .'

'; + case 'admin/settings/disqus/export': + return '

'. t('Clicking "Export" below will export the comments from the Drupal comment system to Disqus. Note that comments will only be exported once.', array('@disqus' => 'http://disqus.com')) .'

'; } } @@ -18,9 +23,179 @@ 'description' => 'Import comments from Disqus into the Drupal comment system.', 'access arguments' => array('administer disqus'), 'page callback' => 'drupal_get_form', - 'page arguments' => array('disqus_admin_settings_import'), + 'page arguments' => array('disqus_migrate_admin_settings_import'), + 'file' => 'disqus_migrate.admin.inc', + 'type' => MENU_LOCAL_TASK, + ); + $items['admin/settings/disqus/export'] = array( + 'title' => 'Export', + 'description' => 'Export comments from the Drupal comment system to Disqus.', + 'access arguments' => array('administer disqus'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('disqus_migrate_admin_settings_export'), 'file' => 'disqus_migrate.admin.inc', 'type' => MENU_LOCAL_TASK, ); return $items; } + +/** + * Processes the comments from Drupal to Disqus. + */ +function disqus_migrate_export_process($form, $form_state, &$context) { + // Make sure we have our batch sandbox in place. + if (!isset($context['sandbox']['progress'])) { + // Retrieve the required information from Disqus. + $context['sandbox']['user_api_key'] = variable_get('disqus_userapikey', ''); + $disqus = disqus($context['sandbox']['user_api_key']); + + // Retrieve the forum ID. + try { + $forum_list = $disqus->get_forum_list(); + $shortname = variable_get('disqus_domain', ''); + foreach ($forum_list as $key => $object) { + if ($object->shortname == $shortname) { + $context['sandbox']['forum_id'] = $object->id; + break; + } + } + if (!isset($context['sandbox']['forum_id'])) { + throw new DisqusException(); + } + } + catch (DisqusException $exception) { + return array( + '#abort' => array( + 'success' => FALSE, + 'query' => t('Could not retrieve the forum ID. Make sure you provide the correct user API key.'), + ), + ); + } + + // Retrieve the forum API key. + try { + $context['sandbox']['forum_api_key'] = $disqus->get_forum_api_key($forum_id); + } + catch (DisqusException $exception) { + return array( + '#abort' => array( + 'success' => FALSE, + 'query' => t('Could not retrieve the forum API key. Make sure you provide the correct user API key.'), + ), + ); + } + + // Setup the batch by figuring out how many comments there are to process. + $context['sandbox']['progress'] = 0; + $context['sandbox']['current_timestamp'] = 0; + $result = db_query(" + SELECT c.*, u.mail as user_mail, n.title as node_title FROM {comments} c + INNER JOIN {users} u ON u.uid = c.uid + INNER JOIN {node} n ON n.nid = c.nid + LEFT JOIN {disqus_migrate_export} d ON d.cid = c.cid AND d.fid = %d + WHERE d.did IS NULL + ORDER BY c.timestamp ASC", $context['sandbox']['forum_id']); + $context['sandbox']['max'] = mysql_num_rows($result); + $context['message'] = t('Preparing to process %num entries.', array('%num' => $context['sandbox']['max'])); + } + + // Create the disqus interface. + $disqus = disqus($context['sandbox']['user_api_key'], $context['sandbox']['forum_api_key']); + + // Retrieve the set of comments to process. + $limit = 5; + $result = db_query(" + SELECT c.*, u.mail as user_mail, n.title as node_title FROM {comments} c + INNER JOIN {users} u ON u.uid = c.uid + INNER JOIN {node} n ON n.nid = c.nid + LEFT JOIN {disqus_migrate_export} d ON d.cid = c.cid AND d.fid = %d + WHERE d.did IS NULL AND + c.timestamp > %d + ORDER BY c.timestamp ASC LIMIT %d", $context['sandbox']['forum_id'], $context['sandbox']['current_timestamp'], $limit); + + while ($comment = db_fetch_object($result)) { + // Set the progress message. + $context['sandbox']['progress']++; + $context['results'][$comment->cid] = $comment; + $context['message'] = t('Now processing comment %cid by %name at @title.', array( + '%cid' => $comment->cid, + '%name' => $comment->name, + '@url' => url('node/' . $comment->nid, array('fragment' => 'comment-' . $comment->cid)), + )); + $context['sandbox']['current_timestamp'] = $comment->timestamp; + + // Retrieve the thread information from Disqus. + $url = url("node/{$comment->nid}", array('absolute' => TRUE, 'alias' => TRUE)); + $thread = NULL; + try { + $thread = $disqus->get_thread_by_url($url); + if (!$thread) { + $thread = $disqus->thread_by_identifier("node-{$comment->nid}", $comment->node_title)->thread; + $disqus->update_thread($thread->id, array('url' => $url)); + $thread = $disqus->get_thread_by_url($url); + } + } + catch (DisqusException $exception) { + // There was an error retrieving the thread, so skip the comment for now. + continue; + } + + // If thread still isn't found, skip this comment (for now). + if (!isset($thread)) { + continue; + } + + // Check if this comment has a parent post. + $parent_post = NULL; + if (isset($comment->pid)) { + $parent_post = db_result(db_query("SELECT did FROM {disqus_migrate_export} WHERE cid = %d AND fid = %d", $comment->pid, $context['sandbox']['forum_id'])); + } + + // Create the comment in the thread on Disqus. + $post = NULL; + try { + $post = $disqus->create_post($thread->id, $comment->comment, $comment->name, $comment->mail ? $comment->mail : $comment->user_mail, array( + 'created_at' => format_date($comment->timestamp, 'custom', 'Y-m-d\TH:i', 0), + 'ip_address' => $comment->ip_address ? $comment->ip_address : '127.0.0.1', + 'parent_post' => $parent_post, + 'state' => $comment->status ? 'unapproved' : 'approved', + )); + } + catch (DisqusException $exception) { + // Since there was an error posting this to Disqus, skip the comment. + continue; + } + + // Save the export information to the Drupal database. + if (isset($post->id)) { + db_query("INSERT INTO {disqus_migrate_export} (did, cid, fid) VALUES (%d, %d, %d)", $post->id, $comment->cid, $forum_id); + } + } + + // Check if we are finished. + if ($context['sandbox']['progress'] != $context['sandbox']['max']) { + if ($context['sandbox']['max'] == 0) { + $context['finished'] = 1; + } + else { + $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; + } + } +} + +/** + * The batch process is complete. + */ +function disqus_migrate_export_finished($success, $results, $operations) { + if ($success) { + // Here we do something meaningful with the results. + $message = t('!num been exported to disqus.', array('!num' => format_plural(count($results), '@count comment has', '@count comments have'))); + } + else { + // An error occurred. + // $operations contains the operations that remained unprocessed. + $error_operation = reset($operations); + $message = 'An error occurred while processing '. $error_operation[0] .' with arguments :'. print_r($error_operation[0], TRUE); + } + drupal_set_message($message); +} \ No newline at end of file Index: disqus_migrate.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/disqus/Attic/disqus_migrate.admin.inc,v retrieving revision 1.1.2.2 diff -u -r1.1.2.2 disqus_migrate.admin.inc --- disqus_migrate.admin.inc 29 Nov 2008 10:03:46 -0000 1.1.2.2 +++ disqus_migrate.admin.inc 24 Mar 2010 09:04:22 -0000 @@ -4,7 +4,7 @@ /** * Menu callback; Import comments from Disqus to the Drupal comment system. */ -function disqus_admin_settings_import() { +function disqus_migrate_admin_settings_import() { $form = array(); $form['#attributes'] = array('enctype' => "multipart/form-data"); $form['file'] = array( @@ -22,7 +22,7 @@ /** * Form submit handler for the import tool. */ -function disqus_admin_settings_import_submit($form, &$form_state) { +function disqus_migrate_admin_settings_import_submit($form, &$form_state) { // See if the file was successfully saved. if ($file = file_save_upload('file')) { $simplexml = @simplexml_load_file($file->filepath); @@ -78,3 +78,42 @@ } } } + +/** + * Menu callback; Export comments from Drupal to the Disqus comment system. + */ +function disqus_migrate_admin_settings_export() { + $form = array(); + $user_api_key = variable_get('disqus_userapikey', ''); + + if (empty($user_api_key)) { + drupal_set_message(t('A user key must be specified in the general settings to export comments.'), 'error'); + drupal_goto('admin/settings/disqus'); + } + else { + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Export'), + ); + } + + return $form; +} + +/** + * Form submit handler for the export tool. + */ +function disqus_migrate_admin_settings_export_submit($form, &$form_state) { + $batch = array( + 'title' => t('Exporting to Disqus'), + 'operations' => array( + array('disqus_migrate_export_process', array($form, $form_state)), + ), + 'file' => drupal_get_path('module', 'disqus_migrate') . 'disqus_migrate.admin.inc', + 'init_message' => t('Initializing Disqus connection.'), + 'error_message' => t('An error occurred during the export.'), + 'progress_message' => t('Processed @current out of @total comments.'), + 'finished' => 'disqus_migrate_export_finished', + ); + batch_set($batch); +} Index: disqus_migrate.install =================================================================== RCS file: disqus_migrate.install diff -N disqus_migrate.install --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ disqus_migrate.install 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,56 @@ + 'The base table for nodes.', + 'fields' => array( + 'did' => array( + 'description' => 'The primary identifier for the Disqus comment.', + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'cid' => array( + 'description' => 'The current {comments}.cid comment identifier.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ), + 'fid' => array( + 'description' => 'The Disqus forum identifier.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ), + ), + 'primary key' => array('did'), + ); + return $schema; +} + +/** + * Installs the disqus export schema. + */ +function disqus_migrate_update_6100() { + drupal_install_schema('disqus_migrate'); + return array(); +}