1<?php 2/** 3 * DokuWiki Plugin Js Action 4 * 5 */ 6 7if (!defined('DOKU_INC')) die(); 8 9/** 10 * Suppress the Javascript of Dokuwiki not needed on a page show 11 */ 12class action_plugin_webcomponent_js extends DokuWiki_Action_Plugin 13{ 14 15 // Wcact Web Component Access 16 const ACCESS = 'wcacc'; 17 // For the public (no user, no admin) 18 const PUBLIC = 'public'; 19 20 /** 21 * Registers our handler for the MANIFEST_SEND event 22 * https://www.dokuwiki.org/devel:event:js_script_list 23 * manipulate the list of JavaScripts that will be concatenated 24 * @param Doku_Event_Handler $controller 25 */ 26 public function register(Doku_Event_Handler $controller) 27 { 28 29 // To add the act query string to tweak the cache at the end. ie http://localhost:81/lib/exe/js.php?t=bootie&tseed=3cf44e7&act=show 30 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handle_header'); 31 32 // To get only the script that we need 33 $controller->register_hook('JS_SCRIPT_LIST', 'AFTER', $this, 'handle_js'); 34 35 } 36 37 /** 38 * Add a query string to the js.php link when it's a SHOW action 39 * @param Doku_Event $event 40 */ 41 public function handle_header(Doku_Event &$event, $param) 42 { 43 44 // If there is no user 45 if (empty($_SERVER['REMOTE_USER'])) { 46 $scripts = &$event->data['script']; 47 foreach ($scripts as &$script) { 48 $pos = strpos($script['src'], 'js.php'); 49 if ($pos !== false) { 50 $script['src'] = $script['src'] . '&'.self::ACCESS.'='.self::PUBLIC.''; 51 } 52 } 53 } 54 55 56 } 57 58 /** 59 * @param Doku_Event $event 60 * @param $param 61 */ 62 public function handle_js(Doku_Event &$event, $param) 63 { 64 65 // It was added by the TPL_METAHEADER_OUTPUT handler (ie function handle_header) 66 $access = $_GET[self::ACCESS]; 67 if ($access == self::PUBLIC) { 68 69 // The directory path for the internal dokuwiki script 70 $dokuScriptPath = DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'scripts' . DIRECTORY_SEPARATOR; 71 // The directory path for the plugin script (we need to keep them) 72 $dokuPluginPath = DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR; 73 74 // Script that we want on the show page 75 $showPageScripts = 76 [ 77 0 => $dokuScriptPath . "qsearch.js", // for the search bar 78 1 => $dokuScriptPath . "script.js", 79 2 => $dokuScriptPath . "hotkeys.js", 80 3 => $dokuScriptPath . "locktimer.js", // Use in js.php - dw_locktimer 81 4 => $dokuScriptPath . "helpers.js", // substr_replace use in qsearch.php 82 5 => 'conf' . DIRECTORY_SEPARATOR . 'userscript.js' 83 ]; 84 85 $scriptsToKeep = array(); 86 87 foreach ($event->data as $scriptPath) { 88 $posPluginPath = strpos($scriptPath, $dokuPluginPath); 89 if ($posPluginPath !== false) { 90 // This is a plugin script, we keep it 91 $scriptsToKeep[] = $scriptPath; 92 } else { 93 94 foreach ($showPageScripts as $showPageScript) { 95 96 $posShowPage = strpos($scriptPath, $showPageScript); 97 if ($posShowPage !== false) { 98 // This is a script needed on the show page 99 $scriptsToKeep[] = $scriptPath; 100 } 101 } 102 } 103 } 104 105 $event->data = $scriptsToKeep; 106 107 } 108 } 109 110} 111 112