Add cache inclalidation for Chronological

This commit is contained in:
2026-02-17 10:57:50 +01:00
parent 6ff7f2b45b
commit b2600485c4
8 changed files with 254 additions and 16 deletions

View File

@@ -3,11 +3,13 @@
use dokuwiki\Extension\ActionPlugin;
use dokuwiki\Extension\Event;
use dokuwiki\Extension\EventHandler;
use dokuwiki\plugin\luxtools\CacheInvalidation;
use dokuwiki\plugin\luxtools\ChronoID;
use dokuwiki\plugin\luxtools\ChronologicalCalendarWidget;
use dokuwiki\plugin\luxtools\ChronologicalDateAutoLinker;
use dokuwiki\plugin\luxtools\ChronologicalDayTemplate;
use dokuwiki\plugin\luxtools\ChronologicalIcsEvents;
use dokuwiki\plugin\luxtools\MenuItem\InvalidateCache;
require_once(__DIR__ . '/autoload.php');
/**
@@ -69,6 +71,18 @@ class action_plugin_luxtools extends ActionPlugin
$this,
"handleCalendarWidgetAjax",
);
$controller->register_hook(
"ACTION_ACT_PREPROCESS",
"BEFORE",
$this,
"handleInvalidateCacheAction",
);
$controller->register_hook(
"MENU_ITEMS_ASSEMBLY",
"AFTER",
$this,
"addInvalidateCacheMenuItem",
);
$controller->register_hook(
"TOOLBAR_DEFINE",
"AFTER",
@@ -137,6 +151,8 @@ class action_plugin_luxtools extends ActionPlugin
return;
}
$this->sendNoStoreHeaders();
$html = ChronologicalCalendarWidget::render($year, $month, $baseNs);
if ($html === '') {
http_status(500);
@@ -391,6 +407,9 @@ class action_plugin_luxtools extends ActionPlugin
if ($id === '') return;
if (!ChronoID::isDayId($id)) return;
$this->sendNoStoreHeaders();
if (function_exists('page_exists') && page_exists($id)) return;
$wikiText = ChronologicalDayTemplate::buildForDayId($id) ?? '';
@@ -578,4 +597,99 @@ class action_plugin_luxtools extends ActionPlugin
"block" => false,
];
}
/**
* Add admin-only page-tools item to invalidate luxtools caches.
*
* @param Event $event
* @param mixed $param
* @return void
*/
public function addInvalidateCacheMenuItem(Event $event, $param)
{
if (!is_array($event->data)) return;
if (($event->data['view'] ?? '') !== 'page') return;
if (!function_exists('auth_isadmin') || !auth_isadmin()) return;
if (!isset($event->data['items']) || !is_array($event->data['items'])) return;
$label = (string)$this->getLang('cache_invalidate_button');
if ($label === '') $label = 'Invalidate Cache';
$title = (string)$this->getLang('cache_invalidate_button_title');
if ($title === '') $title = 'Invalidate luxtools cache for this page';
$event->data['items'][] = new InvalidateCache($label, $title);
}
/**
* Handle manual cache invalidation action.
*
* @param Event $event
* @param mixed $param
* @return void
*/
public function handleInvalidateCacheAction(Event $event, $param)
{
if (!is_string($event->data) || $event->data !== 'show') return;
global $INPUT;
if (!$INPUT->bool('luxtools_invalidate_cache')) return;
global $ID;
$id = is_string($ID) ? $ID : '';
if (function_exists('cleanID')) {
$id = (string)cleanID($id);
}
if (!function_exists('auth_isadmin') || !auth_isadmin()) {
$message = (string)$this->getLang('cache_invalidate_denied');
if ($message === '') $message = 'Only admins can invalidate cache.';
msg($message, -1);
$this->redirectToShow($id);
return;
}
if (!checkSecurityToken()) {
$message = (string)$this->getLang('cache_invalidate_badtoken');
if ($message === '') $message = 'Security token mismatch. Please retry.';
msg($message, -1);
$this->redirectToShow($id);
return;
}
$removed = CacheInvalidation::purgeAll();
$message = (string)$this->getLang('cache_invalidate_success');
if ($message === '') {
$message = 'DokuWiki cache invalidated.';
}
msg($message . ' (' . $removed . ')', 1);
$this->redirectToShow($id);
}
/**
* Redirect to normal show view for the given page.
*
* @param string $id
* @return void
*/
protected function redirectToShow(string $id): void
{
$params = ['do' => 'show'];
send_redirect(wl($id, $params, true, '&'));
}
/**
* Send no-store cache headers for highly dynamic responses.
*
* @return void
*/
protected function sendNoStoreHeaders(): void
{
if (headers_sent()) return;
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Expires: 0');
}
}