xref: /dokuwiki/inc/parserutils.php (revision 5aa52fafe8be8e728c0d2c9ff12c999e80766127)
1<?php
2/**
3 * Utilities for collecting data from config files
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Harry Fuecks <hfuecks@gmail.com>
7 * @author     Andreas Gohr <andi@splitbrain.org>
8 */
9
10  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
11
12  require_once(DOKU_INC.'inc/confutils.php');
13  require_once(DOKU_INC.'inc/pageutils.php');
14  require_once(DOKU_INC.'inc/pluginutils.php');
15  require_once(DOKU_INC.'inc/cache.php');
16
17/**
18 * Returns the parsed Wikitext in XHTML for the given id and revision.
19 *
20 * If $excuse is true an explanation is returned if the file
21 * wasn't found
22 *
23 * @author Andreas Gohr <andi@splitbrain.org>
24 */
25function p_wiki_xhtml($id, $rev='', $excuse=true){
26  $file = wikiFN($id,$rev);
27  $ret  = '';
28
29  //ensure $id is in global $ID (needed for parsing)
30  global $ID;
31  $keep = $ID;
32  $ID   = $id;
33
34  if($rev){
35    if(@file_exists($file)){
36      $ret = p_render('xhtml',p_get_instructions(io_readfile($file)),$info); //no caching on old revisions
37    }elseif($excuse){
38      $ret = p_locale_xhtml('norev');
39    }
40  }else{
41    if(@file_exists($file)){
42      $ret = p_cached_output($file,'xhtml',$id);
43    }elseif($excuse){
44      $ret = p_locale_xhtml('newpage');
45    }
46  }
47
48  //restore ID (just in case)
49  $ID = $keep;
50
51  return $ret;
52}
53
54/**
55 * Returns starting summary for a page (e.g. the first few
56 * paragraphs), marked up in XHTML.
57 *
58 * If $excuse is true an explanation is returned if the file
59 * wasn't found
60 *
61 * @param string wiki page id
62 * @param reference populated with page title from heading or page id
63 * @deprecated
64 * @author Harry Fuecks <hfuecks@gmail.com>
65 */
66function p_wiki_xhtml_summary($id, &$title, $rev='', $excuse=true){
67  $file = wikiFN($id,$rev);
68  $ret  = '';
69
70  //ensure $id is in global $ID (needed for parsing)
71  global $ID;
72  $keep = $ID;
73  $ID   = $id;
74
75  if($rev){
76    if(@file_exists($file)){
77      //no caching on old revisions
78      $ins = p_get_instructions(io_readfile($file));
79    }elseif($excuse){
80      $ret = p_locale_xhtml('norev');
81      //restore ID (just in case)
82      $ID = $keep;
83      return $ret;
84    }
85
86  }else{
87
88    if(@file_exists($file)){
89      // The XHTML for a summary is not cached so use the instruction cache
90      $ins = p_cached_instructions($file);
91    }elseif($excuse){
92      $ret = p_locale_xhtml('newpage');
93      //restore ID (just in case)
94      $ID = $keep;
95      return $ret;
96    }
97  }
98
99  $ret = p_render('xhtmlsummary',$ins,$info);
100
101  if ( $info['sum_pagetitle'] ) {
102    $title = $info['sum_pagetitle'];
103  } else {
104    $title = $id;
105  }
106
107  $ID = $keep;
108  return $ret;
109}
110
111/**
112 * Returns the specified local text in parsed format
113 *
114 * @author Andreas Gohr <andi@splitbrain.org>
115 */
116function p_locale_xhtml($id){
117  //fetch parsed locale
118  $html = p_cached_output(localeFN($id));
119  return $html;
120}
121
122/**
123 *     *** DEPRECATED ***
124 *
125 * use p_cached_output()
126 *
127 * Returns the given file parsed to XHTML
128 *
129 * Uses and creates a cachefile
130 *
131 * @deprecated
132 * @author Andreas Gohr <andi@splitbrain.org>
133 * @todo   rewrite to use mode instead of hardcoded XHTML
134 */
135function p_cached_xhtml($file){
136  return p_cached_output($file);
137}
138
139/**
140 * Returns the given file parsed into the requested output format
141 *
142 * @author Andreas Gohr <andi@splitbrain.org>
143 * @author Chris Smith <chris@jalakai.co.uk>
144 */
145function p_cached_output($file, $format='xhtml', $id='') {
146  global $conf;
147
148  $cache = new cache_renderer($id, $file, $format);
149  if ($cache->useCache()) {
150    $parsed = $cache->retrieveCache();
151    if($conf['allowdebug']) $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n";
152  } else {
153    $parsed = p_render($format, p_cached_instructions($file,false,$id), $info);
154
155    if ($info['cache']) {
156      $cache->storeCache($parsed);               //save cachefile
157      if($conf['allowdebug']) $parsed .= "\n<!-- no cachefile used, but created -->\n";
158    }else{
159      $cache->removeCache();                     //try to delete cachefile
160      if($conf['allowdebug']) $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
161    }
162  }
163
164  return $parsed;
165}
166
167/**
168 * Returns the render instructions for a file
169 *
170 * Uses and creates a serialized cache file
171 *
172 * @author Andreas Gohr <andi@splitbrain.org>
173 */
174function p_cached_instructions($file,$cacheonly=false,$id='') {
175  global $conf;
176
177  $cache = new cache_instructions($id, $file);
178
179  if ($cacheonly || $cache->useCache()) {
180    return $cache->retrieveCache();
181  } else if (@file_exists($file)) {
182    // no cache - do some work
183    $ins = p_get_instructions(io_readfile($file));
184    $cache->storeCache($ins);
185    return $ins;
186  }
187
188  return NULL;
189}
190
191/**
192 * turns a page into a list of instructions
193 *
194 * @author Harry Fuecks <hfuecks@gmail.com>
195 * @author Andreas Gohr <andi@splitbrain.org>
196 */
197function p_get_instructions($text){
198
199  $modes = p_get_parsermodes();
200
201  // Create the parser
202  $Parser = & new Doku_Parser();
203
204  // Add the Handler
205  $Parser->Handler = & new Doku_Handler();
206
207  //add modes to parser
208  foreach($modes as $mode){
209    $Parser->addMode($mode['mode'],$mode['obj']);
210  }
211
212  // Do the parsing
213  trigger_event('PARSER_WIKITEXT_PREPROCESS', $text);
214  $p = $Parser->parse($text);
215//  dbg($p);
216  return $p;
217}
218
219/**
220 * returns the metadata of a page
221 *
222 * @author Esther Brunner <esther@kaffeehaus.ch>
223 */
224function p_get_metadata($id, $key=false, $render=false){
225  global $ID, $INFO, $cache_metadata;
226
227  // cache the current page
228  // Benchmarking shows the current page's metadata is generally the only page metadata
229  // accessed several times. This may catch a few other pages, but that shouldn't be an issue.
230  $cache = ($ID == $id);
231  $meta = p_read_metadata($id, $cache);
232  $file = metaFN($id, '.meta');
233
234  // metadata has never been rendered before - do it!
235  if ((!file_exists($file) && file_exists(wikiFN($id))) || ($render && !$meta['description']['abstract'])){
236    $meta = p_render_metadata($id, $meta);
237    io_saveFile($file, serialize($meta));
238
239    // sync cached copies, including $INFO metadata
240	  if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta;
241    if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; }
242  }
243
244  // filter by $key
245  if ($key){
246    list($key, $subkey) = explode(' ', $key, 2);
247    if (trim($subkey)) return $meta['current'][$key][$subkey];
248
249    return $meta['current'][$key];
250  }
251
252  return $meta['current'];
253}
254
255/**
256 * sets metadata elements of a page
257 *
258 * @author Esther Brunner <esther@kaffeehaus.ch>
259 */
260function p_set_metadata($id, $data, $render=false, $persistent=true){
261  if (!is_array($data)) return false;
262
263  global $ID;
264
265  // cache the current page
266  $cache = ($ID == $id);
267  $orig = p_read_metadata($id, $cache);
268
269  // render metadata first?
270  $meta = $render ? p_render_metadata($id, $orig) : $orig;
271
272  // now add the passed metadata
273  $protected = array('description', 'date', 'contributor');
274  foreach ($data as $key => $value){
275
276    // be careful with sub-arrays of $meta['relation']
277    if ($key == 'relation'){
278
279      foreach ($value as $subkey => $subvalue){
280        $meta['current'][$key][$subkey] = array_merge($meta['current'][$key][$subkey], $subvalue);
281        if ($persistent)
282          $meta['persistent'][$key][$subkey] = array_merge($meta['persistent'][$key][$subkey], $subvalue);
283      }
284
285    // be careful with some senisitive arrays of $meta
286    } elseif (in_array($key, $protected)){
287
288      if (is_array($value)){
289        #FIXME not sure if this is the intended thing:
290        if(!is_array($meta['current'][$key])) $meta['current'][$key] = array($meta['current'][$key]);
291        $meta['current'][$key] = array_merge($meta['current'][$key], $value);
292
293        if ($persistent) {
294          if(!is_array($meta['persistent'][$key])) $meta['persistent'][$key] = array($meta['persistent'][$key]);
295          $meta['persistent'][$key] = array_merge($meta['persistent'][$key], $value);
296        }
297      }
298
299    // no special treatment for the rest
300    } else {
301      $meta['current'][$key] = $value;
302      if ($persistent) $meta['persistent'][$key] = $value;
303    }
304  }
305
306  // save only if metadata changed
307  if ($meta == $orig) return true;
308
309  // sync cached copies, including $INFO metadata
310  global $cache_metadata, $INFO;
311
312  if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta;
313  if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; }
314
315  return io_saveFile(metaFN($id, '.meta'), serialize($meta));
316}
317
318/**
319 * read the metadata from source/cache for $id
320 * (internal use only - called by p_get_metadata & p_set_metadata)
321 *
322 * this function also converts the metadata from the original format to
323 * the current format ('current' & 'persistent' arrays)
324 *
325 * @author   Christopher Smith <chris@jalakai.co.uk>
326 *
327 * @param    string   $id      absolute wiki page id
328 * @param    bool     $cache   whether or not to cache metadata in memory
329 *                             (only use for metadata likely to be accessed several times)
330 *
331 * @return   array             metadata
332 */
333function p_read_metadata($id,$cache=false) {
334  global $cache_metadata;
335
336  if (isset($cache_metadata[$id])) return $cache_metadata[$id];
337
338  $file = metaFN($id, '.meta');
339  $meta = @file_exists($file) ? unserialize(io_readFile($file, false)) : array('current'=>array(),'persistent'=>array());
340
341  // convert $meta from old format to new (current+persistent) format
342  if (!isset($meta['current'])) {
343    $meta = array('current'=>$meta,'persistent'=>$meta);
344
345    // remove non-persistent keys
346    unset($meta['persistent']['title']);
347    unset($meta['persistent']['description']['abstract']);
348    unset($meta['persistent']['description']['tableofcontents']);
349    unset($meta['persistent']['relation']['haspart']);
350    unset($meta['persistent']['relation']['references']);
351    unset($meta['persistent']['date']['valid']);
352
353    if (empty($meta['persistent']['description'])) unset($meta['persistent']['description']);
354    if (empty($meta['persistent']['relation'])) unset($meta['persistent']['relation']);
355    if (empty($meta['persistent']['date'])) unset($meta['persistent']['date']);
356
357    // save converted metadata
358    io_saveFile($file, serialize($meta));
359  }
360
361  if ($cache) {
362    $cache_metadata[$id] = $meta;
363  }
364
365  return $meta;
366}
367
368/**
369 * renders the metadata of a page
370 *
371 * @author Esther Brunner <esther@kaffeehaus.ch>
372 */
373function p_render_metadata($id, $orig){
374
375  // add an extra key for the event - to tell event handlers the page whose metadata this is
376	$orig['page'] = $id;
377  $evt = new Doku_Event('PARSER_METADATA_RENDER', $orig);
378  if ($evt->advise_before()) {
379
380    require_once DOKU_INC."inc/parser/metadata.php";
381
382    // get instructions
383    $instructions = p_cached_instructions(wikiFN($id),false,$id);
384
385    // set up the renderer
386    $renderer = & new Doku_Renderer_metadata();
387    $renderer->meta = $orig['current'];
388    $renderer->persistent = $orig['persistent'];
389
390    // loop through the instructions
391    foreach ($instructions as $instruction){
392      // execute the callback against the renderer
393      call_user_func_array(array(&$renderer, $instruction[0]), $instruction[1]);
394    }
395
396    $evt->result = array('current'=>$renderer->meta,'persistent'=>$renderer->persistent);
397  }
398  $evt->advise_after();
399
400  return $evt->result;
401}
402
403/**
404 * returns all available parser syntax modes in correct order
405 *
406 * @author Andreas Gohr <andi@splitbrain.org>
407 */
408function p_get_parsermodes(){
409  global $conf;
410
411  //reuse old data
412  static $modes = null;
413  if($modes != null){
414    return $modes;
415  }
416
417  //import parser classes and mode definitions
418  require_once DOKU_INC . 'inc/parser/parser.php';
419
420  // we now collect all syntax modes and their objects, then they will
421  // be sorted and added to the parser in correct order
422  $modes = array();
423
424  // add syntax plugins
425  $pluginlist = plugin_list('syntax');
426  if(count($pluginlist)){
427    global $PARSER_MODES;
428    $obj = null;
429    foreach($pluginlist as $p){
430      if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj
431      $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
432      //add to modes
433      $modes[] = array(
434                   'sort' => $obj->getSort(),
435                   'mode' => "plugin_$p",
436                   'obj'  => $obj,
437                 );
438      unset($obj); //remove the reference
439    }
440  }
441
442  // add default modes
443  $std_modes = array('listblock','preformatted','notoc','nocache',
444                     'header','table','linebreak','footnote','hr',
445                     'unformatted','php','html','code','file','quote',
446                     'internallink','rss','media','externallink',
447                     'emaillink','windowssharelink','eol');
448  if($conf['typography']){
449    $std_modes[] = 'quotes';
450    $std_modes[] = 'multiplyentity';
451  }
452  foreach($std_modes as $m){
453    $class = "Doku_Parser_Mode_$m";
454    $obj   = new $class();
455    $modes[] = array(
456                 'sort' => $obj->getSort(),
457                 'mode' => $m,
458                 'obj'  => $obj
459               );
460  }
461
462  // add formatting modes
463  $fmt_modes = array('strong','emphasis','underline','monospace',
464                     'subscript','superscript','deleted');
465  foreach($fmt_modes as $m){
466    $obj   = new Doku_Parser_Mode_formatting($m);
467    $modes[] = array(
468                 'sort' => $obj->getSort(),
469                 'mode' => $m,
470                 'obj'  => $obj
471               );
472  }
473
474  // add modes which need files
475  $obj     = new Doku_Parser_Mode_smiley(array_keys(getSmileys()));
476  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj'  => $obj );
477  $obj     = new Doku_Parser_Mode_acronym(array_keys(getAcronyms()));
478  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj'  => $obj );
479  $obj     = new Doku_Parser_Mode_entity(array_keys(getEntities()));
480  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj'  => $obj );
481
482
483  // add optional camelcase mode
484  if($conf['camelcase']){
485    $obj     = new Doku_Parser_Mode_camelcaselink();
486    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj'  => $obj );
487  }
488
489  //sort modes
490  usort($modes,'p_sort_modes');
491
492  return $modes;
493}
494
495/**
496 * Callback function for usort
497 *
498 * @author Andreas Gohr <andi@splitbrain.org>
499 */
500function p_sort_modes($a, $b){
501  if($a['sort'] == $b['sort']) return 0;
502  return ($a['sort'] < $b['sort']) ? -1 : 1;
503}
504
505/**
506 * Renders a list of instruction to the specified output mode
507 *
508 * In the $info array are informations from the renderer returned
509 *
510 * @author Harry Fuecks <hfuecks@gmail.com>
511 * @author Andreas Gohr <andi@splitbrain.org>
512 */
513function p_render($mode,$instructions,& $info){
514  if(is_null($instructions)) return '';
515
516  // Create the renderer
517  $file = DOKU_INC.'lib/plugins/'.$mode.'/renderer.php';
518  if(!@file_exists($file)){
519    $file = DOKU_INC."inc/parser/$mode.php";
520  }
521  if(!@file_exists($file)){
522    msg("No renderer for $mode found",-1);
523    return null;
524  }
525
526  require_once $file;
527
528  $rclass = "Doku_Renderer_$mode";
529  if ( !class_exists($rclass) ) {
530    trigger_error("Unable to resolve render class $rclass",E_USER_WARNING);
531    msg("Renderer for $mode not valid",-1);
532    return null;
533  }
534  $Renderer = & new $rclass(); #FIXME any way to check for class existance?
535
536  $Renderer->smileys = getSmileys();
537  $Renderer->entities = getEntities();
538  $Renderer->acronyms = getAcronyms();
539  $Renderer->interwiki = getInterwiki();
540  #$Renderer->badwords = getBadWords();
541
542  // Loop through the instructions
543  foreach ( $instructions as $instruction ) {
544      // Execute the callback against the Renderer
545      call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
546  }
547
548  //set info array
549  $info = $Renderer->info;
550
551  // Post process and return the output
552  $data = array($mode,& $Renderer->doc);
553  trigger_event('RENDERER_CONTENT_POSTPROCESS',$data);
554  return $Renderer->doc;
555}
556
557/**
558 * Gets the first heading from a file
559 *
560 * @author Andreas Gohr <andi@splitbrain.org>
561 */
562function p_get_first_heading($id){
563  global $conf;
564  return $conf['useheading'] ? p_get_metadata($id,'title') : null;
565}
566
567/**
568 * Wrapper for GeSHi Code Highlighter, provides caching of its output
569 *
570 * @author Christopher Smith <chris@jalakai.co.uk>
571 */
572function p_xhtml_cached_geshi($code, $language) {
573  $cache = getCacheName($language.$code,".code");
574
575  if (@file_exists($cache) && !$_REQUEST['purge'] &&
576     (filemtime($cache) > filemtime(DOKU_INC . 'inc/geshi.php'))) {
577
578    $highlighted_code = io_readFile($cache, false);
579    @touch($cache);
580
581  } else {
582
583    require_once(DOKU_INC . 'inc/geshi.php');
584
585    $geshi = new GeSHi($code, strtolower($language), DOKU_INC . 'inc/geshi');
586    $geshi->set_encoding('utf-8');
587    $geshi->enable_classes();
588    $geshi->set_header_type(GESHI_HEADER_PRE);
589    $geshi->set_overall_class("code $language");
590    $geshi->set_link_target($conf['target']['extern']);
591
592    $highlighted_code = $geshi->parse_code();
593
594    io_saveFile($cache,$highlighted_code);
595  }
596
597  return $highlighted_code;
598}
599
600//Setup VIM: ex: et ts=2 enc=utf-8 :
601