PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/2.1/wp-admin/export.php

#
PHP | 218 lines | 186 code | 30 blank | 2 comment | 25 complexity | e979e8e6d37414cf2b87518fdf98b50c MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. require_once ('admin.php');
  3. $title = __('Export');
  4. $parent_file = 'edit.php';
  5. if ( isset( $_GET['download'] ) )
  6. export_wp();
  7. require_once ('admin-header.php');
  8. ?>
  9. <div class="wrap">
  10. <h2><?php _e('Export'); ?></h2>
  11. <div class="narrow">
  12. <p><?php _e('When you click the button below WordPress will create an XML file for you to save to your computer.'); ?></p>
  13. <p><?php _e('This format, which we call WordPress eXtended RSS or WXR, will contain your posts, comments, custom fields, and categories.'); ?></p>
  14. <p><?php _e('Once you&#8217;ve saved the download file, you can use the Import function on another WordPress blog to import this blog.'); ?></p>
  15. <form action="" method="get">
  16. <h3><?php _e('Optional options'); ?></h3>
  17. <table>
  18. <tr>
  19. <th><?php _e('Restrict Author:'); ?></th>
  20. <td>
  21. <select name="author">
  22. <option value="all" selected="selected"><?php _e('All'); ?></option>
  23. <?php
  24. $authors = $wpdb->get_col( "SELECT post_author FROM $wpdb->posts GROUP BY post_author" );
  25. foreach ( $authors as $id ) {
  26. $o = get_userdata( $id );
  27. echo "<option value='$o->ID'>$o->display_name</option>";
  28. }
  29. ?>
  30. </select>
  31. </td>
  32. </tr>
  33. </table>
  34. <p class="submit"><input type="submit" name="submit" value="<?php _e('Download Export File'); ?> &raquo;" />
  35. <input type="hidden" name="download" value="true" />
  36. </p>
  37. </form>
  38. </div>
  39. </div>
  40. <?php
  41. function export_wp() {
  42. global $wpdb, $posts, $post;
  43. $filename = 'wordpress.' . date('Y-m-d') . '.xml';
  44. header('Content-Description: File Transfer');
  45. header("Content-Disposition: attachment; filename=$filename");
  46. header('Content-type: text/xml; charset=' . get_option('blog_charset'), true);
  47. $where = '';
  48. if ( isset( $_GET['author'] ) && $_GET['author'] != 'all' ) {
  49. $author_id = (int) $_GET['author'];
  50. $where = " WHERE post_author = '$author_id' ";
  51. }
  52. $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts $where ORDER BY post_date_gmt ASC");
  53. $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");
  54. function wxr_missing_parents($categories) {
  55. if ( !is_array($categories) || empty($categories) )
  56. return array();
  57. foreach ( $categories as $category )
  58. $parents[$category->cat_ID] = $category->category_parent;
  59. $parents = array_unique(array_diff($parents, array_keys($parents)));
  60. if ( $zero = array_search('0', $parents) )
  61. unset($parents[$zero]);
  62. return $parents;
  63. }
  64. while ( $parents = wxr_missing_parents($categories) ) {
  65. $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) . ")");
  66. if ( is_array($found_parents) && count($found_parents) )
  67. $categories = array_merge($categories, $found_parents);
  68. else
  69. break;
  70. }
  71. // Put them in order to be inserted with no child going before its parent
  72. $pass = 0;
  73. $passes = 1000 + count($categories);
  74. while ( ( $cat = array_shift($categories) ) && ++$pass < $passes ) {
  75. if ( $cat->category_parent == 0 || isset($cats[$cat->category_parent]) ) {
  76. $cats[$cat->cat_ID] = $cat;
  77. } else {
  78. $categories[] = $cat;
  79. }
  80. }
  81. unset($categories);
  82. function wxr_cdata($str) {
  83. if ( seems_utf8($str) == false )
  84. $str = utf8_encode($str);
  85. // $str = ent2ncr(wp_specialchars($str));
  86. $str = "<![CDATA[$str" . ( ( substr($str, -1) == ']' ) ? ' ' : '') . "]]>";
  87. return $str;
  88. }
  89. function wxr_cat_name($c) {
  90. if ( empty($c->cat_name) )
  91. return;
  92. echo '<wp:cat_name>' . wxr_cdata($c->cat_name) . '</wp:cat_name>';
  93. }
  94. function wxr_category_description($c) {
  95. if ( empty($c->category_description) )
  96. return;
  97. echo '<wp:category_description>' . wxr_cdata($c->category_description) . '</wp:category_description>';
  98. }
  99. ?>
  100. <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. -->
  101. <!-- It contains information about your blog's posts, comments, and categories. -->
  102. <!-- You may use this file to transfer that content from one site to another. -->
  103. <!-- This file is not intended to serve as a complete backup of your blog. -->
  104. <!-- To import this information into a WordPress blog follow these steps. -->
  105. <!-- 1. Log into that blog as an administrator. -->
  106. <!-- 2. Go to Manage: Import in the blog's admin panels. -->
  107. <!-- 3. Choose "WordPress" from the list. -->
  108. <!-- 4. Upload this file using the form provided on that page. -->
  109. <!-- 5. You will first be asked to map the authors in this export file to users -->
  110. <!-- on the blog. For each author, you may choose to map to an -->
  111. <!-- existing user on the blog or to create a new user -->
  112. <!-- 6. WordPress will then import each of the posts, comments, and categories -->
  113. <!-- contained in this file into your blog -->
  114. <!-- generator="wordpress/<?php bloginfo_rss('version') ?>" created="<?php echo date('Y-m-d H:m'); ?>"-->
  115. <rss version="2.0"
  116. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  117. xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  118. xmlns:dc="http://purl.org/dc/elements/1.1/"
  119. xmlns:wp="http://wordpress.org/export/1.0/"
  120. >
  121. <channel>
  122. <title><?php bloginfo_rss('name'); ?></title>
  123. <link><?php bloginfo_rss('url') ?></link>
  124. <description><?php bloginfo_rss("description") ?></description>
  125. <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></pubDate>
  126. <generator>http://wordpress.org/?v=<?php bloginfo_rss('version'); ?></generator>
  127. <language><?php echo get_option('rss_language'); ?></language>
  128. <?php if ( $cats ) : foreach ( $cats as $c ) : ?>
  129. <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>
  130. <?php endforeach; endif; ?>
  131. <?php do_action('rss2_head'); ?>
  132. <?php if ($posts) { foreach ($posts as $post) { start_wp(); ?>
  133. <item>
  134. <title><?php the_title_rss() ?></title>
  135. <link><?php permalink_single_rss() ?></link>
  136. <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
  137. <dc:creator><?php the_author() ?></dc:creator>
  138. <?php the_category_rss() ?>
  139. <guid isPermaLink="false"><?php the_guid(); ?></guid>
  140. <description></description>
  141. <content:encoded><![CDATA[<?php echo $post->post_content ?>]]></content:encoded>
  142. <wp:post_id><?php echo $post->ID; ?></wp:post_id>
  143. <wp:post_date><?php echo $post->post_date; ?></wp:post_date>
  144. <wp:post_date_gmt><?php echo $post->post_date_gmt; ?></wp:post_date_gmt>
  145. <wp:comment_status><?php echo $post->comment_status; ?></wp:comment_status>
  146. <wp:ping_status><?php echo $post->ping_status; ?></wp:ping_status>
  147. <wp:post_name><?php echo $post->post_name; ?></wp:post_name>
  148. <wp:status><?php echo $post->post_status; ?></wp:status>
  149. <wp:post_parent><?php echo $post->post_parent; ?></wp:post_parent>
  150. <wp:post_type><?php echo $post->post_type; ?></wp:post_type>
  151. <?php
  152. $postmeta = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE post_id = $post->ID");
  153. if ( $postmeta ) {
  154. ?>
  155. <?php foreach( $postmeta as $meta ) { ?>
  156. <wp:postmeta>
  157. <wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key>
  158. <wp:meta_value><?Php echo $meta->meta_value; ?></wp:meta_value>
  159. </wp:postmeta>
  160. <?php } ?>
  161. <?php } ?>
  162. <?php
  163. $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID");
  164. if ( $comments ) { foreach ( $comments as $c ) { ?>
  165. <wp:comment>
  166. <wp:comment_id><?php echo $c->comment_ID; ?></wp:comment_id>
  167. <wp:comment_author><?php echo $c->comment_author; ?></wp:comment_author>
  168. <wp:comment_author_email><?php echo $c->comment_author_email; ?></wp:comment_author_email>
  169. <wp:comment_author_url><?php echo $c->comment_author_url; ?></wp:comment_author_url>
  170. <wp:comment_author_IP><?php echo $c->comment_author_IP; ?></wp:comment_author_IP>
  171. <wp:comment_date><?php echo $c->comment_date; ?></wp:comment_date>
  172. <wp:comment_date_gmt><?php echo $c->comment_date_gmt; ?></wp:comment_date_gmt>
  173. <wp:comment_content><?php echo $c->comment_content; ?></wp:comment_content>
  174. <wp:comment_approved><?php echo $c->comment_approved; ?></wp:comment_approved>
  175. <wp:comment_type><?php echo $c->comment_type; ?></wp:comment_type>
  176. <wp:comment_parent><?php echo $c->comment_parent; ?></wp:comment_parent>
  177. </wp:comment>
  178. <?php } } ?>
  179. </item>
  180. <?php } } ?>
  181. </channel>
  182. </rss>
  183. <?php
  184. die();
  185. }
  186. include ('admin-footer.php');
  187. ?>