<?php
error_reporting(E_ALL);
//
// Include The configuration file
//
include('config.php');
//
// Create a new connection to the Wordpress Database
//
$db = new ezSQL_mysql($wp_db_user, $wp_db_pass, $wp_db, $wp_db_host);
echo "Initialised WP DB<br/>";
//
// Create a connection to the Joomla Database
//
$jb = new ezSQL_mysql($j_db_user, $j_db_pass, $j_db, $j_db_host);
echo "Initialised JOS DB<br/>";
//
// Create a new table in your Joomla database to store the new
// category and section ID's so that we can map posts and articles to them. We will destroy this table as one of the final steps
//
$jb->query( "CREATE TABLE `jos_wp_category_map` (`wp_cat_id` INT NOT NULL ,`jos_cat_id` INT NOT NULL ,`jos_sec_id` INT NOT NULL) ENGINE = MYISAM ");
echo "Created Category Map table<br>";
//
// This function iterates through the categories and children and updates the ID's
//
function updateCategory($category, $newParentID)
{
global $db, $jb, $wp_prefix, $j_prefix;
$children = $db->get_results("SELECT t.term_id as term_id, t.name as tname, t.slug as tslug, tt.description as description, tt.parent as parent FROM ".$wp_prefix."terms AS t INNER JOIN ".$wp_prefix."term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('category') and parent=".$category." ORDER BY t.name");
foreach($children as $child) {
updateCategory($child->term_id, $newParentID);
//Check if we are insterting into 1.5 or 1.0.x
if($j_version=='1.5') {
//Insert the latest category
$jb->query("insert into ".$j_prefix."categories (title, alias, section, description, published) Values('".$child->tname."', '".$child->tslug."', '".$newParentID."', '".$child->description."', 1)");
}
else
{
//Insert a matching Category for this top level category (now exists as Joomla Section and Category)
$jb->query("insert into ".$j_prefix."categories (title, name, section, description, published) Values('".$child->tname."', '".$child->tslug."', '".$newParentID."', '".$child->description."', 1)");
}
// Insert into our temp storage/reference table
$jb->query("insert into jos_wp_category_map (wp_cat_id, jos_cat_id, jos_sec_id) values(".$child->term_id.", ".$jb->insert_id.", ".$newParentID.")");
}
}
//
// This query grabs all of the categories from the wp tables, wp stores categories in a taxonomy format, which essentially means that there
// is a multipurpose table which holds everything from categories, to tags, this consists of 3 tables in total, but we just want to grab information
// from 2 of them, term_taxonomy and terms. We could limit this query to grab only what we want, but I use the full query for anyone interested
// in seeing exactly what information is stored for category in Wordpress, more an FYI query than being specific and to the point, and does not harm
// since we will in 99% of cases be grabbing less than 1000 rows.
$categories = $db->get_results("SELECT t.term_id as term_id, t.name as tname, t.slug as tslug, tt.description as description, tt.parent as parent FROM ".$wp_prefix."terms AS t INNER JOIN ".$wp_prefix."term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('category') and parent=0 ORDER BY t.name");
echo "<br/>Categories Selected from WP";
//
// Loop through the results array and work our magic
//
foreach($categories as $category) {
//Check if we are insterting into 1.5 or 1.0.x
if($j_version=='1.5') {
// Insert the Parent Category into Joomla as a "Section"
$jb->query("insert into ".$j_prefix."sections (title, alias, scope, description, published) values ('".$category->tname."', '".$category->tslug."', 'content', '".$category->description."', 1)");
$newParentID = $jb->insert_id;
//Insert the section as a category of itself
$jb->query("insert into ".$j_prefix."categories (title, alias, section, description, published) Values ('".$category->tname."', '".$category->tslug."', '".$newParentID."', '".$category->description."', 1)");
}
else
{
// Insert the Parent Category into Joomla as a "Section"
$jb->query("insert into ".$j_prefix."sections (title, name, scope, description, published) values ('".$category->tname."', '".$category->tslug."', 'content', '".$category->description."', 1)");
$newParentID = $jb->insert_id;
//Insert as a category to it's own section
$jb->query("insert into ".$j_prefix."categories (title, name, section, description, published) Values ('".$category->tname."', '".$category->tname."', '".$newParentID."', '".$category->description."', 1)");
}
// Insert into our temp storage/reference table
$jb->query("insert into jos_wp_category_map (wp_cat_id, jos_cat_id, jos_sec_id) values(".$category->term_id.", ".$jb->insert_id.", ".$newParentID.")");
//Jump into the recursive function and update all of the subcategories and align them in a Joomla! style of section vs Category
updateCategory($category->term_id, $newParentID);
}
echo "Category Import completed";
?> |