Last active
December 15, 2015 02:19
-
-
Save ryandemmer/5185962 to your computer and use it in GitHub Desktop.
Ninjaboard to EasyDiscuss converter Based on the Ninjaboard to Kunena converter from AmmoniteNetworks - https://github.com/AmmoniteNetworks/Ninjaboard-To-Kunena-Converter/blob/master/cli/ninjaboardToKunena.php
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 | |
// Make sure we're being called from the command line, not a web interface | |
if (array_key_exists('REQUEST_METHOD', $_SERVER)) | |
die(); | |
// Set flag that this is a parent file. | |
define('_JEXEC', 1); | |
define('DS', DIRECTORY_SEPARATOR); | |
error_reporting(E_ALL | E_NOTICE); | |
ini_set('display_errors', 1); | |
// Load system defines | |
if (file_exists(dirname(__FILE__) . '/defines.php')) { | |
require_once dirname(dirname(__FILE__)) . '/defines.php'; | |
} | |
if (!defined('_JDEFINES')) { | |
define('JPATH_BASE', dirname(dirname(__FILE__))); | |
require_once JPATH_BASE . '/includes/defines.php'; | |
} | |
require_once JPATH_LIBRARIES . '/import.php'; | |
require_once JPATH_LIBRARIES . '/cms.php'; | |
// Load the configuration | |
require_once JPATH_CONFIGURATION . '/configuration.php'; | |
define( 'DISCUSS_POSTER_MEMBER' , 'member' ); | |
define( 'DISCUSS_MEDIA' , JPATH_ROOT . '/media/com_easydiscuss' ); | |
define( 'DISCUSS_ID_UNPUBLISHED' , 0 ); | |
define( 'DISCUSS_ID_PUBLISHED' , 1 ); | |
define( 'DISCUSS_ID_SCHEDULED' , 2 ); | |
define( 'DISCUSS_ID_DRAFT' , 3 ); | |
define( 'DISCUSS_ID_PENDING' , 4 ); | |
class MigrateNinjaBoard extends JApplicationCli { | |
public function rebuildOrdering($parentId = null, $leftId = 0 ) | |
{ | |
$db = JFactory::getDBO(); | |
$query = 'select `id` from `#__discuss_category`'; | |
$query .= ' where parent_id = ' . $db->Quote( $parentId ); | |
$query .= ' order by lft'; | |
$db->setQuery( $query ); | |
$children = $db->loadObjectList(); | |
// The right value of this node is the left value + 1 | |
$rightId = $leftId + 1; | |
// execute this function recursively over all children | |
foreach ($children as $node) | |
{ | |
// $rightId is the current right value, which is incremented on recursion return. | |
// Increment the level for the children. | |
// Add this item's alias to the path (but avoid a leading /) | |
$rightId = $this->rebuildOrdering($node->id, $rightId ); | |
// If there is an update failure, return false to break out of the recursion. | |
if ($rightId === false) return false; | |
} | |
// We've got the left value, and now that we've processed | |
// the children of this node we also know the right value. | |
$updateQuery = 'update `#__discuss_category` set'; | |
$updateQuery .= ' `lft` = ' . $db->Quote( $leftId ); | |
$updateQuery .= ', `rgt` = ' . $db->Quote( $rightId ); | |
$updateQuery .= ' where `id` = ' . $db->Quote($parentId); | |
$db->setQuery($updateQuery); | |
// If there is an update failure, return false to break out of the recursion. | |
if (! $db->query()) | |
{ | |
return false; | |
} | |
// Return the right value of this node + 1. | |
return $rightId + 1; | |
} | |
public function insertForum($NBForum) { | |
$db = JFactory::getDBO(); | |
$Forum = NEW stdClass; | |
$Forum -> id = $NBForum -> ninjaboard_forum_id; | |
$Forum -> parent_id = $NBForum -> parent_id; | |
$Forum -> title = $NBForum -> title; | |
$Forum -> published = '1'; | |
$Forum -> description = $NBForum -> description; | |
$Forum -> alias = strtolower(preg_replace(array('#[^a-z0-9 ]#i', '#\s+#'), array('', '-'), $NBForum -> title)); | |
$query = 'SELECT ninjaboard_forum_id FROM #__ninjaboard_forums WHERE parent_id = ' . (int) $NBForum -> ninjaboard_forum_id . ' ORDER BY ninjaboard_forum_id'; | |
$db->setQuery($query); | |
$result = $db->loadResultArray(); | |
$total = count($result); | |
if ($total) { | |
$Forum -> lft = (int) $result[0] - 1; | |
//$Forum -> rgt = (int) $result[$total - 1] + 1; | |
} else { | |
$Forum -> lft = $NBForum -> ninjaboard_forum_id + 1; | |
//$Forum -> rgt = $NBForum -> ninjaboard_forum_id + 2; | |
} | |
$db -> insertObject('#__discuss_category', $Forum, 'id') or die($db -> stderr(TRUE)); | |
$this->rebuildOrdering($Forum -> parent_id, $Forum -> lft); | |
} | |
function insertMessage($topic, $post) { | |
$db = JFactory::getDBO(); | |
/*$item->set( 'content' , $content ); | |
$item->set( 'title' , $kItem->subject ); | |
$item->set( 'category_id' , $this->getNewCategory( $kItem ) ); | |
$item->set( 'user_id' , $kItem->userid ); | |
$item->set( 'user_type' , DISCUSS_POSTER_MEMBER ); | |
$item->set( 'hits' , $kItem->hits ); | |
$item->set( 'created' , DiscussHelper::getDate( $kItem->time )->toMySQL() ); | |
$item->set( 'modified' , DiscussHelper::getDate( $kItem->time )->toMySQL() ); | |
$item->set( 'replied' , DiscussHelper::getDate( $kItem->time )->toMySQL() ); | |
$item->set( 'poster_name' , $kItem->name ); | |
$item->set( 'parent_id' , 0 );*/ | |
$juser = JFactory::getUser(); | |
if (!$post -> created_user_id || !$juser->load($post -> created_user_id)) { | |
return false; | |
} | |
$Message = new stdClass; | |
$Message -> id = $post -> ninjaboard_post_id; | |
if ($topic -> first_post_id == $post -> ninjaboard_post_id) { | |
$Message -> parent_id = '0'; | |
} else { | |
$Message -> parent_id = $topic -> first_post_id; | |
} | |
$Message -> title = $post -> subject; | |
$Message -> category_id = $topic -> forum_id; | |
$Message -> user_id = $post -> created_user_id; | |
//$Message -> poster_name = $user->name; | |
$Message -> user_type = DISCUSS_POSTER_MEMBER; | |
$Message -> created = $post -> created_time; | |
$Message -> modified = $post -> modified; | |
$Message -> hits = $topic -> hits; | |
$text = $post -> text; | |
// clean up quotes | |
if (strpos($text, '[quote') !== false) { | |
$text = preg_replace('#\[(\/)?quote[^\]]+\]#i', '[$1quote]', $text); | |
} | |
$Message -> content = $text; | |
$Message -> content_type = preg_match('#\[(quote|code|url=|img=)#i', $text) ? 'bbcode' : ''; | |
$Message -> published = DISCUSS_ID_PUBLISHED; | |
$db->setQuery('SELECT created_time FROM #__ninjaboard_posts WHERE ninjaboard_topic_id = ' . (int) $post -> ninjaboard_topic_id . ' AND ninjaboard_post_id > ' . (int) $post -> ninjaboard_post_id); | |
$replied = $db->loadColumn(); | |
if ($replied) { | |
$Message -> replied = $replied[0]; | |
} | |
$Message -> alias = strtolower(preg_replace(array('#[^a-z0-9 ]#i', '#\s+#'), array('', '-'), $post -> subject)); | |
if (!$db -> insertObject('#__discuss_posts', $Message, 'id')) { | |
echo $db -> stderr(TRUE); | |
return false; | |
} | |
jimport('joomla.filesystem.folder'); | |
jimport('joomla.filesystem.file'); | |
jimport('joomla.utilities.utility'); | |
if (!is_dir(DISCUSS_MEDIA . '/attachments')) { | |
JFolder::create(DISCUSS_MEDIA . '/attachments'); | |
} | |
// attachments | |
$query = "SELECT * FROM #__ninjaboard_attachments WHERE post_id = " . (int) $post -> ninjaboard_post_id; | |
$db -> setQuery($query); | |
$item = $db->loadObject(); | |
if ($item) { | |
$attach = new StdClass(); | |
$attach->id = $item->ninjaboard_attachment_id; | |
$attach->uid = $item->post_id; | |
$attach->title = $item->params; | |
$attach->path = JUtility::getHash( trim($item->params) . $post -> created_time ); | |
if (is_file(JPATH_SITE . '/media/com_ninjaboard/attachments/' . $item->file)) { | |
if (JFile::copy(JPATH_BASE . '/media/com_ninjaboard/attachments/' . $item->file, DISCUSS_MEDIA . '/attachments/' . $attach->path)) { | |
$attach->size = filesize(DISCUSS_MEDIA . '/attachments/' . $attach->path); | |
$attach->created = $post -> created_time; | |
if (preg_match('#\.(jpg|jpeg|png|gif|zip)$#i', $item->file, $matches)) { | |
$type = $matches[1]; | |
if (preg_match('#(jpg|jpeg|png|gif)#i', $type)) { | |
require_once JPATH_ROOT . '/components/com_easydiscuss/classes/simpleimage.php'; | |
$image = new SimpleImage; | |
@$image->load( DISCUSS_MEDIA . '/attachments/' . $attach->path ); | |
@$image->resizeToFill( 160 , 120 ); | |
@$image->save( DISCUSS_MEDIA . '/attachments/' . $attach->path . '_thumb', $image->image_type); | |
} | |
if ($type == 'jpg') { | |
$type = 'jpeg'; | |
} | |
$attach->mime = $type == 'zip' ? 'application/zip' : 'image/' . $type; | |
} | |
$attach->type = $Message -> parent_id ? 'replies' : 'questions'; | |
$attach->published = 1; | |
$db -> insertObject('#__discuss_attachments', $attach, 'id'); | |
} | |
} | |
} | |
// migrate user | |
$db->setQuery("SELECT * FROM #__ninjaboard_people WHERE ninjaboard_person_id = " . (int) $post -> created_user_id); | |
$item = $db->loadObject(); | |
if (!is_dir(DISCUSS_MEDIA . '/avatars')) { | |
JFolder::create(DISCUSS_MEDIA . '/avatars'); | |
} | |
if ($item) { | |
$user = new StdClass(); | |
$user->id = $item->ninjaboard_person_id; | |
$user->signature = $item->signature; | |
$user->nickname = $juser->name; | |
if ($item -> avatar != '/media/com_ninjaboard/images/avatar.png' && !empty($item -> avatar)) { | |
// copy avatar | |
if (is_file(JPATH_SITE . $item -> avatar)) { | |
if (JFile::copy(JPATH_SITE . $item -> avatar, DISCUSS_MEDIA . '/avatars/' . basename($item -> avatar))) { | |
$user -> avatar = basename($item -> avatar); | |
} | |
} | |
} | |
$db->setQuery("SELECT COUNT(id) FROM #__discuss_users WHERE id = " . (int) $user->id); | |
if ($db->loadResult() == 0) { | |
$db -> insertObject('#__discuss_users', $user, 'id'); | |
} | |
} | |
} | |
function insertSubscriptions($topic) { | |
$db = JFactory::getDBO(); | |
$limit = strtotime('6 months ago'); | |
$date = JFactory::getDate($limit) -> toMySQL(); | |
// notifications | |
$db->setQuery('SELECT created_on, created_by FROM #__ninjaboard_subscriptions WHERE subscription_type_id = ' . (int) $topic -> ninjaboard_topic_id); | |
$items = $db->loadObjectList(); | |
if (!empty($items)) { | |
foreach($items as $item) { | |
$sub = new StdClass(); | |
$user = JFactory::getUser(); | |
if ($user->load($item->created_by) && $item->created_on > $date) { | |
$sub->userid = (int) $user->id; | |
$sub->type = 'post'; | |
$sub->cid = (int) $topic -> first_post_id; | |
$sub->email = $user->email; | |
$sub->fullname = $user->name; | |
$sub->interval = 'instant'; | |
$sub->created = $item->created_on; | |
$sub->sent_out = $item->created_on; | |
$sub->member = 1; | |
$db -> insertObject('#__discuss_subscription', $sub, 'id'); | |
} | |
} | |
} | |
} | |
function clearTables() { | |
$db = JFactory::getDBO(); | |
$query = "TRUNCATE #__discuss_category"; | |
$db -> setQuery($query); | |
$result = $db -> query(); | |
if ($result) { | |
$this -> out('TRUNCATE #__discuss_category success'); | |
} | |
$query = "TRUNCATE #__discuss_posts"; | |
$db -> setQuery($query); | |
$result = $db -> query(); | |
if ($result) { | |
$this -> out('TRUNCATE #__discuss_posts success'); | |
} | |
$query = "TRUNCATE #__discuss_users"; | |
$db -> setQuery($query); | |
$result = $db -> query(); | |
if ($result) { | |
$this -> out('TRUNCATE #__discuss_users success'); | |
} | |
$query = "TRUNCATE #__discuss_migrators"; | |
$db -> setQuery($query); | |
$result = $db -> query(); | |
if ($result) { | |
$this -> out('TRUNCATE #__discuss_migrators success'); | |
} | |
$query = "TRUNCATE #__discuss_acl_group"; | |
$db -> setQuery($query); | |
$result = $db -> query(); | |
if ($result) { | |
$this -> out('TRUNCATE #__discuss_acl_group success'); | |
} | |
$query = "TRUNCATE #__discuss_views"; | |
$db -> setQuery($query); | |
$result = $db -> query(); | |
if ($result) { | |
$this -> out('TRUNCATE #__discuss_views success'); | |
} | |
$query = "TRUNCATE #__discuss_attachments"; | |
$db -> setQuery($query); | |
$result = $db -> query(); | |
if ($result) { | |
$this -> out('TRUNCATE #__discuss_attachments success'); | |
} | |
$query = "TRUNCATE #__discuss_tags"; | |
$db -> setQuery($query); | |
$result = $db -> query(); | |
if ($result) { | |
$this -> out('TRUNCATE #__discuss_tags success'); | |
} | |
$query = "TRUNCATE #__discuss_subscription"; | |
$db -> setQuery($query); | |
$result = $db -> query(); | |
if ($result) { | |
$this -> out('TRUNCATE #__discuss_subscription'); | |
} | |
} | |
public function execute() { | |
$this -> out('MIGRATING NINJABOARD'); | |
$this -> out('============================'); | |
jimport('joomla.database.database'); | |
jimport('joomla.utilities.date'); | |
jimport('joomla.user.user'); | |
jimport('joomla.filesystem.folder'); | |
// Purge all old records | |
$db = JFactory::getDBO(); | |
$this -> clearTables(); | |
// clear attachments and avatars | |
if (JFolder::delete(DISCUSS_MEDIA . '/attachments')) { | |
$this -> out('DELETE FOLDER "attachments"'); | |
} | |
if (JFolder::delete(DISCUSS_MEDIA . '/avatars')) { | |
$this -> out('DELETE FOLDER "avatars"'); | |
} | |
// Get the Forums | |
$this -> out('MIGRATING NINJABOARD - FORUMS'); | |
$query = "Select * from `#__ninjaboard_forums`"; | |
$db -> setQuery($query); | |
$forums = $db -> loadObjectList(); | |
foreach($forums as $forum) { | |
$this -> insertForum($forum); | |
// OK getting Ninjaboard Topics and messages and converting and adding them to discussions | |
$this -> out('Getting Ninjaboard Topics and Posts for forum ' . $forum -> title); | |
$query = "Select * from `#__ninjaboard_topics` WHERE forum_id = " . $forum -> ninjaboard_forum_id; | |
$db -> setQuery($query); | |
$topics = $db -> loadObjectList(); | |
foreach ($topics as $topic) { | |
// create subscriptions | |
$this -> insertSubscriptions($topic); | |
$postQuery = "SELECT * FROM #__ninjaboard_posts where ninjaboard_topic_id = " . $topic -> ninjaboard_topic_id; | |
$db -> setQuery($postQuery); | |
$posts = $db -> loadObjectList(); | |
foreach ($posts as $post) { | |
$this -> insertMessage($topic, $post); | |
sleep(10); | |
} | |
} | |
} | |
} | |
} | |
// DO THE DO | |
JApplicationCli::getInstance('MigrateNinjaBoard') -> execute(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment