print_line('Discovered DISQUS forum shortname as %s', $forum_url);
print_line('---------------------------------------------------------');
$imported = true;
if (in_array('--reset', $argv)) {
    $last_comment_id = 0;
} else {
    $last_comment_id = get_option('disqus_last_comment_id');
}
$force = in_array('--force', $argv);
$total = 0;
$global_start = microtime();
$memory_usage = memory_get_peak_usage();
while ($imported) {
    print_line('  Importing chunk starting at comment id %d', $last_comment_id);
    $start = microtime();
    $result = dsq_sync_forum($last_comment_id, $force);
    if ($result === false) {
        print_line('---------------------------------------------------------');
        print_line('There was an error communicating with DISQUS!');
        print_line($dsq_api->get_last_error());
        print_line('---------------------------------------------------------');
        die;
        break;
    } else {
        list($imported, $last_comment_id) = $result;
    }
    $total += $imported;
    $time = abs(microtime() - $start);
    // assuming the cache is the internal, reset it's value to empty to avoid
    // large memory consump
    $wp_object_cache->cache = array();
}
print_line('---------------------------------------------------------');
print_line('Discovered DISQUS forum shortname as %s', $forum_url);
print_line('---------------------------------------------------------');
$imported = true;
if (in_array('--reset', $argv)) {
    $last_comment_id = 0;
} else {
    $last_comment_id = get_option('disqus_last_comment_id');
}
$total = 0;
$global_start = microtime();
while ($imported) {
    print_line('  Importing chunk starting at comment id %d', $last_comment_id);
    $start = microtime();
    $result = dsq_sync_forum($last_comment_id);
    if ($result === false) {
        print_line('---------------------------------------------------------');
        print_line('There was an error communicating with DISQUS!');
        print_line($dsq_api->get_last_error());
        print_line('---------------------------------------------------------');
        die;
        break;
    } else {
        list($imported, $last_comment_id) = $result;
    }
    $total += $imported;
    $time = abs(microtime() - $start);
    print_line('    %d comments imported (took %.2fs)', $imported, $time);
}
$total_time = abs(microtime() - $global_start);
Exemple #3
0
function dsq_request_handler()
{
    global $dsq_response;
    global $dsq_api;
    global $post;
    global $wpdb;
    if (!empty($_GET['cf_action'])) {
        switch ($_GET['cf_action']) {
            case 'sync_comments':
                if (!($post_id = $_GET['post_id'])) {
                    header("HTTP/1.0 400 Bad Request");
                    die;
                }
                // schedule the event for 5 minutes from now in case they
                // happen to make a quick post
                dsq_add_pending_post_id($post_id);
                if (DISQUS_DEBUG) {
                    $response = dsq_sync_forum();
                    if (!$response) {
                        die('// error: ' . $dsq_api->get_last_error());
                    } else {
                        list($last_comment_id, $comments) = $response;
                        die('// synced ' . $comments . ' comments');
                    }
                } else {
                    $ts = time() + 300;
                    wp_schedule_single_event($ts, 'dsq_sync_forum');
                    die('// sync scheduled');
                }
                break;
            case 'export_comments':
                if (current_user_can('manage_options') && DISQUS_CAN_EXPORT) {
                    $msg = '';
                    $result = '';
                    $response = null;
                    $timestamp = intval($_GET['timestamp']);
                    $post_id = intval($_GET['post_id']);
                    if (isset($_GET['_dsqexport_wpnonce']) === false) {
                        $msg = dsq_i('Unable to export comments. Make sure you are accessing this page from the Wordpress dashboard.');
                        $result = 'fail';
                    } else {
                        // Check nonce
                        check_admin_referer('dsq-wpnonce_export', '_dsqexport_wpnonce');
                        global $wpdb, $dsq_api;
                        $post = $wpdb->get_results($wpdb->prepare("\r\n                            SELECT *\r\n                            FROM {$wpdb->posts}\r\n                            WHERE post_type != 'revision'\r\n                            AND post_status = 'publish'\r\n                            AND comment_count > 0\r\n                            AND ID > %d\r\n                            ORDER BY ID ASC\r\n                            LIMIT 1\r\n                        ", $post_id));
                        $post = $post[0];
                        $post_id = $post->ID;
                        $max_post_id = $wpdb->get_var("\r\n                            SELECT MAX(Id)\r\n                            FROM {$wpdb->posts}\r\n                            WHERE post_type != 'revision'\r\n                            AND post_status = 'publish'\r\n                            AND comment_count > 0\r\n                        ");
                        $eof = (int) ($post_id == $max_post_id);
                        if ($eof) {
                            $status = 'complete';
                            $msg = dsq_i('Your comments have been sent to Disqus and queued for import!');
                            $msg .= '<br/><a href="' . DISQUS_IMPORTER_URL . '" target="_blank">';
                            $msg .= dsq_i('See the status of your import at Disqus') . '</a>';
                        } else {
                            $status = 'partial';
                            $msg = dsq_i('Processed comments on post') . ' #' . $post_id . '&hellip;';
                        }
                        $result = 'fail';
                        if ($post) {
                            require_once dirname(__FILE__) . '/export.php';
                            $wxr = dsq_export_wp($post);
                            $response = $dsq_api->import_wordpress_comments($wxr, $timestamp, $eof);
                            if (!($response['group_id'] > 0)) {
                                $result = 'fail';
                                $msg = '<p class="status dsq-export-fail">';
                                $msg .= dsq_i('Sorry, something unexpected happened with the export. Please try again.');
                                $msg .= '</p><p>';
                                $msg .= dsq_i('If your API key has changed, you may need to reinstall Disqus (deactivate the plugin and then reactivate it).');
                                $msg .= dsq_i('If you are still having issues, refer to the %s WordPress help page', '<a href="https://help.disqus.com/customer/portal/articles/472005" onclick="window.open(this.href); return false">');
                                $msg .= '</a></p>';
                                $response = $dsq_api->get_last_error();
                            } else {
                                if ($eof) {
                                    $msg = dsq_i('Your comments have been sent to Disqus and queued for import!');
                                    $msg .= '<br/><a href="' . $response['link'] . '" target="_blank">';
                                    $msg .= dsq_i('See the status of your import at Disqus');
                                    $msg .= '</a>';
                                }
                                $result = 'success';
                            }
                        }
                    }
                    // send AJAX response
                    $response = compact('result', 'timestamp', 'status', 'post_id', 'msg', 'eof', 'response');
                    header('Content-type: text/javascript');
                    echo cf_json_encode($response);
                    die;
                }
                break;
            case 'import_comments':
                if (current_user_can('manage_options')) {
                    $msg = '';
                    $result = '';
                    $response = null;
                    if (isset($_GET['_dsqimport_wpnonce']) === false) {
                        $msg = dsq_i('Unable to import comments. Make sure you are accessing this page from the Wordpress dashboard.');
                        $result = 'fail';
                    } else {
                        // Check nonce
                        check_admin_referer('dsq-wpnonce_import', '_dsqimport_wpnonce');
                        if (!isset($_GET['last_comment_id'])) {
                            $last_comment_id = false;
                        } else {
                            $last_comment_id = $_GET['last_comment_id'];
                        }
                        if ($_GET['wipe'] == '1') {
                            $wpdb->query("DELETE FROM `" . $wpdb->prefix . "commentmeta` WHERE meta_key IN ('dsq_post_id', 'dsq_parent_post_id')");
                            $wpdb->query("DELETE FROM `" . $wpdb->prefix . "comments` WHERE comment_agent LIKE 'Disqus/%%'");
                        }
                        ob_start();
                        $response = dsq_sync_forum($last_comment_id, true);
                        $debug = ob_get_clean();
                        if (!$response) {
                            $status = 'error';
                            $result = 'fail';
                            $error = $dsq_api->get_last_error();
                            $msg = '<p class="status dsq-export-fail">' . dsq_i('There was an error downloading your comments from Disqus.') . '<br/>' . esc_attr($error) . '</p>';
                        } else {
                            list($comments, $last_comment_id) = $response;
                            if (!$comments) {
                                $status = 'complete';
                                $msg = dsq_i('Your comments have been downloaded from Disqus and saved in your local database.');
                            } else {
                                $status = 'partial';
                                $msg = dsq_i('Import in progress (last post id: %s)', $last_comment_id) . ' &hellip;';
                            }
                            $result = 'success';
                        }
                        $debug = explode("\n", $debug);
                        $response = compact('result', 'status', 'comments', 'msg', 'last_comment_id', 'debug');
                        header('Content-type: text/javascript');
                        echo cf_json_encode($response);
                        die;
                    }
                }
                break;
        }
    }
}
Exemple #4
0
function dsq_request_handler()
{
    global $dsq_response;
    global $dsq_api;
    global $post;
    global $wpdb;
    if (!empty($_GET['cf_action'])) {
        switch ($_GET['cf_action']) {
            case 'sync_comments':
                if (!($post_id = $_GET['post_id'])) {
                    header("HTTP/1.0 400 Bad Request");
                    die;
                }
                // schedule the event for 30 seconds from now in case they
                // happen to make a quick post
                if (DISQUS_DEBUG) {
                    dsq_sync_post($post_id);
                    $response = dsq_sync_forum();
                    if (!$response) {
                        die('// error: ' . $dsq_api->get_last_error());
                    } else {
                        list($last_comment_id, $comments) = $response;
                        die('// synced ' . $comments . ' comments');
                    }
                } else {
                    wp_schedule_single_event(time(), 'dsq_sync_post', array($post_id));
                    wp_schedule_single_event(time(), 'dsq_sync_forum');
                    die('// sync scheduled');
                }
                break;
            case 'export_comments':
                if (current_user_can('manage_options') && DISQUS_CAN_EXPORT) {
                    $timestamp = intval($_GET['timestamp']);
                    $post_id = intval($_GET['post_id']);
                    $limit = 2;
                    global $wpdb, $dsq_api;
                    $posts = $wpdb->get_results($wpdb->prepare("\n\t\t\t\t\t\tSELECT * \n\t\t\t\t\t\tFROM {$wpdb->posts} \n\t\t\t\t\t\tWHERE post_type != 'revision'\n\t\t\t\t\t\tAND post_status = 'publish'\n\t\t\t\t\t\tAND comment_count > 0\n\t\t\t\t\t\tAND ID > %d\n\t\t\t\t\t\tORDER BY ID ASC\n\t\t\t\t\t\tLIMIT {$limit}\n\t\t\t\t\t", $post_id));
                    $first_post_id = $posts[0]->ID;
                    $last_post_id = $posts[count($posts) - 1]->ID;
                    $max_post_id = $wpdb->get_var($wpdb->prepare("\n\t\t\t\t\t\tSELECT MAX(ID)\n\t\t\t\t\t\tFROM {$wpdb->posts} \n\t\t\t\t\t\tWHERE post_type != 'revision'\n\t\t\t\t\t\tAND post_status = 'publish'\n\t\t\t\t\t\tAND comment_count > 0\n\t\t\t\t\t\tAND ID > %d\n\t\t\t\t\t", $post_id));
                    $eof = (int) ($last_post_id == $max_post_id);
                    if ($eof) {
                        $status = 'complete';
                        $msg = 'Your comments have been sent to Disqus and queued for import!<br/><a href="' . DISQUS_IMPORTER_URL . '" target="_blank">See the status of your import at Disqus</a>';
                    } else {
                        $status = 'partial';
                        if (count($posts) == 1) {
                            $msg = dsq_i('Processed comments on post #%s&hellip;', $first_post_id);
                        } else {
                            $msg = dsq_i('Processed comments on posts #%s-%s&hellip;', $first_post_id, $last_post_id);
                        }
                    }
                    $result = 'fail';
                    $response = null;
                    if (count($posts)) {
                        include_once dirname(__FILE__) . '/export.php';
                        $wxr = dsq_export_wp($posts);
                        $response = $dsq_api->import_wordpress_comments($wxr, $timestamp, $eof);
                        if (!($response['group_id'] > 0)) {
                            $result = 'fail';
                            $msg = '<p class="status dsq-export-fail">' . dsq_i('Sorry, something unexpected happened with the export. Please <a href="#" id="dsq_export_retry">try again</a></p><p>If your API key has changed, you may need to reinstall Disqus (deactivate the plugin and then reactivate it). If you are still having issues, refer to the <a href="%s" onclick="window.open(this.href); return false">WordPress help page</a>.', 'http://disqus.com/help/wordpress') . '</p>';
                            $response = $dsq_api->get_last_error();
                        } else {
                            if ($eof) {
                                $msg = dsq_i('Your comments have been sent to Disqus and queued for import!<br/><a href="%s" target="_blank">See the status of your import at Disqus</a>', $response['link']);
                            }
                            $result = 'success';
                        }
                    }
                    // send AJAX response
                    $response = compact('result', 'timestamp', 'status', 'last_post_id', 'msg', 'eof', 'response');
                    header('Content-type: text/javascript');
                    echo cf_json_encode($response);
                    die;
                }
                break;
            case 'import_comments':
                if (current_user_can('manage_options')) {
                    if (!isset($_GET['last_comment_id'])) {
                        $last_comment_id = false;
                    } else {
                        $last_comment_id = $_GET['last_comment_id'];
                    }
                    if ($_GET['wipe'] == '1') {
                        $wpdb->query("DELETE FROM `" . $wpdb->prefix . "commentmeta` WHERE meta_key IN ('dsq_post_id', 'dsq_parent_post_id')");
                        $wpdb->query("DELETE FROM `" . $wpdb->prefix . "comments` WHERE comment_agent LIKE 'Disqus/%%'");
                    }
                    ob_start();
                    $response = dsq_sync_forum($last_comment_id);
                    $debug = ob_get_clean();
                    if (!$response) {
                        $status = 'error';
                        $result = 'fail';
                        $error = $dsq_api->get_last_error();
                        $msg = '<p class="status dsq-export-fail">' . dsq_i('There was an error downloading your comments from Disqus.') . '<br/>' . htmlspecialchars($error) . '</p>';
                    } else {
                        list($comments, $last_comment_id) = $response;
                        if (!$comments) {
                            $status = 'complete';
                            $msg = dsq_i('Your comments have been downloaded from Disqus and saved in your local database.');
                        } else {
                            $status = 'partial';
                            $msg = dsq_i('Import in progress (last post id: %s) &hellip;', $last_comment_id);
                        }
                        $result = 'success';
                    }
                    $debug = explode("\n", $debug);
                    $response = compact('result', 'status', 'comments', 'msg', 'last_comment_id', 'debug');
                    header('Content-type: text/javascript');
                    echo cf_json_encode($response);
                    die;
                }
                break;
        }
    }
}