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__).'/../../').'/'); 10define('NOSESSION',true); // we do not use a session or authentication here (better caching) 11require_once(DOKU_INC.'inc/init.php'); 12require_once(DOKU_INC.'inc/pageutils.php'); 13require_once(DOKU_INC.'inc/io.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 js_dynamicout(); 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 55 // get possible plugin scripts 56 $plugins = js_pluginscripts(); 57 58 // check cache age here 59 if(js_cacheok($cache,array_merge($files,$plugins))){ 60 readfile($cache); 61 return; 62 } 63 64 // start output buffering and build the script 65 ob_start(); 66 67 // add some translation strings and global variables 68 print "var alertText = '".str_replace('\\\\n','\\n',addslashes($lang['qb_alert']))."';"; 69 print "var notSavedYet = '".str_replace('\\\\n','\\n',addslashes($lang['notsavedyet']))."';"; 70 print "var DOKU_BASE = '".DOKU_BASE."';"; 71 72 // load files 73 foreach($files as $file){ 74 readfile($file); 75 } 76 77 // init stuff 78 js_runonstart("ajax_qsearch.init('qsearch_in','qsearch_out')"); 79 js_runonstart("addEvent(document,'click',closePopups)"); 80 81 if($edit){ 82 // size controls 83 js_runonstart("initSizeCtl('sizectl','wikitext')"); 84 85 if($write){ 86 require_once(DOKU_INC.'inc/toolbar.php'); 87 toolbar_JSdefines('toolbar'); 88 js_runonstart("initToolbar('toolbar','wikitext',toolbar)"); 89 90 // add pageleave check 91 js_runonstart("initChangeCheck('".js_escape($lang['notsavedyet'])."')"); 92 93 // add lock timer 94 js_runonstart("init_locktimer(".($conf['locktime']-60).",'".js_escape($lang['willexpire'])."')"); 95 96 // load spell checker 97 if($conf['spellchecker']){ 98 js_runonstart("ajax_spell.init('". 99 js_escape($lang['spell_start'])."','". 100 js_escape($lang['spell_stop'])."','". 101 js_escape($lang['spell_wait'])."','". 102 js_escape($lang['spell_noerr'])."','". 103 js_escape($lang['spell_nosug'])."','". 104 js_escape($lang['spell_change'])."')"); 105 } 106 } 107 } 108 109 // load plugin scripts (suppress warnings for missing ones) 110 foreach($plugins as $plugin){ 111 @readfile($plugin); 112 } 113 114 // load user script 115 @readfile(DOKU_CONF.'userscript.js'); 116 117 // end output buffering and get contents 118 $js = ob_get_contents(); 119 ob_end_clean(); 120 121 // compress whitespace and comments 122 if($conf['compress']){ 123 $js = js_compress($js); 124 } 125 126 // save cache file 127 io_saveFile($cache,$js); 128 129 // finally send output 130 print $js; 131} 132 133/** 134 * Adds some dynamic JavaScript using the readonly Session 135 * 136 * @author Andreas Gohr <andi@splitbrain.org> 137 */ 138function js_dynamicout(){ 139 $edit = (bool) $_REQUEST['edit']; // edit or preview mode? 140 $write = (bool) $_REQUEST['write']; // writable? 141 $sig = (bool) $_REQUEST['sig']; // show sig button? 142 if($edit && $write && $sig){ 143 require_once(DOKU_INC.'inc/toolbar.php'); 144 toolbar_addsigbutton('toolbar'); 145 } 146} 147 148/** 149 * Checks if a JavaScript Cache file still is valid 150 * 151 * @author Andreas Gohr <andi@splitbrain.org> 152 */ 153function js_cacheok($cache,$files){ 154 $ctime = @filemtime($cache); 155 if(!$ctime) return false; //There is no cache 156 157 // some additional files to check 158 $files[] = DOKU_CONF.'dokuwiki.conf'; 159 $files[] = DOKU_CONF.'local.conf'; 160 $files[] = DOKU_CONF.'userscript.js'; 161 $files[] = __FILE__; 162 163 // now walk the files 164 foreach($files as $file){ 165 if(@filemtime($file) > $ctime){ 166 return false; 167 } 168 } 169 return true; 170} 171 172/** 173 * Returns a list of possible Plugin Scripts (no existance check here) 174 * 175 * @author Andreas Gohr <andi@splitbrain.org> 176 */ 177function js_pluginscripts(){ 178 $list = array(); 179 $plugins = plugin_list(); 180 foreach ($plugins as $p){ 181 $list[] = DOKU_PLUGIN."$p/script.js"; 182 } 183 return $list; 184} 185 186/** 187 * Escapes a String to be embedded in a JavaScript call, keeps \n 188 * as newline 189 * 190 * @author Andreas Gohr <andi@splitbrain.org> 191 */ 192function js_escape($string){ 193 return str_replace('\\\\n','\\n',addslashes($string)); 194} 195 196/** 197 * Adds the given JavaScript code to the window.onload() event 198 * 199 * @author Andreas Gohr <andi@splitbrain.org> 200 */ 201function js_runonstart($func){ 202 print "addEvent(window,'load',function(){ $func; });"; 203} 204 205/** 206 * Strip comments and whitespaces from given JavaScript Code 207 * 208 * This is a rewrite of Nick Galbreaths python tool jsstrip.py which is 209 * released under BSD license. See link for original code. 210 * 211 * @author Nick Galbreath <nickg@modp.com> 212 * @author Andreas Gohr <andi@splitbrain.org> 213 * @link http://modp.com/release/jsstrip/ 214 */ 215function js_compress($s){ 216 $i = 0; 217 $line = 0; 218 $s .= "\n"; 219 $len = strlen($s); 220 221 // items that don't need spaces next to them 222 $chars = '^&|!+\-*\/%=:;,{}()<>% \t\n\r'; 223 224 ob_start(); 225 while($i < $len){ 226 $ch = $s{$i}; 227 228 // multiline comments 229 if($ch == '/' && $s{$i+1} == '*'){ 230 $endC = strpos($s,'*/',$i+2); 231 if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR); 232 $i = $endC + 2; 233 continue; 234 } 235 236 // singleline 237 if($ch == '/' && $s{$i+1} == '/'){ 238 $endC = strpos($s,"\n",$i+2); 239 if($endC === false) trigger_error('Invalid comment', E_USER_ERROR); 240 $i = $endC; 241 continue; 242 } 243 244 // tricky. might be an RE 245 if($ch == '/'){ 246 // rewind, skip white space 247 $j = 1; 248 while($s{$i-$j} == ' '){ 249 $j = $j + 1; 250 } 251 if( ($s{$i-$j} == '=') || ($s{$i-$j} == '(') ){ 252 // yes, this is an re 253 // now move forward and find the end of it 254 $j = 1; 255 while($s{$i+$j} != '/'){ 256 while( ($s{$i+$j} != '\\') && ($s{$i+$j} != '/')){ 257 $j = $j + 1; 258 } 259 if($s{$i+$j} == '\\') $j = $j + 2; 260 } 261 echo substr($s,$i,$j+1); 262 $i = $i + $j + 1; 263 continue; 264 } 265 } 266 267 // double quote strings 268 if($ch == '"'){ 269 $j = 1; 270 while( $s{$i+$j} != '"' ){ 271 while( ($s{$i+$j} != '\\') && ($s{$i+$j} != '"') ){ 272 $j = $j + 1; 273 } 274 if($s{$i+$j} == '\\') $j = $j + 2; 275 } 276 echo substr($s,$i,$j+1); 277 $i = $i + $j + 1; 278 continue; 279 } 280 281 // single quote strings 282 if($ch == "'"){ 283 $j = 1; 284 while( $s{$i+$j} != "'" ){ 285 while( ($s{$i+$j} != '\\') && ($s{$i+$j} != "'") ){ 286 $j = $j + 1; 287 } 288 if ($s{$i+$j} == '\\') $j = $j + 2; 289 } 290 echo substr($s,$i,$j+1); 291 $i = $i + $j + 1; 292 continue; 293 } 294 295 // newlines 296 if($ch == "\n" || $ch == "\r"){ 297 $i = $i+1; 298 continue; 299 } 300 301 // leading spaces 302 if( ( $ch == ' ' || 303 $ch == "\n" || 304 $ch == "\t" ) && 305 !preg_match('/['.$chars.']/',$s{$i+1}) ){ 306 $i = $i+1; 307 continue; 308 } 309 310 // trailing spaces 311 if( ( $ch == ' ' || 312 $ch == "\n" || 313 $ch == "\t" ) && 314 !preg_match('/['.$chars.']/',$s{$i-1}) ){ 315 $i = $i+1; 316 continue; 317 } 318 319 // other chars 320 echo $ch; 321 $i = $i + 1; 322 } 323 324 325 $out = ob_get_contents(); 326 ob_end_clean(); 327 return $out; 328} 329 330//Setup VIM: ex: et ts=4 enc=utf-8 : 331?> 332