Saturngod

What's on my mind

Harbari to Wordpress PHP Code

Copy the following code and create wp_convert.php in habari folder. It's using habari class for connection database, tag,comment,etc. You need to add wordpress database name,username and password in there. After that run wp_convert.php and convert all post to the wordpress database. Habari don't have category and all the post will under categorize.  I got that code from [http://chrismeller.com/user/files/2009/08/wp_migrate_script.php](http://chrismeller.com/user/files/2009/08/wp_migrate_script.php)
<?php

// config info
$import = array(
‘wp_host’ => ‘localhost’,    // hostname
‘wp_user’ => ‘wordpress’,    // username
‘wp_pass’ => ‘wordpress’,    // password
‘wp_name’ => ‘wordpress’,    // name of the database
‘wpprefix’ => ‘wp‘,        // table prefix
);

echo ‘<pre>’;

// keep habari from executing
define( ‘UNIT_TEST’, true );

// bootstrap it
include( ‘index.php’ );

// create a connection to our wordpress database
try {
$wpdb = DatabaseConnection::ConnectionFactory( "mysql:host=" . $import[‘wp_host’] . ";dbname=" . $import[‘wp_name’] );
$wpdb->connect( "mysql:host=" . $import[‘wp_host’] . ";dbname=" . $import[‘wp_name’], $import[‘wp_user’], $import[‘wp_pass’] );
}
catch( Exception $e ) {
die(‘Unable to connect to WordPress database. ‘ . $e->getMessage());
}

// users

$habari_users = Users::get();    // get all the habari users
$wp_users = array();

foreach ( $habari_users as $habari_user ) {

// see if the user exists already in WordPress
$wp_user = $wpdb->get_row( ‘select id, user_login from ‘ . $import[‘wp_prefix’] . ‘users where user_login = ?’, array( $habari_user->username ) );

// if it doesn’t, create it
if ( !$wp_user ) {

$wpdb->query( ‘insert into ‘ . $import[‘wp_prefix’] . ‘users (
user_login,
user_pass,
user_nicename,
user_email,
user_registered,
display_name
) values ( ?, ?, ?, ?, UTC_TIMESTAMP(), ? ) ‘, array(
$habari_user->username,
$habari_user->password,
$habari_user->username,
$habari_user->email,
$habari_user->info->displayname
)
);

$wp_users[ $habari_user->username ] = $wpdb->last_insert_id();

echo ‘Created user ‘ . $habari_user->username . ‘ with id ‘ . $wpdb->last_insert_id() . "n";

}
else {

echo ‘Found existing user ‘ . $wp_user->user_login . ‘ with id ‘ . $wp_user->id . "n";

$wp_users[ $wp_user->user_login ] = $wp_user->id;

}

}

// posts and pages

// get the total number of posts, either published or draft
$total_posts = Posts::get( array( ‘count’ => true, ‘ignore_permissions’ => true, ‘content_type’ => array( ‘entry’, ‘page’ ), ‘status’ => array( ‘published’, ‘draft’ ) ) );
$wp_posts = array();

echo ‘Total Posts: ‘ . $total_posts . "n";

for ( $i = 0; $i < $total_posts / 10; $i++ ) {

$posts = Posts::get( array( ‘limit’ => 10, ‘offset’ => $i  10, ‘ignore_permissions’ => true, ‘content_type’ => array( ‘entry’, ‘page’ ), ‘status’ => array( ‘published’, ‘draft’ ) ) );

echo ‘Got ‘ . count( $posts ) . ‘ posts’ . "n";

foreach ( $posts as $post ) {

$insert_query = ‘insert into ‘ . $import[‘wp_prefix’] . ‘posts (
post_author,
post_date,
post_date_gmt,
post_content,
post_title,
post_status,
comment_status,
post_name,
post_modified,
post_modified_gmt,
guid,
post_type
) values (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)’;

$insert_params = array(
$wp_users[ $post->author->username ],                    // post author ID, converted to the WordPress user’s
$post->pubdate->format( ‘Y-m-d H-i-s’ ),
gmdate( ‘Y-m-d H-i-s’, $post->pubdate->int ),
$post->content,
$post->title,
Post::status_name( $post->status ) == ‘published’ ? ‘publish’ : Post::status_name( $post->status ),                        // post status (published or draft, basically), converted to a string
$post->info->comments_disabled ? ‘closed’ : ‘open’,
$post->slug,
$post->modified->format( ‘Y-m-d H-i-s’ ),
gmdate( ‘Y-m-d H-i-s’, $post->modified->int ),
$post->guid,
Post::type_name( $post->content_type ) == ‘entry’ ? ‘post’ : ‘page’
);

$result = $wpdb->query( $insert_query, $insert_params );

if ( !$result ) {
echo ‘Failed to insert post ‘ . $post->slug . "n";
}
else {
$wp_posts[ $post->id ] = $wpdb->last_insert_id();
}

}

}

// tags

// here we don’t use the habari API because it would be cumbersome - if only it were as robust as the Posts API is…
$total_tags = DB::get_value( ‘select count() from {tags}’ );
$wp_tax = array();

echo ‘Total Tags: ‘ . $total_tags . "n";

for ( $i = 0; $i < $total_tags / 10; $i++ ) {

$tags = DB::get_results( ‘select id, tag_text, tag_slug from {tags} order by id limit 10 offset ‘ . $i  10 );

echo ‘Got ‘ . count( $tags ) . ‘ tags’ . "n";

foreach ( $tags as $tag ) {

// first, see if it already exists as a term in WordPress
$wp_tag = $wpdb->get_row( ‘select term_id, name, slug from ‘ . $import[‘wp_prefix’] . ‘terms where slug = ?’, array( $tag->tag_slug ) );

if ( !$wp_tag ) {

$insert_query = ‘insert into ‘ . $import[‘wp_prefix’] . ‘terms ( name, slug ) values ( ?, ? )’;
$insert_params = array( $tag->tag_text, $tag->tag_slug );

$wpdb->query( $insert_query, $insert_params );

//$wp_tags[ $tag->tag_slug ] = $wpdb->last_insert_id();

$tag_id = $wpdb->last_insert_id();

}
else {

//$wp_tags[ $wp_tag->slug ] = $wp_tag->term_id;

$tag_id = $wp_tag->term_id;

}

// and either way, make sure it’s actually specified as a part of the tag taxonomy
$wp_taxonomy = $wpdb->get_row( ‘select term_taxonomy_id from ‘ . $import[‘wp_prefix’] . ‘term_taxonomy where taxonomy = ? and term_id = ?’, array( ‘post_tag’, $tag_id ) );

if ( !$wp_taxonomy ) {

$insert_query = ‘insert into ‘ . $import[‘wp_prefix’] . ‘term_taxonomy ( term_id, taxonomy ) values ( ?, ? )’;
$insert_params = array( $tag_id, ‘post_tag’ );

$wpdb->query( $insert_query, $insert_params );

$wp_tax[ $tag->id ] = $wpdb->last_insert_id();

}
else {

$wp_tax[ $tag->id ] = $wp_taxonomy->term_taxonomy_id;

}

}

}

// now we have to link all our tags and posts

$total_joins = DB::get_value( ‘select count() from {tag2post}’ );

echo ‘Total Tag to Post relationships: ‘ . $total_joins . "n";

for ( $i = 0; $i < $total_joins / 10; $i++ ) {

// get the joins
$joins = DB::get_results( ‘select tag_id, post_id from {tag2post} order by tag_id, post_id limit 10 offset ‘ . $i  10 );

echo ‘Got ‘ . count( $joins ) . ‘ relationships’ . "n";

foreach ( $joins as $join ) {

// if it’s not in the list of posts we imported earlier, skip it - it’s probably a different content type
if ( !array_key_exists( $join->post_id, $wp_posts ) ) {
continue;
}

$insert_query = ‘insert into ‘ . $import[‘wp_prefix’] . ‘term_relationships ( object_id, term_taxonomy_id ) values ( ?, ? )’;
$insert_params = array( $wp_posts[ $join->post_id ], $wp_tax[ $join->tag_id ] );

$wpdb->query( $insert_query, $insert_params );

}

}

// update the tag counts
$wpdb->query( ‘update ‘ . $import[‘wp_prefix’] . ‘term_taxonomy t set count = ( select count(object_id) from ‘ . $import[‘wp_prefix’] . ‘term_relationships where term_taxonomy_id = t.term_taxonomy_id )’);

// comments

$total_comments = DB::get_value ( ‘select count() from {comments}’ );

echo ‘Total comments: ‘ . $total_comments . "n";

// get the comment types from habari
$comment_types = Comment::list_comment_types();
$comment_statuses = Comment::list_comment_statuses();

for ( $i = 0; $i < $total_comments / 10; $i++ ) {

// get the comments
$comments = DB::get_results( ‘select post_id, name, email, url, ip, date, content, status, type from {comments} order by id limit 10 offset ‘ . $i  10 );

echo ‘Got ‘ . count( $comments ) . ‘ comments’ . "n";

foreach ( $comments as $comment ) {

$comment->date = HabariDateTime::date_create( $comment->date );

$comment_status = $comment_statuses[ $comment->status ];
$comment_type = $comment_types[ $comment->type ];

if ( $comment_status == ‘approved’ ) {
$comment_approved = true;
}
else {
$comment_approved = false;
}

$insert_query = ‘insert into ‘ . $import[‘wp_prefix’] . ‘comments (
comment_post_id,
comment_author,
comment_author_email,
comment_author_url,
comment_author_ip,
comment_date,
comment_date_gmt,
comment_content,
comment_approved,
comment_type
) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )’;

$insert_params = array(
$wp_posts[ $comment->post_id ],
$comment->name,
$comment->email,
$comment->url,
long2ip( $comment->ip ),
$comment->date->format( ‘Y-m-d H-i-s’ ),
gmdate( ‘Y-m-d H-i-s’, $comment->date->int ),
$comment->content,
$comment_approved,
$comment_type
);

$wpdb->query( $insert_query, $insert_params );

}

}

