xref: /plugin/combo/action/js.php (revision ad54dffd2a785f08006601bf247e440e47fc7b18)
1007225e5Sgerardnico<?php
2007225e5Sgerardnico
3*ad54dffdSgerardnicorequire_once(__DIR__ . '/../vendor/autoload.php');
4*ad54dffdSgerardnico
504fd306cSNickeauuse ComboStrap\Identity;
6c3437056SNickeau
7007225e5Sgerardnico
8007225e5Sgerardnico/**
9007225e5Sgerardnico * Suppress the Javascript of Dokuwiki not needed on a page show
10007225e5Sgerardnico */
11007225e5Sgerardnicoclass action_plugin_combo_js extends DokuWiki_Action_Plugin
12007225e5Sgerardnico{
13007225e5Sgerardnico
14007225e5Sgerardnico    // Because the javascript is a second request call
15007225e5Sgerardnico    // we don't known if we are in the admin or public interface
16007225e5Sgerardnico    // we add them to the js href the below query key
17007225e5Sgerardnico    const ACCESS_PROPERTY_KEY = 'wcacc';
18007225e5Sgerardnico    // For the public (no user, no admin)
19007225e5Sgerardnico    const ACCESS_PROPERTY_VALUE_PUBLIC = 'public';
20007225e5Sgerardnico
21007225e5Sgerardnico    /**
22007225e5Sgerardnico     * Registers our handler for the MANIFEST_SEND event
23007225e5Sgerardnico     * https://www.dokuwiki.org/devel:event:js_script_list
24007225e5Sgerardnico     * manipulate the list of JavaScripts that will be concatenated
25007225e5Sgerardnico     * @param Doku_Event_Handler $controller
26007225e5Sgerardnico     */
27007225e5Sgerardnico    public function register(Doku_Event_Handler $controller)
28007225e5Sgerardnico    {
29007225e5Sgerardnico
30007225e5Sgerardnico        // 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
31007225e5Sgerardnico        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handle_header');
32007225e5Sgerardnico
33007225e5Sgerardnico        // To get only the script that we need
34007225e5Sgerardnico        $controller->register_hook('JS_SCRIPT_LIST', 'AFTER', $this, 'handle_js');
35007225e5Sgerardnico
36007225e5Sgerardnico    }
37007225e5Sgerardnico
38007225e5Sgerardnico    /**
39007225e5Sgerardnico     * Add a query string to the js.php link when it's a SHOW action
40007225e5Sgerardnico     * @param Doku_Event $event
41007225e5Sgerardnico     */
42007225e5Sgerardnico    public function handle_header(Doku_Event &$event, $param)
43007225e5Sgerardnico    {
44007225e5Sgerardnico
45007225e5Sgerardnico        // If there is no user
4604fd306cSNickeau        if (Identity::isAnonymous()) {
47007225e5Sgerardnico            $scripts = &$event->data['script'];
48007225e5Sgerardnico            foreach ($scripts as &$script) {
49007225e5Sgerardnico                $pos = strpos($script['src'], 'js.php');
50007225e5Sgerardnico                if ($pos !== false) {
51007225e5Sgerardnico                    $script['src'] = $script['src'] . '&' . self::ACCESS_PROPERTY_KEY . '=' . self::ACCESS_PROPERTY_VALUE_PUBLIC . '';
52007225e5Sgerardnico                }
53007225e5Sgerardnico            }
54007225e5Sgerardnico        }
55007225e5Sgerardnico
56007225e5Sgerardnico
57007225e5Sgerardnico    }
58007225e5Sgerardnico
59007225e5Sgerardnico    /**
60007225e5Sgerardnico     * This is to handle the HTTP call
61007225e5Sgerardnico     * /lib/exe/js.php?t=strap&tseed=2eb19bd2d8991a9bb11366d787d4225e&wcacc=public
62007225e5Sgerardnico     * @param Doku_Event $event
63007225e5Sgerardnico     * @param            $param
64007225e5Sgerardnico     */
65007225e5Sgerardnico    public function handle_js(Doku_Event &$event, $param)
66007225e5Sgerardnico    {
67007225e5Sgerardnico
68531e725cSNickeau        /**
69531e725cSNickeau         * The directory separator is hard written
70531e725cSNickeau         * in the list {@link js_out()}
71531e725cSNickeau         * We then use it here also as hardcoded
72531e725cSNickeau         */
73531e725cSNickeau        $directorySeparatorInDokuwikiList = "/";
74531e725cSNickeau
75007225e5Sgerardnico        // It was added by the TPL_METAHEADER_OUTPUT handler (ie function handle_header)
76007225e5Sgerardnico        $access = $_GET[self::ACCESS_PROPERTY_KEY];
77007225e5Sgerardnico        if ($access == self::ACCESS_PROPERTY_VALUE_PUBLIC) {
78007225e5Sgerardnico
79007225e5Sgerardnico            // The directory path for the internal dokuwiki script
80531e725cSNickeau            $dokuScriptPath = $directorySeparatorInDokuwikiList . 'lib' . $directorySeparatorInDokuwikiList . 'scripts' . $directorySeparatorInDokuwikiList;
81007225e5Sgerardnico            // The directory path for the plugin script (we need to keep them)
82531e725cSNickeau            $dokuPluginPath = $directorySeparatorInDokuwikiList . 'lib' . $directorySeparatorInDokuwikiList . 'plugins' . $directorySeparatorInDokuwikiList;
83007225e5Sgerardnico
84007225e5Sgerardnico            // Script that we want on the show page
85007225e5Sgerardnico            $showPageScripts =
86007225e5Sgerardnico                [
87007225e5Sgerardnico                    0 => $dokuScriptPath . "qsearch.js", // for the search bar
88007225e5Sgerardnico                    1 => $dokuScriptPath . "script.js",
89007225e5Sgerardnico                    2 => $dokuScriptPath . "hotkeys.js",
90007225e5Sgerardnico                    3 => $dokuScriptPath . "locktimer.js", // Use in js.php - dw_locktimer
91007225e5Sgerardnico                    4 => $dokuScriptPath . "helpers.js", // substr_replace use in qsearch.php
92531e725cSNickeau                    5 => 'conf' . $directorySeparatorInDokuwikiList . 'userscript.js',
93531e725cSNickeau                    6 => $dokuScriptPath . "cookie.js", // plugin may depend on this library such as styling (lib/plugins/styling/script.js)
94531e725cSNickeau                    7 => $dokuScriptPath . "jquery/jquery.cookie.js" // cookie.js depends on it
95007225e5Sgerardnico                ];
96007225e5Sgerardnico
97007225e5Sgerardnico            $scriptsToKeep = array();
98007225e5Sgerardnico
99007225e5Sgerardnico            foreach ($event->data as $scriptPath) {
100007225e5Sgerardnico                $posPluginPath = strpos($scriptPath, $dokuPluginPath);
101007225e5Sgerardnico                if ($posPluginPath !== false) {
102007225e5Sgerardnico                    // This is a plugin script, we keep it
103007225e5Sgerardnico                    $scriptsToKeep[] = $scriptPath;
104007225e5Sgerardnico                } else {
105007225e5Sgerardnico
106007225e5Sgerardnico                    foreach ($showPageScripts as $showPageScript) {
107007225e5Sgerardnico
108007225e5Sgerardnico                        $posShowPage = strpos($scriptPath, $showPageScript);
109007225e5Sgerardnico                        if ($posShowPage !== false) {
110007225e5Sgerardnico                            // This is a script needed on the show page
111007225e5Sgerardnico                            $scriptsToKeep[] = $scriptPath;
112007225e5Sgerardnico                        }
113007225e5Sgerardnico                    }
114007225e5Sgerardnico                }
115007225e5Sgerardnico            }
116007225e5Sgerardnico
117007225e5Sgerardnico            $event->data = $scriptsToKeep;
118007225e5Sgerardnico
119007225e5Sgerardnico        }
120007225e5Sgerardnico    }
121007225e5Sgerardnico
122007225e5Sgerardnico}
123007225e5Sgerardnico
124