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