Skip to content

Commit da5f168

Browse files
authored
Release 2.8.6 2 (#1062)
* Rework task runners * dbcache comment updates * dbcache/garbage-collection@blog-2 by priming the blog site
1 parent 6f31d24 commit da5f168

File tree

8 files changed

+166
-79
lines changed

8 files changed

+166
-79
lines changed

Cache_File.php

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,27 +96,33 @@ function add( $key, &$var, $expire = 0, $group = '' ) {
9696
/**
9797
* Sets data
9898
*
99-
* @param string $key
100-
* @param mixed $var
101-
* @param integer $expire
102-
* @param string $group Used to differentiate between groups of cache values
103-
* @return boolean
99+
* @param string $key An MD5 of the DB query.
100+
* @param mixed $content Data to be cached.
101+
* @param int $expiration Time to expire. If 0, then the data will never expire.
102+
* @param string $group Used to differentiate between groups of cache values.
103+
* @return bool
104104
*/
105-
function set( $key, $var, $expire = 0, $group = '' ) {
105+
function set( $key, $content, $expiration = 0, $group = '' ) {
106+
/**
107+
* Get the file pointer of the cache file.
108+
* The $key is transformed to a storage key (format "w3tc_INSTANCEID_HOST_BLOGID_dbcache_HASH").
109+
* The file path is in the format: CACHEDIR/db/BLOGID/GROUP/[0-9a-f]{3}/[0-9a-f]{3}/[0-9a-f]{32}.
110+
*/
106111
$fp = $this->fopen_write( $key, $group, 'wb' );
112+
107113
if ( !$fp )
108114
return false;
109115

110116
if ( $this->_locking )
111117
@flock( $fp, LOCK_EX );
112118

113-
if ( $expire <= 0 || $expire > W3TC_CACHE_FILE_EXPIRE_MAX )
114-
$expire = W3TC_CACHE_FILE_EXPIRE_MAX;
119+
if ( $expiration <= 0 || $expiration > W3TC_CACHE_FILE_EXPIRE_MAX )
120+
$expiration = W3TC_CACHE_FILE_EXPIRE_MAX;
115121

116-
$expires_at = time() + $expire;
122+
$expires_at = time() + $expiration;
117123
@fputs( $fp, pack( 'L', $expires_at ) );
118124
@fputs( $fp, '<?php exit; ?>' );
119-
@fputs( $fp, @serialize( $var ) );
125+
@fputs( $fp, @serialize( $content ) );
120126
@fclose( $fp );
121127

122128
if ( $this->_locking )
@@ -332,16 +338,17 @@ function mtime( $key, $group = '' ) {
332338
}
333339

334340
/**
335-
* Returns file path for key
341+
* Returns subpath for the cache file (format: [0-9a-f]{3}/[0-9a-f]{3}/[0-9a-f]{32}).
336342
*
337-
* @param string $key
343+
* @param string $key Storage key (format: "w3tc_INSTANCEID_HOST_BLOGID_dbcache_HASH").
344+
* @param string $group Used to differentiate between groups of cache values.
338345
* @return string
339346
*/
340347
function _get_path( $key, $group = '' ) {
341348
if ( $this->_use_wp_hash && function_exists( 'wp_hash' ) )
342-
$hash = wp_hash( $key );
349+
$hash = wp_hash( $key ); // Most common.
343350
else
344-
$hash = md5( $key );
351+
$hash = md5( $key ); // Less common, but still used in some cases.
345352

346353
return ( $group ? $group . DIRECTORY_SEPARATOR : '' ) . sprintf( '%s/%s/%s.php', substr( $hash, 0, 3 ), substr( $hash, 3, 3 ), $hash );
347354
}
@@ -463,21 +470,34 @@ public function counter_get( $key ) {
463470
return $count;
464471
}
465472

473+
/**
474+
* Open the cache file for writing and return the file pointer.
475+
*
476+
* @param string $key An MD5 of the DB query.
477+
* @param string $group Cache group.
478+
* @param string $mode File mode. For example: 'wb' for write binary.
479+
* @return resource|false File pointer on success, false on failure.
480+
*/
466481
private function fopen_write( $key, $group, $mode ) {
482+
// Get the storage key (format: "w3tc_INSTANCEID_HOST_BLOGID_dbcache_$key").
467483
$storage_key = $this->get_item_key( $key );
468484

485+
// Get the subpath for the cache file (format: [0-9a-f]{3}/[0-9a-f]{3}/[0-9a-f]{32}).
469486
$sub_path = $this->_get_path( $storage_key, $group );
487+
488+
// Ge the entire path of the cache file.
470489
$path = $this->_cache_dir . DIRECTORY_SEPARATOR . $sub_path;
471490

491+
// Create the directory if it does not exist.
472492
$dir = dirname( $path );
473493

474494
if ( !@is_dir( $dir ) ) {
475495
if ( !Util_File::mkdir_from( $dir, dirname( W3TC_CACHE_DIR ) ) )
476496
return false;
477497
}
478498

479-
$fp = @fopen( $path, $mode );
480-
return $fp;
499+
// Open the cache file for writing.
500+
return @fopen( $path, $mode );
481501
}
482502

483503
/**

Generic_Plugin.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -831,12 +831,17 @@ public function post_update_tasks(): void {
831831
$last_run_version = $state->get_string( 'tasks.generic.last_run_version' );
832832

833833
if ( empty( $last_run_version ) || \version_compare( W3TC_VERSION, $last_run_version, '>' ) ) {
834-
$ran_versions = get_option( 'w3tc_post_update_tasks_ran_versions', array() );
834+
$ran_versions = get_option( 'w3tc_post_update_generic_tasks_ran_versions', array() );
835+
$has_completed = false;
835836

836837
// Check if W3TC was updated to 2.8.6 or higher.
837838
if ( \version_compare( W3TC_VERSION, '2.8.6', '>=' ) && ! in_array( '2.8.6', $ran_versions, true ) ) {
838-
// Disable Object Cache if using Disk, purge the cache files, and show a notice in wp-admin.
839-
if ( $this->_config->get_boolean( 'objectcache.enabled' ) && 'file' === $this->_config->get_string( 'objectcache.engine' ) ) {
839+
// Disable Object Cache if using Disk, purge the cache files, and show a notice in wp-admin. Only for main/blog ID 1.
840+
if (
841+
1 === get_current_blog_id() &&
842+
$this->_config->get_boolean( 'objectcache.enabled' ) &&
843+
'file' === $this->_config->get_string( 'objectcache.engine' )
844+
) {
840845
$this->_config->set( 'objectcache.enabled', false );
841846
$this->_config->save();
842847

@@ -849,12 +854,17 @@ public function post_update_tasks(): void {
849854

850855
// Mark the task as ran.
851856
$ran_versions[] = '2.8.6';
852-
update_option( 'w3tc_post_update_tasks_ran_versions', $ran_versions, false );
857+
$has_completed = true;
853858

854859
// Delete cached notices.
855860
delete_option( 'w3tc_cached_notices' );
856861
}
857862

863+
// Mark completed tasks as ran.
864+
if ( $has_completed ) {
865+
update_option( 'w3tc_post_update_generic_tasks_ran_versions', $ran_versions, false );
866+
}
867+
858868
// Mark the task runner as ran for the current version.
859869
$state->set( 'tasks.generic.last_run_version', W3TC_VERSION );
860870
$state->save();

Generic_Plugin_Admin.php

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,25 +1326,48 @@ public function admin_notices() {
13261326
public function post_update_tasks(): void {
13271327
// Check if W3TC was updated.
13281328
$state = Dispatcher::config_state();
1329-
$last_run_version = $state->get_string( 'tasks.last_run_version' );
1330-
1331-
if ( empty( $last_run_version ) || version_compare( W3TC_VERSION, $last_run_version, '>' ) ) {
1332-
switch ( W3TC_VERSION ) {
1333-
case '2.8.1':
1334-
// Fix environment.
1335-
Util_Admin::fix_on_event( $this->_config, 'w3tc_plugin_updated' );
1336-
1337-
// Adjust "objectcache.file.gc".
1338-
if ( $this->_config->get_integer( 'objectcache.file.gc' ) === 3600 ) {
1339-
$this->_config->set( 'objectcache.file.gc', 600 );
1340-
$this->_config->save();
1341-
}
1342-
break;
1343-
default:
1344-
break;
1329+
$last_run_version = $state->get_string( 'tasks.admin.last_run_version' );
1330+
1331+
if ( empty( $last_run_version ) || \version_compare( W3TC_VERSION, $last_run_version, '>' ) ) {
1332+
$ran_versions = get_option( 'w3tc_post_update_admin_tasks_ran_versions', array() );
1333+
$has_completed = false;
1334+
1335+
// Check if W3TC was updated to 2.8.1 or higher and not already run.
1336+
if ( \version_compare( W3TC_VERSION, '2.8.1', '>=' ) && ! in_array( '2.8.1', $ran_versions, true ) ) {
1337+
// Fix environment.
1338+
Util_Admin::fix_on_event( $this->_config, 'w3tc_plugin_updated' );
1339+
1340+
// Adjust "objectcache.file.gc".
1341+
if ( $this->_config->get_integer( 'objectcache.file.gc' ) === 3600 ) {
1342+
$this->_config->set( 'objectcache.file.gc', 600 );
1343+
$this->_config->save();
1344+
}
1345+
1346+
// Mark the task as ran.
1347+
$ran_versions[] = '2.8.1';
1348+
$has_completed = true;
1349+
}
1350+
1351+
// Check if W3TC was updated to 2.8.6 or higher and not already run.
1352+
if ( \version_compare( W3TC_VERSION, '2.8.6', '>=' ) && ! in_array( '2.8.6', $ran_versions, true ) ) {
1353+
// Delete old option.
1354+
delete_option( 'w3tc_post_update_tasks_ran_versions' );
1355+
1356+
// Null old state key.
1357+
$state->set( 'tasks.last_run_version', null );
1358+
1359+
// Mark the task as ran.
1360+
$ran_versions[] = '2.8.6';
1361+
$has_completed = true;
1362+
}
1363+
1364+
// Mark completed tasks as ran.
1365+
if ( $has_completed ) {
1366+
update_option( 'w3tc_post_update_admin_tasks_ran_versions', $ran_versions, false );
13451367
}
13461368

1347-
$state->set( 'tasks.last_run_version', W3TC_VERSION );
1369+
// Mark the task runner as ran for the current version.
1370+
$state->set( 'tasks.admin.last_run_version', W3TC_VERSION );
13481371
$state->save();
13491372
}
13501373
}

qa/lib/sys.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const util = require('util');
2121
const exec = util.promisify(require('child_process').exec);
2222
const wp = requireRoot('lib/wp');
2323
const env = requireRoot('lib/environment');
24+
const w3tc = require('./w3tc');
2425

2526
/**
2627
* beforeDefault.
@@ -63,6 +64,9 @@ async function beforeDefault() {
6364
const r = await exec('/share/scripts/restart-http.rb');
6465
expect(r.stdout).contains('restartHttpSuccess');
6566

67+
// Mark generic tasks complete for "2.8.6".
68+
await w3tc.w3tcMarkGenericTasksVersionsComplete('2.8.6');
69+
6670
global.adminPage = await browser.newPage();
6771

6872
adminPage.setViewport({width: 1900, height: 1000});

0 commit comments

Comments
 (0)