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