Preserve page hierarchy. Props takayukister. fixes #4025

git-svn-id: http://svn.automattic.com/wordpress/trunk@5245 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-04-12 01:34:15 +00:00
parent 43c0541e81
commit 69d3f2df59
1 changed files with 112 additions and 84 deletions

View File

@ -3,6 +3,8 @@
class WP_Import { class WP_Import {
var $posts = array (); var $posts = array ();
var $posts_processed = array ();
// Array of arrays. [[0] => XML fragment, [1] => New post ID]
var $file; var $file;
var $id; var $id;
var $mtnames = array (); var $mtnames = array ();
@ -86,6 +88,12 @@ class WP_Import {
$importdata = preg_replace("/(\r\n|\n|\r)/", "\n", $importdata); $importdata = preg_replace("/(\r\n|\n|\r)/", "\n", $importdata);
preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts); preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
$this->posts = $this->posts[1]; $this->posts = $this->posts[1];
foreach ($this->posts as $post) {
$post_ID = (int) $this->get_tag( $post, 'wp:post_id' );
if ($post_ID)
$this->posts_processed[$post_ID][0] = &$post;
$this->posts_processed[$post_ID][1] = 0;
}
preg_match_all('|<wp:category>(.*?)</wp:category>|is', $importdata, $this->categories); preg_match_all('|<wp:category>(.*?)</wp:category>|is', $importdata, $this->categories);
$this->categories = $this->categories[1]; $this->categories = $this->categories[1];
} }
@ -208,10 +216,25 @@ class WP_Import {
} }
function process_posts() { function process_posts() {
global $wpdb;
$i = -1; $i = -1;
echo '<ol>'; echo '<ol>';
foreach ($this->posts as $post) {
foreach ($this->posts as $post)
$this->process_post($post);
echo '</ol>';
wp_import_cleanup($this->id);
echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>';
}
function process_post($post) {
global $wpdb;
$post_ID = (int) $this->get_tag( $post, 'wp:post_id' );
if ( $post_ID && !empty($this->posts_processed[$post_ID][1]) ) // Processed already
return 0;
// There are only ever one of these // There are only ever one of these
$post_title = $this->get_tag( $post, 'title' ); $post_title = $this->get_tag( $post, 'title' );
@ -246,6 +269,14 @@ class WP_Import {
echo '<li>'; echo '<li>';
printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title)); printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
} else { } else {
// If it has parent, process parent first.
$post_parent = (int) $post_parent;
if ($parent = $this->posts_processed[$post_parent]) {
if (!$parent[1]) $this->process_post($parent[0]); // If not yet, process the parent first.
$post_parent = $parent[1]; // New ID of the parent;
}
echo '<li>'; echo '<li>';
printf(__('Importing post <i>%s</i>...'), stripslashes($post_title)); printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
@ -253,10 +284,15 @@ class WP_Import {
$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'); $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');
$comment_post_ID = $post_id = wp_insert_post($postdata); $comment_post_ID = $post_id = wp_insert_post($postdata);
// Memorize old and new ID.
if ( $post_id && $post_ID && $this->posts_processed[$post_ID] )
$this->posts_processed[$post_ID][1] = $post_id; // New ID.
// Add categories. // Add categories.
if (0 != count($categories)) { if ( 0 != count($categories) )
wp_create_categories($categories, $post_id); wp_create_categories($categories, $post_id);
}
} }
// Now for comments // Now for comments
@ -281,6 +317,7 @@ class WP_Import {
$num_comments++; $num_comments++;
} }
} } } }
if ( $num_comments ) if ( $num_comments )
printf(' '.__('(%s comments)'), $num_comments); printf(' '.__('(%s comments)'), $num_comments);
@ -292,15 +329,6 @@ class WP_Import {
$value = $this->get_tag( $p, 'wp:meta_value' ); $value = $this->get_tag( $p, 'wp:meta_value' );
add_post_meta( $post_id, $key, $value ); add_post_meta( $post_id, $key, $value );
} } } }
$index++;
}
echo '</ol>';
wp_import_cleanup($this->id);
echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>';
} }
function import() { function import() {