Ticket #5347: export-refactor-r6324-a.patch

File export-refactor-r6324-a.patch, 17.9 KB (added by tellyworth, 5 years ago)

pass author as a function parameter

  • wordpress/wp-admin/export.php

     
    11<?php 
    22require_once ('admin.php'); 
     3require_once('includes/export.php'); 
    34$title = __('Export'); 
    45$parent_file = 'edit.php'; 
    56 
    6 if ( isset( $_GET['download'] ) ) 
    7         export_wp(); 
     7if ( isset( $_GET['download'] ) ) {      
     8        export_wp( $_GET['author'] ); 
     9        die(); 
     10} 
    811 
    912require_once ('admin-header.php'); 
    1013?> 
     
    4447 
    4548<?php 
    4649 
    47 function export_wp() { 
    48 global $wpdb, $post_ids, $post; 
    4950 
    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 <?php the_generator('export');?> 
    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  
    27151include ('admin-footer.php'); 
    27252?> 
  • wordpress/wp-admin/includes/export.php

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