PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/export.php

https://github.com/ianloic/wordpress-ianloic
PHP | 255 lines | 205 code | 39 blank | 11 comment | 33 complexity | 4e2f0d93a46c596eeb5cbb2de3823b55 MD5 | raw file
  1. <?php
  2. // version number for the export format. bump this when something changes that might affect compatibility.
  3. define('WXR_VERSION', '1.0');
  4. function export_wp($author='') {
  5. global $wpdb, $post_ids, $post;
  6. do_action('export_wp');
  7. $filename = 'wordpress.' . date('Y-m-d') . '.xml';
  8. header('Content-Description: File Transfer');
  9. header("Content-Disposition: attachment; filename=$filename");
  10. header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);
  11. $where = '';
  12. if ( $author and $author != 'all' ) {
  13. $author_id = (int) $author;
  14. $where = " WHERE post_author = '$author_id' ";
  15. }
  16. // grab a snapshot of post IDs, just in case it changes during the export
  17. $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts $where ORDER BY post_date_gmt ASC");
  18. $categories = (array) get_categories('get=all');
  19. $tags = (array) get_tags('get=all');
  20. function wxr_missing_parents($categories) {
  21. if ( !is_array($categories) || empty($categories) )
  22. return array();
  23. foreach ( $categories as $category )
  24. $parents[$category->term_id] = $category->parent;
  25. $parents = array_unique(array_diff($parents, array_keys($parents)));
  26. if ( $zero = array_search('0', $parents) )
  27. unset($parents[$zero]);
  28. return $parents;
  29. }
  30. while ( $parents = wxr_missing_parents($categories) ) {
  31. $found_parents = get_categories("include=" . join(', ', $parents));
  32. if ( is_array($found_parents) && count($found_parents) )
  33. $categories = array_merge($categories, $found_parents);
  34. else
  35. break;
  36. }
  37. // Put them in order to be inserted with no child going before its parent
  38. $pass = 0;
  39. $passes = 1000 + count($categories);
  40. while ( ( $cat = array_shift($categories) ) && ++$pass < $passes ) {
  41. if ( $cat->parent == 0 || isset($cats[$cat->parent]) ) {
  42. $cats[$cat->term_id] = $cat;
  43. } else {
  44. $categories[] = $cat;
  45. }
  46. }
  47. unset($categories);
  48. function wxr_cdata($str) {
  49. if ( seems_utf8($str) == false )
  50. $str = utf8_encode($str);
  51. // $str = ent2ncr(wp_specialchars($str));
  52. $str = "<![CDATA[$str" . ( ( substr($str, -1) == ']' ) ? ' ' : '') . "]]>";
  53. return $str;
  54. }
  55. function wxr_site_url() {
  56. global $current_site;
  57. // mu: the base url
  58. if ( isset($current_site->domain) ) {
  59. return 'http://'.$current_site->domain.$current_site->path;
  60. }
  61. // wp: the blog url
  62. else {
  63. return get_bloginfo_rss('url');
  64. }
  65. }
  66. function wxr_cat_name($c) {
  67. if ( empty($c->name) )
  68. return;
  69. echo '<wp:cat_name>' . wxr_cdata($c->name) . '</wp:cat_name>';
  70. }
  71. function wxr_category_description($c) {
  72. if ( empty($c->description) )
  73. return;
  74. echo '<wp:category_description>' . wxr_cdata($c->description) . '</wp:category_description>';
  75. }
  76. function wxr_tag_name($t) {
  77. if ( empty($t->name) )
  78. return;
  79. echo '<wp:tag_name>' . wxr_cdata($t->name) . '</wp:tag_name>';
  80. }
  81. function wxr_tag_description($t) {
  82. if ( empty($t->description) )
  83. return;
  84. echo '<wp:tag_description>' . wxr_cdata($t->description) . '</wp:tag_description>';
  85. }
  86. function wxr_post_taxonomy() {
  87. $categories = get_the_category();
  88. $tags = get_the_tags();
  89. $the_list = '';
  90. $filter = 'rss';
  91. if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
  92. $cat_name = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
  93. // for backwards compatibility
  94. $the_list .= "\n\t\t<category><![CDATA[$cat_name]]></category>\n";
  95. // forwards compatibility: use a unique identifier for each cat to avoid clashes
  96. // http://trac.wordpress.org/ticket/5447
  97. $the_list .= "\n\t\t<category domain=\"category\" nicename=\"{$category->slug}\"><![CDATA[$cat_name]]></category>\n";
  98. }
  99. if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
  100. $tag_name = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
  101. $the_list .= "\n\t\t<category domain=\"tag\"><![CDATA[$tag_name]]></category>\n";
  102. // forwards compatibility as above
  103. $the_list .= "\n\t\t<category domain=\"tag\" nicename=\"{$tag->slug}\"><![CDATA[$tag_name]]></category>\n";
  104. }
  105. echo $the_list;
  106. }
  107. echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . '"?' . ">\n";
  108. ?>
  109. <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. -->
  110. <!-- It contains information about your blog's posts, comments, and categories. -->
  111. <!-- You may use this file to transfer that content from one site to another. -->
  112. <!-- This file is not intended to serve as a complete backup of your blog. -->
  113. <!-- To import this information into a WordPress blog follow these steps. -->
  114. <!-- 1. Log into that blog as an administrator. -->
  115. <!-- 2. Go to Manage: Import in the blog's admin panels. -->
  116. <!-- 3. Choose "WordPress" from the list. -->
  117. <!-- 4. Upload this file using the form provided on that page. -->
  118. <!-- 5. You will first be asked to map the authors in this export file to users -->
  119. <!-- on the blog. For each author, you may choose to map to an -->
  120. <!-- existing user on the blog or to create a new user -->
  121. <!-- 6. WordPress will then import each of the posts, comments, and categories -->
  122. <!-- contained in this file into your blog -->
  123. <?php the_generator('export');?>
  124. <rss version="2.0"
  125. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  126. xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  127. xmlns:dc="http://purl.org/dc/elements/1.1/"
  128. xmlns:wp="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/"
  129. >
  130. <channel>
  131. <title><?php bloginfo_rss('name'); ?></title>
  132. <link><?php bloginfo_rss('url') ?></link>
  133. <description><?php bloginfo_rss("description") ?></description>
  134. <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></pubDate>
  135. <generator>http://wordpress.org/?v=<?php bloginfo_rss('version'); ?></generator>
  136. <language><?php echo get_option('rss_language'); ?></language>
  137. <wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
  138. <wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
  139. <wp:base_blog_url><?php bloginfo_rss('url'); ?></wp:base_blog_url>
  140. <?php if ( $cats ) : foreach ( $cats as $c ) : ?>
  141. <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>
  142. <?php endforeach; endif; ?>
  143. <?php if ( $tags ) : foreach ( $tags as $t ) : ?>
  144. <wp:tag><wp:tag_slug><?php echo $t->slug; ?></wp:tag_slug><?php wxr_tag_name($t); ?><?php wxr_tag_description($t); ?></wp:tag>
  145. <?php endforeach; endif; ?>
  146. <?php do_action('rss2_head'); ?>
  147. <?php if ($post_ids) {
  148. global $wp_query;
  149. $wp_query->in_the_loop = true; // Fake being in the loop.
  150. // fetch 20 posts at a time rather than loading the entire table into memory
  151. while ( $next_posts = array_splice($post_ids, 0, 20) ) {
  152. $where = "WHERE ID IN (".join(',', $next_posts).")";
  153. $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts $where ORDER BY post_date_gmt ASC");
  154. foreach ($posts as $post) {
  155. setup_postdata($post); ?>
  156. <item>
  157. <title><?php echo apply_filters('the_title_rss', $post->post_title); ?></title>
  158. <link><?php the_permalink_rss() ?></link>
  159. <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
  160. <dc:creator><?php echo wxr_cdata(get_the_author()); ?></dc:creator>
  161. <?php wxr_post_taxonomy() ?>
  162. <guid isPermaLink="false"><?php the_guid(); ?></guid>
  163. <description></description>
  164. <content:encoded><?php echo wxr_cdata( apply_filters('the_content_export', $post->post_content) ); ?></content:encoded>
  165. <wp:post_id><?php echo $post->ID; ?></wp:post_id>
  166. <wp:post_date><?php echo $post->post_date; ?></wp:post_date>
  167. <wp:post_date_gmt><?php echo $post->post_date_gmt; ?></wp:post_date_gmt>
  168. <wp:comment_status><?php echo $post->comment_status; ?></wp:comment_status>
  169. <wp:ping_status><?php echo $post->ping_status; ?></wp:ping_status>
  170. <wp:post_name><?php echo $post->post_name; ?></wp:post_name>
  171. <wp:status><?php echo $post->post_status; ?></wp:status>
  172. <wp:post_parent><?php echo $post->post_parent; ?></wp:post_parent>
  173. <wp:menu_order><?php echo $post->menu_order; ?></wp:menu_order>
  174. <wp:post_type><?php echo $post->post_type; ?></wp:post_type>
  175. <wp:post_password><?php echo $post->post_password; ?></wp:post_password>
  176. <?php
  177. if ($post->post_type == 'attachment') { ?>
  178. <wp:attachment_url><?php echo wp_get_attachment_url($post->ID); ?></wp:attachment_url>
  179. <?php } ?>
  180. <?php
  181. $postmeta = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE post_id = $post->ID");
  182. if ( $postmeta ) {
  183. ?>
  184. <?php foreach( $postmeta as $meta ) { ?>
  185. <wp:postmeta>
  186. <wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key>
  187. <wp:meta_value><?Php echo $meta->meta_value; ?></wp:meta_value>
  188. </wp:postmeta>
  189. <?php } ?>
  190. <?php } ?>
  191. <?php
  192. $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID");
  193. if ( $comments ) { foreach ( $comments as $c ) { ?>
  194. <wp:comment>
  195. <wp:comment_id><?php echo $c->comment_ID; ?></wp:comment_id>
  196. <wp:comment_author><?php echo wxr_cdata($c->comment_author); ?></wp:comment_author>
  197. <wp:comment_author_email><?php echo $c->comment_author_email; ?></wp:comment_author_email>
  198. <wp:comment_author_url><?php echo $c->comment_author_url; ?></wp:comment_author_url>
  199. <wp:comment_author_IP><?php echo $c->comment_author_IP; ?></wp:comment_author_IP>
  200. <wp:comment_date><?php echo $c->comment_date; ?></wp:comment_date>
  201. <wp:comment_date_gmt><?php echo $c->comment_date_gmt; ?></wp:comment_date_gmt>
  202. <wp:comment_content><?php echo wxr_cdata($c->comment_content) ?></wp:comment_content>
  203. <wp:comment_approved><?php echo $c->comment_approved; ?></wp:comment_approved>
  204. <wp:comment_type><?php echo $c->comment_type; ?></wp:comment_type>
  205. <wp:comment_parent><?php echo $c->comment_parent; ?></wp:comment_parent>
  206. <wp:comment_user_id><?php echo $c->user_id; ?></wp:comment_user_id>
  207. </wp:comment>
  208. <?php } } ?>
  209. </item>
  210. <?php } } } ?>
  211. </channel>
  212. </rss>
  213. <?php
  214. }
  215. ?>