Command `cli cache prune` doesn't clear cache when file timemods are all the same
When I run cli cache prune it does nothing and echos "Success: Cache pruned."
When I run cli cache clear it does remove cache files and echos "Success: Cache cleared."
Adding --debug did not add anything interesting to the output, as far as I could tell.
My environment is a CentOS from root within a perl script, calling with backticks:
sudo -u foo $php /usr/local/bin/wp cli cache prune --path=/home/foo/public_html
where
$php = '/usr/local/bin/php -c ./php-wpcli.ini';
The cache is, for example:
ls -1 /home/foo/.wp-cli/cache/plugin
akismet-4.1.6.zip autoptimize-2.7.3.zip classic-editor-1.6.zip disable-comments-1.11.0.zip jetpack-8.6.1.zip jetpack-8.7.1.zip jetpack-8.9.1.zip loginizer-1.4.4.zip loginizer-1.4.8.zip loginizer-1.6.0.zip master-slider-3.6.2.zip page-links-to-3.3.4.zip wordpress-seo-14.3.zip wordpress-seo-14.4.1.zip wordpress-seo-14.5.zip wordpress-seo-15.0.zip
The file timemods seem to be the date the script ran.
Proposed code fix:
public function prune() {
if ( ! $this->enabled ) {
return false;
}
/** @var Finder $finder */
$finder = $this->get_finder()->sortByName();
$files_maxversion = [];
$files_maxversionpath = [];
foreach ( $finder as $file ) {
$pieces = explode( '-', $file->getBasename( $file->getExtension() ) );
$version = end( $pieces );
$basename_without_version = str_replace( '-' . $version, '', $file->getBasename() );
// There's a file with an older version, delete it.
if ( isset( $files_maxversion[ $basename_without_version ] ) ) {
$vcomp=version_compare($basename_without_version,$files_maxversion[ $basename_without_version ]);
if ($vcomp == -1) {
//this version is older, so delete this one
unlink($file->getRealPath());
} else {
//the other version is older, delete it and save this version
unlink( $files_maxversionpath[ $basename_without_version ] );
$files_maxversionpath[ $basename_without_version ] = $file->getRealPath();
$files_maxversion[ $basename_without_version ] = $version;
}
} else {
$files_maxversionpath[ $basename_without_version ] = $file->getRealPath();
$files_maxversion[ $basename_without_version ] = $version;
}
}
return true;
}
@bret-miller Can you send this change as a Pull Request (PR)?