Index: disqus_migrate.module =================================================================== --- disqus_migrate.module 2008-11-29 04:59:51.000000000 -0500 +++ disqus_migrate.module 2010-03-22 18:06:30.000000000 -0400 @@ -22,5 +22,14 @@ function disqus_migrate_menu() { '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_admin_settings_export'), + 'file' => 'disqus_migrate.admin.inc', + 'type' => MENU_LOCAL_TASK, + ); return $items; -} +} Index: disqus_migrate.install =================================================================== --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ disqus_migrate.install 2010-03-22 18:06:30.000000000 -0400 @@ -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(); +} Index: disqus_migrate.admin.inc =================================================================== --- disqus_migrate.admin.inc 2008-11-29 05:03:46.000000000 -0500 +++ disqus_migrate.admin.inc 2010-03-23 09:27:57.000000000 -0400 @@ -78,3 +78,102 @@ function disqus_admin_settings_import_su } } } + +/** + * Menu callback; Export comments from Drupal to the Disqus comment system. + */ +function disqus_admin_settings_export() { + $form = array(); + $user_api_key = variable_get('disqus_userapikey', ''); + + if (!$user_api_key) { + $form['key_required']['#value'] = t('A user key must be specified in the general settings to export comments.'); + } + else { + // List of disqus forums. + $disqus = disqus($user_api_key); + $forum_list = $disqus->get_forum_list(); + $forum_options = array(); + foreach ($forum_list as $key => $object) { + $forum_options[$object->id] = $object->name; + } + + $form['disqus_forum'] = array( + '#type' => 'select', + '#title' => t('Disqus Forum'), + '#options' => $forum_options, + '#required' => TRUE + ); + $form['disqus_moderated'] = array( + '#type' => 'checkbox', + '#title' => t('Include unapproved comments in export.'), + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Export'), + ); + } + + return $form; +} + +/** + * Form submit handler for the export tool. + */ +function disqus_admin_settings_export_submit($form, &$form_state) { + // Include the disqus API. + $user_api_key = variable_get('disqus_userapikey', ''); + $disqus = disqus($user_api_key); + $forum_id = $form_state['values']['disqus_forum']; + $forum_api_key = $disqus->get_forum_api_key($forum_id); + $disqus = disqus($user_api_key, $forum_api_key); + + // Find all comments not already copied to disqus. + $results = 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.status IN (%s) + ORDER BY c.timestamp ASC", $forum_id, ($form_state['values']['disqus_moderated'] ? '0,1' : '0')); + + $count = 0; + while ($comment = db_fetch_object($results)) { + $url = url("node/{$comment->nid}", array('absolute' => TRUE, 'alias' => TRUE)); + + // Check if a thread exists for this node on disqus. + $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); + } + + // If thread still isn't found, skip this comment (for now). + if (!$thread) { + continue; + } + + // Check if this comment has a parent post. + $parent_post = NULL; + if ($comment->pid) { + $parent_post = db_result(db_query("SELECT did FROM {disqus_migrate_export} WHERE cid = %d AND fid = %d", $comment->pid, $forum_id)); + } + + // Create the comment in the thread on disqus. + $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', + )); + + // Save the export information to the drupal database. + if ($post->id) { + db_query("INSERT INTO {disqus_migrate_export} (did, cid, fid) VALUES (%d, %d, %d)", $post->id, $comment->cid, $forum_id); + $count++; + } + } + + drupal_set_message(t('!num been exported to disqus.', array('!num' => format_plural($count, "@count comment has", "@count comments have")))); +}