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