147 lines
4.4 KiB
PHP
147 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace dokuwiki\plugin\luxtools;
|
|
|
|
/**
|
|
* Cache invalidation helpers for luxtools.
|
|
*
|
|
* Provides a full DokuWiki cache purge (all rendered pages, instructions,
|
|
* CSS/JS bundles) as well as luxtools-specific cache cleanup (thumbnails,
|
|
* pagelink mapping).
|
|
*/
|
|
class CacheInvalidation
|
|
{
|
|
/**
|
|
* Purge the DokuWiki cache directory, preserving the luxtools subfolder.
|
|
*
|
|
* Removes all files and subdirectories inside $conf['cachedir'] except
|
|
* the `luxtools/` subdirectory, which contains plugin-specific caches
|
|
* (thumbnails, pagelink mapping) that are expensive to rebuild and are
|
|
* managed separately via {@see purgePagelinks()} and {@see purgeThumbs()}.
|
|
*
|
|
* @return int Number of removed entries.
|
|
*/
|
|
public static function purgeAll(): int
|
|
{
|
|
global $conf;
|
|
|
|
$cacheDir = rtrim((string)($conf['cachedir'] ?? ''), '/');
|
|
if ($cacheDir === '' || !is_dir($cacheDir)) {
|
|
return 0;
|
|
}
|
|
|
|
return self::removeDirectoryEntries($cacheDir, ['luxtools']);
|
|
}
|
|
|
|
/**
|
|
* Purge the luxtools pagelink cache file.
|
|
*
|
|
* Deletes {@see PageLink::CACHE_FILE} from the luxtools cache sub-directory
|
|
* ($conf['cachedir']/luxtools/pagelink_cache.json). The file is rebuilt
|
|
* automatically on the next page request that needs it.
|
|
*
|
|
* @return int 1 if the file was removed, 0 otherwise.
|
|
*/
|
|
public static function purgePagelinks(): int
|
|
{
|
|
global $conf;
|
|
|
|
$cacheDir = rtrim((string)($conf['cachedir'] ?? ''), '/');
|
|
if ($cacheDir === '') {
|
|
return 0;
|
|
}
|
|
|
|
$file = $cacheDir . '/luxtools/pagelink_cache.json';
|
|
if (!file_exists($file)) {
|
|
return 0;
|
|
}
|
|
|
|
return @unlink($file) ? 1 : 0;
|
|
}
|
|
|
|
/**
|
|
* Purge the luxtools thumbnail cache directory.
|
|
*
|
|
* Removes all files and sub-directories inside
|
|
* $conf['cachedir']/luxtools/thumbs. Thumbnails are regenerated
|
|
* on demand by file.php.
|
|
*
|
|
* @return int Number of removed entries.
|
|
*/
|
|
public static function purgeThumbs(): int
|
|
{
|
|
global $conf;
|
|
|
|
$cacheDir = rtrim((string)($conf['cachedir'] ?? ''), '/');
|
|
if ($cacheDir === '') {
|
|
return 0;
|
|
}
|
|
|
|
$thumbsDir = $cacheDir . '/luxtools/thumbs';
|
|
if (!is_dir($thumbsDir)) {
|
|
return 0;
|
|
}
|
|
|
|
return self::removeDirectoryEntries($thumbsDir);
|
|
}
|
|
|
|
/**
|
|
* Execute a selective cache purge.
|
|
*
|
|
* Always purges the DokuWiki cache (skipping the luxtools subfolder).
|
|
* Optionally also purges the pagelinks cache and/or the thumbnail cache.
|
|
*
|
|
* @param bool $pagelinks Whether to also purge the pagelink cache.
|
|
* @param bool $thumbs Whether to also purge the thumbnail cache.
|
|
* @return array{dokuwiki: int, pagelinks: int|null, thumbs: int|null}
|
|
* Removed-entry counts per component; pagelinks/thumbs are null when not purged.
|
|
*/
|
|
public static function purgeSelected(bool $pagelinks, bool $thumbs): array
|
|
{
|
|
return [
|
|
'dokuwiki' => self::purgeAll(),
|
|
'pagelinks' => $pagelinks ? self::purgePagelinks() : null,
|
|
'thumbs' => $thumbs ? self::purgeThumbs() : null,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Remove all files and subdirectories inside a directory
|
|
* without removing the directory itself.
|
|
*
|
|
* @param string $directory
|
|
* @param string[] $skip Entry names (not paths) to leave untouched.
|
|
* @return int Number of removed files/directories.
|
|
*/
|
|
public static function removeDirectoryEntries(string $directory, array $skip = []): int
|
|
{
|
|
if ($directory === '' || !is_dir($directory) || !is_readable($directory)) {
|
|
return 0;
|
|
}
|
|
|
|
$entries = @scandir($directory);
|
|
if (!is_array($entries)) return 0;
|
|
|
|
$removed = 0;
|
|
foreach ($entries as $entry) {
|
|
if ($entry === '.' || $entry === '..') continue;
|
|
if ($skip !== [] && in_array($entry, $skip, true)) continue;
|
|
|
|
$path = $directory . '/' . $entry;
|
|
if (is_dir($path) && !is_link($path)) {
|
|
$removed += self::removeDirectoryEntries($path);
|
|
if (@rmdir($path)) {
|
|
$removed++;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if ((is_file($path) || is_link($path)) && @unlink($path)) {
|
|
$removed++;
|
|
}
|
|
}
|
|
|
|
return $removed;
|
|
}
|
|
}
|