Last active
March 29, 2020 03:44
-
-
Save prahladyeri/e22e4e232416ff841be670601b396c62 to your computer and use it in GitHub Desktop.
Tool to import disqus comments to the wordpress system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Tool to migrate disqus comments to the wordpress system. | |
* | |
* @author Prahlad Yeri<prahladyeri@yahoo.com> | |
* @date 2017-09-06 | |
* | |
* Updated on 2020-03-11: fixes and refactoring. | |
* */ | |
require_once('disqus_parse.php'); | |
$sleep_interval = 2; | |
if( php_sapi_name() !== 'cli' ) {die("Meant to be run from command line");} | |
if (count($argv) < 2) { print("Incorrect arguments. Provide path to file\n"); exit; } | |
$comments = parse_disqus_file($argv[1]); | |
function find_wordpress_base_path() { | |
$dir = dirname(__FILE__); | |
do { | |
//it is possible to check for other files here | |
if( file_exists($dir."/wp-config.php") ) { | |
return $dir; | |
} | |
} while( $dir = realpath("$dir/..") ); | |
return null; | |
} | |
define( 'BASE_PATH', find_wordpress_base_path()."/" ); | |
define('WP_USE_THEMES', false); | |
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header; | |
require(BASE_PATH . 'wp-load.php'); | |
print(BASE_PATH.'wp-load.php'."\n"); | |
print("Ready."); | |
$q = new WP_Query(array('post_type'=>'post', 'posts_per_page' => -1)); | |
$cnt = 0; | |
$processed = []; | |
while($q->have_posts()) { | |
$t_post = $q->the_post(); | |
$t_title = get_the_title($t_post); | |
//$t_title = the_title(); | |
$t_url = get_permalink($t_post); | |
$t_url = str_replace("http://", "", $t_url); | |
$t_url = str_replace("/apps/wp", "", $t_url); | |
$idx = strpos($t_url, "/"); | |
$t_url = substr($t_url, $idx); | |
array_push($processed, $t_url); | |
echo 'processing '. $t_title ."\n"; | |
$arr = array(); | |
foreach($comments as $comment){ | |
$idx = strpos($comment['url'], "/"); | |
$c_url = substr($comment['url'],$idx); | |
$cpath = parse_url($comment['url'], PHP_URL_PATH); | |
$tpath = parse_url($t_url, PHP_URL_PATH); | |
if ($cpath === $tpath) { | |
$cnt++; | |
if (!array_search($post->ID.$comment['email'].$comment['body'], $arr)) { | |
if (is_array($comment['name'])) { | |
$comment_author = (string)$comment['name'][0]; | |
} | |
else { | |
$comment_author = (string)$comment['name']; | |
} | |
$comment_id = wp_new_comment(array( | |
'comment_post_ID' => $post->ID, | |
'comment_author' => $comment_author, | |
'comment_author_email' => (string)$comment['email'], | |
'comment_author_url' => '', | |
'comment_content' => $comment['body'], | |
'comment_type' => '',//empty for regular comments, 'pingback' for pingbacks, 'trackback' for trackbacks | |
'comment_parent' => 0, //0 if it's not a reply to another comment; | |
'comment_date' => $comment['created_at'], | |
//'user_id' => $current_user->ID, | |
), true); | |
if ( is_wp_error( $comment_id ) ) { | |
echo "ERROR OCCURRED: "; | |
print_r( $comment_id->get_error_message() ); | |
} | |
} | |
array_push($arr, $post->ID.$comment['email'].$comment['body']); | |
sleep($sleep_interval); | |
} | |
else { | |
echo "Match not found:\n"; | |
echo $t_url."\n"; | |
echo $c_url."\n\n"; | |
} | |
} | |
} | |
echo "$cnt comments imported.\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment