1<?php 2 3// must be run within Dokuwiki 4if(!defined('DOKU_INC')) die(); 5 6use dokuwiki\Extension\ActionPlugin; 7use dokuwiki\Extension\EventHandler; 8use dokuwiki\Extension\Event; 9use dokuwiki\Logger; 10 11/** 12 * DokuWiki Plugin jstweak (Action Component) 13 * 14 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 15 * @author Volker Bürckel <volker@vrbdev.eu> 16 */ 17 18class action_plugin_jstweak extends ActionPlugin 19{ 20 const PLUGIN_PATH = DOKU_INC . 'lib/plugins/'; 21 const SCRIPT_NAME = 'script.js'; 22 23 /** @inheritDoc */ 24 public function register(EventHandler $controller) 25 { 26 $controller->register_hook('JS_SCRIPT_LIST', 'AFTER', $this, 'handleJsScriptList'); 27 } 28 29 /** 30 * Event handler for JS_SCRIPT_LIST 31 * 32 * For any plugin's script.js found in the list, it checks for existence 33 * of a local override script.js in the subdirectory 34 * plugins.script.local.js.d/<pluginName>/ 35 * of the conf directory. 36 * 37 * @see https://www.dokuwiki.org/devel:events:JS_SCRIPT_LIST 38 * @param Event $event Event object, $event->data holds the list 39 * @param mixed $param optional parameter passed when event was registered 40 * @return void 41 */ 42 public function handleJsScriptList(Event $event, $param) 43 { 44 // exit function if tweaking is switched off 45 if (!$this->getConf('active')) { 46 Logger::debug("jstweak: tweaking is switched off in config"); 47 return; 48 } 49 $overridePath = DOKU_CONF . $this->getConf('local_script_dir'); 50 $pluginPathLen = strlen(self::PLUGIN_PATH); 51 $scriptNameLen = strlen(self::SCRIPT_NAME); 52 53 if (!str_ends_with($overridePath, '/')) { 54 $overridePath .= '/'; 55 } 56 foreach ($event->data as $key => $entry) { 57 // if we have a plugin's script.js file... 58 if (str_starts_with($entry, self::PLUGIN_PATH) 59 && str_ends_with($entry, self::SCRIPT_NAME)) { 60 // ... extract the plugin's name, ... 61 $pluginName = substr( 62 $entry, 63 $pluginPathLen, 64 strlen($entry) - $pluginPathLen - $scriptNameLen - 1 65 ); 66 // ... check if there's a local replacement file ... 67 $newEntry = $overridePath . $pluginName . '/' . self::SCRIPT_NAME; 68 if (file_exists($newEntry)) { 69 // ... and if yes: change the script list. 70 $event->data[$key] = $newEntry; 71 Logger::debug("jstweak: new script.js for plugin $pluginName -> $newEntry"); 72 } 73 } 74 } 75 } 76} 77