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