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