PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/src/php/wp-cli/commands/internals/export.php

https://github.com/MatthewEppelsheimer/wp-cli
PHP | 459 lines | 380 code | 56 blank | 23 comment | 71 complexity | a27fa0c6f1d5e45a739f8fc07bcbda0d MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. WP_CLI::add_command('export', 'Export_Command');
  3. /**
  4. * Implement export command
  5. *
  6. * @package wp-cli
  7. * @subpackage commands/internals
  8. */
  9. class Export_Command extends WP_CLI_Command {
  10. /**
  11. * Argument validation functions below
  12. */
  13. public function validate_arguments( $args, $assoc_args ) {
  14. $defaults = array(
  15. 'path' => NULL,
  16. 'start_date' => NULL,
  17. 'end_date' => NULL,
  18. 'post_type' => NULL,
  19. 'author' => NULL,
  20. 'category' => NULL,
  21. 'post_status' => NULL,
  22. 'skip_comments' => NULL,
  23. );
  24. $args = wp_parse_args( $assoc_args, $defaults );
  25. $has_errors = false;
  26. foreach( $defaults as $argument => $default_value ) {
  27. if ( is_callable( array( &$this, 'check_' . $argument ) ) ) {
  28. $result = call_user_func( array( &$this, 'check_' . $argument ), $args[$argument] );
  29. if ( false === $result && false === $has_errors )
  30. $has_errors = true;
  31. }
  32. }
  33. if ( $has_errors ) {
  34. exit(1);
  35. }
  36. $this->wxr_path = $assoc_args['path'];
  37. WP_CLI::line( 'Starting export process...' );
  38. WP_CLI::line();
  39. $this->export_wp( $this->export_args );
  40. }
  41. private function check_path( $path ) {
  42. if ( empty( $path ) ) {
  43. WP_CLI::warning( 'missing --path parameter' );
  44. return false;
  45. }
  46. if ( !is_dir( $path ) ) {
  47. WP_CLI::error( sprintf( "The path %s does not exist", $path ) );
  48. }
  49. return true;
  50. }
  51. private function check_start_date( $date ) {
  52. if ( is_null( $date ) )
  53. return true;
  54. $time = strtotime( $date );
  55. if ( !empty( $date ) && !$time ) {
  56. WP_CLI::warning( sprintf( "The start_date %s is invalid", $date ) );
  57. return false;
  58. }
  59. $this->export_args['start_date'] = date( 'Y-m-d', $time );
  60. return true;
  61. }
  62. private function check_end_date( $date ) {
  63. if ( is_null( $date ) )
  64. return true;
  65. $time = strtotime( $date );
  66. if ( !empty( $date ) && !$time ) {
  67. WP_CLI::warning( sprintf( "The end_date %s is invalid", $date ) );
  68. return false;
  69. }
  70. $this->export_args['start_date'] = date( 'Y-m-d', $time );
  71. return true;
  72. }
  73. private function check_post_type( $post_type ) {
  74. if ( is_null( $post_type ) )
  75. return true;
  76. $post_types = get_post_types();
  77. if ( !in_array( $post_type, $post_types ) ) {
  78. WP_CLI::warning( sprintf( 'The post type %s does not exists. Choose "all" or any of these existing post types instead: %s', $post_type, implode( ", ", $post_types ) ) );
  79. return false;
  80. }
  81. $this->export_args['content'] = $post_type;
  82. return true;
  83. }
  84. private function check_author( $author ) {
  85. if ( is_null( $author ) )
  86. return true;
  87. $authors = get_users_of_blog();
  88. if ( empty( $authors ) || is_wp_error( $authors ) ) {
  89. WP_CLI::warning( sprintf( "Could not find any authors in this blog" ) );
  90. return false;
  91. }
  92. $hit = false;
  93. foreach( $authors as $user ) {
  94. if ( $hit )
  95. break;
  96. if ( (int) $author == $user->ID || $author == $user->user_login )
  97. $hit = $user->ID;
  98. }
  99. if ( false === $hit ) {
  100. $authors_nice = array();
  101. foreach( $authors as $_author )
  102. $authors_nice[] = sprintf( '%s (%s)', $_author->user_login, $_author->display_name );
  103. WP_CLI::warning( sprintf( 'Could not find a matching author for %s. The following authors exist: %s', $author, implode( ", ", $authors_nice ) ) );
  104. return false;
  105. }
  106. $this->export_args['author'] = $hit;
  107. return true;
  108. }
  109. private function check_category( $category ) {
  110. if ( is_null( $category ) )
  111. return true;
  112. $term = category_exists( $category );
  113. if ( empty( $term ) || is_wp_error( $term ) ) {
  114. WP_CLI::warning( sprintf( 'Could not find a category matching %s', $category ) );
  115. return false;
  116. }
  117. $this->export_args['category'] = $category;
  118. return true;
  119. }
  120. private function check_post_status( $status ) {
  121. if ( is_null( $status ) )
  122. return true;
  123. $stati = get_post_statuses();
  124. if ( empty( $stati ) || is_wp_error( $stati ) ) {
  125. WP_CLI::warning( sprintf( 'Could not find any post stati', $category ) );
  126. return false;
  127. }
  128. if ( !isset( $stati[$status] ) ) {
  129. WP_CLI::warning( sprintf( 'Could not find a post_status matching %s. Here is a list of available stati: %s', $status, implode( ", ", array_keys( $stati ) ) ) );
  130. return false;
  131. }
  132. $this->export_args['status'] = $status;
  133. return true;
  134. }
  135. private function check_skip_comments( $skip ) {
  136. if ( is_null( $skip ) )
  137. return true;
  138. if ( (int) $skip <> 0 && (int) $skip <> 1 ) {
  139. WP_CLI::warning( sprintf( 'skip_comments needs to be 0 (no) or 1 (yes)', $category ) );
  140. return false;
  141. }
  142. $this->export_args['skip_comments'] = $skip;
  143. return true;
  144. }
  145. /**
  146. * Workaround to prevent memory leaks from growing variables
  147. */
  148. private function stop_the_insanity() {
  149. global $wpdb, $wp_object_cache;
  150. $wpdb->queries = array(); // or define( 'WP_IMPORTING', true );
  151. if ( !is_object( $wp_object_cache ) )
  152. return;
  153. $wp_object_cache->group_ops = array();
  154. $wp_object_cache->stats = array();
  155. $wp_object_cache->memcache_debug = array();
  156. $wp_object_cache->cache = array();
  157. $wp_object_cache->__remoteset(); // important
  158. }
  159. /**
  160. * Export function as it is defined in the original code of export_wp defined in wp-admin/includes/export.php
  161. */
  162. private function export_wp( $args = array() ) {
  163. require_once ABSPATH . 'wp-admin/includes/export.php';
  164. global $wpdb, $post;
  165. // call export_wp as we need the functions defined in it.
  166. $dummy_args = array( 'content' => 'i-do-not-exist' );
  167. ob_start();
  168. export_wp( $dummy_args );
  169. ob_end_clean();
  170. /**
  171. * This is mostly the original code of export_wp defined in wp-admin/includes/export.php
  172. */
  173. $defaults = array( 'content' => 'all', 'author' => false, 'category' => false,
  174. 'start_date' => false, 'end_date' => false, 'status' => false, 'skip_comments' => false,
  175. );
  176. $args = wp_parse_args( $args, $defaults );
  177. WP_CLI::line( "Exporting with export_wp with arguments: " . var_export( $args, true ) );
  178. do_action( 'export_wp' );
  179. $sitename = sanitize_key( get_bloginfo( 'name' ) );
  180. if ( ! empty( $sitename ) )
  181. $sitename .= '.';
  182. $append = array( date( 'Y-m-d' ) );
  183. foreach( array_keys( $args ) as $arg_key ) {
  184. if ( $defaults[$arg_key] <> $args[$arg_key] )
  185. $append[]= "$arg_key-" . (string) $args[$arg_key];
  186. }
  187. $file_name_base = $sitename . 'wordpress.' . implode( ".", $append );
  188. if ( 'all' != $args['content'] && post_type_exists( $args['content'] ) ) {
  189. $ptype = get_post_type_object( $args['content'] );
  190. if ( ! $ptype->can_export )
  191. $args['content'] = 'post';
  192. $where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] );
  193. } else {
  194. $post_types = get_post_types( array( 'can_export' => true ) );
  195. $esses = array_fill( 0, count( $post_types ), '%s' );
  196. $where = $wpdb->prepare( "{$wpdb->posts}.post_type IN (" . implode( ',', $esses ) . ')', $post_types );
  197. }
  198. if ( $args['status'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) )
  199. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_status = %s", $args['status'] );
  200. else
  201. $where .= " AND {$wpdb->posts}.post_status != 'auto-draft'";
  202. $join = '';
  203. if ( $args['category'] && 'post' == $args['content'] ) {
  204. if ( $term = term_exists( $args['category'], 'category' ) ) {
  205. $join = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
  206. $where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_taxonomy_id'] );
  207. }
  208. }
  209. if ( 'post' == $args['content'] || 'page' == $args['content'] ) {
  210. if ( $args['author'] )
  211. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );
  212. if ( $args['start_date'] )
  213. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", date( 'Y-m-d', strtotime( $args['start_date'] ) ) );
  214. if ( $args['end_date'] )
  215. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", date( 'Y-m-d', strtotime( '+1 month', strtotime( $args['end_date'] ) ) ) );
  216. }
  217. // grab a snapshot of post IDs, just in case it changes during the export
  218. $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" );
  219. // get the requested terms ready, empty unless posts filtered by category or all content
  220. $cats = $tags = $terms = array();
  221. if ( isset( $term ) && $term ) {
  222. $cat = get_term( $term['term_id'], 'category' );
  223. $cats = array( $cat->term_id => $cat );
  224. unset( $term, $cat );
  225. } else if ( 'all' == $args['content'] ) {
  226. $categories = (array) get_categories( array( 'get' => 'all' ) );
  227. $tags = (array) get_tags( array( 'get' => 'all' ) );
  228. $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
  229. $custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
  230. // put categories in order with no child going before its parent
  231. while ( $cat = array_shift( $categories ) ) {
  232. if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) )
  233. $cats[$cat->term_id] = $cat;
  234. else
  235. $categories[] = $cat;
  236. }
  237. // put terms in order with no child going before its parent
  238. while ( $t = array_shift( $custom_terms ) ) {
  239. if ( $t->parent == 0 || isset( $terms[$t->parent] ) )
  240. $terms[$t->term_id] = $t;
  241. else
  242. $custom_terms[] = $t;
  243. }
  244. unset( $categories, $custom_taxonomies, $custom_terms );
  245. }
  246. WP_CLI::line( 'Exporting ' . count( $post_ids ) . ' items' );
  247. WP_CLI::line( 'Exporting ' . count( $cats ) . ' cateogries' );
  248. WP_CLI::line( 'Exporting ' . count( $tags ) . ' tags' );
  249. WP_CLI::line( 'Exporting ' . count( $terms ) . ' terms' );
  250. WP_CLI::line();
  251. $progress = new \cli\progress\Bar( 'Exporting', count( $post_ids ) );
  252. ob_start();
  253. echo '<?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . "\" ?>\n";
  254. ?>
  255. <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your site. -->
  256. <!-- It contains information about your site's posts, pages, comments, categories, and other content. -->
  257. <!-- You may use this file to transfer that content from one site to another. -->
  258. <!-- This file is not intended to serve as a complete backup of your site. -->
  259. <!-- To import this information into a WordPress site follow these steps: -->
  260. <!-- 1. Log in to that site as an administrator. -->
  261. <!-- 2. Go to Tools: Import in the WordPress admin panel. -->
  262. <!-- 3. Install the "WordPress" importer from the list. -->
  263. <!-- 4. Activate & Run Importer. -->
  264. <!-- 5. Upload this file using the form provided on that page. -->
  265. <!-- 6. You will first be asked to map the authors in this export file to users -->
  266. <!-- on the site. For each author, you may choose to map to an -->
  267. <!-- existing user on the site or to create a new user. -->
  268. <!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. -->
  269. <!-- contained in this file into your site. -->
  270. <?php the_generator( 'export' ); ?>
  271. <rss version="2.0"
  272. xmlns:excerpt="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/excerpt/"
  273. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  274. xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  275. xmlns:dc="http://purl.org/dc/elements/1.1/"
  276. xmlns:wp="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/"
  277. >
  278. <channel>
  279. <title><?php bloginfo_rss( 'name' ); ?></title>
  280. <link><?php bloginfo_rss( 'url' ); ?></link>
  281. <description><?php bloginfo_rss( 'description' ); ?></description>
  282. <pubDate><?php echo date( 'D, d M Y H:i:s +0000' ); ?></pubDate>
  283. <language><?php echo get_option( 'rss_language' ); ?></language>
  284. <wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
  285. <wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
  286. <wp:base_blog_url><?php bloginfo_rss( 'url' ); ?></wp:base_blog_url>
  287. <?php wxr_authors_list(); ?>
  288. <?php foreach ( $cats as $c ) : ?>
  289. <wp:category><wp:term_id><?php echo $c->term_id ?></wp:term_id><wp:category_nicename><?php echo $c->slug; ?></wp:category_nicename><wp:category_parent><?php echo $c->parent ? $cats[$c->parent]->slug : ''; ?></wp:category_parent><?php wxr_cat_name( $c ); ?><?php wxr_category_description( $c ); ?></wp:category>
  290. <?php endforeach; ?>
  291. <?php foreach ( $tags as $t ) : ?>
  292. <wp:tag><wp:term_id><?php echo $t->term_id ?></wp:term_id><wp:tag_slug><?php echo $t->slug; ?></wp:tag_slug><?php wxr_tag_name( $t ); ?><?php wxr_tag_description( $t ); ?></wp:tag>
  293. <?php endforeach; ?>
  294. <?php foreach ( $terms as $t ) : ?>
  295. <wp:term><wp:term_id><?php echo $t->term_id ?></wp:term_id><wp:term_taxonomy><?php echo $t->taxonomy; ?></wp:term_taxonomy><wp:term_slug><?php echo $t->slug; ?></wp:term_slug><wp:term_parent><?php echo $t->parent ? $terms[$t->parent]->slug : ''; ?></wp:term_parent><?php wxr_term_name( $t ); ?><?php wxr_term_description( $t ); ?></wp:term>
  296. <?php endforeach; ?>
  297. <?php if ( 'all' == $args['content'] ) wxr_nav_menu_terms(); ?>
  298. <?php do_action( 'rss2_head' ); ?>
  299. <?php if ( $post_ids ) {
  300. global $wp_query;
  301. $wp_query->in_the_loop = true; // Fake being in the loop.
  302. // fetch 20 posts at a time rather than loading the entire table into memory
  303. while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
  304. $where = 'WHERE ID IN (' . join( ',', $next_posts ) . ')';
  305. $posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" );
  306. // Begin Loop
  307. foreach ( $posts as $post ) {
  308. $progress->tick();
  309. setup_postdata( $post );
  310. $is_sticky = is_sticky( $post->ID ) ? 1 : 0;
  311. ?>
  312. <item>
  313. <title><?php echo apply_filters( 'the_title_rss', $post->post_title ); ?></title>
  314. <link><?php the_permalink_rss() ?></link>
  315. <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>
  316. <dc:creator><?php echo get_the_author_meta( 'login' ); ?></dc:creator>
  317. <guid isPermaLink="false"><?php esc_url( the_guid() ); ?></guid>
  318. <description></description>
  319. <content:encoded><?php echo wxr_cdata( apply_filters( 'the_content_export', $post->post_content ) ); ?></content:encoded>
  320. <excerpt:encoded><?php echo wxr_cdata( apply_filters( 'the_excerpt_export', $post->post_excerpt ) ); ?></excerpt:encoded>
  321. <wp:post_id><?php echo $post->ID; ?></wp:post_id>
  322. <wp:post_date><?php echo $post->post_date; ?></wp:post_date>
  323. <wp:post_date_gmt><?php echo $post->post_date_gmt; ?></wp:post_date_gmt>
  324. <wp:comment_status><?php echo $post->comment_status; ?></wp:comment_status>
  325. <wp:ping_status><?php echo $post->ping_status; ?></wp:ping_status>
  326. <wp:post_name><?php echo $post->post_name; ?></wp:post_name>
  327. <wp:status><?php echo $post->post_status; ?></wp:status>
  328. <wp:post_parent><?php echo $post->post_parent; ?></wp:post_parent>
  329. <wp:menu_order><?php echo $post->menu_order; ?></wp:menu_order>
  330. <wp:post_type><?php echo $post->post_type; ?></wp:post_type>
  331. <wp:post_password><?php echo $post->post_password; ?></wp:post_password>
  332. <wp:is_sticky><?php echo $is_sticky; ?></wp:is_sticky>
  333. <?php if ( $post->post_type == 'attachment' ) : ?>
  334. <wp:attachment_url><?php echo wp_get_attachment_url( $post->ID ); ?></wp:attachment_url>
  335. <?php endif; ?>
  336. <?php wxr_post_taxonomy(); ?>
  337. <?php $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
  338. foreach ( $postmeta as $meta ) : if ( $meta->meta_key != '_edit_lock' ) : ?>
  339. <wp:postmeta>
  340. <wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key>
  341. <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
  342. </wp:postmeta>
  343. <?php endif; endforeach; ?>
  344. <?php if ( false === $args['skip_comments'] ): ?>
  345. <?php $comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) );
  346. foreach ( $comments as $c ) : ?>
  347. <wp:comment>
  348. <wp:comment_id><?php echo $c->comment_ID; ?></wp:comment_id>
  349. <wp:comment_author><?php echo wxr_cdata( $c->comment_author ); ?></wp:comment_author>
  350. <wp:comment_author_email><?php echo $c->comment_author_email; ?></wp:comment_author_email>
  351. <wp:comment_author_url><?php echo esc_url_raw( $c->comment_author_url ); ?></wp:comment_author_url>
  352. <wp:comment_author_IP><?php echo $c->comment_author_IP; ?></wp:comment_author_IP>
  353. <wp:comment_date><?php echo $c->comment_date; ?></wp:comment_date>
  354. <wp:comment_date_gmt><?php echo $c->comment_date_gmt; ?></wp:comment_date_gmt>
  355. <wp:comment_content><?php echo wxr_cdata( $c->comment_content ) ?></wp:comment_content>
  356. <wp:comment_approved><?php echo $c->comment_approved; ?></wp:comment_approved>
  357. <wp:comment_type><?php echo $c->comment_type; ?></wp:comment_type>
  358. <wp:comment_parent><?php echo $c->comment_parent; ?></wp:comment_parent>
  359. <wp:comment_user_id><?php echo $c->user_id; ?></wp:comment_user_id>
  360. <?php $c_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $c->comment_ID ) );
  361. foreach ( $c_meta as $meta ) : ?>
  362. <wp:commentmeta>
  363. <wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key>
  364. <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
  365. </wp:commentmeta>
  366. <?php endforeach; ?>
  367. </wp:comment>
  368. <?php endforeach; ?>
  369. <?php endif; ?>
  370. </item>
  371. <?php
  372. }
  373. }
  374. } ?>
  375. </channel>
  376. </rss>
  377. <?php
  378. $progress->finish();
  379. $result = ob_get_clean();
  380. $full_path = $this->wxr_path . $file_name_base . '.wxr';
  381. if ( !file_exists( $full_path ) || is_writeable( $full_path ) ) {
  382. WP_CLI::line( 'Writing to ' . $full_path );
  383. file_put_contents( $full_path, $result );
  384. }
  385. }
  386. }