xref: /dokuwiki/inc/parserutils.php (revision cde6a01b90b199cd9d42a8e23a61d467992b6d67)
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 {$cache->cache} -->\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
233  // metadata has never been rendered before - do it!
234  if ($render && !$meta['description']['abstract']){
235    $meta = p_render_metadata($id, $meta);
236    io_saveFile(metaFN($id, '.meta'), serialize($meta));
237
238    // sync cached copies, including $INFO metadata
239    if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta;
240    if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; }
241  }
242
243  // filter by $key
244  if ($key){
245    list($key, $subkey) = explode(' ', $key, 2);
246    if (trim($subkey)) return $meta['current'][$key][$subkey];
247
248    return $meta['current'][$key];
249  }
250
251  return $meta['current'];
252}
253
254/**
255 * sets metadata elements of a page
256 *
257 * @author Esther Brunner <esther@kaffeehaus.ch>
258 */
259function p_set_metadata($id, $data, $render=false, $persistent=true){
260  if (!is_array($data)) return false;
261
262  global $ID;
263
264  // cache the current page
265  $cache = ($ID == $id);
266  $orig = p_read_metadata($id, $cache);
267
268  // render metadata first?
269  $meta = $render ? p_render_metadata($id, $orig) : $orig;
270
271  // now add the passed metadata
272  $protected = array('description', 'date', 'contributor');
273  foreach ($data as $key => $value){
274
275    // be careful with sub-arrays of $meta['relation']
276    if ($key == 'relation'){
277
278      foreach ($value as $subkey => $subvalue){
279        $meta['current'][$key][$subkey] = array_merge($meta['current'][$key][$subkey], $subvalue);
280        if ($persistent)
281          $meta['persistent'][$key][$subkey] = array_merge($meta['persistent'][$key][$subkey], $subvalue);
282      }
283
284    // be careful with some senisitive arrays of $meta
285    } elseif (in_array($key, $protected)){
286
287      if (is_array($value)){
288        #FIXME not sure if this is the intended thing:
289        if(!is_array($meta['current'][$key])) $meta['current'][$key] = array($meta['current'][$key]);
290        $meta['current'][$key] = array_merge($meta['current'][$key], $value);
291
292        if ($persistent) {
293          if(!is_array($meta['persistent'][$key])) $meta['persistent'][$key] = array($meta['persistent'][$key]);
294          $meta['persistent'][$key] = array_merge($meta['persistent'][$key], $value);
295        }
296      }
297
298    // no special treatment for the rest
299    } else {
300      $meta['current'][$key] = $value;
301      if ($persistent) $meta['persistent'][$key] = $value;
302    }
303  }
304
305  // save only if metadata changed
306  if ($meta == $orig) return true;
307
308  // sync cached copies, including $INFO metadata
309  global $cache_metadata, $INFO;
310
311  if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta;
312  if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; }
313
314  return io_saveFile(metaFN($id, '.meta'), serialize($meta));
315}
316
317/**
318 * read the metadata from source/cache for $id
319 * (internal use only - called by p_get_metadata & p_set_metadata)
320 *
321 * this function also converts the metadata from the original format to
322 * the current format ('current' & 'persistent' arrays)
323 *
324 * @author   Christopher Smith <chris@jalakai.co.uk>
325 *
326 * @param    string   $id      absolute wiki page id
327 * @param    bool     $cache   whether or not to cache metadata in memory
328 *                             (only use for metadata likely to be accessed several times)
329 *
330 * @return   array             metadata
331 */
332function p_read_metadata($id,$cache=false) {
333  global $cache_metadata;
334
335  if (isset($cache_metadata[$id])) return $cache_metadata[$id];
336
337  $file = metaFN($id, '.meta');
338  $meta = @file_exists($file) ? unserialize(io_readFile($file, false)) : array('current'=>array(),'persistent'=>array());
339
340  // convert $meta from old format to new (current+persistent) format
341  if (!isset($meta['current'])) {
342    $meta = array('current'=>$meta,'persistent'=>$meta);
343
344    // remove non-persistent keys
345    unset($meta['persistent']['title']);
346    unset($meta['persistent']['description']['abstract']);
347    unset($meta['persistent']['description']['tableofcontents']);
348    unset($meta['persistent']['relation']['haspart']);
349    unset($meta['persistent']['relation']['references']);
350    unset($meta['persistent']['date']['valid']);
351
352    if (empty($meta['persistent']['description'])) unset($meta['persistent']['description']);
353    if (empty($meta['persistent']['relation'])) unset($meta['persistent']['relation']);
354    if (empty($meta['persistent']['date'])) unset($meta['persistent']['date']);
355
356    // save converted metadata
357    io_saveFile($file, serialize($meta));
358  }
359
360  if ($cache) {
361    $cache_metadata[$id] = $meta;
362  }
363
364  return $meta;
365}
366
367/**
368 * renders the metadata of a page
369 *
370 * @author Esther Brunner <esther@kaffeehaus.ch>
371 */
372function p_render_metadata($id, $orig){
373
374  // add an extra key for the event - to tell event handlers the page whose metadata this is
375  $orig['page'] = $id;
376  $evt = new Doku_Event('PARSER_METADATA_RENDER', $orig);
377  if ($evt->advise_before()) {
378
379    require_once DOKU_INC."inc/parser/metadata.php";
380
381    // get instructions
382    $instructions = p_cached_instructions(wikiFN($id),false,$id);
383    if(is_null($instructions)) return null; // something went wrong with the instructions
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
517  // try default renderer first:
518  $file = DOKU_INC."inc/parser/$mode.php";
519  if(@file_exists($file)){
520    require_once $file;
521    $rclass = "Doku_Renderer_$mode";
522
523    if ( !class_exists($rclass) ) {
524      trigger_error("Unable to resolve render class $rclass",E_USER_WARNING);
525      msg("Renderer for $mode not valid",-1);
526      return null;
527    }
528    $Renderer = & new $rclass();
529  }else{
530    // Maybe a plugin is available?
531    $Renderer =& plugin_load('renderer',$mode);
532    if(is_null($Renderer)){
533      msg("No renderer for $mode found",-1);
534      return null;
535    }
536  }
537
538  $Renderer->smileys = getSmileys();
539  $Renderer->entities = getEntities();
540  $Renderer->acronyms = getAcronyms();
541  $Renderer->interwiki = getInterwiki();
542  #$Renderer->badwords = getBadWords();
543
544  // Loop through the instructions
545  foreach ( $instructions as $instruction ) {
546      // Execute the callback against the Renderer
547      call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
548  }
549
550  //set info array
551  $info = $Renderer->info;
552
553  // Post process and return the output
554  $data = array($mode,& $Renderer->doc);
555  trigger_event('RENDERER_CONTENT_POSTPROCESS',$data);
556  return $Renderer->doc;
557}
558
559/**
560 * Gets the first heading from a file
561 *
562 * @param   string   $id       dokuwiki page id
563 * @param   bool     $render   rerender if first heading not known
564 *                             default: false  -- this protects against loops where $id requires a
565 *                                                first heading further pages which eventually result
566 *                                                in a request for a first heading from a page already
567 *                                                in the chain (FS#1010)
568 *
569 *
570 * @author Andreas Gohr <andi@splitbrain.org>
571 */
572function p_get_first_heading($id, $render=false){
573  global $conf;
574  return $conf['useheading'] ? p_get_metadata($id,'title',$render) : null;
575}
576
577/**
578 * Wrapper for GeSHi Code Highlighter, provides caching of its output
579 *
580 * @author Christopher Smith <chris@jalakai.co.uk>
581 */
582function p_xhtml_cached_geshi($code, $language) {
583  $cache = getCacheName($language.$code,".code");
584
585  if (@file_exists($cache) && !$_REQUEST['purge'] &&
586     (filemtime($cache) > filemtime(DOKU_INC . 'inc/geshi.php'))) {
587
588    $highlighted_code = io_readFile($cache, false);
589    @touch($cache);
590
591  } else {
592
593    require_once(DOKU_INC . 'inc/geshi.php');
594
595    $geshi = new GeSHi($code, strtolower($language), DOKU_INC . 'inc/geshi');
596    $geshi->set_encoding('utf-8');
597    $geshi->enable_classes();
598    $geshi->set_header_type(GESHI_HEADER_PRE);
599    $geshi->set_overall_class("code $language");
600    $geshi->set_link_target($conf['target']['extern']);
601
602    $highlighted_code = $geshi->parse_code();
603
604    io_saveFile($cache,$highlighted_code);
605  }
606
607  return $highlighted_code;
608}
609
610//Setup VIM: ex: et ts=2 enc=utf-8 :
611