PageRenderTime 59ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/export.php

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