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