Verified Commit 749be479 authored by Erick Hitter's avatar Erick Hitter
Browse files

Improve command to make batch size variable and automatically have it loop over all results

parent 09d155b6
......@@ -7,7 +7,7 @@ class One_Time_Fixers extends \WPCOM_VIP_CLI_Command {
/**
* Remove corrupt Cron Control data resulting from initial plugin deployment
*
* eg.: `wp --allow-root cron-control remove-all-plugin-data`
* eg.: `wp --allow-root cron-control remove-all-plugin-data --batch-size=15 --dry-run=true`
*
* @subcommand remove-all-plugin-data
*/
......@@ -28,7 +28,7 @@ class One_Time_Fixers extends \WPCOM_VIP_CLI_Command {
if ( is_numeric( $count ) ) {
$count = (int) $count;
\WP_CLI::line( sprintf( __( 'Found %s entries', 'automattic-cron-control' ), number_format_i18n( $count ) ) . "\n\n" );
\WP_CLI::line( sprintf( __( 'Found %s total items', 'automattic-cron-control' ), number_format_i18n( $count ) ) . "\n\n" );
} else {
\WP_CLI::line( __( 'Something went wrong...aborting!', 'automattic-cron-control' ) );
return;
......@@ -41,27 +41,65 @@ class One_Time_Fixers extends \WPCOM_VIP_CLI_Command {
\WP_CLI::line( "\n" . __( 'Starting...', 'automattic-cron-control' ) . "\n" );
}
// Work through all items until there are no more
$items = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = %s LIMIT 250;", 'a8c_cron_ctrl_event' ) );
// Determine how many batches this will take
if ( isset( $assoc_args['batch-size'] ) ) {
$page_size = max( 1, min( absint( $assoc_args['batch-size'] ), 500 ) );
} else {
$page_size = 250;
}
\WP_CLI::line( sprintf( __( 'Processing in batches of %s', 'automattic-cron-control' ), number_format_i18n( $page_size ) ) . "\n\n" );
$pages = 1;
$page = 1;
if ( $count > $page_size ) {
$pages = ceil( $count / $page_size );
}
// Let's get on with it
do {
\WP_CLI::line( "\n\n" . sprintf( __( 'Processing page %s of %s', 'automattic-cron-control' ), number_format_i18n( $page ), number_format_i18n( $pages ) ) . "\n" );
if ( is_array( $items ) && ! empty( $items ) ) {
foreach ( $items as $item ) {
\WP_CLI::line( "{$item->ID}, `{$item->post_title}`" );
$items = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = %s LIMIT %d", 'a8c_cron_ctrl_event', $page_size ) );
// Nothing more to do
if ( ! is_array( $items ) || empty( $items ) ) {
\WP_CLI::line( __( 'No more items found!', 'automattic-cron-control' ) );
break;
}
if ( ! $dry_run ) {
\WP_CLI::line( __( 'Removing...', 'automattic-cron-control' ) );
\WP_CLI::line( sprintf( __( 'Found %s items in this batch' ), number_format_i18n( count( $items ) ) ) );
if ( ! $dry_run ) {
foreach ( $items as $item ) {
\WP_CLI::line( "{$item->ID}, `{$item->post_title}`" );
wp_delete_post( $item->ID, true );
\WP_CLI::line( '' );
}
}
}
// Some cleanup
unset( $items );
$this->stop_the_insanity();
// Prepare for the next batch
$page++;
if ( $page > $pages ) {
break;
}
// Don't rush into the next batch
sleep( 5 );
} while( true );
// Remove the now-stale cache when actively run
if ( ! $dry_run ) {
wp_cache_delete( 'a8c_cron_ctrl_option' );
\WP_CLI::line( sprintf( __( 'Cleared the %s cache', 'automattic-cron-control' ), 'Cron Control' ) );
\WP_CLI::line( "\n" . sprintf( __( 'Cleared the %s cache', 'automattic-cron-control' ), 'Cron Control' ) );
}
\WP_CLI::line( "\n" . __( 'All done. Note that new entries may have been created as the result of other requests made to the site during this process.', 'automattic-cron-control' ) );
// Fin
\WP_CLI::line( "\n" . __( 'All done.', 'automattic-cron-control' ) );
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment