Skip to content
This repository has been archived by the owner on Feb 23, 2019. It is now read-only.

WP-CLI - Prime the Page Cache #324

Merged
merged 1 commit into from
Jan 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 101 additions & 21 deletions Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ function fix_environment( $args = array(), $vars = array() ) {
$environment = Dispatcher::component( 'Root_Environment' );
$environment->fix_in_wpadmin( $config, true );
} catch ( Util_Environment_Exceptions $e ) {
\WP_CLI::error( __( 'Environment adjustment failed with error', 'w3-total-cache' ),
$e->getCombinedMessage() );
\WP_CLI::error( __( 'Environment adjustment failed with error:' . $e->getCombinedMessage(), 'w3-total-cache' ) );
}

\WP_CLI::success( __( 'Environment adjusted.', 'w3-total-cache' ) );
Expand Down Expand Up @@ -217,7 +216,6 @@ function option( $args = array(), $vars = array() ) {

if ( empty( $name ) ) {
\WP_CLI::error( __( '<name> parameter is not specified', 'w3-total-cache' ) );
return;
}
if ( strpos( $name, '::' ) !== FALSE ) {
$name = explode('::', $name);
Expand Down Expand Up @@ -249,7 +247,6 @@ function option( $args = array(), $vars = array() ) {
$v = json_encode( $c->get_array( $name ), JSON_PRETTY_PRINT );
else {
\WP_CLI::error( __( 'Unknown type ' . $type, 'w3-total-cache' ) );
return;
}

echo $v . "\n";
Expand All @@ -258,7 +255,6 @@ function option( $args = array(), $vars = array() ) {

if ( count( $args ) <= 0 ) {
\WP_CLI::error( __( '<value> parameter is not specified', 'w3-total-cache' ) );
return;
}
$value = array_shift( $args );

Expand All @@ -269,7 +265,6 @@ function option( $args = array(), $vars = array() ) {
$v = false;
else {
\WP_CLI::error( __( '<value> parameter ' . $value . ' is not boolean', 'w3-total-cache' ) );
return;
}
} elseif ( $type == 'integer' )
$v = (integer)$value;
Expand All @@ -280,7 +275,6 @@ function option( $args = array(), $vars = array() ) {
$v = explode($delimiter, $value );
} else {
\WP_CLI::error( __( 'Unknown type ' . $type, 'w3-total-cache' ) );
return;
}

try {
Expand All @@ -305,9 +299,7 @@ function querystring() {
$w3_querystring->browsercache_flush();
}
catch ( \Exception $e ) {
\WP_CLI::error( sprintf(
__( 'updating the query string failed. with error %s', 'w3-total-cache' ),
$e ) );
\WP_CLI::error( __( 'updating the query string failed. with error: ' . $e->getMessage(), 'w3-total-cache' ) );
}

\WP_CLI::success( __( 'The query string was updated successfully.', 'w3-total-cache' ) );
Expand All @@ -331,7 +323,7 @@ function cdn_purge( $args = array() ) {
$w3_cdn_purge->cdn_purge_files( $purgeitems );
}
catch ( \Exception $e ) {
\WP_CLI::error( __( 'Files did not successfully purge with error %s', 'w3-total-cache' ), $e );
\WP_CLI::error( __( 'Files did not successfully purge with error: ' . $e->getMessage(), 'w3-total-cache' ) );
}
\WP_CLI::success( __( 'Files purged successfully.', 'w3-total-cache' ) );

Expand Down Expand Up @@ -367,14 +359,14 @@ function opcache_flush_file( $args = array() ) {
);
$result = wp_remote_post( $url, $post );
if ( is_wp_error( $result ) ) {
\WP_CLI::error( __( 'Files did not successfully reload with error %s', 'w3-total-cache' ), $result );
\WP_CLI::error( __( 'Files did not successfully reload with error: ' . $result, 'w3-total-cache' ) );
} elseif ( $result['response']['code'] != '200' ) {
\WP_CLI::error( __( 'Files did not successfully reload with message: ', 'w3-total-cache' ) . $result['body'] );
}
}
}
catch ( \Exception $e ) {
\WP_CLI::error( __( 'Files did not successfully reload with error %s', 'w3-total-cache' ), $e );
\WP_CLI::error( __( 'Files did not successfully reload with error: ' . $e->getMessage(), 'w3-total-cache' ) );
}
\WP_CLI::success( __( 'Files reloaded successfully.', 'w3-total-cache' ) );

Expand Down Expand Up @@ -410,14 +402,14 @@ function opcache_flush( $args = array() ) {
);
$result = wp_remote_post( $url, $post );
if ( is_wp_error( $result ) ) {
\WP_CLI::error( __( 'Files did not successfully delete with error %s', 'w3-total-cache' ), $result );
\WP_CLI::error( __( 'Files did not successfully delete with error ' . $result, 'w3-total-cache' ) );
} elseif ( $result['response']['code'] != '200' ) {
\WP_CLI::error( __( 'Files did not successfully delete with message: ', 'w3-total-cache' ). $result['body'] );
}
}
}
catch ( \Exception $e ) {
\WP_CLI::error( __( 'Files did not successfully delete with error %s', 'w3-total-cache' ), $e );
\WP_CLI::error( __( 'Files did not successfully delete with error: ' . $e->getMessage(), 'w3-total-cache' ) );
}
\WP_CLI::success( __( 'Files deleted successfully.', 'w3-total-cache' ) );

Expand All @@ -431,16 +423,104 @@ function pgcache_cleanup() {
$pgcache_cleanup = Dispatcher::component( 'PgCache_Plugin_Admin' );
$pgcache_cleanup->cleanup();
} catch ( \Exception $e ) {
\WP_CLI::error( __( 'PageCache Garbage cleanup did not start with error %s',
'w3-total-cache' ), $e );
\WP_CLI::error( __( 'PageCache Garbage cleanup did not start with error: ' . $e->getMessage(), 'w3-total-cache' ) );
}

\WP_CLI::success( __( 'PageCache Garbage cleanup triggered successfully.',
'w3-total-cache' ) );
\WP_CLI::success( __( 'PageCache Garbage cleanup triggered successfully.', 'w3-total-cache' ) );
}
}


