code style: multi statements
[dokuwiki.git] / lib / exe / js.php
blob36e1814f618bd44f1849cc41c41e12671701ca0e
1 <?php
3 /**
4 * DokuWiki JavaScript creator
6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author Andreas Gohr <andi@splitbrain.org>
8 */
10 use dokuwiki\Utf8\PhpString;
11 use dokuwiki\Cache\Cache;
12 use dokuwiki\Extension\Event;
13 use splitbrain\JSStrip\Exception as JSStripException;
14 use splitbrain\JSStrip\JSStrip;
16 if (!defined('DOKU_INC')) define('DOKU_INC', __DIR__ . '/../../');
17 if (!defined('NOSESSION')) define('NOSESSION', true); // we do not use a session or authentication here (better caching)
18 if (!defined('NL')) define('NL', "\n");
19 if (!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT', 1); // we gzip ourself here
20 require_once(DOKU_INC . 'inc/init.php');
22 // Main (don't run when UNIT test)
23 if (!defined('SIMPLE_TEST')) {
24 header('Content-Type: application/javascript; charset=utf-8');
25 js_out();
29 // ---------------------- functions ------------------------------
31 /**
32 * Output all needed JavaScript
34 * @author Andreas Gohr <andi@splitbrain.org>
36 function js_out()
38 global $conf;
39 global $lang;
40 global $config_cascade;
41 global $INPUT;
43 // decide from where to get the template
44 $tpl = trim(preg_replace('/[^\w-]+/', '', $INPUT->str('t')));
45 if (!$tpl) $tpl = $conf['template'];
47 // array of core files
48 $files = [
49 DOKU_INC . 'lib/scripts/jquery/jquery.cookie.js',
50 DOKU_INC . 'inc/lang/' . $conf['lang'] . '/jquery.ui.datepicker.js',
51 DOKU_INC . "lib/scripts/fileuploader.js",
52 DOKU_INC . "lib/scripts/fileuploaderextended.js",
53 DOKU_INC . 'lib/scripts/helpers.js',
54 DOKU_INC . 'lib/scripts/delay.js',
55 DOKU_INC . 'lib/scripts/cookie.js',
56 DOKU_INC . 'lib/scripts/script.js',
57 DOKU_INC . 'lib/scripts/qsearch.js',
58 DOKU_INC . 'lib/scripts/search.js',
59 DOKU_INC . 'lib/scripts/tree.js',
60 DOKU_INC . 'lib/scripts/index.js',
61 DOKU_INC . 'lib/scripts/textselection.js',
62 DOKU_INC . 'lib/scripts/toolbar.js',
63 DOKU_INC . 'lib/scripts/edit.js',
64 DOKU_INC . 'lib/scripts/editor.js',
65 DOKU_INC . 'lib/scripts/locktimer.js',
66 DOKU_INC . 'lib/scripts/linkwiz.js',
67 DOKU_INC . 'lib/scripts/media.js',
68 DOKU_INC . 'lib/scripts/compatibility.js',
69 # disabled for FS#1958 DOKU_INC.'lib/scripts/hotkeys.js',
70 DOKU_INC . 'lib/scripts/behaviour.js',
71 DOKU_INC . 'lib/scripts/page.js',
72 tpl_incdir($tpl) . 'script.js',
75 // add possible plugin scripts and userscript
76 $files = array_merge($files, js_pluginscripts());
77 if (is_array($config_cascade['userscript']['default'])) {
78 foreach ($config_cascade['userscript']['default'] as $userscript) {
79 $files[] = $userscript;
83 // Let plugins decide to either put more scripts here or to remove some
84 Event::createAndTrigger('JS_SCRIPT_LIST', $files);
86 // The generated script depends on some dynamic options
87 $cache = new Cache('scripts' . $_SERVER['HTTP_HOST'] . $_SERVER['SERVER_PORT'] . md5(serialize($files)), '.js');
88 $cache->setEvent('JS_CACHE_USE');
90 $cache_files = array_merge($files, getConfigFiles('main'));
91 $cache_files[] = __FILE__;
93 // check cache age & handle conditional request
94 // This may exit if a cache can be used
95 $cache_ok = $cache->useCache(['files' => $cache_files]);
96 http_cached($cache->cache, $cache_ok);
98 // start output buffering and build the script
99 ob_start();
101 // add some global variables
102 echo "var DOKU_BASE = '" . DOKU_BASE . "';";
103 echo "var DOKU_TPL = '" . tpl_basedir($tpl) . "';";
104 echo "var DOKU_COOKIE_PARAM = " . json_encode([
105 'path' => empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'],
106 'secure' => $conf['securecookie'] && is_ssl()
107 ], JSON_THROW_ON_ERROR) . ";";
108 // FIXME: Move those to JSINFO
109 echo "Object.defineProperty(window, 'DOKU_UHN', { get: function() {" .
110 "console.warn('Using DOKU_UHN is deprecated. Please use JSINFO.useHeadingNavigation instead');" .
111 "return JSINFO.useHeadingNavigation; } });";
112 echo "Object.defineProperty(window, 'DOKU_UHC', { get: function() {" .
113 "console.warn('Using DOKU_UHC is deprecated. Please use JSINFO.useHeadingContent instead');" .
114 "return JSINFO.useHeadingContent; } });";
116 // load JS specific translations
117 $lang['js']['plugins'] = js_pluginstrings();
118 $templatestrings = js_templatestrings($tpl);
119 if (!empty($templatestrings)) {
120 $lang['js']['template'] = $templatestrings;
122 echo 'LANG = ' . json_encode($lang['js'], JSON_THROW_ON_ERROR) . ";\n";
124 // load toolbar
125 toolbar_JSdefines('toolbar');
127 // load files
128 foreach ($files as $file) {
129 if (!file_exists($file)) continue;
130 $ismin = (substr($file, -7) == '.min.js');
131 $debugjs = ($conf['allowdebug'] && strpos($file, DOKU_INC . 'lib/scripts/') !== 0);
133 echo "\n\n/* XXXXXXXXXX begin of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n";
134 if ($ismin) echo "\n/* BEGIN NOCOMPRESS */\n";
135 if ($debugjs) echo "\ntry {\n";
136 js_load($file);
137 if ($debugjs) echo "\n} catch (e) {\n logError(e, '" . str_replace(DOKU_INC, '', $file) . "');\n}\n";
138 if ($ismin) echo "\n/* END NOCOMPRESS */\n";
139 echo "\n\n/* XXXXXXXXXX end of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n";
142 // init stuff
143 if ($conf['locktime'] != 0) {
144 js_runonstart("dw_locktimer.init(" . ($conf['locktime'] - 60) . "," . $conf['usedraft'] . ")");
146 // init hotkeys - must have been done after init of toolbar
147 # disabled for FS#1958 js_runonstart('initializeHotkeys()');
149 // end output buffering and get contents
150 $js = ob_get_contents();
151 ob_end_clean();
153 // strip any source maps
154 stripsourcemaps($js);
156 // compress whitespace and comments
157 if ($conf['compress']) {
158 try {
159 $js = (new JSStrip())->compress($js);
160 } catch (JSStripException $e) {
161 $js .= "\nconsole.error(" . json_encode($e->getMessage(), JSON_THROW_ON_ERROR) . ");\n";
165 $js .= "\n"; // https://bugzilla.mozilla.org/show_bug.cgi?id=316033
167 http_cached_finish($cache->cache, $js);
171 * Load the given file, handle include calls and print it
173 * @param string $file filename path to file
175 * @author Andreas Gohr <andi@splitbrain.org>
177 function js_load($file)
179 if (!file_exists($file)) return;
180 static $loaded = [];
182 $data = io_readFile($file);
183 while (preg_match('#/\*\s*DOKUWIKI:include(_once)?\s+([\w\.\-_/]+)\s*\*/#', $data, $match)) {
184 $ifile = $match[2];
186 // is it a include_once?
187 if ($match[1]) {
188 $base = PhpString::basename($ifile);
189 if (array_key_exists($base, $loaded) && $loaded[$base] === true) {
190 $data = str_replace($match[0], '', $data);
191 continue;
193 $loaded[$base] = true;
196 if ($ifile[0] != '/') $ifile = dirname($file) . '/' . $ifile;
198 $idata = '';
199 if (file_exists($ifile)) {
200 $ismin = (substr($ifile, -7) == '.min.js');
201 if ($ismin) $idata .= "\n/* BEGIN NOCOMPRESS */\n";
202 $idata .= io_readFile($ifile);
203 if ($ismin) $idata .= "\n/* END NOCOMPRESS */\n";
205 $data = str_replace($match[0], $idata, $data);
207 echo "$data\n";
211 * Returns a list of possible Plugin Scripts (no existance check here)
213 * @return array
215 * @author Andreas Gohr <andi@splitbrain.org>
217 function js_pluginscripts()
219 $list = [];
220 $plugins = plugin_list();
221 foreach ($plugins as $p) {
222 $list[] = DOKU_PLUGIN . "$p/script.js";
224 return $list;
228 * Return an two-dimensional array with strings from the language file of each plugin.
230 * - $lang['js'] must be an array.
231 * - Nothing is returned for plugins without an entry for $lang['js']
233 * @return array
234 * @author Gabriel Birke <birke@d-scribe.de>
237 function js_pluginstrings()
239 global $conf, $config_cascade;
240 $pluginstrings = [];
241 $plugins = plugin_list();
242 foreach ($plugins as $p) {
243 $path = DOKU_PLUGIN . $p . '/lang/';
245 if (isset($lang)) unset($lang);
246 if (file_exists($path . "en/lang.php")) {
247 include $path . "en/lang.php";
249 foreach ($config_cascade['lang']['plugin'] as $config_file) {
250 if (file_exists($config_file . $p . '/en/lang.php')) {
251 include($config_file . $p . '/en/lang.php');
254 if (isset($conf['lang']) && $conf['lang'] != 'en') {
255 if (file_exists($path . $conf['lang'] . "/lang.php")) {
256 include($path . $conf['lang'] . '/lang.php');
258 foreach ($config_cascade['lang']['plugin'] as $config_file) {
259 if (file_exists($config_file . $p . '/' . $conf['lang'] . '/lang.php')) {
260 include($config_file . $p . '/' . $conf['lang'] . '/lang.php');
265 if (isset($lang['js'])) {
266 $pluginstrings[$p] = $lang['js'];
269 return $pluginstrings;
273 * Return an two-dimensional array with strings from the language file of current active template.
275 * - $lang['js'] must be an array.
276 * - Nothing is returned for template without an entry for $lang['js']
278 * @param string $tpl
279 * @return array
281 function js_templatestrings($tpl)
283 global $conf, $config_cascade;
285 $path = tpl_incdir() . 'lang/';
287 $templatestrings = [];
288 if (file_exists($path . "en/lang.php")) {
289 include $path . "en/lang.php";
291 foreach ($config_cascade['lang']['template'] as $config_file) {
292 if (file_exists($config_file . $conf['template'] . '/en/lang.php')) {
293 include($config_file . $conf['template'] . '/en/lang.php');
296 if (isset($conf['lang']) && $conf['lang'] != 'en' && file_exists($path . $conf['lang'] . "/lang.php")) {
297 include $path . $conf['lang'] . "/lang.php";
299 if (isset($conf['lang']) && $conf['lang'] != 'en') {
300 if (file_exists($path . $conf['lang'] . "/lang.php")) {
301 include $path . $conf['lang'] . "/lang.php";
303 foreach ($config_cascade['lang']['template'] as $config_file) {
304 if (file_exists($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php')) {
305 include($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php');
310 if (isset($lang['js'])) {
311 $templatestrings[$tpl] = $lang['js'];
313 return $templatestrings;
317 * Escapes a String to be embedded in a JavaScript call, keeps \n
318 * as newline
320 * @param string $string
321 * @return string
323 * @author Andreas Gohr <andi@splitbrain.org>
325 function js_escape($string)
327 return str_replace('\\\\n', '\\n', addslashes($string));
331 * Adds the given JavaScript code to the window.onload() event
333 * @param string $func
335 * @author Andreas Gohr <andi@splitbrain.org>
337 function js_runonstart($func)
339 echo "jQuery(function(){ $func; });" . NL;