home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-admin / includes / export.php < prev    next >
Encoding:
PHP Script  |  2008-05-21  |  9.8 KB  |  257 lines

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