xref: /plugin/jstweak/action.php (revision bcbaaa3d431037a63cc8b27ee95948f28639066a)
1758a8295Sv-r-b<?php
2758a8295Sv-r-b
3758a8295Sv-r-b// must be run within Dokuwiki
4758a8295Sv-r-bif(!defined('DOKU_INC')) die();
5758a8295Sv-r-b
6758a8295Sv-r-buse dokuwiki\Extension\ActionPlugin;
7758a8295Sv-r-buse dokuwiki\Extension\EventHandler;
8758a8295Sv-r-buse dokuwiki\Extension\Event;
9758a8295Sv-r-buse dokuwiki\Logger;
10758a8295Sv-r-b
11758a8295Sv-r-b/**
12758a8295Sv-r-b * DokuWiki Plugin jstweak (Action Component)
13758a8295Sv-r-b *
14758a8295Sv-r-b * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
15758a8295Sv-r-b * @author Volker Bürckel <volker@vrbdev.eu>
16758a8295Sv-r-b */
17*bcbaaa3dSv-r-b
18758a8295Sv-r-bclass action_plugin_jstweak extends ActionPlugin
19758a8295Sv-r-b{
20758a8295Sv-r-b    const PLUGIN_PATH = DOKU_INC . 'lib/plugins/';
21758a8295Sv-r-b    const SCRIPT_NAME = 'script.js';
22758a8295Sv-r-b
23758a8295Sv-r-b    /** @inheritDoc */
24758a8295Sv-r-b    public function register(EventHandler $controller)
25758a8295Sv-r-b    {
26758a8295Sv-r-b        $controller->register_hook('JS_SCRIPT_LIST', 'AFTER', $this, 'handleJsScriptList');
27758a8295Sv-r-b    }
28758a8295Sv-r-b
29758a8295Sv-r-b    /**
30758a8295Sv-r-b     * Event handler for JS_SCRIPT_LIST
31758a8295Sv-r-b     *
32758a8295Sv-r-b     * For any plugin's script.js found in the list, it checks for existence
33758a8295Sv-r-b     * of a local override script.js in the subdirectory
34758a8295Sv-r-b     *   plugins.script.local.js.d/<pluginName>/
35758a8295Sv-r-b     * of the conf directory.
36758a8295Sv-r-b     *
37758a8295Sv-r-b     * @see https://www.dokuwiki.org/devel:events:JS_SCRIPT_LIST
38758a8295Sv-r-b     * @param Event $event Event object, $event->data holds the list
39758a8295Sv-r-b     * @param mixed $param optional parameter passed when event was registered
40758a8295Sv-r-b     * @return void
41758a8295Sv-r-b     */
42758a8295Sv-r-b    public function handleJsScriptList(Event $event, $param)
43758a8295Sv-r-b    {
44*bcbaaa3dSv-r-b        // exit function if tweaking is switched off
45*bcbaaa3dSv-r-b        if (!$this->getConf('active')) {
46*bcbaaa3dSv-r-b            Logger::debug("jstweak: tweaking is switched off in config");
47*bcbaaa3dSv-r-b            return;
48*bcbaaa3dSv-r-b        }
49*bcbaaa3dSv-r-b        $overridePath = DOKU_CONF . $this->getConf('local_script_dir');
50758a8295Sv-r-b        $pluginPathLen = strlen(self::PLUGIN_PATH);
51758a8295Sv-r-b        $scriptNameLen = strlen(self::SCRIPT_NAME);
52758a8295Sv-r-b
53*bcbaaa3dSv-r-b        if (!str_ends_with($overridePath, '/')) {
54*bcbaaa3dSv-r-b            $overridePath .= '/';
55*bcbaaa3dSv-r-b        }
56758a8295Sv-r-b        foreach ($event->data as $key => $entry) {
57758a8295Sv-r-b            // if we have a plugin's script.js file...
58758a8295Sv-r-b            if (str_starts_with($entry, self::PLUGIN_PATH)
59758a8295Sv-r-b                && str_ends_with($entry, self::SCRIPT_NAME)) {
60758a8295Sv-r-b                // ... extract the plugin's name, ...
61758a8295Sv-r-b                $pluginName = substr(
62758a8295Sv-r-b                    $entry,
63758a8295Sv-r-b                    $pluginPathLen,
64758a8295Sv-r-b                    strlen($entry) - $pluginPathLen - $scriptNameLen - 1
65758a8295Sv-r-b                );
66758a8295Sv-r-b                // ... check if there's a local replacement file ...
67*bcbaaa3dSv-r-b                $newEntry = $overridePath . $pluginName . '/' . self::SCRIPT_NAME;
68758a8295Sv-r-b                if (file_exists($newEntry)) {
69758a8295Sv-r-b                    // ... and if yes: change the script list.
70758a8295Sv-r-b                    $event->data[$key] = $newEntry;
71758a8295Sv-r-b                    Logger::debug("jstweak: new script.js for plugin $pluginName -> $newEntry");
72758a8295Sv-r-b                }
73758a8295Sv-r-b            }
74758a8295Sv-r-b        }
75758a8295Sv-r-b    }
76758a8295Sv-r-b}
77