/**
* Prime the page cache (cache preloader)
*
* ## OPTIONS
*
* [<stop>]
* : Stop the active page cache prime session.
*
* [--batch=<size>]
* : Max number of pages to create per batch. If not set, the value given in
* W3TC's Page Cache > Pages per Interval field is used. If size is 0 then
* all pages within the sitemap will be created/cached without the use of a
* batch and without waiting.
*
* [--interval=<seconds>]
* : Number of seconds to wait before creating another batch. If not set,
* the value given in W3TC's Page Cache > Update Interval field is used.
*
* [--sitemap=<url>]
* : The sitemap url specifying the pages to prime cache. If not set, the
* value given in W3TC's Page Cache > Sitemap URL field is used.
*
* ## EXAMPLES
*
* # Prime the page cache using settings held within the W3TC config.
* $ wp w3-total-cache prime
*
* # Stop the currently active prime process.
* $ wp w3-total-cache prime stop
*
* # Prime the page cache (2 pages every 30 seconds).
* $ wp w3-total-cache prime --batch=2 --interval=30
*
* # Prime the page cache every 30 seconds using the given sitemap.
* $ wp w3-total-cache prime --interval=30 --sitemap=http://site.com/sitemap.xml
*/
function prime( $args = array() , $vars = array() ) {
try {
$action = array_shift( $args ) ;
$w3_prime = Dispatcher::component( 'PgCache_Plugin_Admin' );

if ( $action == 'stop' ) {
if ( w3tc_wpcli_stop_prime( $result ) == false ) {
\WP_CLI::warning( __( $result, 'w3-total-cache' ) );
} else {
\WP_CLI::success( __( 'Page cache priming stopped.', 'w3-total-cache' ) );
}
} elseif ( strlen( $action ) > 0 ) {
$val = \WP_CLI::colorize( __( "%Y$action%n", 'w3-total-cache' ) );
\WP_CLI::error( __( "Unrecognized argument - $val.", 'w3-total-cache' ) );
} else {
$config = Dispatcher::config();
$user_limit = - 1;
$user_interval = - 1;
$user_sitemap = "";

if ( isset( $vars['interval'] ) && is_numeric( $vars['interval'] ) ) {
$user_interval = intval( $vars['interval'] );
}

if ( isset( $vars['batch'] ) && is_numeric( $vars['batch'] ) ) {
$user_limit = intval( $vars['batch'] );
}

if ( isset( $vars['sitemap'] ) && !empty( $vars['sitemap'] ) ) {
$user_sitemap = trim( $vars['sitemap'] );
}

$limit = $user_limit == - 1 ? $config->get_integer( 'pgcache.prime.limit' ) : $user_limit;
$interval = $user_interval == - 1 ? $config->get_integer( 'pgcache.prime.interval' ) : $user_interval;
$sitemap = empty( $user_sitemap ) ? $config->get_string( 'pgcache.prime.sitemap' ) : $user_sitemap;

if ( empty( $sitemap ) ) {
\WP_CLI::error( __( "Prime page cache halted - Unable to load sitemap. A sitemap is needed to prime the page cache.", 'w3-total-cache' ) );
} elseif ( ( $res = $w3_prime->prime_cli( $limit, $interval, $sitemap, 0, true ) ) === false ) {
\WP_CLI::warning( __( 'Page cache priming is already active.', 'w3-total-cache' ) );
} else {
/**
* Use inter-process messaging, if available, to help manage the prime
*/
if ( extension_loaded( 'sysvmsg' ) ) {
msg_send( msg_get_queue( 99909 ) , 99, "prime_started" );
}

\WP_CLI::success( __( "Page cache priming started $res.", 'w3-total-cache' ) );
}
}
} catch( \Exception $e ) {
\WP_CLI::error( __( 'Error: ' . $e->getMessage(), 'w3-total-cache' ) );
}
}
}

if ( method_exists( '\WP_CLI', 'add_command' ) ) {
\WP_CLI::add_command( 'w3-total-cache', '\W3TC\W3TotalCache_Command' );
Expand Down
16 changes: 16 additions & 0 deletions PgCache_Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ function run() {
}

add_action( 'w3_pgcache_prime', array( $this, 'prime' ) );

add_action( 'w3_pgcache_prime_cli', array( $this, 'prime_cli' ), 10, 4 );

Util_AttachToActions::flush_posts_on_actions();

Expand Down Expand Up @@ -115,6 +117,20 @@ function prime( $start = 0 ) {
$this->_get_admin()->prime( $start );
}

/**
* Prime cache (WP_CLI)
*
* @param integer $user_limit Pages per batch size
* @param integer $user_interval Number of seconds to wait before creating another batch
* @param string $user_sitemap The sitemap url to use
* @param integer $start Index position within the sitemap to prime cache
* @param boolean $boot Indicates if the prime cache is about to start for the first time
* @return void
*/
function prime_cli( $user_limit, $user_interval, $user_sitemap, $start, $boot = false ) {
$this->_get_admin()->prime_cli( $user_limit, $user_interval, $user_sitemap, $start, $boot );
}

/**
* Instantiates worker on demand
*/
Expand Down
Loading