register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handle_header'); // To get only the script that we need $controller->register_hook('JS_SCRIPT_LIST', 'AFTER', $this, 'handle_js'); } /** * Add a query string to the js.php link when it's a SHOW action * @param Doku_Event $event */ public function handle_header(Doku_Event &$event, $param) { // If there is no user if (empty($_SERVER['REMOTE_USER'])) { $scripts = &$event->data['script']; foreach ($scripts as &$script) { $pos = strpos($script['src'], 'js.php'); if ($pos !== false) { $script['src'] = $script['src'] . '&'.self::ACCESS.'='.self::PUBLIC.''; } } } } /** * @param Doku_Event $event * @param $param */ public function handle_js(Doku_Event &$event, $param) { // It was added by the TPL_METAHEADER_OUTPUT handler (ie function handle_header) $access = $_GET[self::ACCESS]; if ($access == self::PUBLIC) { // The directory path for the internal dokuwiki script $dokuScriptPath = DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'scripts' . DIRECTORY_SEPARATOR; // The directory path for the plugin script (we need to keep them) $dokuPluginPath = DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR; // Script that we want on the show page $showPageScripts = [ 0 => $dokuScriptPath . "qsearch.js", // for the search bar 1 => $dokuScriptPath . "script.js", 2 => $dokuScriptPath . "hotkeys.js", 3 => $dokuScriptPath . "locktimer.js", // Use in js.php - dw_locktimer 4 => $dokuScriptPath . "helpers.js", // substr_replace use in qsearch.php 5 => 'conf' . DIRECTORY_SEPARATOR . 'userscript.js' ]; $scriptsToKeep = array(); foreach ($event->data as $scriptPath) { $posPluginPath = strpos($scriptPath, $dokuPluginPath); if ($posPluginPath !== false) { // This is a plugin script, we keep it $scriptsToKeep[] = $scriptPath; } else { foreach ($showPageScripts as $showPageScript) { $posShowPage = strpos($scriptPath, $showPageScript); if ($posShowPage !== false) { // This is a script needed on the show page $scriptsToKeep[] = $scriptPath; } } } } $event->data = $scriptsToKeep; } } }