1<?php 2/** 3 * DokuWiki JavaScript creator 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../'); 10if(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching) 11if(!defined('NL')) define('NL',"\n"); 12if(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here 13require_once(DOKU_INC.'inc/init.php'); 14 15// Main (don't run when UNIT test) 16if(!defined('SIMPLE_TEST')){ 17 header('Content-Type: text/javascript; charset=utf-8'); 18 js_out(); 19} 20 21 22// ---------------------- functions ------------------------------ 23 24/** 25 * Output all needed JavaScript 26 * 27 * @author Andreas Gohr <andi@splitbrain.org> 28 */ 29function js_out(){ 30 global $conf; 31 global $lang; 32 global $config_cascade; 33 34 // The generated script depends on some dynamic options 35 $cache = new cache('scripts'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.js'); 36 $cache->_event = 'JS_CACHE_USE'; 37 38 // load minified version for some files 39 $min = $conf['compress'] ? '.min' : ''; 40 41 // array of core files 42 $files = array( 43 DOKU_INC."lib/scripts/jquery/jquery$min.js", 44 DOKU_INC.'lib/scripts/jquery/jquery.cookie.js', 45 DOKU_INC."lib/scripts/jquery/jquery-ui$min.js", 46 DOKU_INC."lib/scripts/jquery/jquery-migrate$min.js", 47 DOKU_INC."lib/scripts/fileuploader.js", 48 DOKU_INC."lib/scripts/fileuploaderextended.js", 49 DOKU_INC.'lib/scripts/helpers.js', 50 DOKU_INC.'lib/scripts/delay.js', 51 DOKU_INC.'lib/scripts/cookie.js', 52 DOKU_INC.'lib/scripts/script.js', 53 DOKU_INC.'lib/scripts/tw-sack.js', 54 DOKU_INC.'lib/scripts/qsearch.js', 55 DOKU_INC.'lib/scripts/tree.js', 56 DOKU_INC.'lib/scripts/index.js', 57 DOKU_INC.'lib/scripts/drag.js', 58 DOKU_INC.'lib/scripts/textselection.js', 59 DOKU_INC.'lib/scripts/toolbar.js', 60 DOKU_INC.'lib/scripts/edit.js', 61 DOKU_INC.'lib/scripts/editor.js', 62 DOKU_INC.'lib/scripts/locktimer.js', 63 DOKU_INC.'lib/scripts/linkwiz.js', 64 DOKU_INC.'lib/scripts/media.js', 65# deprecated DOKU_INC.'lib/scripts/compatibility.js', 66# disabled for FS#1958 DOKU_INC.'lib/scripts/hotkeys.js', 67 DOKU_INC.'lib/scripts/behaviour.js', 68 DOKU_INC.'lib/scripts/page.js', 69 tpl_incdir().'script.js', 70 ); 71 72 // add possible plugin scripts and userscript 73 $files = array_merge($files,js_pluginscripts()); 74 if(isset($config_cascade['userscript']['default'])){ 75 $files[] = $config_cascade['userscript']['default']; 76 } 77 78 $cache_files = array_merge($files, getConfigFiles('main')); 79 $cache_files[] = __FILE__; 80 81 // check cache age & handle conditional request 82 // This may exit if a cache can be used 83 $cache_ok = $cache->useCache(array('files' => $cache_files)); 84 http_cached($cache->cache, $cache_ok); 85 86 // start output buffering and build the script 87 ob_start(); 88 89 // add some global variables 90 print "var DOKU_BASE = '".DOKU_BASE."';"; 91 print "var DOKU_TPL = '".tpl_basedir()."';"; 92 // FIXME: Move those to JSINFO 93 print "var DOKU_UHN = ".((int) useHeading('navigation')).";"; 94 print "var DOKU_UHC = ".((int) useHeading('content')).";"; 95 96 // load JS specific translations 97 $json = new JSON(); 98 $lang['js']['plugins'] = js_pluginstrings(); 99 $templatestrings = js_templatestrings(); 100 if(!empty($templatestrings)) { 101 $lang['js']['template'] = $templatestrings; 102 } 103 echo 'LANG = '.$json->encode($lang['js']).";\n"; 104 105 // load toolbar 106 toolbar_JSdefines('toolbar'); 107 108 // load files 109 foreach($files as $file){ 110 $ismin = (substr($file,-7) == '.min.js'); 111 $debugjs = ($conf['allowdebug'] && strpos($file, DOKU_INC.'lib/scripts/') !== 0); 112 113 echo "\n\n/* XXXXXXXXXX begin of ".str_replace(DOKU_INC, '', $file) ." XXXXXXXXXX */\n\n"; 114 if($ismin) echo "\n/* BEGIN NOCOMPRESS */\n"; 115 if ($debugjs) echo "\ntry {\n"; 116 js_load($file); 117 if ($debugjs) echo "\n} catch (e) {\n logError(e, '".str_replace(DOKU_INC, '', $file)."');\n}\n"; 118 if($ismin) echo "\n/* END NOCOMPRESS */\n"; 119 echo "\n\n/* XXXXXXXXXX end of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n"; 120 } 121 122 // init stuff 123 if($conf['locktime'] != 0){ 124 js_runonstart("dw_locktimer.init(".($conf['locktime'] - 60).",".$conf['usedraft'].")"); 125 } 126 // init hotkeys - must have been done after init of toolbar 127# disabled for FS#1958 js_runonstart('initializeHotkeys()'); 128 129 // end output buffering and get contents 130 $js = ob_get_contents(); 131 ob_end_clean(); 132 133 // compress whitespace and comments 134 if($conf['compress']){ 135 $js = js_compress($js); 136 } 137 138 $js .= "\n"; // https://bugzilla.mozilla.org/show_bug.cgi?id=316033 139 140 http_cached_finish($cache->cache, $js); 141} 142 143/** 144 * Load the given file, handle include calls and print it 145 * 146 * @author Andreas Gohr <andi@splitbrain.org> 147 */ 148function js_load($file){ 149 if(!@file_exists($file)) return; 150 static $loaded = array(); 151 152 $data = io_readFile($file); 153 while(preg_match('#/\*\s*DOKUWIKI:include(_once)?\s+([\w\.\-_/]+)\s*\*/#',$data,$match)){ 154 $ifile = $match[2]; 155 156 // is it a include_once? 157 if($match[1]){ 158 $base = utf8_basename($ifile); 159 if($loaded[$base]) continue; 160 $loaded[$base] = true; 161 } 162 163 if($ifile{0} != '/') $ifile = dirname($file).'/'.$ifile; 164 165 if(@file_exists($ifile)){ 166 $idata = io_readFile($ifile); 167 }else{ 168 $idata = ''; 169 } 170 $data = str_replace($match[0],$idata,$data); 171 } 172 echo "$data\n"; 173} 174 175/** 176 * Returns a list of possible Plugin Scripts (no existance check here) 177 * 178 * @author Andreas Gohr <andi@splitbrain.org> 179 */ 180function js_pluginscripts(){ 181 $list = array(); 182 $plugins = plugin_list(); 183 foreach ($plugins as $p){ 184 $list[] = DOKU_PLUGIN."$p/script.js"; 185 } 186 return $list; 187} 188 189/** 190 * Return an two-dimensional array with strings from the language file of each plugin. 191 * 192 * - $lang['js'] must be an array. 193 * - Nothing is returned for plugins without an entry for $lang['js'] 194 * 195 * @author Gabriel Birke <birke@d-scribe.de> 196 */ 197function js_pluginstrings() { 198 global $conf; 199 $pluginstrings = array(); 200 $plugins = plugin_list(); 201 foreach ($plugins as $p){ 202 if (isset($lang)) unset($lang); 203 if (@file_exists(DOKU_PLUGIN."$p/lang/en/lang.php")) { 204 include DOKU_PLUGIN."$p/lang/en/lang.php"; 205 } 206 if (isset($conf['lang']) && $conf['lang']!='en' && @file_exists(DOKU_PLUGIN."$p/lang/".$conf['lang']."/lang.php")) { 207 include DOKU_PLUGIN."$p/lang/".$conf['lang']."/lang.php"; 208 } 209 if (isset($lang['js'])) { 210 $pluginstrings[$p] = $lang['js']; 211 } 212 } 213 return $pluginstrings; 214} 215 216function js_templatestrings() { 217 global $conf; 218 $templatestrings = array(); 219 if (@file_exists(tpl_incdir()."lang/en/lang.php")) { 220 include tpl_incdir()."lang/en/lang.php"; 221 } 222 if (isset($conf['lang']) && $conf['lang']!='en' && @file_exists(tpl_incdir()."lang/".$conf['lang']."/lang.php")) { 223 include tpl_incdir()."lang/".$conf['lang']."/lang.php"; 224 } 225 if (isset($lang['js'])) { 226 $templatestrings[$conf['template']] = $lang['js']; 227 } 228 return $templatestrings; 229} 230 231/** 232 * Escapes a String to be embedded in a JavaScript call, keeps \n 233 * as newline 234 * 235 * @author Andreas Gohr <andi@splitbrain.org> 236 */ 237function js_escape($string){ 238 return str_replace('\\\\n','\\n',addslashes($string)); 239} 240 241/** 242 * Adds the given JavaScript code to the window.onload() event 243 * 244 * @author Andreas Gohr <andi@splitbrain.org> 245 */ 246function js_runonstart($func){ 247 echo "jQuery(function(){ $func; });".NL; 248} 249 250/** 251 * Strip comments and whitespaces from given JavaScript Code 252 * 253 * This is a port of Nick Galbreath's python tool jsstrip.py which is 254 * released under BSD license. See link for original code. 255 * 256 * @author Nick Galbreath <nickg@modp.com> 257 * @author Andreas Gohr <andi@splitbrain.org> 258 * @link http://code.google.com/p/jsstrip/ 259 */ 260function js_compress($s){ 261 $s = ltrim($s); // strip all initial whitespace 262 $s .= "\n"; 263 $i = 0; // char index for input string 264 $j = 0; // char forward index for input string 265 $line = 0; // line number of file (close to it anyways) 266 $slen = strlen($s); // size of input string 267 $lch = ''; // last char added 268 $result = ''; // we store the final result here 269 270 // items that don't need spaces next to them 271 $chars = "^&|!+\-*\/%=\?:;,{}()<>% \t\n\r'\"[]"; 272 273 $regex_starters = array("(", "=", "[", "," , ":", "!"); 274 275 $whitespaces_chars = array(" ", "\t", "\n", "\r", "\0", "\x0B"); 276 277 while($i < $slen){ 278 // skip all "boring" characters. This is either 279 // reserved word (e.g. "for", "else", "if") or a 280 // variable/object/method (e.g. "foo.color") 281 while ($i < $slen && (strpos($chars,$s[$i]) === false) ){ 282 $result .= $s{$i}; 283 $i = $i + 1; 284 } 285 286 $ch = $s{$i}; 287 // multiline comments (keeping IE conditionals) 288 if($ch == '/' && $s{$i+1} == '*' && $s{$i+2} != '@'){ 289 $endC = strpos($s,'*/',$i+2); 290 if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR); 291 292 // check if this is a NOCOMPRESS comment 293 if(substr($s, $i, $endC+2-$i) == '/* BEGIN NOCOMPRESS */'){ 294 $endNC = strpos($s, '/* END NOCOMPRESS */', $endC+2); 295 if($endNC === false) trigger_error('Found invalid NOCOMPRESS comment', E_USER_ERROR); 296 297 // verbatim copy contents, trimming but putting it on its own line 298 $result .= "\n".trim(substr($s, $i + 22, $endNC - ($i + 22)))."\n"; // BEGIN comment = 22 chars 299 $i = $endNC + 20; // END comment = 20 chars 300 }else{ 301 $i = $endC + 2; 302 } 303 continue; 304 } 305 306 // singleline 307 if($ch == '/' && $s{$i+1} == '/'){ 308 $endC = strpos($s,"\n",$i+2); 309 if($endC === false) trigger_error('Invalid comment', E_USER_ERROR); 310 $i = $endC; 311 continue; 312 } 313 314 // tricky. might be an RE 315 if($ch == '/'){ 316 // rewind, skip white space 317 $j = 1; 318 while(in_array($s{$i-$j}, $whitespaces_chars)){ 319 $j = $j + 1; 320 } 321 if( in_array($s{$i-$j}, $regex_starters) ){ 322 // yes, this is an re 323 // now move forward and find the end of it 324 $j = 1; 325 while($s{$i+$j} != '/'){ 326 if($s{$i+$j} == '\\') $j = $j + 2; 327 else $j++; 328 } 329 $result .= substr($s,$i,$j+1); 330 $i = $i + $j + 1; 331 continue; 332 } 333 } 334 335 // double quote strings 336 if($ch == '"'){ 337 $j = 1; 338 while( $s{$i+$j} != '"' && ($i+$j < $slen)){ 339 if( $s{$i+$j} == '\\' && ($s{$i+$j+1} == '"' || $s{$i+$j+1} == '\\') ){ 340 $j += 2; 341 }else{ 342 $j += 1; 343 } 344 } 345 $string = substr($s,$i,$j+1); 346 // remove multiline markers: 347 $string = str_replace("\\\n",'',$string); 348 $result .= $string; 349 $i = $i + $j + 1; 350 continue; 351 } 352 353 // single quote strings 354 if($ch == "'"){ 355 $j = 1; 356 while( $s{$i+$j} != "'" && ($i+$j < $slen)){ 357 if( $s{$i+$j} == '\\' && ($s{$i+$j+1} == "'" || $s{$i+$j+1} == '\\') ){ 358 $j += 2; 359 }else{ 360 $j += 1; 361 } 362 } 363 $string = substr($s,$i,$j+1); 364 // remove multiline markers: 365 $string = str_replace("\\\n",'',$string); 366 $result .= $string; 367 $i = $i + $j + 1; 368 continue; 369 } 370 371 // whitespaces 372 if( $ch == ' ' || $ch == "\r" || $ch == "\n" || $ch == "\t" ){ 373 // leading spaces 374 if($i+1 < $slen && (strpos($chars,$s[$i+1]) !== false)){ 375 $i = $i + 1; 376 continue; 377 } 378 // trailing spaces 379 // if this ch is space AND the last char processed 380 // is special, then skip the space 381 $lch = substr($result,-1); 382 if($lch && (strpos($chars,$lch) !== false)){ 383 $i = $i + 1; 384 continue; 385 } 386 // else after all of this convert the "whitespace" to 387 // a single space. It will get appended below 388 $ch = ' '; 389 } 390 391 // other chars 392 $result .= $ch; 393 $i = $i + 1; 394 } 395 396 return trim($result); 397} 398 399//Setup VIM: ex: et ts=4 : 400