Make WordPress Core

Changeset 4558


Ignore:
Timestamp:
11/30/2006 06:15:14 PM (19 years ago)
Author:
ryan
Message:

Import/export category data. Props andy. fixes #3403

Location:
trunk/wp-admin
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/export.php

    r4495 r4558  
    6161
    6262$posts = $wpdb->get_results("SELECT * FROM $wpdb->posts $where ORDER BY post_date_gmt ASC");
     63
     64$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");
     65
     66function wxr_missing_parents($categories) {
     67    if ( !is_array($categories) || empty($categories) )
     68        return array();
     69
     70    foreach ( $categories as $category )
     71        $parents[$category->cat_ID] = $category->category_parent;
     72
     73    $parents = array_unique(array_diff($parents, array_keys($parents)));
     74
     75    if ( $zero = array_search('0', $parents) )
     76        unset($parents[$zero]);
     77
     78    return $parents;
     79}
     80
     81while ( $parents = wxr_missing_parents($categories) ) {
     82    $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) . ")");
     83    if ( is_array($found_parents) && count($found_parents) )
     84        $categories = array_merge($categories, $found_parents);
     85    else
     86        break;
     87}
     88
     89// Put them in order to be inserted with no child going before its parent
     90$pass = 0;
     91$passes = 1000 + count($categories);
     92while ( ( $cat = array_shift($categories) ) && ++$pass < $passes ) {
     93    if ( $cat->category_parent == 0 || isset($cats[$cat->category_parent]) ) {
     94        $cats[$cat->cat_ID] = $cat;
     95    } else {
     96        $categories[] = $cat;
     97    }
     98}
     99unset($categories);
     100
     101function wxr_cdata($str) {
     102    if ( seems_utf8($str) == false )
     103        $str = utf8_encode($str);
     104
     105    // $str = ent2ncr(wp_specialchars($str));
     106
     107    $str = "<![CDATA[$str" . ( ( substr($str, -1) == ']' ) ? ' ' : '') . "]]>";
     108
     109    return $str;
     110}
     111
     112function wxr_cat_name($c) {
     113    if ( empty($c->cat_name) )
     114        return;
     115
     116    echo '<wp:cat_name>' . wxr_cdata($c->cat_name) . '</wp:cat_name>';
     117}
     118
     119function wxr_category_description($c) {
     120    if ( empty($c->category_description) )
     121        return;
     122
     123    echo '<wp:category_description>' . wxr_cdata($c->category_description) . '</wp:category_description>';
     124}
    63125?>
    64126<!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. -->
     
    87149    <generator>http://wordpress.org/?v=<?php bloginfo_rss('version'); ?></generator>
    88150    <language><?php echo get_option('rss_language'); ?></language>
     151<?php if ( $cats ) : foreach ( $cats as $c ) : ?>
     152    <wp:category><wp:category_nicename><?php echo $c->category_nicename; ?></wp:category_nicename><wp:category_parent><?php echo $c->category_parent ? $cats[$c->category_parent]->cat_name : ''; ?></wp:category_parent><wp:posts_private><?php echo $c->posts_private ? '1' : '0'; ?></wp:posts_private><wp:links_private><?php echo $c->links_private ? '1' : '0'; ?></wp:links_private><?php wxr_cat_name($c); ?><?php wxr_category_description($c); ?></wp:category>
     153<?php endforeach; endif; ?>
    89154    <?php do_action('rss2_head'); ?>
    90155    <?php if ($posts) { foreach ($posts as $post) { start_wp(); ?>
  • trunk/wp-admin/import/wordpress.php

    r4495 r4558  
    8686        preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
    8787        $this->posts = $this->posts[1];
     88        preg_match_all('|<wp:category>(.*?)</wp:category>|is', $importdata, $this->categories);
     89        $this->categories = $this->categories[1];
    8890    }
    8991
     
    176178        $this->get_entries();
    177179        $this->wp_authors_form();
     180    }
     181
     182    function process_categories() {
     183        global $wpdb;
     184
     185        $cat_names = (array) $wpdb->get_col("SELECT cat_name FROM $wpdb->categories");
     186
     187        while ( $c = array_shift($this->categories) ) {
     188            $cat_name = trim(str_replace(array ('<![CDATA[', ']]>'), '', $this->get_tag( $c, 'wp:cat_name' )));
     189
     190            // If the category exists we leave it alone
     191            if ( in_array($cat_name, $cat_names) )
     192                continue;
     193
     194            $category_nicename  = $this->get_tag( $c, 'wp:category_nicename' );
     195            $posts_private      = (int) $this->get_tag( $c, 'wp:posts_private' );
     196            $links_private      = (int) $this->get_tag( $c, 'wp:links_private' );
     197
     198            $parent = $this->get_tag( $c, 'wp:category_parent' );
     199
     200            if ( empty($parent) )
     201                $category_parent = '0';
     202            else
     203                $category_parent = (int) category_exists($parent);
     204
     205            $catarr = compact('category_nicename', 'category_parent', 'posts_private', 'links_private', 'posts_private', 'cat_name');
     206
     207            $cat_ID = wp_insert_category($catarr);
     208        }
    178209    }
    179210
     
    207238            $cat_index = 0;
    208239            foreach ($categories as $category) {
    209                 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
     240                $categories[$cat_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $category)));
    210241                $cat_index++;
    211242            }
     
    278309        $this->get_authors_from_post();
    279310        $this->get_entries();
     311        $this->process_categories();
    280312        $this->process_posts();
    281313    }
Note: See TracChangeset for help on using the changeset viewer.