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 echo 'LANG = '.$json->encode($lang['js']).";\n"; 100 101 // load toolbar 102 toolbar_JSdefines('toolbar'); 103 104 // load files 105 foreach($files as $file){ 106 $ismin = (substr($file,-7) == '.min.js'); 107 $debugjs = ($conf['allowdebug'] && strpos($file, DOKU_INC.'lib/scripts/') !== 0); 108 109 echo "\n\n/* XXXXXXXXXX begin of ".str_replace(DOKU_INC, '', $file) ." XXXXXXXXXX */\n\n"; 110 if($ismin) echo "\n/* BEGIN NOCOMPRESS */\n"; 111 if ($debugjs) echo "\ntry {\n"; 112 js_load($file); 113 if ($debugjs) echo "\n} catch (e) {\n logError(e, '".str_replace(DOKU_INC, '', $file)."');\n}\n"; 114 if($ismin) echo "\n/* END NOCOMPRESS */\n"; 115 echo "\n\n/* XXXXXXXXXX end of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n"; 116 } 117 118 // init stuff 119 if($conf['locktime'] != 0){ 120 js_runonstart("dw_locktimer.init(".($conf['locktime'] - 60).",".$conf['usedraft'].")"); 121 } 122 // init hotkeys - must have been done after init of toolbar 123# disabled for FS#1958 js_runonstart('initializeHotkeys()'); 124 125 // end output buffering and get contents 126 $js = ob_get_contents(); 127 ob_end_clean(); 128 129 // compress whitespace and comments 130 if($conf['compress']){ 131 $js = js_compress($js); 132 } 133 134 $js .= "\n"; // https://bugzilla.mozilla.org/show_bug.cgi?id=316033 135 136 http_cached_finish($cache->cache, $js); 137} 138 139/** 140 * Load the given file, handle include calls and print it 141 * 142 * @author Andreas Gohr <andi@splitbrain.org> 143 */ 144function js_load($file){ 145 if(!@file_exists($file)) return; 146 static $loaded = array(); 147 148 $data = io_readFile($file); 149 while(preg_match('#/\*\s*DOKUWIKI:include(_once)?\s+([\w\.\-_/]+)\s*\*/#',$data,$match)){ 150 $ifile = $match[2]; 151 152 // is it a include_once? 153 if($match[1]){ 154 $base = utf8_basename($ifile); 155 if($loaded[$base]) continue; 156 $loaded[$base] = true; 157 } 158 159 if($ifile{0} != '/') $ifile = dirname($file).'/'.$ifile; 160 161 if(@file_exists($ifile)){ 162 $idata = io_readFile($ifile); 163 }else{ 164 $idata = ''; 165 } 166 $data = str_replace($match[0],$idata,$data); 167 } 168 echo "$data\n"; 169} 170 171/** 172 * Returns a list of possible Plugin Scripts (no existance check here) 173 * 174 * @author Andreas Gohr <andi@splitbrain.org> 175 */ 176function js_pluginscripts(){ 177 $list = array(); 178 $plugins = plugin_list(); 179 foreach ($plugins as $p){ 180 $list[] = DOKU_PLUGIN."$p/script.js"; 181 } 182 return $list; 183} 184 185/** 186 * Return an two-dimensional array with strings from the language file of each plugin. 187 * 188 * - $lang['js'] must be an array. 189 * - Nothing is returned for plugins without an entry for $lang['js'] 190 * 191 * @author Gabriel Birke <birke@d-scribe.de> 192 */ 193function js_pluginstrings() 194{ 195 global $conf; 196 $pluginstrings = array(); 197 $plugins = plugin_list(); 198 foreach ($plugins as $p){ 199 if (isset($lang)) unset($lang); 200 if (@file_exists(DOKU_PLUGIN."$p/lang/en/lang.php")) { 201 include DOKU_PLUGIN."$p/lang/en/lang.php"; 202 } 203 if (isset($conf['lang']) && $conf['lang']!='en' && @file_exists(DOKU_PLUGIN."$p/lang/".$conf['lang']."/lang.php")) { 204 include DOKU_PLUGIN."$p/lang/".$conf['lang']."/lang.php"; 205 } 206 if (isset($lang['js'])) { 207 $pluginstrings[$p] = $lang['js']; 208 } 209 } 210 return $pluginstrings; 211} 212 213/** 214 * Escapes a String to be embedded in a JavaScript call, keeps \n 215 * as newline 216 * 217 * @author Andreas Gohr <andi@splitbrain.org> 218 */ 219function js_escape($string){ 220 return str_replace('\\\\n','\\n',addslashes($string)); 221} 222 223/** 224 * Adds the given JavaScript code to the window.onload() event 225 * 226 * @author Andreas Gohr <andi@splitbrain.org> 227 */ 228function js_runonstart($func){ 229 echo "jQuery(function(){ $func; });".NL; 230} 231 232/** 233 * Strip comments and whitespaces from given JavaScript Code 234 * 235 * This is a port of Nick Galbreath's python tool jsstrip.py which is 236 * released under BSD license. See link for original code. 237 * 238 * @author Nick Galbreath <nickg@modp.com> 239 * @author Andreas Gohr <andi@splitbrain.org> 240 * @link http://code.google.com/p/jsstrip/ 241 */ 242function js_compress($s){ 243 $s = ltrim($s); // strip all initial whitespace 244 $s .= "\n"; 245 $i = 0; // char index for input string 246 $j = 0; // char forward index for input string 247 $line = 0; // line number of file (close to it anyways) 248 $slen = strlen($s); // size of input string 249 $lch = ''; // last char added 250 $result = ''; // we store the final result here 251 252 // items that don't need spaces next to them 253 $chars = "^&|!+\-*\/%=\?:;,{}()<>% \t\n\r'\"[]"; 254 255 $regex_starters = array("(", "=", "[", "," , ":", "!"); 256 257 $whitespaces_chars = array(" ", "\t", "\n", "\r", "\0", "\x0B"); 258 259 while($i < $slen){ 260 // skip all "boring" characters. This is either 261 // reserved word (e.g. "for", "else", "if") or a 262 // variable/object/method (e.g. "foo.color") 263 while ($i < $slen && (strpos($chars,$s[$i]) === false) ){ 264 $result .= $s{$i}; 265 $i = $i + 1; 266 } 267 268 $ch = $s{$i}; 269 // multiline comments (keeping IE conditionals) 270 if($ch == '/' && $s{$i+1} == '*' && $s{$i+2} != '@'){ 271 $endC = strpos($s,'*/',$i+2); 272 if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR); 273 274 // check if this is a NOCOMPRESS comment 275 if(substr($s, $i, $endC+2-$i) == '/* BEGIN NOCOMPRESS */'){ 276 $endNC = strpos($s, '/* END NOCOMPRESS */', $endC+2); 277 if($endNC === false) trigger_error('Found invalid NOCOMPRESS comment', E_USER_ERROR); 278 279 // verbatim copy contents, trimming but putting it on its own line 280 $result .= "\n".trim(substr($s, $i + 22, $endNC - ($i + 22)))."\n"; // BEGIN comment = 22 chars 281 $i = $endNC + 20; // END comment = 20 chars 282 }else{ 283 $i = $endC + 2; 284 } 285 continue; 286 } 287 288 // singleline 289 if($ch == '/' && $s{$i+1} == '/'){ 290 $endC = strpos($s,"\n",$i+2); 291 if($endC === false) trigger_error('Invalid comment', E_USER_ERROR); 292 $i = $endC; 293 continue; 294 } 295 296 // tricky. might be an RE 297 if($ch == '/'){ 298 // rewind, skip white space 299 $j = 1; 300 while(in_array($s{$i-$j}, $whitespaces_chars)){ 301 $j = $j + 1; 302 } 303 if( in_array($s{$i-$j}, $regex_starters) ){ 304 // yes, this is an re 305 // now move forward and find the end of it 306 $j = 1; 307 while($s{$i+$j} != '/'){ 308 if($s{$i+$j} == '\\') $j = $j + 2; 309 else $j++; 310 } 311 $result .= substr($s,$i,$j+1); 312 $i = $i + $j + 1; 313 continue; 314 } 315 } 316 317 // double quote strings 318 if($ch == '"'){ 319 $j = 1; 320 while( $s{$i+$j} != '"' && ($i+$j < $slen)){ 321 if( $s{$i+$j} == '\\' && ($s{$i+$j+1} == '"' || $s{$i+$j+1} == '\\') ){ 322 $j += 2; 323 }else{ 324 $j += 1; 325 } 326 } 327 $string = substr($s,$i,$j+1); 328 // remove multiline markers: 329 $string = str_replace("\\\n",'',$string); 330 $result .= $string; 331 $i = $i + $j + 1; 332 continue; 333 } 334 335 // single 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 // whitespaces 354 if( $ch == ' ' || $ch == "\r" || $ch == "\n" || $ch == "\t" ){ 355 // leading spaces 356 if($i+1 < $slen && (strpos($chars,$s[$i+1]) !== false)){ 357 $i = $i + 1; 358 continue; 359 } 360 // trailing spaces 361 // if this ch is space AND the last char processed 362 // is special, then skip the space 363 $lch = substr($result,-1); 364 if($lch && (strpos($chars,$lch) !== false)){ 365 $i = $i + 1; 366 continue; 367 } 368 // else after all of this convert the "whitespace" to 369 // a single space. It will get appended below 370 $ch = ' '; 371 } 372 373 // other chars 374 $result .= $ch; 375 $i = $i + 1; 376 } 377 378 return trim($result); 379} 380 381//Setup VIM: ex: et ts=4 : 382