WordPress.org

Make WordPress Core

Changeset 5245


Ignore:
Timestamp:
04/12/2007 01:34:15 AM (11 years ago)
Author:
ryan
Message:

Preserve page hierarchy. Props takayukister. fixes #4025

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/import/wordpress.php

    r5208 r5245  
    44
    55    var $posts = array ();
     6    var $posts_processed = array ();
     7    // Array of arrays. [[0] => XML fragment, [1] => New post ID]
    68    var $file;
    79    var $id;
     
    8789        preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
    8890        $this->posts = $this->posts[1];
     91        foreach ($this->posts as $post) {
     92            $post_ID = (int) $this->get_tag( $post, 'wp:post_id' );
     93            if ($post_ID)
     94                $this->posts_processed[$post_ID][0] = &$post;
     95                $this->posts_processed[$post_ID][1] = 0;
     96        }
    8997        preg_match_all('|<wp:category>(.*?)</wp:category>|is', $importdata, $this->categories);
    9098        $this->categories = $this->categories[1];
     
    209217
    210218    function process_posts() {
    211         global $wpdb;
    212219        $i = -1;
    213220        echo '<ol>';
    214         foreach ($this->posts as $post) {
    215 
    216             // There are only ever one of these
    217             $post_title     = $this->get_tag( $post, 'title' );
    218             $post_date      = $this->get_tag( $post, 'wp:post_date' );
    219             $post_date_gmt  = $this->get_tag( $post, 'wp:post_date_gmt' );
    220             $comment_status = $this->get_tag( $post, 'wp:comment_status' );
    221             $ping_status    = $this->get_tag( $post, 'wp:ping_status' );
    222             $post_status    = $this->get_tag( $post, 'wp:status' );
    223             $post_name      = $this->get_tag( $post, 'wp:post_name' );
    224             $post_parent    = $this->get_tag( $post, 'wp:post_parent' );
    225             $menu_order     = $this->get_tag( $post, 'wp:menu_order' );
    226             $post_type      = $this->get_tag( $post, 'wp:post_type' );
    227             $guid           = $this->get_tag( $post, 'guid' );
    228             $post_author    = $this->get_tag( $post, 'dc:creator' );
    229 
    230             $post_content = $this->get_tag( $post, 'content:encoded' );
    231             $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $post_content);
    232             $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
    233             $post_content = str_replace('<br>', '<br />', $post_content);
    234             $post_content = str_replace('<hr>', '<hr />', $post_content);
    235 
    236             preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
    237             $categories = $categories[1];
    238 
    239             $cat_index = 0;
    240             foreach ($categories as $category) {
    241                 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $category)));
    242                 $cat_index++;
    243             }
    244 
    245             if ($post_id = post_exists($post_title, '', $post_date)) {
    246                 echo '<li>';
    247                 printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
    248             } else {
    249                 echo '<li>';
    250                 printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
    251 
    252                 $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor
    253 
    254                 $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'post_name', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt', 'guid', 'post_parent', 'menu_order', 'post_type');
    255                 $comment_post_ID = $post_id = wp_insert_post($postdata);
    256                 // Add categories.
    257                 if (0 != count($categories)) {
    258                     wp_create_categories($categories, $post_id);
    259                 }
    260             }
    261 
    262                 // Now for comments
    263                 preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments);
    264                 $comments = $comments[1];
    265                 $num_comments = 0;
    266                 if ( $comments) { foreach ($comments as $comment) {
    267                     $comment_author       = $this->get_tag( $comment, 'wp:comment_author');
    268                     $comment_author_email = $this->get_tag( $comment, 'wp:comment_author_email');
    269                     $comment_author_IP    = $this->get_tag( $comment, 'wp:comment_author_IP');
    270                     $comment_author_url   = $this->get_tag( $comment, 'wp:comment_author_url');
    271                     $comment_date         = $this->get_tag( $comment, 'wp:comment_date');
    272                     $comment_date_gmt     = $this->get_tag( $comment, 'wp:comment_date_gmt');
    273                     $comment_content      = $this->get_tag( $comment, 'wp:comment_content');
    274                     $comment_approved     = $this->get_tag( $comment, 'wp:comment_approved');
    275                     $comment_type         = $this->get_tag( $comment, 'wp:comment_type');
    276                     $comment_parent       = $this->get_tag( $comment, 'wp:comment_parent');
    277 
    278                     if ( !comment_exists($comment_author, $comment_date) ) {
    279                         $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_approved', 'comment_type', 'comment_parent');
    280                         wp_insert_comment($commentdata);
    281                         $num_comments++;
    282                     }
    283                 } }
    284                 if ( $num_comments )
    285                     printf(' '.__('(%s comments)'), $num_comments);
    286 
    287                 // Now for post meta
    288                 preg_match_all('|<wp:postmeta>(.*?)</wp:postmeta>|is', $post, $postmeta);
    289                 $postmeta = $postmeta[1];
    290                 if ( $postmeta) { foreach ($postmeta as $p) {
    291                     $key   = $this->get_tag( $p, 'wp:meta_key' );
    292                     $value = $this->get_tag( $p, 'wp:meta_value' );
    293                     add_post_meta( $post_id, $key, $value );
    294                 } }
    295 
    296             $index++;
    297         }
     221
     222        foreach ($this->posts as $post)
     223            $this->process_post($post);
    298224
    299225        echo '</ol>';
     
    302228
    303229        echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>';
     230    }
     231 
     232    function process_post($post) {
     233        global $wpdb;
     234
     235        $post_ID = (int) $this->get_tag( $post, 'wp:post_id' );
     236        if ( $post_ID && !empty($this->posts_processed[$post_ID][1]) ) // Processed already
     237            return 0;
     238     
     239        // There are only ever one of these
     240        $post_title     = $this->get_tag( $post, 'title' );
     241        $post_date      = $this->get_tag( $post, 'wp:post_date' );
     242        $post_date_gmt  = $this->get_tag( $post, 'wp:post_date_gmt' );
     243        $comment_status = $this->get_tag( $post, 'wp:comment_status' );
     244        $ping_status    = $this->get_tag( $post, 'wp:ping_status' );
     245        $post_status    = $this->get_tag( $post, 'wp:status' );
     246        $post_name      = $this->get_tag( $post, 'wp:post_name' );
     247        $post_parent    = $this->get_tag( $post, 'wp:post_parent' );
     248        $menu_order     = $this->get_tag( $post, 'wp:menu_order' );
     249        $post_type      = $this->get_tag( $post, 'wp:post_type' );
     250        $guid           = $this->get_tag( $post, 'guid' );
     251        $post_author    = $this->get_tag( $post, 'dc:creator' );
     252
     253        $post_content = $this->get_tag( $post, 'content:encoded' );
     254        $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $post_content);
     255        $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
     256        $post_content = str_replace('<br>', '<br />', $post_content);
     257        $post_content = str_replace('<hr>', '<hr />', $post_content);
     258
     259        preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
     260        $categories = $categories[1];
     261
     262        $cat_index = 0;
     263        foreach ($categories as $category) {
     264            $categories[$cat_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $category)));
     265            $cat_index++;
     266        }
     267
     268        if ($post_id = post_exists($post_title, '', $post_date)) {
     269            echo '<li>';
     270            printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
     271        } else {
     272
     273            // If it has parent, process parent first.
     274            $post_parent = (int) $post_parent;
     275            if ($parent = $this->posts_processed[$post_parent]) {
     276                if (!$parent[1]) $this->process_post($parent[0]); // If not yet, process the parent first.
     277                $post_parent = $parent[1]; // New ID of the parent;
     278            }
     279
     280            echo '<li>';
     281            printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
     282
     283            $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor
     284
     285            $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'post_name', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt', 'guid', 'post_parent', 'menu_order', 'post_type');
     286            $comment_post_ID = $post_id = wp_insert_post($postdata);
     287
     288            // Memorize old and new ID.
     289            if ( $post_id && $post_ID && $this->posts_processed[$post_ID] )
     290                $this->posts_processed[$post_ID][1] = $post_id; // New ID.
     291
     292            // Add categories.
     293            if ( 0 != count($categories) )
     294                wp_create_categories($categories, $post_id);
     295               
     296        }
     297
     298        // Now for comments
     299        preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments);
     300        $comments = $comments[1];
     301        $num_comments = 0;
     302        if ( $comments) { foreach ($comments as $comment) {
     303            $comment_author       = $this->get_tag( $comment, 'wp:comment_author');
     304            $comment_author_email = $this->get_tag( $comment, 'wp:comment_author_email');
     305            $comment_author_IP    = $this->get_tag( $comment, 'wp:comment_author_IP');
     306            $comment_author_url   = $this->get_tag( $comment, 'wp:comment_author_url');
     307            $comment_date         = $this->get_tag( $comment, 'wp:comment_date');
     308            $comment_date_gmt     = $this->get_tag( $comment, 'wp:comment_date_gmt');
     309            $comment_content      = $this->get_tag( $comment, 'wp:comment_content');
     310            $comment_approved     = $this->get_tag( $comment, 'wp:comment_approved');
     311            $comment_type         = $this->get_tag( $comment, 'wp:comment_type');
     312            $comment_parent       = $this->get_tag( $comment, 'wp:comment_parent');
     313
     314            if ( !comment_exists($comment_author, $comment_date) ) {
     315                $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_approved', 'comment_type', 'comment_parent');
     316                wp_insert_comment($commentdata);
     317                $num_comments++;
     318            }
     319        } }
     320
     321        if ( $num_comments )
     322            printf(' '.__('(%s comments)'), $num_comments);
     323
     324        // Now for post meta
     325        preg_match_all('|<wp:postmeta>(.*?)</wp:postmeta>|is', $post, $postmeta);
     326        $postmeta = $postmeta[1];
     327        if ( $postmeta) { foreach ($postmeta as $p) {
     328            $key   = $this->get_tag( $p, 'wp:meta_key' );
     329            $value = $this->get_tag( $p, 'wp:meta_value' );
     330            add_post_meta( $post_id, $key, $value );
     331        } }
    304332    }
    305333
Note: See TracChangeset for help on using the changeset viewer.