web/wp-admin/includes/export.php
changeset 136 bde1974c263b
child 194 32102edaa81b
equal deleted inserted replaced
135:53cff4b4a802 136:bde1974c263b
       
     1 <?php
       
     2 /**
       
     3  * WordPress Export Administration API
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Administration
       
     7  */
       
     8 
       
     9 /**
       
    10  * Version number for the export format.
       
    11  *
       
    12  * Bump this when something changes that might affect compatibility.
       
    13  *
       
    14  * @since unknown
       
    15  * @var string
       
    16  */
       
    17 define('WXR_VERSION', '1.0');
       
    18 
       
    19 /**
       
    20  * {@internal Missing Short Description}}
       
    21  *
       
    22  * @since unknown
       
    23  *
       
    24  * @param unknown_type $author
       
    25  */
       
    26 function export_wp($author='') {
       
    27 global $wpdb, $post_ids, $post, $wp_taxonomies;
       
    28 
       
    29 do_action('export_wp');
       
    30 
       
    31 $filename = 'wordpress.' . date('Y-m-d') . '.xml';
       
    32 
       
    33 header('Content-Description: File Transfer');
       
    34 header("Content-Disposition: attachment; filename=$filename");
       
    35 header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);
       
    36 
       
    37 $where = '';
       
    38 if ( $author and $author != 'all' ) {
       
    39 	$author_id = (int) $author;
       
    40 	$where = $wpdb->prepare(" WHERE post_author = %d ", $author_id);
       
    41 }
       
    42 
       
    43 // grab a snapshot of post IDs, just in case it changes during the export
       
    44 $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts $where ORDER BY post_date_gmt ASC");
       
    45 
       
    46 $categories = (array) get_categories('get=all');
       
    47 $tags = (array) get_tags('get=all');
       
    48 
       
    49 $custom_taxonomies = $wp_taxonomies;
       
    50 unset($custom_taxonomies['category']);
       
    51 unset($custom_taxonomies['post_tag']);
       
    52 unset($custom_taxonomies['link_category']);
       
    53 $custom_taxonomies = array_keys($custom_taxonomies);
       
    54 $terms = (array) get_terms($custom_taxonomies, 'get=all');
       
    55 
       
    56 /**
       
    57  * {@internal Missing Short Description}}
       
    58  *
       
    59  * @since unknown
       
    60  *
       
    61  * @param unknown_type $categories
       
    62  */
       
    63 function wxr_missing_parents($categories) {
       
    64 	if ( !is_array($categories) || empty($categories) )
       
    65 		return array();
       
    66 
       
    67 	foreach ( $categories as $category )
       
    68 		$parents[$category->term_id] = $category->parent;
       
    69 
       
    70 	$parents = array_unique(array_diff($parents, array_keys($parents)));
       
    71 
       
    72 	if ( $zero = array_search('0', $parents) )
       
    73 		unset($parents[$zero]);
       
    74 
       
    75 	return $parents;
       
    76 }
       
    77 
       
    78 while ( $parents = wxr_missing_parents($categories) ) {
       
    79 	$found_parents = get_categories("include=" . join(', ', $parents));
       
    80 	if ( is_array($found_parents) && count($found_parents) )
       
    81 		$categories = array_merge($categories, $found_parents);
       
    82 	else
       
    83 		break;
       
    84 }
       
    85 
       
    86 // Put them in order to be inserted with no child going before its parent
       
    87 $pass = 0;
       
    88 $passes = 1000 + count($categories);
       
    89 while ( ( $cat = array_shift($categories) ) && ++$pass < $passes ) {
       
    90 	if ( $cat->parent == 0 || isset($cats[$cat->parent]) ) {
       
    91 		$cats[$cat->term_id] = $cat;
       
    92 	} else {
       
    93 		$categories[] = $cat;
       
    94 	}
       
    95 }
       
    96 unset($categories);
       
    97 
       
    98 /**
       
    99  * Place string in CDATA tag.
       
   100  *
       
   101  * @since unknown
       
   102  *
       
   103  * @param string $str String to place in XML CDATA tag.
       
   104  */
       
   105 function wxr_cdata($str) {
       
   106 	if ( seems_utf8($str) == false )
       
   107 		$str = utf8_encode($str);
       
   108 
       
   109 	// $str = ent2ncr(esc_html($str));
       
   110 
       
   111 	$str = "<![CDATA[$str" . ( ( substr($str, -1) == ']' ) ? ' ' : '') . "]]>";
       
   112 
       
   113 	return $str;
       
   114 }
       
   115 
       
   116 /**
       
   117  * {@internal Missing Short Description}}
       
   118  *
       
   119  * @since unknown
       
   120  *
       
   121  * @return string Site URL.
       
   122  */
       
   123 function wxr_site_url() {
       
   124 	global $current_site;
       
   125 
       
   126 	// mu: the base url
       
   127 	if ( isset($current_site->domain) ) {
       
   128 		return 'http://'.$current_site->domain.$current_site->path;
       
   129 	}
       
   130 	// wp: the blog url
       
   131 	else {
       
   132 		return get_bloginfo_rss('url');
       
   133 	}
       
   134 }
       
   135 
       
   136 /**
       
   137  * {@internal Missing Short Description}}
       
   138  *
       
   139  * @since unknown
       
   140  *
       
   141  * @param object $c Category Object
       
   142  */
       
   143 function wxr_cat_name($c) {
       
   144 	if ( empty($c->name) )
       
   145 		return;
       
   146 
       
   147 	echo '<wp:cat_name>' . wxr_cdata($c->name) . '</wp:cat_name>';
       
   148 }
       
   149 
       
   150 /**
       
   151  * {@internal Missing Short Description}}
       
   152  *
       
   153  * @since unknown
       
   154  *
       
   155  * @param object $c Category Object
       
   156  */
       
   157 function wxr_category_description($c) {
       
   158 	if ( empty($c->description) )
       
   159 		return;
       
   160 
       
   161 	echo '<wp:category_description>' . wxr_cdata($c->description) . '</wp:category_description>';
       
   162 }
       
   163 
       
   164 /**
       
   165  * {@internal Missing Short Description}}
       
   166  *
       
   167  * @since unknown
       
   168  *
       
   169  * @param object $t Tag Object
       
   170  */
       
   171 function wxr_tag_name($t) {
       
   172 	if ( empty($t->name) )
       
   173 		return;
       
   174 
       
   175 	echo '<wp:tag_name>' . wxr_cdata($t->name) . '</wp:tag_name>';
       
   176 }
       
   177 
       
   178 /**
       
   179  * {@internal Missing Short Description}}
       
   180  *
       
   181  * @since unknown
       
   182  *
       
   183  * @param object $t Tag Object
       
   184  */
       
   185 function wxr_tag_description($t) {
       
   186 	if ( empty($t->description) )
       
   187 		return;
       
   188 
       
   189 	echo '<wp:tag_description>' . wxr_cdata($t->description) . '</wp:tag_description>';
       
   190 }
       
   191 
       
   192 /**
       
   193  * {@internal Missing Short Description}}
       
   194  *
       
   195  * @since unknown
       
   196  *
       
   197  * @param object $t Term Object
       
   198  */
       
   199 function wxr_term_name($t) {
       
   200 	if ( empty($t->name) )
       
   201 		return;
       
   202 
       
   203 	echo '<wp:term_name>' . wxr_cdata($t->name) . '</wp:term_name>';
       
   204 }
       
   205 
       
   206 /**
       
   207  * {@internal Missing Short Description}}
       
   208  *
       
   209  * @since unknown
       
   210  *
       
   211  * @param object $t Term Object
       
   212  */
       
   213 function wxr_term_description($t) {
       
   214 	if ( empty($t->description) )
       
   215 		return;
       
   216 
       
   217 	echo '<wp:term_description>' . wxr_cdata($t->description) . '</wp:term_description>';
       
   218 }
       
   219 
       
   220 /**
       
   221  * {@internal Missing Short Description}}
       
   222  *
       
   223  * @since unknown
       
   224  */
       
   225 function wxr_post_taxonomy() {
       
   226 	$categories = get_the_category();
       
   227 	$tags = get_the_tags();
       
   228 	$the_list = '';
       
   229 	$filter = 'rss';
       
   230 
       
   231 	if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
       
   232 		$cat_name = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
       
   233 		// for backwards compatibility
       
   234 		$the_list .= "\n\t\t<category><![CDATA[$cat_name]]></category>\n";
       
   235 		// forwards compatibility: use a unique identifier for each cat to avoid clashes
       
   236 		// http://trac.wordpress.org/ticket/5447
       
   237 		$the_list .= "\n\t\t<category domain=\"category\" nicename=\"{$category->slug}\"><![CDATA[$cat_name]]></category>\n";
       
   238 	}
       
   239 
       
   240 	if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
       
   241 		$tag_name = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
       
   242 		$the_list .= "\n\t\t<category domain=\"tag\"><![CDATA[$tag_name]]></category>\n";
       
   243 		// forwards compatibility as above
       
   244 		$the_list .= "\n\t\t<category domain=\"tag\" nicename=\"{$tag->slug}\"><![CDATA[$tag_name]]></category>\n";
       
   245 	}
       
   246 
       
   247 	echo $the_list;
       
   248 }
       
   249 
       
   250 echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . '"?' . ">\n";
       
   251 
       
   252 ?>
       
   253 <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. -->
       
   254 <!-- It contains information about your blog's posts, comments, and categories. -->
       
   255 <!-- You may use this file to transfer that content from one site to another. -->
       
   256 <!-- This file is not intended to serve as a complete backup of your blog. -->
       
   257 
       
   258 <!-- To import this information into a WordPress blog follow these steps. -->
       
   259 <!-- 1. Log in to that blog as an administrator. -->
       
   260 <!-- 2. Go to Tools: Import in the blog's admin panels (or Manage: Import in older versions of WordPress). -->
       
   261 <!-- 3. Choose "WordPress" from the list. -->
       
   262 <!-- 4. Upload this file using the form provided on that page. -->
       
   263 <!-- 5. You will first be asked to map the authors in this export file to users -->
       
   264 <!--    on the blog.  For each author, you may choose to map to an -->
       
   265 <!--    existing user on the blog or to create a new user -->
       
   266 <!-- 6. WordPress will then import each of the posts, comments, and categories -->
       
   267 <!--    contained in this file into your blog -->
       
   268 
       
   269 <?php the_generator('export');?>
       
   270 <rss version="2.0"
       
   271 	xmlns:excerpt="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/excerpt/"
       
   272 	xmlns:content="http://purl.org/rss/1.0/modules/content/"
       
   273 	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
       
   274 	xmlns:dc="http://purl.org/dc/elements/1.1/"
       
   275 	xmlns:wp="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/"
       
   276 >
       
   277 
       
   278 <channel>
       
   279 	<title><?php bloginfo_rss('name'); ?></title>
       
   280 	<link><?php bloginfo_rss('url') ?></link>
       
   281 	<description><?php bloginfo_rss("description") ?></description>
       
   282 	<pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></pubDate>
       
   283 	<generator>http://wordpress.org/?v=<?php bloginfo_rss('version'); ?></generator>
       
   284 	<language><?php echo get_option('rss_language'); ?></language>
       
   285 	<wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
       
   286 	<wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
       
   287 	<wp:base_blog_url><?php bloginfo_rss('url'); ?></wp:base_blog_url>
       
   288 <?php if ( $cats ) : foreach ( $cats as $c ) : ?>
       
   289 	<wp:category><wp:category_nicename><?php echo $c->slug; ?></wp:category_nicename><wp:category_parent><?php echo $c->parent ? $cats[$c->parent]->name : ''; ?></wp:category_parent><?php wxr_cat_name($c); ?><?php wxr_category_description($c); ?></wp:category>
       
   290 <?php endforeach; endif; ?>
       
   291 <?php if ( $tags ) : foreach ( $tags as $t ) : ?>
       
   292 	<wp:tag><wp:tag_slug><?php echo $t->slug; ?></wp:tag_slug><?php wxr_tag_name($t); ?><?php wxr_tag_description($t); ?></wp:tag>
       
   293 <?php endforeach; endif; ?>
       
   294 <?php if ( $terms ) : foreach ( $terms as $t ) : ?>
       
   295 	<wp:term><wp:term_taxonomy><?php echo $t->taxonomy; ?></wp:term_taxonomy><wp:term_slug><?php echo $t->slug; ?></wp:term_slug><wp:term_parent><?php echo $t->parent ? $custom_taxonomies[$t->parent]->name : ''; ?></wp:term_parent><?php wxr_term_name($t); ?><?php wxr_term_description($t); ?></wp:term>
       
   296 <?php endforeach; endif; ?>
       
   297 	<?php do_action('rss2_head'); ?>
       
   298 	<?php if ($post_ids) {
       
   299 		global $wp_query;
       
   300 		$wp_query->in_the_loop = true;  // Fake being in the loop.
       
   301 		// fetch 20 posts at a time rather than loading the entire table into memory
       
   302 		while ( $next_posts = array_splice($post_ids, 0, 20) ) {
       
   303 			$where = "WHERE ID IN (".join(',', $next_posts).")";
       
   304 			$posts = $wpdb->get_results("SELECT * FROM $wpdb->posts $where ORDER BY post_date_gmt ASC");
       
   305 				foreach ($posts as $post) {
       
   306 			// Don't export revisions.  They bloat the export.
       
   307 			if ( 'revision' == $post->post_type )
       
   308 				continue;
       
   309 			setup_postdata($post);
       
   310 
       
   311 			$is_sticky = 0;
       
   312 			if ( is_sticky( $post->ID ) )
       
   313 				$is_sticky = 1;
       
   314 
       
   315 ?>
       
   316 <item>
       
   317 <title><?php echo apply_filters('the_title_rss', $post->post_title); ?></title>
       
   318 <link><?php the_permalink_rss() ?></link>
       
   319 <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
       
   320 <dc:creator><?php echo wxr_cdata(get_the_author()); ?></dc:creator>
       
   321 <?php wxr_post_taxonomy() ?>
       
   322 
       
   323 <guid isPermaLink="false"><?php the_guid(); ?></guid>
       
   324 <description></description>
       
   325 <content:encoded><?php echo wxr_cdata( apply_filters('the_content_export', $post->post_content) ); ?></content:encoded>
       
   326 <excerpt:encoded><?php echo wxr_cdata( apply_filters('the_excerpt_export', $post->post_excerpt) ); ?></excerpt:encoded>
       
   327 <wp:post_id><?php echo $post->ID; ?></wp:post_id>
       
   328 <wp:post_date><?php echo $post->post_date; ?></wp:post_date>
       
   329 <wp:post_date_gmt><?php echo $post->post_date_gmt; ?></wp:post_date_gmt>
       
   330 <wp:comment_status><?php echo $post->comment_status; ?></wp:comment_status>
       
   331 <wp:ping_status><?php echo $post->ping_status; ?></wp:ping_status>
       
   332 <wp:post_name><?php echo $post->post_name; ?></wp:post_name>
       
   333 <wp:status><?php echo $post->post_status; ?></wp:status>
       
   334 <wp:post_parent><?php echo $post->post_parent; ?></wp:post_parent>
       
   335 <wp:menu_order><?php echo $post->menu_order; ?></wp:menu_order>
       
   336 <wp:post_type><?php echo $post->post_type; ?></wp:post_type>
       
   337 <wp:post_password><?php echo $post->post_password; ?></wp:post_password>
       
   338 <wp:is_sticky><?php echo $is_sticky; ?></wp:is_sticky>
       
   339 <?php
       
   340 if ($post->post_type == 'attachment') { ?>
       
   341 <wp:attachment_url><?php echo wp_get_attachment_url($post->ID); ?></wp:attachment_url>
       
   342 <?php } ?>
       
   343 <?php
       
   344 $postmeta = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID) );
       
   345 if ( $postmeta ) {
       
   346 ?>
       
   347 <?php foreach( $postmeta as $meta ) { ?>
       
   348 <wp:postmeta>
       
   349 <wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key>
       
   350 <wp:meta_value><?Php echo $meta->meta_value; ?></wp:meta_value>
       
   351 </wp:postmeta>
       
   352 <?php } ?>
       
   353 <?php } ?>
       
   354 <?php
       
   355 $comments = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d", $post->ID) );
       
   356 if ( $comments ) { foreach ( $comments as $c ) { ?>
       
   357 <wp:comment>
       
   358 <wp:comment_id><?php echo $c->comment_ID; ?></wp:comment_id>
       
   359 <wp:comment_author><?php echo wxr_cdata($c->comment_author); ?></wp:comment_author>
       
   360 <wp:comment_author_email><?php echo $c->comment_author_email; ?></wp:comment_author_email>
       
   361 <wp:comment_author_url><?php echo esc_url_raw( $c->comment_author_url ); ?></wp:comment_author_url>
       
   362 <wp:comment_author_IP><?php echo $c->comment_author_IP; ?></wp:comment_author_IP>
       
   363 <wp:comment_date><?php echo $c->comment_date; ?></wp:comment_date>
       
   364 <wp:comment_date_gmt><?php echo $c->comment_date_gmt; ?></wp:comment_date_gmt>
       
   365 <wp:comment_content><?php echo wxr_cdata($c->comment_content) ?></wp:comment_content>
       
   366 <wp:comment_approved><?php echo $c->comment_approved; ?></wp:comment_approved>
       
   367 <wp:comment_type><?php echo $c->comment_type; ?></wp:comment_type>
       
   368 <wp:comment_parent><?php echo $c->comment_parent; ?></wp:comment_parent>
       
   369 <wp:comment_user_id><?php echo $c->user_id; ?></wp:comment_user_id>
       
   370 </wp:comment>
       
   371 <?php } } ?>
       
   372 	</item>
       
   373 <?php } } } ?>
       
   374 </channel>
       
   375 </rss>
       
   376 <?php
       
   377 }
       
   378 
       
   379 ?>