Advertisement
janoulle

Chris Meller's WP Migration Script

May 23rd, 2011
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.67 KB | None | 0 0
  1. <?php
  2.  
  3.     // config info
  4.     $import = array(
  5.         'wp_host' => 'localhost',    // hostname
  6.         'wp_user' => 'wordpress',    // username
  7.         'wp_pass' => 'wordpress',    // password
  8.         'wp_name' => 'wordpress',    // name of the database
  9.         'wp_prefix' => 'wp_',        // table prefix
  10.     );
  11.  
  12.     echo '<pre>';
  13.  
  14.     // keep habari from executing
  15.     define( 'UNIT_TEST', true );
  16.    
  17.     // bootstrap it
  18.     include( 'index.php' );
  19.    
  20.     // create a connection to our wordpress database
  21.     try {
  22.         $wpdb = DatabaseConnection::ConnectionFactory( "mysql:host=" . $import['wp_host'] . ";dbname=" . $import['wp_name'] );
  23.         $wpdb->connect( "mysql:host=" . $import['wp_host'] . ";dbname=" . $import['wp_name'], $import['wp_user'], $import['wp_pass'] );
  24.     }
  25.     catch( Exception $e ) {
  26.         die('Unable to connect to WordPress database. ' . $e->getMessage());
  27.     }
  28.    
  29.    
  30.     // users
  31.    
  32.     $habari_users = Users::get();    // get all the habari users
  33.     $wp_users = array();
  34.        
  35.     foreach ( $habari_users as $habari_user ) {
  36.        
  37.         // see if the user exists already in WordPress
  38.         $wp_user = $wpdb->get_row( 'select id, user_login from ' . $import['wp_prefix'] . 'users where user_login = ?', array( $habari_user->username ) );
  39.        
  40.         // if it doesn't, create it
  41.         if ( !$wp_user ) {
  42.                
  43.             $wpdb->query( 'insert into ' . $import['wp_prefix'] . 'users (
  44.                    user_login,
  45.                    user_pass,
  46.                    user_nicename,
  47.                    user_email,
  48.                    user_registered,
  49.                    display_name
  50.                ) values ( ?, ?, ?, ?, UTC_TIMESTAMP(), ? ) ', array(
  51.                     $habari_user->username,
  52.                     $habari_user->password,
  53.                     $habari_user->username,
  54.                     $habari_user->email,
  55.                     $habari_user->info->displayname
  56.                 )
  57.             );
  58.            
  59.             $wp_users[ $habari_user->username ] = $wpdb->last_insert_id();
  60.            
  61.             echo 'Created user ' . $habari_user->username . ' with id ' . $wpdb->last_insert_id() . "\n";
  62.            
  63.         }
  64.         else {
  65.            
  66.             echo 'Found existing user ' . $wp_user->user_login . ' with id ' . $wp_user->id . "\n";
  67.            
  68.             $wp_users[ $wp_user->user_login ] = $wp_user->id;
  69.            
  70.         }
  71.        
  72.     }
  73.    
  74.    
  75.     // posts and pages
  76.    
  77.     // get the total number of posts, either published or draft
  78.     $total_posts = Posts::get( array( 'count' => true, 'ignore_permissions' => true, 'content_type' => array( 'entry', 'page' ), 'status' => array( 'published', 'draft' ) ) );
  79.     $wp_posts = array();
  80.    
  81.     echo 'Total Posts: ' . $total_posts . "\n";
  82.    
  83.     for ( $i = 0; $i < $total_posts / 10; $i++ ) {
  84.        
  85.         $posts = Posts::get( array( 'limit' => 10, 'offset' => $i * 10, 'ignore_permissions' => true, 'content_type' => array( 'entry', 'page' ), 'status' => array( 'published', 'draft' ) ) );
  86.        
  87.         echo 'Got ' . count( $posts ) . ' posts' . "\n";
  88.        
  89.         foreach ( $posts as $post ) {
  90.  
  91.             $insert_query = 'insert into ' . $import['wp_prefix'] . 'posts (
  92.                post_author,
  93.                post_date,
  94.                post_date_gmt,
  95.                post_content,
  96.                post_title,
  97.                post_status,
  98.                comment_status,
  99.                post_name,
  100.                post_modified,
  101.                post_modified_gmt,
  102.                guid,
  103.                post_type
  104.            ) values (
  105.                ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
  106.            )';
  107.            
  108.             $insert_params = array(
  109.                 $wp_users[ $post->author->username ],                    // post author ID, converted to the WordPress user's
  110.                 $post->pubdate->format( 'Y-m-d H-i-s' ),
  111.                 gmdate( 'Y-m-d H-i-s', $post->pubdate->int ),
  112.                 $post->content,
  113.                 $post->title,
  114.                 Post::status_name( $post->status ) == 'published' ? 'publish' : Post::status_name( $post->status ),                        // post status (published or draft, basically), converted to a string
  115.                 $post->info->comments_disabled ? 'closed' : 'open',
  116.                 $post->slug,
  117.                 $post->modified->format( 'Y-m-d H-i-s' ),
  118.                 gmdate( 'Y-m-d H-i-s', $post->modified->int ),
  119.                 $post->guid,
  120.                 Post::type_name( $post->content_type ) == 'entry' ? 'post' : 'page'
  121.             );
  122.            
  123.             $result = $wpdb->query( $insert_query, $insert_params );
  124.        
  125.             if ( !$result ) {
  126.                 echo 'Failed to insert post ' . $post->slug . "\n";
  127.             }
  128.             else {
  129.                 $wp_posts[ $post->id ] = $wpdb->last_insert_id();
  130.             }
  131.            
  132.         }
  133.        
  134.     }    
  135.    
  136.     // tags
  137.    
  138.     // here we don't use the habari API because it would be cumbersome - if only it were as robust as the Posts API is...
  139.     $total_tags = DB::get_value( 'select count(*) from {tags}' );
  140.     $wp_tax = array();
  141.    
  142.     echo 'Total Tags: ' . $total_tags . "\n";
  143.    
  144.     for ( $i = 0; $i < $total_tags / 10; $i++ ) {
  145.        
  146.         $tags = DB::get_results( 'select id, tag_text, tag_slug from {tags} order by id limit 10 offset ' . $i * 10 );
  147.        
  148.         echo 'Got ' . count( $tags ) . ' tags' . "\n";
  149.        
  150.         foreach ( $tags as $tag ) {
  151.            
  152.             // first, see if it already exists as a term in WordPress
  153.             $wp_tag = $wpdb->get_row( 'select term_id, name, slug from ' . $import['wp_prefix'] . 'terms where slug = ?', array( $tag->tag_slug ) );
  154.            
  155.             if ( !$wp_tag ) {
  156.                
  157.                 $insert_query = 'insert into ' . $import['wp_prefix'] . 'terms ( name, slug ) values ( ?, ? )';
  158.                 $insert_params = array( $tag->tag_text, $tag->tag_slug );
  159.                
  160.                 $wpdb->query( $insert_query, $insert_params );
  161.                
  162.                 //$wp_tags[ $tag->tag_slug ] = $wpdb->last_insert_id();
  163.                
  164.                 $tag_id = $wpdb->last_insert_id();
  165.                
  166.             }
  167.             else {
  168.                
  169.                 //$wp_tags[ $wp_tag->slug ] = $wp_tag->term_id;
  170.                
  171.                 $tag_id = $wp_tag->term_id;
  172.                
  173.             }
  174.            
  175.             // and either way, make sure it's actually specified as a part of the tag taxonomy
  176.             $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 ) );
  177.            
  178.             if ( !$wp_taxonomy ) {
  179.                
  180.                 $insert_query = 'insert into ' . $import['wp_prefix'] . 'term_taxonomy ( term_id, taxonomy ) values ( ?, ? )';
  181.                 $insert_params = array( $tag_id, 'post_tag' );
  182.                
  183.                 $wpdb->query( $insert_query, $insert_params );
  184.                
  185.                 $wp_tax[ $tag->id ] = $wpdb->last_insert_id();
  186.                
  187.             }
  188.             else {
  189.                
  190.                 $wp_tax[ $tag->id ] = $wp_taxonomy->term_taxonomy_id;
  191.                
  192.             }
  193.                        
  194.         }
  195.        
  196.     }
  197.    
  198.     // now we have to link all our tags and posts
  199.    
  200.     $total_joins = DB::get_value( 'select count(*) from {tag2post}' );
  201.    
  202.     echo 'Total Tag to Post relationships: ' . $total_joins . "\n";
  203.    
  204.     for ( $i = 0; $i < $total_joins / 10; $i++ ) {
  205.        
  206.         // get the joins
  207.         $joins = DB::get_results( 'select tag_id, post_id from {tag2post} order by tag_id, post_id limit 10 offset ' . $i * 10 );
  208.        
  209.         echo 'Got ' . count( $joins ) . ' relationships' . "\n";
  210.        
  211.         foreach ( $joins as $join ) {
  212.            
  213.             // if it's not in the list of posts we imported earlier, skip it - it's probably a different content type
  214.             if ( !array_key_exists( $join->post_id, $wp_posts ) ) {
  215.                 continue;
  216.             }
  217.            
  218.             $insert_query = 'insert into ' . $import['wp_prefix'] . 'term_relationships ( object_id, term_taxonomy_id ) values ( ?, ? )';
  219.             $insert_params = array( $wp_posts[ $join->post_id ], $wp_tax[ $join->tag_id ] );
  220.            
  221.             $wpdb->query( $insert_query, $insert_params );
  222.            
  223.         }
  224.        
  225.     }
  226.    
  227.    
  228.     // update the tag counts
  229.     $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 )');
  230.    
  231.    
  232.     // comments
  233.    
  234.     $total_comments = DB::get_value ( 'select count(*) from {comments}' );
  235.    
  236.     echo 'Total comments: ' . $total_comments . "\n";
  237.    
  238.     // get the comment types from habari
  239.     $comment_types = Comment::list_comment_types();
  240.     $comment_statuses = Comment::list_comment_statuses();
  241.    
  242.     for ( $i = 0; $i < $total_comments / 10; $i++ ) {
  243.        
  244.         // get the comments
  245.         $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 );
  246.        
  247.         echo 'Got ' . count( $comments ) . ' comments' . "\n";
  248.        
  249.         foreach ( $comments as $comment ) {
  250.            
  251.             $comment->date = HabariDateTime::date_create( $comment->date );
  252.                        
  253.             $comment_status = $comment_statuses[ $comment->status ];
  254.             $comment_type = $comment_types[ $comment->type ];
  255.            
  256.             if ( $comment_status == 'approved' ) {
  257.                 $comment_approved = true;
  258.             }
  259.             else {
  260.                 $comment_approved = false;
  261.             }
  262.            
  263.             $insert_query = 'insert into ' . $import['wp_prefix'] . 'comments (
  264.                    comment_post_id,
  265.                    comment_author,
  266.                    comment_author_email,
  267.                    comment_author_url,
  268.                    comment_author_ip,
  269.                    comment_date,
  270.                    comment_date_gmt,
  271.                    comment_content,
  272.                    comment_approved,
  273.                    comment_type
  274.                ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )';
  275.            
  276.             $insert_params = array(
  277.                 $wp_posts[ $comment->post_id ],
  278.                 $comment->name,
  279.                 $comment->email,
  280.                 $comment->url,
  281.                 long2ip( $comment->ip ),
  282.                 $comment->date->format( 'Y-m-d H-i-s' ),
  283.                 gmdate( 'Y-m-d H-i-s', $comment->date->int ),
  284.                 $comment->content,
  285.                 $comment_approved,
  286.                 $comment_type
  287.             );
  288.            
  289.             $wpdb->query( $insert_query, $insert_params );
  290.            
  291.         }
  292.        
  293.     }
  294.    
  295.     // update comment user_id links based on user display names
  296.     $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 )');
  297.    
  298.     // update post comment counts
  299.     $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\' )' );
  300.    
  301.     echo '</pre>';
  302.  
  303. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement