Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // config info
- $import = array(
- 'wp_host' => 'localhost', // hostname
- 'wp_user' => 'wordpress', // username
- 'wp_pass' => 'wordpress', // password
- 'wp_name' => 'wordpress', // name of the database
- 'wp_prefix' => '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>';
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement