root/tags/1.3/wp-admin/export.php

Revision 1139, 9.9 kB (checked in by donncha, 1 year ago)

Merge with WordPress? 2.3.1

Line 
1 <?php
2 require_once ('admin.php');
3 $title = __('Export');
4 $parent_file = 'edit.php';
5
6 if ( isset( $_GET['download'] ) )
7     export_wp();
8
9 require_once ('admin-header.php');
10 ?>
11
12 <div class="wrap">
13 <h2><?php _e('Export'); ?></h2>
14 <div class="narrow">
15 <p><?php _e('When you click the button below WordPress will create an XML file for you to save to your computer.'); ?></p>
16 <p><?php _e('This format, which we call WordPress eXtended RSS or WXR, will contain your posts, comments, custom fields, and categories.'); ?></p>
17 <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>
18 <form action="" method="get">
19 <h3><?php _e('Optional options'); ?></h3>
20
21 <table>
22 <tr>
23 <th><?php _e('Restrict Author:'); ?></th>
24 <td>
25 <select name="author">
26 <option value="all" selected="selected"><?php _e('All'); ?></option>
27 <?php
28 $authors = $wpdb->get_col( "SELECT post_author FROM $wpdb->posts GROUP BY post_author" );
29 foreach ( $authors as $id ) {
30     $o = get_userdata( $id );
31     echo "<option value='$o->ID'>$o->display_name</option>";
32 }
33 ?>
34 </select>
35 </td>
36 </tr>
37 </table>
38 <p class="submit"><input type="submit" name="submit" value="<?php _e('Download Export File'); ?> &raquo;" />
39 <input type="hidden" name="download" value="true" />
40 </p>
41 </form>
42 <p><?php _e('If you are considering moving your blog to another host we recommend a number of <a href="http://wordpress.org/hosting/">hosting services</a>.'); ?></p>
43 </div>
44 </div>
45
46 <?php
47
48 function export_wp() {
49 global $wpdb, $post_ids, $post;
50
51 do_action('export_wp');
52
53 $filename = 'wordpress.' . date('Y-m-d') . '.xml';
54
55 header('Content-Description: File Transfer');
56 header("Content-Disposition: attachment; filename=$filename");
57 header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);
58
59 $where = '';
60 if ( isset( $_GET['author'] ) && $_GET['author'] != 'all' ) {
61     $author_id = (int) $_GET['author'];
62     $where = " WHERE post_author = '$author_id' ";
63 }
64
65 // grab a snapshot of post IDs, just in case it changes during the export
66 $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts $where ORDER BY post_date_gmt ASC");
67
68 $categories = (array) get_categories('get=all');
69 $tags = (array) get_tags('get=all');
70
71 function wxr_missing_parents($categories) {
72     if ( !is_array($categories) || empty($categories) )
73         return array();
74
75     foreach ( $categories as $category )
76         $parents[$category->term_id] = $category->parent;
77
78     $parents = array_unique(array_diff($parents, array_keys($parents)));
79
80     if ( $zero = array_search('0', $parents) )
81         unset($parents[$zero]);
82
83     return $parents;
84 }
85
86 while ( $parents = wxr_missing_parents($categories) ) {
87     $found_parents = get_categories("include=" . join(', ', $parents));
88     if ( is_array($found_parents) && count($found_parents) )
89         $categories = array_merge($categories, $found_parents);
90     else
91         break;
92 }
93
94 // Put them in order to be inserted with no child going before its parent
95 $pass = 0;
96 $passes = 1000 + count($categories);
97 while ( ( $cat = array_shift($categories) ) && ++$pass < $passes ) {
98     if ( $cat->parent == 0 || isset($cats[$cat->parent]) ) {
99         $cats[$cat->term_id] = $cat;
100     } else {
101         $categories[] = $cat;
102     }
103 }
104 unset($categories);
105
106 function wxr_cdata($str) {
107     if ( seems_utf8($str) == false )
108         $str = utf8_encode($str);
109
110     // $str = ent2ncr(wp_specialchars($str));
111
112     $str = "<![CDATA[$str" . ( ( substr($str, -1) == ']' ) ? ' ' : '') . "]]>";
113
114     return $str;
115 }
116
117 function wxr_cat_name($c) {
118     if ( empty($c->name) )
119         return;
120
121     echo '<wp:cat_name>' . wxr_cdata($c->name) . '</wp:cat_name>';
122 }
123
124 function wxr_category_description($c) {
125     if ( empty($c->description) )
126         return;
127
128     echo '<wp:category_description>' . wxr_cdata($c->description) . '</wp:category_description>';
129 }
130
131 function wxr_tag_name($t) {
132     if ( empty($t->name) )
133         return;
134
135     echo '<wp:tag_name>' . wxr_cdata($t->name) . '</wp:tag_name>';
136 }
137
138 function wxr_tag_description($t) {
139     if ( empty($t->description) )
140         return;
141
142     echo '<wp:tag_description>' . wxr_cdata($t->description) . '</wp:tag_description>';
143 }
144
145 function wxr_post_taxonomy() {
146     $categories = get_the_category();
147     $tags = get_the_tags();
148     $cat_names = array();
149     $tag_names = array();
150     $the_list = '';
151     $filter = 'rss';
152
153     if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
154         $cat_name = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
155         $the_list .= "\n\t\t<category><![CDATA[$cat_name]]></category>\n";
156     }
157
158     if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
159         $tag_name = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
160         $the_list .= "\n\t\t<category domain=\"tag\"><![CDATA[$tag_name]]></category>\n";
161     }
162
163     echo $the_list;
164 }
165
166 echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . '"?' . ">\n";
167
168 ?>
169 <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. -->
170 <!-- It contains information about your blog's posts, comments, and categories. -->
171 <!-- You may use this file to transfer that content from one site to another. -->
172 <!-- This file is not intended to serve as a complete backup of your blog. -->
173
174 <!-- To import this information into a WordPress blog follow these steps. -->
175 <!-- 1. Log into that blog as an administrator. -->
176 <!-- 2. Go to Manage: Import in the blog's admin panels. -->
177 <!-- 3. Choose "WordPress" from the list. -->
178 <!-- 4. Upload this file using the form provided on that page. -->
179 <!-- 5. You will first be asked to map the authors in this export file to users -->
180 <!--    on the blog.  For each author, you may choose to map to an -->
181 <!--    existing user on the blog or to create a new user -->
182 <!-- 6. WordPress will then import each of the posts, comments, and categories -->
183 <!--    contained in this file into your blog -->
184
185 <!-- generator="wordpress/<?php bloginfo_rss('version') ?>" created="<?php echo date('Y-m-d H:i'); ?>"-->
186 <rss version="2.0"
187     xmlns:content="http://purl.org/rss/1.0/modules/content/"
188     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
189     xmlns:dc="http://purl.org/dc/elements/1.1/"
190     xmlns:wp="http://wordpress.org/export/1.0/"
191 >
192
193 <channel>
194     <title><?php bloginfo_rss('name'); ?></title>
195     <link><?php bloginfo_rss('url') ?></link>
196     <description><?php bloginfo_rss("description") ?></description>
197     <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></pubDate>
198     <generator>http://wordpress.org/?v=<?php bloginfo_rss('version'); ?></generator>
199     <language><?php echo get_option('rss_language'); ?></language>
200 <?php if ( $cats ) : foreach ( $cats as $c ) : ?>
201     <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>
202 <?php endforeach; endif; ?>
203 <?php if ( $tags ) : foreach ( $tags as $t ) : ?>
204     <wp:tag><wp:tag_slug><?php echo $t->slug; ?></wp:tag_slug><?php wxr_tag_name($t); ?><?php wxr_tag_description($t); ?></wp:tag>
205 <?php endforeach; endif; ?>
206     <?php do_action('rss2_head'); ?>
207     <?php if ($post_ids) {
208         global $wp_query;
209         $wp_query->in_the_loop = true// Fake being in the loop.
210         // fetch 20 posts at a time rather than loading the entire table into memory
211         while ( $next_posts = array_splice($post_ids, 0, 20) ) {
212             $where = "WHERE ID IN (".join(',', $next_posts).")";
213             $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts $where ORDER BY post_date_gmt ASC");
214                 foreach ($posts as $post) {
215             setup_postdata($post); ?>
216 <item>
217 <title><?php the_title_rss() ?></title>
218 <link><?php the_permalink_rss() ?></link>
219 <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
220 <dc:creator><?php the_author() ?></dc:creator>
221 <?php wxr_post_taxonomy() ?>
222
223 <guid isPermaLink="false"><?php the_guid(); ?></guid>
224 <description></description>
225 <content:encoded><![CDATA[<?php echo $post->post_content ?>]]></content:encoded>
226 <wp:post_id><?php echo $post->ID; ?></wp:post_id>
227 <wp:post_date><?php echo $post->post_date; ?></wp:post_date>
228 <wp:post_date_gmt><?php echo $post->post_date_gmt; ?></wp:post_date_gmt>
229 <wp:comment_status><?php echo $post->comment_status; ?></wp:comment_status>
230 <wp:ping_status><?php echo $post->ping_status; ?></wp:ping_status>
231 <wp:post_name><?php echo $post->post_name; ?></wp:post_name>
232 <wp:status><?php echo $post->post_status; ?></wp:status>
233 <wp:post_parent><?php echo $post->post_parent; ?></wp:post_parent>
234 <wp:menu_order><?php echo $post->menu_order; ?></wp:menu_order>
235 <wp:post_type><?php echo $post->post_type; ?></wp:post_type>
236 <?php
237 $postmeta = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE post_id = $post->ID");
238 if ( $postmeta ) {
239 ?>
240 <?php foreach( $postmeta as $meta ) { ?>
241 <wp:postmeta>
242 <wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key>
243 <wp:meta_value><?Php echo $meta->meta_value; ?></wp:meta_value>
244 </wp:postmeta>
245 <?php } ?>
246 <?php } ?>
247 <?php
248 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID");
249 if ( $comments ) { foreach ( $comments as $c ) { ?>
250 <wp:comment>
251 <wp:comment_id><?php echo $c->comment_ID; ?></wp:comment_id>
252 <wp:comment_author><?php echo wxr_cdata($c->comment_author); ?></wp:comment_author>
253 <wp:comment_author_email><?php echo $c->comment_author_email; ?></wp:comment_author_email>
254 <wp:comment_author_url><?php echo $c->comment_author_url; ?></wp:comment_author_url>
255 <wp:comment_author_IP><?php echo $c->comment_author_IP; ?></wp:comment_author_IP>
256 <wp:comment_date><?php echo $c->comment_date; ?></wp:comment_date>
257 <wp:comment_date_gmt><?php echo $c->comment_date_gmt; ?></wp:comment_date_gmt>
258 <wp:comment_content><?php echo $c->comment_content; ?></wp:comment_content>
259 <wp:comment_approved><?php echo $c->comment_approved; ?></wp:comment_approved>
260 <wp:comment_type><?php echo $c->comment_type; ?></wp:comment_type>
261 <wp:comment_parent><?php echo $c->comment_parent; ?></wp:comment_parent>
262 </wp:comment>
263 <?php } } ?>
264     </item>
265 <?php } } } ?>
266 </channel>
267 </rss>
268 <?php
269     die();
270 }
271
272 include ('admin-footer.php');
273 ?>
274
Note: See TracBrowser for help on using the browser.