Skip to content

Instantly share code, notes, and snippets.

@ethanliu
Created September 20, 2013 09:04
Show Gist options
  • Save ethanliu/6634987 to your computer and use it in GitHub Desktop.
Save ethanliu/6634987 to your computer and use it in GitHub Desktop.
Convert xml exported from disqus to wordpress format for importing back to disuqs
<?php
/**
* Convert xml exported from disqus to wordpress format for importing back to disuqs
*
* @author Ethan Liu <ethan@creativecrap.com>
* @copyright Creativecrap.com, 20 September, 2013
**/
$feed = new SimpleXMLElement(file_get_contents("src.xml"));
$threads = array();
$posts = array();
// collecting threads
foreach ($feed->thread as $thread) {
$thread_id = intval($thread->attributes('dsq', true)->id);
$threads[$thread_id] = array(
'title' => (string)$thread->title,
'link' => (string)$thread->link,
'content' => (string)$thread->forum,
'thread_identifier' => (string)$thread->id,
'post_date_gmt' => date("Y-m-d H:i:s", strtotime($thread->createdAt)),
);
}
// collecting posts
foreach ($feed->post as $post) {
$post_id = intval($post->attributes('dsq', true)->id);
$thread_id = intval($post->thread->attributes('dsq', true)->id);
$parent_id = intval($post->parent->attributes('dsq', true)->id);
$posts[$thread_id][] = array(
'comment_id' => $post_id,
// 'comment_id' => (string)$post->id,
'comment_author' => (string)$post->author->name,
'comment_author_email' => (string)$post->author->email,
'comment_author_url' => '',
'comment_author_IP' => (string)$post->ipAddress,
'comment_date_gmt' => date("Y-m-d H:i:s", strtotime($post->createdAt)),
'comment_content' => (string)$post->message,
'comment_parent' => $parent_id,
);
}
?>
<?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>
<?php
foreach ($threads as $thread_id => $thread) {
?>
<item>
<title><?php echo $thread['title']; ?></title>
<link><?php echo $thread['link']; ?></link>
<content:encoded><![CDATA[]]></content:encoded>
<dsq:thread_identifier><?php echo $thread['thread_identifier']; ?></dsq:thread_identifier>
<wp:post_date_gmt><?php echo $thread['post_date_gmt']; ?></wp:post_date_gmt>
<wp:comment_status>open</wp:comment_status>
<?php
foreach ($posts[$thread_id] as $post) {
?>
<wp:comment>
<wp:comment_id><?php echo $post['comment_id']; ?></wp:comment_id>
<wp:comment_author><?php echo $post['comment_author']; ?></wp:comment_author>
<wp:comment_author_email><?php echo $post['comment_author_email']; ?></wp:comment_author_email>
<wp:comment_author_url><?php echo $post['comment_author_url']; ?></wp:comment_author_url>
<wp:comment_author_IP><?php echo $post['comment_author_IP']; ?></wp:comment_author_IP>
<wp:comment_date_gmt><?php echo $post['comment_date_gmt']; ?></wp:comment_date_gmt>
<wp:comment_content><![CDATA[<?php echo $post['comment_content']; ?>]]></wp:comment_content>
<wp:comment_approved>1</wp:comment_approved>
<wp:comment_parent><?php echo $post['comment_parent']; ?></wp:comment_parent>
</wp:comment>
<?php
}
?>
</item>
<?php
}
?>
</channel>
</rss>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment