diff --git a/wp-admin/export.php b/wp-admin/export.php
index 75e18de160..ad654b008c 100644
--- a/wp-admin/export.php
+++ b/wp-admin/export.php
@@ -60,6 +60,68 @@ if ( isset( $_GET['author'] ) && $_GET['author'] != 'all' ) {
}
$posts = $wpdb->get_results("SELECT * FROM $wpdb->posts $where ORDER BY post_date_gmt ASC");
+
+$categories = (array) $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename, category_description, category_parent, posts_private, links_private FROM $wpdb->categories LEFT JOIN $wpdb->post2cat ON (category_id = cat_id) LEFT JOIN $wpdb->posts ON (post_id <=> id) $where GROUP BY cat_id");
+
+function wxr_missing_parents($categories) {
+ if ( !is_array($categories) || empty($categories) )
+ return array();
+
+ foreach ( $categories as $category )
+ $parents[$category->cat_ID] = $category->category_parent;
+
+ $parents = array_unique(array_diff($parents, array_keys($parents)));
+
+ if ( $zero = array_search('0', $parents) )
+ unset($parents[$zero]);
+
+ return $parents;
+}
+
+while ( $parents = wxr_missing_parents($categories) ) {
+ $found_parents = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename, category_description, category_parent, posts_private, links_private FROM $wpdb->categories WHERE cat_ID IN (" . join(', ', $parents) . ")");
+ if ( is_array($found_parents) && count($found_parents) )
+ $categories = array_merge($categories, $found_parents);
+ else
+ break;
+}
+
+// Put them in order to be inserted with no child going before its parent
+$pass = 0;
+$passes = 1000 + count($categories);
+while ( ( $cat = array_shift($categories) ) && ++$pass < $passes ) {
+ if ( $cat->category_parent == 0 || isset($cats[$cat->category_parent]) ) {
+ $cats[$cat->cat_ID] = $cat;
+ } else {
+ $categories[] = $cat;
+ }
+}
+unset($categories);
+
+function wxr_cdata($str) {
+ if ( seems_utf8($str) == false )
+ $str = utf8_encode($str);
+
+ // $str = ent2ncr(wp_specialchars($str));
+
+ $str = "";
+
+ return $str;
+}
+
+function wxr_cat_name($c) {
+ if ( empty($c->cat_name) )
+ return;
+
+ echo '' . wxr_cdata($c->cat_name) . '';
+}
+
+function wxr_category_description($c) {
+ if ( empty($c->category_description) )
+ return;
+
+ echo '' . wxr_cdata($c->category_description) . '';
+}
?>
@@ -86,6 +148,9 @@ $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts $where ORDER BY post_dat
http://wordpress.org/?v=
+
+ category_nicename; ?>category_parent ? $cats[$c->category_parent]->cat_name : ''; ?>posts_private ? '1' : '0'; ?>links_private ? '1' : '0'; ?>
+
-
diff --git a/wp-admin/import/wordpress.php b/wp-admin/import/wordpress.php
index 747103a01b..6147a19448 100644
--- a/wp-admin/import/wordpress.php
+++ b/wp-admin/import/wordpress.php
@@ -85,6 +85,8 @@ class WP_Import {
$importdata = preg_replace("/(\r\n|\n|\r)/", "\n", $importdata);
preg_match_all('|
- (.*?)
|is', $importdata, $this->posts);
$this->posts = $this->posts[1];
+ preg_match_all('|(.*?)|is', $importdata, $this->categories);
+ $this->categories = $this->categories[1];
}
function get_wp_authors() {
@@ -177,6 +179,35 @@ class WP_Import {
$this->wp_authors_form();
}
+ function process_categories() {
+ global $wpdb;
+
+ $cat_names = (array) $wpdb->get_col("SELECT cat_name FROM $wpdb->categories");
+
+ while ( $c = array_shift($this->categories) ) {
+ $cat_name = trim(str_replace(array (''), '', $this->get_tag( $c, 'wp:cat_name' )));
+
+ // If the category exists we leave it alone
+ if ( in_array($cat_name, $cat_names) )
+ continue;
+
+ $category_nicename = $this->get_tag( $c, 'wp:category_nicename' );
+ $posts_private = (int) $this->get_tag( $c, 'wp:posts_private' );
+ $links_private = (int) $this->get_tag( $c, 'wp:links_private' );
+
+ $parent = $this->get_tag( $c, 'wp:category_parent' );
+
+ if ( empty($parent) )
+ $category_parent = '0';
+ else
+ $category_parent = (int) category_exists($parent);
+
+ $catarr = compact('category_nicename', 'category_parent', 'posts_private', 'links_private', 'posts_private', 'cat_name');
+
+ $cat_ID = wp_insert_category($catarr);
+ }
+ }
+
function process_posts() {
global $wpdb;
$i = -1;
@@ -206,7 +237,7 @@ class WP_Import {
$cat_index = 0;
foreach ($categories as $category) {
- $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
+ $categories[$cat_index] = $wpdb->escape($this->unhtmlentities(str_replace(array (''), '', $category)));
$cat_index++;
}
@@ -277,6 +308,7 @@ class WP_Import {
$this->file = get_attached_file($this->id);
$this->get_authors_from_post();
$this->get_entries();
+ $this->process_categories();
$this->process_posts();
}