View source
<?php
function disqus_migrate_admin_export_settings() {
$form = array();
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => t('Export Settings'),
'#description' => t('Select the following options to effect what comments are exported.'),
);
$status_options = array(
'--' => t('No Change'),
0 => t('Disabled'),
1 => t('Read Only'),
);
$form['settings']['disqus_migrate_export_status_alter'] = array(
'#type' => 'select',
'#default_value' => variable_get('disqus_migrate_export_status_alter', '--'),
'#title' => t('Node comment status change'),
'#description' => t('After comments for a node have been exported, the commenting status can be altered. Useful for disabling Drupal commenting once comments have been exported.'),
'#options' => $status_options,
);
$api_limit_options = array(
20 => 20,
100 => 100,
250 => 250,
500 => 500,
);
$form['settings']['disqus_migrate_export_api_limit'] = array(
'#type' => 'select',
'#options' => $api_limit_options,
'#default_value' => variable_get('disqus_migrate_export_api_limit', 100),
'#title' => t('Batch limit for API export'),
'#description' => t('How many comments should be exported at a time? This does not apply to the XML export. The default value is usually the best bet for performance.'),
);
return system_settings_form($form);
}
function disqus_migrate_admin_export() {
$form = array();
$form['export'] = array(
'#type' => 'fieldset',
'#title' => t('Export Now'),
);
$pending_comments = db_result(db_query("SELECT COUNT(*) FROM {comments} c WHERE c.cid NOT IN (SELECT dm.cid FROM {disqus_migrate} dm)"));
if ($pending_comments === FALSE) {
$pending_comments = 0;
}
$form['export']['pending'] = array(
'#type' => 'markup',
'#prefix' => '<p>',
'#value' => t('There are @pending comments awaiting export.', array(
'@pending' => $pending_comments,
)),
'#suffix' => '</p>',
);
$form['export']['export_api'] = array(
'#type' => 'submit',
'#value' => t('Export awaiting comments using API'),
'#validate' => array(
'_disqus_migrate_export_api_validate',
),
'#submit' => array(
'_disqus_migrate_export_api',
),
);
$form['export']['export_xml'] = array(
'#type' => 'submit',
'#value' => t('Export all comments to XML file'),
'#submit' => array(
'_disqus_migrate_export_wxr',
),
);
return $form;
}
function _disqus_migrate_export_api_validate($form, &$form_state) {
if (!file_exists('sites/all/libraries/disqusapi/disqusapi.php')) {
form_set_error('', t('Download the <a href="@disqusapiurl">PHP API implementation here</a> and upload the disqusapi folder to your sites/all/libraries folder. Also ensure that you have authenticated with Disqus.', array(
'@disqusapiurl' => url('https://github.com/disqus/disqus-php', array(
'absolute' => TRUE,
)),
)));
}
$api_secret = variable_get('disqus_secretkey', '');
if (empty($api_secret)) {
form_set_error('', t('Your secret key must be entered before using this export functionality. Please get your application API keys from Disqus and enter them in the "Advanced" fieldset on the settings page.'));
}
}
function _disqus_migrate_export_api($form, &$form_state) {
$api_secret = variable_get('disqus_secretkey', '');
include_once 'sites/all/libraries/disqusapi/disqusapi.php';
$dapi = new DisqusAPI($api_secret);
$status_change = variable_get('disqus_migrate_export_status_alter', '--');
$thread_data = _disqus_migrate_export_gather();
$comments_exported = 0;
foreach ($thread_data as $nid => $thread) {
$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) {
}
}
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) {
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) {
$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),
'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);
$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) {
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;
}
}
if ($status_change != '--') {
$update = db_query("UPDATE {node} SET comment = %d WHERE nid = %d", $status_change, $nid);
}
}
}
drupal_set_message(t("@exported comments have been exported from Drupal into Disqus.", array(
'@exported' => $comments_exported,
)));
}
function _disqus_migrate_export_wxr($form, &$form_state) {
$thread_data = _disqus_migrate_export_gather(TRUE);
$status_change = variable_get('disqus_migrate_export_status_alter', '--');
if (!empty($thread_data)) {
$output = '';
$output .= '<?xml version="1.0" encoding="UTF-8"?>';
$output .= '<rss version="2.0"';
$output .= ' xmlns:content="http://purl.org/rss/1.0/modules/content/"';
$output .= ' xmlns:dsq="http://www.disqus.com/"';
$output .= ' xmlns:dc="http://purl.org/dc/elements/1.1/"';
$output .= ' xmlns:wp="http://wordpress.org/export/1.0/"';
$output .= '>';
$output .= ' <channel>';
foreach ($thread_data as $nid => $thread) {
if (empty($thread['comments'])) {
continue;
}
$output .= '<item>';
$output .= '<title>' . _disqus_migrate_cleanse_xml($thread['title']) . '</title>';
$output .= '<link>' . $thread['link'] . '</link>';
$output .= '<content:encoded></content:encoded>';
$output .= '<dsq:thread_identifier>' . $thread['identifier'] . '</dsq:thread_identifier>';
$output .= '<wp:post_date_gmt>' . $thread['post_date_gmt'] . '</wp:post_date_gmt>';
$output .= '<wp:comment_status>open</wp:comment_status>';
foreach ($thread['comments'] as $comment) {
$output .= '<wp:comment>';
$output .= '<wp:comment_id>' . $comment['id'] . '</wp:comment_id>';
$output .= '<wp:comment_author>' . $comment['author'] . '</wp:comment_author>';
$output .= '<wp:comment_author_email>' . $comment['author_email'] . '</wp:comment_author_email>';
$output .= '<wp:comment_author_url>' . $comment['author_url'] . '</wp:comment_author_url>';
$output .= '<wp:comment_author_IP>' . $comment['author_IP'] . '</wp:comment_author_IP>';
$output .= '<wp:comment_date_gmt>' . $comment['date_gmt'] . '</wp:comment_date_gmt>';
$output .= '<wp:comment_content><![CDATA[' . $comment['content'] . ']]></wp:comment_content>';
$output .= '<wp:comment_approved>' . $comment['approved'] . '</wp:comment_approved>';
$output .= '<wp:comment_parent>' . $comment['parent'] . '</wp:comment_parent>';
$output .= '</wp:comment>';
}
$output .= '</item>';
if ($status_change != '--') {
$update = db_query("UPDATE {node} SET comment = %d WHERE nid = %d", $status_change, $nid);
}
}
$output .= ' </channel>';
$output .= '</rss>';
header("Content-disposition: attachment; filename=drupalcomments.xml");
header("Content-Type: text/xml; charset=utf-8");
print $output;
exit;
}
else {
drupal_set_message(t('No comments to export.'), 'error');
}
}
function _disqus_migrate_export_gather($select_all = FALSE) {
$thread_data = array();
date_default_timezone_set('GMT');
$export_threshold = variable_get('disqus_migrate_export_api_limit', 100);
$comments_gathered = 0;
$user_emails = array();
if ($select_all) {
$node_results = db_query("SELECT DISTINCT c.nid FROM {comments} c");
}
else {
$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)) {
$node_data_query = db_query("SELECT title, created, status FROM {node} WHERE nid = %d", $nid);
$node_data = db_fetch_object($node_data_query);
$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,
);
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)) {
$max_comment_id = $comment->cid > $max_comment_id ? $comment->cid : $max_comment_id;
if ($comment->uid != 0) {
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;
$user_emails[$comment->uid] == $user_mail;
}
}
elseif ($comment->mail) {
$comment_mail = $comment->mail;
}
else {
$random = rand(1000, 100000);
$comment_mail = "anon" . $random . "@anonymous.com";
}
if ($comment->name) {
$comment_name = $comment->name;
}
else {
$comment_name = "Anonymous";
}
$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++;
if ($comments_gathered >= $export_threshold && !$select_all) {
break 2;
}
}
}
return $thread_data;
}
function _disqus_migrate_cleanse_xml($string) {
$find = array(
'>',
'<',
'&',
'"',
"'",
);
$replace = array(
'>',
'<',
'&',
'"',
''',
);
return str_replace($find, $replace, $string);
}