Skip to content

Instantly share code, notes, and snippets.

@robwent
Last active August 29, 2015 14:19
Show Gist options
  • Save robwent/c2a06fce388add738cb0 to your computer and use it in GitHub Desktop.
Save robwent/c2a06fce388add738cb0 to your computer and use it in GitHub Desktop.
Exports K2 comments to be imported into Disqus (Wordpress format)
<?php
// Turn off all error reporting
error_reporting(0);
/**
* Constant that is checked in included files to prevent direct access.
* define() is used in the installation folder rather than "const" to not error for PHP 5.2 and lower
*/
define('_JEXEC', 1);
define( 'DS', DIRECTORY_SEPARATOR );
if (file_exists(__DIR__ . '/defines.php'))
{
include_once __DIR__ . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', __DIR__);
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';
// Mark afterLoad in the profiler.
JDEBUG ? $_PROFILER->mark('afterLoad') : null;
//Set the domain we're going to import to (no trailing slash). If blank the current domain will be used
$site = '';
// Instantiate the application.
$app = JFactory::getApplication('site');
$config = JFactory::getConfig();
$siteTitle = $config->get('sitename');
if ($site == '') {
$site = JURI::base();
}
$site = rtrim($site, '/');
//import the k2 router to construct the urls
require_once(JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'helpers'.DS.'route.php');
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Select all items with the needed parts to create the urls and the post title
$query
->select(array('a.id', 'a.alias', 'a.catid', 'a.title'))
->select($db->quoteName('b.alias', 'catalias'))
->from($db->quoteName('#__k2_items', 'a'))
->join('INNER', $db->quoteName('#__k2_categories', 'b') . ' ON (' . $db->quoteName('a.catid') . ' = ' . $db->quoteName('b.id') . ')')
->order($db->quoteName('a.id') . ' ASC');
//$query->setLimit('400');
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects.
$items = $db->loadObjectList();
$xmlItems = array();
//Loop through the posts and get the associated comments
foreach ($items as $item) {
$query = $db->getQuery(true);
$query
->select('*')
->from($db->quoteName('#__k2_comments'))
->where($db->quoteName('itemID') . ' = \''.$item->id.'\'')
->order($db->quoteName('id') . ' ASC');
$db->setQuery($query);
$comments = $db->loadObjectList();
if ($comments){
$itemLink = urldecode(JRoute::_(K2HelperRoute::getItemRoute($item->id.':'.urlencode($item->alias), $item->catid.':'.urlencode($item->catalias))));
$itemTitle = $item->title;
$item = "\t\t".'<item>'."\n";
$item .= "\t\t".'<title>'.$itemTitle.'</title>'."\n";
$item .= "\t\t".'<link>'.$site.$itemLink.'</link>'."\n";
foreach ($comments as $comment) {
$item .= "\t\t\t".'<wp:comment>'."\n";
$item .= "\t\t\t\t".'<wp:comment_id>'.$comment->id.'</wp:comment_id>'."\n";
$item .= "\t\t\t\t".'<wp:comment_author><![CDATA['.$comment->userName.']]></wp:comment_author>'."\n";
$item .= "\t\t\t\t".'<wp:comment_author_email><![CDATA['.$comment->commentEmail.']]></wp:comment_author_email>'."\n";
$item .= "\t\t\t\t".'<wp:comment_date>'.$comment->commentDate.'</wp:comment_date>'."\n";
$item .= "\t\t\t\t".'<wp:comment_date_gmt>'.$comment->commentDate.'</wp:comment_date_gmt>'."\n";
$item .= "\t\t\t\t".'<wp:comment_content><![CDATA['.$comment->commentText.']]></wp:comment_content>'."\n";
$item .= "\t\t\t\t".'<wp:comment_approved>'.$comment->published.'</wp:comment_approved>'."\n";
$item .= "\t\t\t\t".'<wp:comment_parent>0</wp:comment_parent>'."\n";
$item .= "\t\t\t".'</wp:comment>'."\n";
}
$item .= "\t\t".'</item>'."\n";
//push into the items
$xmlItems[] = $item;
}
}
// Output XML header.
$xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
$xml .= '<rss version="2.0" xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wp="http://wordpress.org/export/1.2/" >' . "\n";
// Output root element.
$xml .= '<channel>'."\n";
$xml .= '<title>'.$siteTitle.'</title>'."\n";
$xml .= '<link>'.$site.'</link>'."\n";
// Output the data.
if(!empty($xmlItems)){
foreach ($xmlItems as $xmlItem){
$xml .= $xmlItem;
}
}
// Terminate root element.
$xml .= '</channel>'."\n";
// Terminate rss element.
$xml .= '</rss>'."\n";
header('Content-disposition: attachment; filename=k2_comment_export.xml');
header ("Content-Type:text/xml; charset=utf-8");
//output the XML data
echo $xml;
header("Expires: 0");
@robwent
Copy link
Author

robwent commented Apr 16, 2015

good reminder for me there. I was limiting to 400 but I need to export 3000.

I'll comment that bit out...

edit That limits to 400 posts so if you have more posts than that you will need to change the limit or up the number

@dion1231
Copy link

Thanks Rob, I did change the limit and it worked! I also have sh404sef component running, is there a way to get the links from this instead of Joomla's SEF URLs? I've tried ways around this in Disqus's admin tools to do a redirect crawler which works in the admin panel however the threads don't actually show up on the site.

@dion1231
Copy link

As well is there a way to change the SQL query to filter from a category?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment