get_var('SELECT cat_ID FROM '.$wpdb->categories.' WHERE category_nicename="'.$category_nicename.'"');
return $name;
}
}
if(!function_exists('get_comment_count'))
{
function get_comment_count($post_ID)
{
global $wpdb;
return $wpdb->get_var('SELECT count(*) FROM '.$wpdb->comments.' WHERE comment_post_ID = '.$post_ID);
}
}
if(!function_exists('link_exists'))
{
function link_exists($linkname)
{
global $wpdb;
return $wpdb->get_var('SELECT link_id FROM '.$wpdb->links.' WHERE link_name = "'.$wpdb->escape($linkname).'"');
}
}
/**
The Main Importer Class
**/
class Geeklog_Import {
function header()
{
echo '
';
echo '
'.__('Import GeekLog').'
';
echo '
'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'
';
}
function footer()
{
echo '
';
}
function greet()
{
echo ''.__('This importer allows you to extract topics, posts, users, and comments from GeekLog 1.3.9 into your blog. This has not been tested on any other versions of GeekLog. Mileage may vary.').'
';
echo ''.__('Please enter your GeekLog database onfiguration settings:').'
';
echo '';
}
function get_gl_cats()
{
global $wpdb;
// General Housekeeping
$gldb = new wpdb(get_option('gluser'), get_option('glpass'), get_option('glname'), get_option('glhost'));
set_magic_quotes_runtime(0);
$prefix = get_option('tpre');
// Get Categories
return $gldb->get_results('SELECT
tid,
topic
FROM '.$prefix.'topics',
ARRAY_A);
}
function get_gl_users()
{
global $wpdb;
// General Housekeeping
$gldb = new wpdb(get_option('gluser'), get_option('glpass'), get_option('glname'), get_option('glhost'));
set_magic_quotes_runtime(0);
$prefix = get_option('tpre');
// Get Users
return $gldb->get_results('SELECT
uid,
username,
passwd,
fullname,
email,
homepage,
regdate
FROM '.$prefix.'users', ARRAY_A);
}
function get_gl_posts()
{
// General Housekeeping
$gldb = new wpdb(get_option('gluser'), get_option('glpass'), get_option('glname'), get_option('glhost'));
set_magic_quotes_runtime(0);
$prefix = get_option('tpre');
// Get Posts
return $gldb->get_results('SELECT
sid,
uid,
tid,
date,
title,
introtext,
bodytext,
comments,
commentcode,
postmode,
draft_flag,
hits
FROM '.$prefix.'stories', ARRAY_A);
}
function get_gl_comments()
{
global $wpdb;
// General Housekeeping
$gldb = new wpdb(get_option('gluser'), get_option('glpass'), get_option('glname'), get_option('glhost'));
set_magic_quotes_runtime(0);
$prefix = get_option('tpre');
// Get Comments (only ones belonging to stories, not polls or anything)
return $gldb->get_results('SELECT * FROM '.$prefix.'comments WHERE type="article"', ARRAY_A);
}
function get_gl_links()
{
//General Housekeeping
$gldb = new wpdb(get_option('gluser'), get_option('glpass'), get_option('glname'), get_option('glhost'));
set_magic_quotes_runtime(0);
$prefix = get_option('tpre');
return $gldb->get_results('SELECT
id,
date,
category,
url,
linkname,
description
FROM '.$prefix.'gl_link',
ARRAY_A);
}
function cat2wp($categories='')
{
// General Housekeeping
global $wpdb;
$count = 0;
$glcat2wpcat = array();
// Do the Magic
if(is_array($categories))
{
echo ''.__('Importing Categories...').'
';
foreach ($categories as $category)
{
$count++;
extract($category);
// Make Nice Variables
$name = $wpdb->escape(strtolower($topic));
$title = $wpdb->escape($topic);
if($cinfo = category_exists($name))
{
$ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title));
}
else
{
$ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title));
}
$glcat2wpcat[$tid] = $ret_id;
}
// Store category translation for future use
add_option('glcat2wpcat',$glcat2wpcat);
echo ''.sprintf(__('Done! %1$s categories imported.'), $count).'
';
return true;
}
echo __('No Categories to Import!');
return false;
}
function users2wp($users='')
{
// General Housekeeping
global $wpdb;
$count = 0;
$glid2wpid = array();
// Midnight Mojo
if(is_array($users))
{
echo ''.__('Importing Users...').'
';
foreach($users as $user)
{
$count++;
extract($user);
// Make Nice Variables
$username = $wpdb->escape($username);
$fullname = $wpdb->escape($fullname);
$homepage = $wpdb->escape($homepage);
if($uinfo = get_userdatabylogin($name))
{
$ret_id = wp_insert_user(array(
'ID' => $uinfo->ID,
'user_login' => $username,
'user_nicename' => $fullname,
'user_email' => $email,
'user_url' => $homepage,
'user_registered' => $regdate,
'display_name' => $name)
);
}
else
{
$ret_id = wp_insert_user(array(
'user_login' => $username,
'user_nicename' => $fullname,
'user_email' => $email,
'user_url' => $homepage,
'user_registered' => $regdate,
'display_name' => $name)
);
}
$glid2wpid[$uid] = $ret_id;
// We only have the encrypted password, but wp_insert_user will encrypt it
// again, so we have to set it manually.
$wpdb->query("UPDATE $wpdb->users SET user_pass='$passwd' WHERE ID=$ret_id");
update_usermeta( $ret_id, 'rich_editing', 'false');
}// End foreach($users as $user)
// Store id translation array for future use
add_option('glid2wpid',$glid2wpid);
echo ''.sprintf(__('Done! %1$s users imported.'), $count).'
';
return true;
}// End if(is_array($users)
echo __('No Users to Import!');
return false;
}// End function user2wp()
function posts2wp($posts='')
{
// General Housekeeping
global $wpdb;
$count = 0;
$glposts2wpposts = array();
$glcat2wpcat = get_option('glcat2wpcat');
$glid2wpid = get_option('glid2wpid');
$cats = array();
// Do the Magic
if(is_array($posts))
{
echo ''.__('Importing Posts...').'
';
foreach($posts as $post)
{
$count++;
extract($post);
$authorid = $glid2wpid[$uid] ;
$Title = $title;
$Body = $introtext.$bodytext;
$Excerpt = $introtext;
$post_status = $draft_flag ? 'draft' : 'publish';
$comment_status = ($commentcode == 0) ? 'open' : 'closed';
if ($postmode == 'plaintext') {
// For some screwy reason, Geeklog double-escapes plaintext posts.
// So if it's plaintext, we need to unescape it one more time.
$Title = stripslashes($Title);
$Body = stripslashes($Body);
$Excerpt = stripslashes($Excerpt);
}
// Import Post data into WordPress
if($pinfo = post_exists($Title,$Body,$date))
{
$ret_id = wp_insert_post(array(
'ID' => $pinfo,
'post_date' => $date,
'post_author' => $authorid,
'post_modified' => $date,
'post_title' => $wpdb->escape($Title),
'post_content' => $wpdb->escape($Body),
'post_excerpt' => $wpdb->escape($Excerpt),
'post_status' => $post_status,
'comment_status' => $comment_status,
'comment_count' => $comments)
);
}
else
{
$ret_id = wp_insert_post(array(
'post_date' => $date,
'post_author' => $authorid,
'post_modified' => $date,
'post_title' => $wpdb->escape($Title),
'post_content' => $wpdb->escape($Body),
'post_excerpt' => $wpdb->escape($Excerpt),
'post_status' => $post_status,
'comment_status' => $comment_status,
'comment_count' => $comments)
);
}
$glposts2wpposts[$sid] = $ret_id;
// Make Post-to-Category associations
wp_set_post_cats('', $ret_id, array($glcat2wpcat[$tid]));
add_post_meta($ret_id, '_gl_sid', $sid);
}
}
// Store ID translation for later use
add_option('glposts2wpposts',$glposts2wpposts);
echo ''.sprintf(__('Done! %1$s posts imported.'), $count).'
';
return true;
}
function comments2wp($comments='')
{
// General Housekeeping
global $wpdb;
$count = 0;
$glcm2wpcm = array();
$glposts2wpposts = get_option('glposts2wpposts');
$glid2wpid = get_option('glid2wpid');
// Magic Mojo
if(is_array($comments))
{
echo ''.__('Importing Comments...').'
';
foreach($comments as $thecomment)
{
$count++;
extract($thecomment);
// WordPressify Data
$comment_ID = ltrim($cid, '0');
$comment_post_ID = $glposts2wpposts[$sid];
$comment_approved = 1;
$name = $wpdb->escape($title);
$comment_parent = $glcm2wpcm[$pid] ? $glcm2wpcm[$pid] : 0;
$user_id = $glid2wpid[$uid] ? $glid2wpid[$uid] : 0;
$user_info = get_userdata($user_id);
if($cinfo = comment_exists($name, $date))
{
// Update comments
$ret_id = wp_update_comment(array(
'comment_ID' => $cinfo,
'comment_post_ID' => $comment_post_ID,
'comment_date' => $date,
'comment_content' => $wpdb->escape($comment),
'comment_parent' => $comment_parent,
'comment_author' => $user_info->display_name,
'comment_author_url' => $user_info->user_url,
'comment_auther_email' => $user_info->user_email,
'user_id' => $user_id,
'comment_approved' => $comment_approved)
);
}
else
{
// Insert comments
$ret_id = wp_insert_comment(array(
'comment_post_ID' => $comment_post_ID,
'comment_date' => $date,
'comment_content' => $wpdb->escape($comment),
'comment_parent' => $comment_parent,
'comment_author' => $user_info->display_name,
'comment_author_url' => $user_info->user_url,
'comment_auther_email' => $user_info->user_email,
'user_id' => $user_id,
'comment_approved' => $comment_approved)
);
}
$glcm2wpcm[$cid] = $ret_id;
}
// Store Comment ID translation for future use
add_option('glcm2wpcm', $glcm2wpcm);
// Associate newly formed categories with posts
get_comment_count($ret_id);
echo ''.sprintf(__('Done! %1$s comments imported.'), $count).'
';
return true;
}
echo __('No Comments to Import!');
return false;
}
function links2wp($links='')
{
// General Housekeeping
global $wpdb;
$count = 0;
// Deal with the links
if(is_array($links))
{
echo ''.__('Importing Links...').'
';
foreach($links as $link)
{
$count++;
extract($link);
// Make nice vars
$category = $wpdb->escape($category);
$linkname = $wpdb->escape($linkname);
$description = $wpdb->escape($description);
if($linfo = link_exists($linkname))
{
$ret_id = wp_insert_link(array(
'link_id' => $linfo,
'link_url' => $url,
'link_name' => $linkname,
'link_category' => $category,
'link_description' => $description,
'link_updated' => $date)
);
}
else
{
$ret_id = wp_insert_link(array(
'link_url' => $url,
'link_name' => $linkname,
'link_category' => $category,
'link_description' => $description,
'link_updated' => $date)
);
}
$gllinks2wplinks[$link_id] = $ret_id;
}
add_option('gllinks2wplinks',$gllinks2wplinks);
echo '';
printf(__('Done! %s Links imported'), $count);
echo '
';
return true;
}
echo __('No Links to Import!');
return false;
}
function import_categories()
{
// Category Import
$cats = $this->get_gl_cats();
$this->cat2wp($cats);
echo '';
}
function import_users()
{
// User Import
$users = $this->get_gl_users();
$this->users2wp($users);
echo '';
}
function import_posts()
{
// Post Import
$posts = $this->get_gl_posts();
$this->posts2wp($posts);
echo '';
}
function import_comments()
{
// Comment Import
$comments = $this->get_gl_comments();
$this->comments2wp($comments);
echo '';
}
function import_links()
{
//Link Import
$links = $this->get_gl_links();
$this->links2wp($links);
add_option('gl_links', $links);
echo '';
}
function cleanup_glimport()
{
delete_option('tpre');
delete_option('glid2wpid');
delete_option('glcat2wpcat');
delete_option('glposts2wpposts');
delete_option('glcm2wpcm');
delete_option('gllinks2wplinks');
delete_option('gluser');
delete_option('glpass');
delete_option('glname');
delete_option('glhost');
$this->tips();
}
function tips()
{
echo ''.__('Welcome to WordPress. We hope (and expect!) that you will find this platform incredibly rewarding! As a new WordPress user coming from Geeklog, there are some things that we would like to point out. Hopefully, they will help your transition go as smoothly as possible.').'
';
echo ''.__('Users').'
';
echo ''.__('You have already setup WordPress and have been assigned an administrative login and password. Forget it. You didn\'t have that login in Geeklog, why should you have it here? Instead we have taken care to import all of your users into our system. All usernames and passwords were imported intact (we can\'t decrypt the passwords, but fortunately Geeklog and Wordpress both use the same encryption on the passwords, so we just copied the encrypted version over).').'
';
echo ''.__('Preserving Authors').'
';
echo ''.__('Secondly, we have attempted to preserve post authors. If you are the only author or contributor to your blog, then you are safe. In most cases, we are successful in this preservation endeavor. However, if we cannot ascertain the name of the writer due to discrepancies between database tables, we assign it to you, the administrative user.').'
';
echo ''.__('WordPress Resources').'
';
echo ''.__('Finally, there are numerous WordPress resources around the internet. Some of them are:').'
';
echo '';
echo ''.sprintf(__('That\'s it! What are you waiting for? Go login!'), '/wp-login.php').'
';
}
function db_form()
{
echo '';
}
function dispatch()
{
if (empty ($_GET['step']))
$step = 0;
else
$step = (int) $_GET['step'];
$this->header();
if ( $step > 0 )
{
if($_POST['dbuser'])
{
if(get_option('gluser'))
delete_option('gluser');
add_option('gluser',$_POST['dbuser']);
}
if($_POST['dbpass'])
{
if(get_option('glpass'))
delete_option('glpass');
add_option('glpass',$_POST['dbpass']);
}
if($_POST['dbname'])
{
if(get_option('glname'))
delete_option('glname');
add_option('glname',$_POST['dbname']);
}
if($_POST['dbhost'])
{
if(get_option('glhost'))
delete_option('glhost');
add_option('glhost',$_POST['dbhost']);
}
if($_POST['dbprefix'])
{
if(get_option('tpre'))
delete_option('tpre');
add_option('tpre',$_POST['dbprefix']);
}
}
switch ($step)
{
default:
case 0 :
$this->greet();
break;
case 1 :
$this->import_categories();
break;
case 2 :
$this->import_users();
break;
case 3 :
$this->import_posts();
break;
case 4 :
$this->import_comments();
break;
//case 5 :
// $this->import_links();
// break;
case 5 :
$this->cleanup_glimport();
break;
}
$this->footer();
}
function Geeklog_Import()
{
// Nothing.
}
}
$gl_import = new Geeklog_Import();
register_importer('geeklog', 'GeekLog', __('Import posts from a GeekLog site'), array ($gl_import, 'dispatch'));
?>