// update comment user_id links based on user display names
$wpdb->query( ‘update ‘ . $import[‘wp_prefix’] . ‘comments c set user_id = ( select ID from ‘ . $import[‘wp_prefix’] . ‘users where display_name = c.comment_author ) where comment_author in ( select distinct display_name from ‘ . $import[‘wp_prefix’] . ‘users )’);

// update post comment counts
$wpdb->query( ‘update ‘ . $import[‘wp_prefix’] . ‘posts p set comment_count = ( select count() from ‘ . $import[‘wp_prefix’] . ‘comments where comment_post_ID = p.ID and comment_approved = ‘1’ )’ );

echo ‘</pre>’;

?>

Why habari too long to next version ?

I am waiting to relase habari 1.0. My blog is using habari and sometime I’m thinking…. I wrong ???? Wordpress have a good community and many plugins. I can customize the wordpress Theme and plugins. They always release update their version. Why not habari ? Habari is light weigth and easy to use. Wordpress is not light weight when compare with habari. But wordpress also easy to use. I want to upgrade my blog in June. I hope, habari will relase habari 1.0 brefore June. If habari is not release, I will change to wordpress. In previous days, I’m working with wordpress and I love so much to wordpress. I worked wordpress customization. I hope, I can write wordpress plugins in this days. Hey!! Habari , please move quickly. I love you,too. You need to grow quicly and faster than wordpress if you want to compare with wordpress. Wordpress is the great opensource beyong the Blog.

Syntax Highlighter For habari

Today, I am searching for syntax highlighter for habari and I found [this](http://gopherwood.info/2008/05/22/syntaxhighlighter-for-habari). I am testing this in [json with jquery](http://en.saturngod.net/json-with-jquery) post. Nice syntax highlighting and thank you for that developer.

Habari Should Be Improve Quickly

Nowsady, many people making blog and share their knowledge or review. Many blog engine are appear and wordpress is the top of the opensource blog engine. I use harbari in my english blog and use wordpress for myanmar blog. Wordpress is a easy to use and easy to change sidebar and also many plugins. Habari, easy to use but too hard to change sidebar because we want to change sidebar, use ftp and after changes we need to upload again. It’s terriable things. Wordpress can change code in theri admin panel. Why habari can’t ? They should make to change template file ( .php , .css , .js ) in admin panel. After that , we can easily change a sidebar.

Habari also have many plugins and many template. They also have wiki and it’s very useful to us. We can check some CMS in there and also see in the wiki. I love habari because of customization. Habari can customize easier than wordpress. I hope habari will popular like wordpress becuase most people use habari in todays.

PS: most of the plugin don’t work in habari 0.6.2.