Make WordPress Core


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

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

File:
1 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(); ?>
Note: See TracChangeset for help on using the changeset viewer.