[ Index ]
 

Code source de DokuWiki 2006-11-06

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/exe/ -> js.php (source)

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


Généré le : Tue Apr 3 20:47:31 2007 par Balluche grâce à PHPXref 0.7