Last active
July 30, 2019 08:10
-
-
Save DonDebonair/6253634 to your computer and use it in GitHub Desktop.
Convert Comments in a MySQL database (in this case from the Diem CMS) to a Wordpress WXR file that can be imported by Disqus
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 | |
/* | |
Convert Comments in a MySQL database (in this case from the Diem CMS) to a Wordpress WXR file that can be imported by Disqus | |
usage: $ php export-comments-for-disqus-xml.php > comments.xml | |
*/ | |
$connection = mysql_connect('localhost', 'username', 'password') or die(mysql_error()); | |
$db = mysql_SELECT_db('databasename', $connection); | |
db_query("set character_set_client='utf8'"); | |
db_query("set character_set_results='utf8'"); | |
db_query("set collation_connection='utf8_general_ci'"); | |
$xml_txt = ''; | |
$xml_txt .= xml_open(); | |
$sql = "SELECT dm_page_translation.id, slug, dm_page_translation.title AS page_title, article.title AS article_title, article.id AS article_id, DATE_ADD(created_at, INTERVAL -1 HOUR) as date FROM dm_page_translation INNER JOIN dm_page ON dm_page_translation.id = dm_page.id INNER JOIN article ON dm_page.record_id = article.id"; | |
$rows = db_query_arr($sql); | |
if (is_array($rows)) { | |
foreach ($rows as $arr_page) { | |
$xml_txt .= '<item>'; | |
$xml_txt .= xml_comment_page($arr_page); | |
$xml_txt .= xml_comment_details($arr_page); | |
$xml_txt .= '</item>'; | |
} | |
} | |
$xml_txt .= xml_close(); | |
$xml_txt = trim($xml_txt); | |
echo $xml_txt; | |
function db_query($sql) | |
{ | |
$results = mysql_query($sql) or die($sql . ' ----- ' . mysql_error()); | |
return $results; | |
} | |
function db_query_arr($sql) | |
{ | |
$results = mysql_query($sql) or die($sql . ' ----- ' . mysql_error()); | |
if (is_bool($results)) { | |
return $results; | |
} else { | |
$final_arr = array(); | |
while ($row = mysql_fetch_array($results)) { | |
$final_arr[] = $row; | |
} | |
return $final_arr; | |
} | |
} | |
function xml_open() | |
{ | |
$str = ' | |
<?xml version="1.0" encoding="UTF-8"?> | |
<rss version="2.0" | |
xmlns:content="http://purl.org/rss/1.0/modules/content/" | |
xmlns:dsq="http://www.disqus.com/" | |
xmlns:dc="http://purl.org/dc/elements/1.1/" | |
xmlns:wp="http://wordpress.org/export/1.0/"> | |
<channel> | |
'; | |
return $str; | |
} | |
function xml_close() | |
{ | |
$str = ' | |
</channel> | |
</rss> | |
'; | |
return $str; | |
} | |
function xml_comment_page($arr) | |
{ | |
$str = ' | |
<title>' . $arr['article_title'] . '</title> | |
<link>' . 'http://dandydev.net/blog/' . $arr['slug'] . '.html</link> | |
<dsq:thread_identifier>' . $arr['slug'] . '</dsq:thread_identifier> | |
<wp:post_date_gmt>' . $arr['date'] . '</wp:post_date_gmt> | |
<wp:comment_status>open</wp:comment_status> | |
'; | |
return $str; | |
} | |
function xml_comment_details($arr) | |
{ | |
$str = ''; | |
$sql = "SELECT id, record_id as article_id, author_name, author_email, author_website, body, DATE_ADD(created_at, INTERVAL -1 HOUR) as date FROM dm_comment WHERE record_id = " . $arr['article_id'] . " ORDER BY ID"; | |
$cm_arr = db_query_arr($sql); | |
if (is_array($cm_arr)) { | |
foreach ($cm_arr as $cmt) { | |
$str .= ' | |
<wp:comment> | |
<wp:comment_id>' . $cmt['id'] . '</wp:comment_id> | |
<wp:comment_author>' . ($cmt['author_email'] != 'daan@dandydev.net' ? $cmt['author_name'] : 'Daan Debie') . '</wp:comment_author> | |
<wp:comment_author_email>' . ($cmt['author_email'] != 'daan@dandydev.net' ? $cmt['author_email'] : 'debie.daan@gmail.com') . '</wp:comment_author_email> | |
<wp:comment_author_url>' . $cmt['author_website'] . '</wp:comment_author_url> | |
<wp:comment_author_IP></wp:comment_author_IP> | |
<wp:comment_date_gmt>' . $cmt['date'] . '</wp:comment_date_gmt> | |
<wp:comment_content><![CDATA[' . $cmt['body'] . ']]></wp:comment_content> | |
<wp:comment_approved>1</wp:comment_approved> | |
<wp:comment_parent>0</wp:comment_parent> | |
</wp:comment> | |
'; | |
} | |
} | |
return $str; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing this! Saved me a lot of time of figuring this out by myself 👍