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',realpath(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"); 12require_once(DOKU_INC.'inc/init.php'); 13require_once(DOKU_INC.'inc/pageutils.php'); 14require_once(DOKU_INC.'inc/io.php'); 15 16// Main (don't run when UNIT test) 17if(!defined('SIMPLE_TEST')){ 18 header('Content-Type: text/javascript; charset=utf-8'); 19 js_out(); 20} 21 22 23// ---------------------- functions ------------------------------ 24 25/** 26 * Output all needed JavaScript 27 * 28 * @author Andreas Gohr <andi@splitbrain.org> 29 */ 30function js_out(){ 31 global $conf; 32 global $lang; 33 $edit = (bool) $_REQUEST['edit']; // edit or preview mode? 34 $write = (bool) $_REQUEST['write']; // writable? 35 36 // The generated script depends on some dynamic options 37 $cache = getCacheName('scripts'.$edit.$write,'.js'); 38 39 // Array of needed files 40 $files = array( 41 DOKU_INC.'lib/scripts/events.js', 42 DOKU_INC.'lib/scripts/script.js', 43 DOKU_INC.'lib/scripts/tw-sack.js', 44 DOKU_INC.'lib/scripts/ajax.js', 45 DOKU_INC.'lib/scripts/domLib.js', 46 DOKU_INC.'lib/scripts/domTT.js', 47 ); 48 if($edit && $write){ 49 $files[] = DOKU_INC.'lib/scripts/edit.js'; 50 if($conf['spellchecker']){ 51 $files[] = DOKU_INC.'lib/scripts/spellcheck.js'; 52 } 53 } 54 $files[] = DOKU_TPLINC.'script.js'; 55 56 // get possible plugin scripts 57 $plugins = js_pluginscripts(); 58 59 // check cache age & handle conditional request 60 header('Cache-Control: public, max-age=3600'); 61 header('Pragma: public'); 62 if(js_cacheok($cache,array_merge($files,$plugins))){ 63 http_conditionalRequest(filemtime($cache)); 64 readfile($cache); 65 return; 66 } else { 67 http_conditionalRequest(time()); 68 } 69 70 // start output buffering and build the script 71 ob_start(); 72 73 // add some translation strings and global variables 74 print "var alertText = '".js_escape($lang['qb_alert'])."';"; 75 print "var notSavedYet = '".js_escape($lang['notsavedyet'])."';"; 76 print "var reallyDel = '".js_escape($lang['del_confirm'])."';"; 77 print "var DOKU_BASE = '".DOKU_BASE."';"; 78 79 // load files 80 foreach($files as $file){ 81 echo "\n\n/* XXXXXXXXXX begin of $file XXXXXXXXXX */\n\n"; 82 @readfile($file); 83 echo "\n\n/* XXXXXXXXXX end of $file XXXXXXXXXX */\n\n"; 84 } 85 86 // init stuff 87 js_runonstart("ajax_qsearch.init('qsearch__in','qsearch__out')"); 88 js_runonstart("addEvent(document,'click',closePopups)"); 89 js_runonstart('addTocToggle()'); 90 91 if($edit){ 92 // size controls 93 js_runonstart("initSizeCtl('size__ctl','wiki__text')"); 94 95 if($write){ 96 require_once(DOKU_INC.'inc/toolbar.php'); 97 toolbar_JSdefines('toolbar'); 98 js_runonstart("initToolbar('tool__bar','wiki__text',toolbar)"); 99 100 // add pageleave check 101 js_runonstart("initChangeCheck('".js_escape($lang['notsavedyet'])."')"); 102 103 // add lock timer 104 js_runonstart("locktimer.init(".($conf['locktime'] - 60).",'".js_escape($lang['willexpire'])."',".$conf['usedraft'].")"); 105 106 // load spell checker 107 if($conf['spellchecker']){ 108 js_runonstart("ajax_spell.init('". 109 js_escape($lang['spell_start'])."','". 110 js_escape($lang['spell_stop'])."','". 111 js_escape($lang['spell_wait'])."','". 112 js_escape($lang['spell_noerr'])."','". 113 js_escape($lang['spell_nosug'])."','". 114 js_escape($lang['spell_change'])."')"); 115 } 116 } 117 } 118 119 // load plugin scripts (suppress warnings for missing ones) 120 foreach($plugins as $plugin){ 121 echo "\n\n/* XXXXXXXXXX begin of $file XXXXXXXXXX */\n\n"; 122 @readfile($plugin); 123 echo "\n\n/* XXXXXXXXXX end of $file XXXXXXXXXX */\n\n"; 124 } 125 126 // load user script 127 @readfile(DOKU_CONF.'userscript.js'); 128 129 // add scroll event and tooltip rewriting 130 js_runonstart('updateAccessKeyTooltip()'); 131 js_runonstart('scrollToMarker()'); 132 133 // initialize init pseudo event 134 echo 'if (document.addEventListener) {'.NL; 135 echo ' document.addEventListener("DOMContentLoaded", window.fireoninit, null);'.NL; 136 echo '}'.NL; 137 echo 'addEvent(window,"load",window.fireoninit);'.NL; 138 139 // end output buffering and get contents 140 $js = ob_get_contents(); 141 ob_end_clean(); 142 143 // compress whitespace and comments 144 if($conf['compress']){ 145 $js = js_compress($js); 146 } 147 148 // save cache file 149 io_saveFile($cache,$js); 150 151 // finally send output 152 print $js; 153} 154 155/** 156 * Checks if a JavaScript Cache file still is valid 157 * 158 * @author Andreas Gohr <andi@splitbrain.org> 159 */ 160function js_cacheok($cache,$files){ 161 $ctime = @filemtime($cache); 162 if(!$ctime) return false; //There is no cache 163 164 // some additional files to check 165 $files[] = DOKU_CONF.'dokuwiki.php'; 166 $files[] = DOKU_CONF.'local.php'; 167 $files[] = DOKU_CONF.'userscript.js'; 168 $files[] = __FILE__; 169 170 // now walk the files 171 foreach($files as $file){ 172 if(@filemtime($file) > $ctime){ 173 return false; 174 } 175 } 176 return true; 177} 178 179/** 180 * Returns a list of possible Plugin Scripts (no existance check here) 181 * 182 * @author Andreas Gohr <andi@splitbrain.org> 183 */ 184function js_pluginscripts(){ 185 $list = array(); 186 $plugins = plugin_list(); 187 foreach ($plugins as $p){ 188 $list[] = DOKU_PLUGIN."$p/script.js"; 189 } 190 return $list; 191} 192 193/** 194 * Escapes a String to be embedded in a JavaScript call, keeps \n 195 * as newline 196 * 197 * @author Andreas Gohr <andi@splitbrain.org> 198 */ 199function js_escape($string){ 200 return str_replace('\\\\n','\\n',addslashes($string)); 201} 202 203/** 204 * Adds the given JavaScript code to the window.onload() event 205 * 206 * @author Andreas Gohr <andi@splitbrain.org> 207 */ 208function js_runonstart($func){ 209 echo "addInitEvent(function(){ $func; });".NL; 210} 211 212/** 213 * Strip comments and whitespaces from given JavaScript Code 214 * 215 * This is a rewrite of Nick Galbreaths python tool jsstrip.py which is 216 * released under BSD license. See link for original code. 217 * 218 * @author Nick Galbreath <nickg@modp.com> 219 * @author Andreas Gohr <andi@splitbrain.org> 220 * @link http://modp.com/release/jsstrip/ 221 */ 222function js_compress($s){ 223 $i = 0; 224 $line = 0; 225 $s .= "\n"; 226 $len = strlen($s); 227 228 // items that don't need spaces next to them 229 $chars = '^&|!+\-*\/%=:;,{}()<>% \t\n\r'; 230 231 ob_start(); 232 while($i < $len){ 233 $ch = $s{$i}; 234 235 // multiline comments 236 if($ch == '/' && $s{$i+1} == '*'){ 237 $endC = strpos($s,'*/',$i+2); 238 if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR); 239 $i = $endC + 2; 240 continue; 241 } 242 243 // singleline 244 if($ch == '/' && $s{$i+1} == '/'){ 245 $endC = strpos($s,"\n",$i+2); 246 if($endC === false) trigger_error('Invalid comment', E_USER_ERROR); 247 $i = $endC; 248 continue; 249 } 250 251 // tricky. might be an RE 252 if($ch == '/'){ 253 // rewind, skip white space 254 $j = 1; 255 while($s{$i-$j} == ' '){ 256 $j = $j + 1; 257 } 258 if( ($s{$i-$j} == '=') || ($s{$i-$j} == '(') ){ 259 // yes, this is an re 260 // now move forward and find the end of it 261 $j = 1; 262 while($s{$i+$j} != '/'){ 263 while( ($s{$i+$j} != '\\') && ($s{$i+$j} != '/')){ 264 $j = $j + 1; 265 } 266 if($s{$i+$j} == '\\') $j = $j + 2; 267 } 268 echo substr($s,$i,$j+1); 269 $i = $i + $j + 1; 270 continue; 271 } 272 } 273 274 // double quote strings 275 if($ch == '"'){ 276 $j = 1; 277 while( $s{$i+$j} != '"' ){ 278 while( ($s{$i+$j} != '\\') && ($s{$i+$j} != '"') ){ 279 $j = $j + 1; 280 } 281 if($s{$i+$j} == '\\') $j = $j + 2; 282 } 283 echo substr($s,$i,$j+1); 284 $i = $i + $j + 1; 285 continue; 286 } 287 288 // single quote strings 289 if($ch == "'"){ 290 $j = 1; 291 while( $s{$i+$j} != "'" ){ 292 while( ($s{$i+$j} != '\\') && ($s{$i+$j} != "'") ){ 293 $j = $j + 1; 294 } 295 if ($s{$i+$j} == '\\') $j = $j + 2; 296 } 297 echo substr($s,$i,$j+1); 298 $i = $i + $j + 1; 299 continue; 300 } 301 302 // newlines 303 if($ch == "\n" || $ch == "\r"){ 304 $i = $i+1; 305 continue; 306 } 307 308 // leading spaces 309 if( ( $ch == ' ' || 310 $ch == "\n" || 311 $ch == "\t" ) && 312 !preg_match('/['.$chars.']/',$s{$i+1}) ){ 313 $i = $i+1; 314 continue; 315 } 316 317 // trailing spaces 318 if( ( $ch == ' ' || 319 $ch == "\n" || 320 $ch == "\t" ) && 321 !preg_match('/['.$chars.']/',$s{$i-1}) ){ 322 $i = $i+1; 323 continue; 324 } 325 326 // other chars 327 echo $ch; 328 $i = $i + 1; 329 } 330 331 332 $out = ob_get_contents(); 333 ob_end_clean(); 334 return $out; 335} 336 337//Setup VIM: ex: et ts=4 enc=utf-8 : 338?> 339