From 67231eb2b73196428dd98f36df894a30c7b0f9ce Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Wed, 21 Sep 2016 06:40:26 -0700 Subject: [PATCH 1/2] Fail gracefully when LCache database table doesn't exist Instead, show an admin notice to the end user that they need to create the tables. --- object-cache.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/object-cache.php b/object-cache.php index eb84431..160025a 100644 --- a/object-cache.php +++ b/object-cache.php @@ -980,6 +980,11 @@ public function wp_action_admin_notices_warn_missing_lcache() { return; } $missing_requirements = self::check_missing_lcache_requirements(); + // @codingStandardsIgnoreStart + if ( ! $GLOBALS['wpdb']->query( "SHOW TABLES LIKE '{$GLOBALS['table_prefix']}lcache'" ) ) { + $missing_requirements['database-error'] = 'LCache database table'; + } + // @codingStandardsIgnoreEnd $message = wp_sprintf( 'Warning! Missing %l, which %s required by WP LCache object cache. See "Installation" for more details.', $missing_requirements, count( $missing_requirements ) > 1 ? 'are' : 'is' ); echo '

' . wp_kses_post( $message ) . '

'; } @@ -1047,11 +1052,14 @@ public function __construct() { $options = array( PDO::ATTR_TIMEOUT => 2, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode="ANSI_QUOTES,STRICT_ALL_TABLES"' ); $dbh = new PDO( $dsn, DB_USER, DB_PASSWORD, $options ); $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); - $l2 = new DatabaseL2( $dbh, $GLOBALS['table_prefix'] ); + $l2 = new DatabaseL2( $dbh, $GLOBALS['table_prefix'], true ); $this->lcache = new Integrated( $l1, $l2 ); $this->lcache->synchronize(); - - if ( function_exists( 'add_action' ) && ! has_action( 'init', array( $this, 'wp_action_init_register_cron' ) ) ) { + // Assume LCache is failed if there are database errors + if ( $errors = $l2->getErrors() ) { + $missing_requirements[] = 'database-error'; + $this->lcache = null; + } else if ( function_exists( 'add_action' ) && ! has_action( 'init', array( $this, 'wp_action_init_register_cron' ) ) ) { add_action( 'init', array( $this, 'wp_action_init_register_cron' ) ); add_action( 'wp_lcache_collect_garbage', array( $this, 'wp_action_wp_lcache_collect_garbage' ) ); } From fd99215eec6fdb25b677d9278c9c354ec06850d4 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Wed, 21 Sep 2016 10:35:35 -0700 Subject: [PATCH 2/2] Capture the missing requirements to a class variable for reuse --- object-cache.php | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/object-cache.php b/object-cache.php index 160025a..991d0d9 100644 --- a/object-cache.php +++ b/object-cache.php @@ -344,6 +344,13 @@ class WP_Object_Cache { */ public $last_triggered_error = ''; + /** + * Any requirements that are missing. + * + * @var array + */ + public $missing_requirements = array(); + /** * Adds data to the cache if it doesn't already exist. * @@ -979,13 +986,7 @@ public function wp_action_admin_notices_warn_missing_lcache() { if ( ! current_user_can( 'manage_options' ) ) { return; } - $missing_requirements = self::check_missing_lcache_requirements(); - // @codingStandardsIgnoreStart - if ( ! $GLOBALS['wpdb']->query( "SHOW TABLES LIKE '{$GLOBALS['table_prefix']}lcache'" ) ) { - $missing_requirements['database-error'] = 'LCache database table'; - } - // @codingStandardsIgnoreEnd - $message = wp_sprintf( 'Warning! Missing %l, which %s required by WP LCache object cache. See "Installation" for more details.', $missing_requirements, count( $missing_requirements ) > 1 ? 'are' : 'is' ); + $message = wp_sprintf( 'Warning! Missing %l, which %s required by WP LCache object cache. See "Installation" for more details.', $this->missing_requirements, count( $this->missing_requirements ) > 1 ? 'are' : 'is' ); echo '

' . wp_kses_post( $message ) . '

'; } @@ -1034,8 +1035,8 @@ public function __construct() { $this->multisite = is_multisite(); $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; - $missing_requirements = self::check_missing_lcache_requirements(); - if ( empty( $missing_requirements ) ) { + $this->missing_requirements = self::check_missing_lcache_requirements(); + if ( empty( $this->missing_requirements ) ) { $l1 = new NullL1(); // APCu isn't available in CLI context unless explicitly enabled if ( php_sapi_name() !== 'cli' || 'on' === ini_get( 'apc.enable_cli' ) ) { @@ -1057,7 +1058,7 @@ public function __construct() { $this->lcache->synchronize(); // Assume LCache is failed if there are database errors if ( $errors = $l2->getErrors() ) { - $missing_requirements[] = 'database-error'; + $this->missing_requirements['database-error'] = 'LCache database table'; $this->lcache = null; } else if ( function_exists( 'add_action' ) && ! has_action( 'init', array( $this, 'wp_action_init_register_cron' ) ) ) { add_action( 'init', array( $this, 'wp_action_init_register_cron' ) ); @@ -1065,7 +1066,7 @@ public function __construct() { } } - if ( ! empty( $missing_requirements ) && function_exists( 'add_action' ) ) { + if ( ! empty( $this->missing_requirements ) && function_exists( 'add_action' ) ) { add_action( 'admin_notices', array( $this, 'wp_action_admin_notices_warn_missing_lcache' ) ); }