reset scroll position when setting selection FS#1707
[dokuwiki/radio.git] / inc / parserutils.php
blobe1f9922f0d8320ee3b7e93ace11ac2e7735d7e8e
1 <?php
2 /**
3 * Utilities for accessing the parser
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 */
10 if(!defined('DOKU_INC')) die('meh.');
11 require_once(DOKU_INC.'inc/confutils.php');
12 require_once(DOKU_INC.'inc/pageutils.php');
13 require_once(DOKU_INC.'inc/pluginutils.php');
14 require_once(DOKU_INC.'inc/cache.php');
16 /**
17 * Returns the parsed Wikitext in XHTML for the given id and revision.
19 * If $excuse is true an explanation is returned if the file
20 * wasn't found
22 * @author Andreas Gohr <andi@splitbrain.org>
24 function p_wiki_xhtml($id, $rev='', $excuse=true){
25 $file = wikiFN($id,$rev);
26 $ret = '';
28 //ensure $id is in global $ID (needed for parsing)
29 global $ID;
30 $keep = $ID;
31 $ID = $id;
33 if($rev){
34 if(@file_exists($file)){
35 $ret = p_render('xhtml',p_get_instructions(io_readWikiPage($file,$id,$rev)),$info); //no caching on old revisions
36 }elseif($excuse){
37 $ret = p_locale_xhtml('norev');
39 }else{
40 if(@file_exists($file)){
41 $ret = p_cached_output($file,'xhtml',$id);
42 }elseif($excuse){
43 $ret = p_locale_xhtml('newpage');
47 //restore ID (just in case)
48 $ID = $keep;
50 return $ret;
53 /**
54 * Returns starting summary for a page (e.g. the first few
55 * paragraphs), marked up in XHTML.
57 * If $excuse is true an explanation is returned if the file
58 * wasn't found
60 * @param string wiki page id
61 * @param reference populated with page title from heading or page id
62 * @deprecated
63 * @author Harry Fuecks <hfuecks@gmail.com>
65 function p_wiki_xhtml_summary($id, &$title, $rev='', $excuse=true){
66 $file = wikiFN($id,$rev);
67 $ret = '';
69 //ensure $id is in global $ID (needed for parsing)
70 global $ID;
71 $keep = $ID;
72 $ID = $id;
74 if($rev){
75 if(@file_exists($file)){
76 //no caching on old revisions
77 $ins = p_get_instructions(io_readWikiPage($file,$id,$rev));
78 }elseif($excuse){
79 $ret = p_locale_xhtml('norev');
80 //restore ID (just in case)
81 $ID = $keep;
82 return $ret;
85 }else{
87 if(@file_exists($file)){
88 // The XHTML for a summary is not cached so use the instruction cache
89 $ins = p_cached_instructions($file);
90 }elseif($excuse){
91 $ret = p_locale_xhtml('newpage');
92 //restore ID (just in case)
93 $ID = $keep;
94 return $ret;
98 $ret = p_render('xhtmlsummary',$ins,$info);
100 if ( $info['sum_pagetitle'] ) {
101 $title = $info['sum_pagetitle'];
102 } else {
103 $title = $id;
106 $ID = $keep;
107 return $ret;
111 * Returns the specified local text in parsed format
113 * @author Andreas Gohr <andi@splitbrain.org>
115 function p_locale_xhtml($id){
116 //fetch parsed locale
117 $html = p_cached_output(localeFN($id));
118 return $html;
122 * *** DEPRECATED ***
124 * use p_cached_output()
126 * Returns the given file parsed to XHTML
128 * Uses and creates a cachefile
130 * @deprecated
131 * @author Andreas Gohr <andi@splitbrain.org>
132 * @todo rewrite to use mode instead of hardcoded XHTML
134 function p_cached_xhtml($file){
135 return p_cached_output($file);
139 * Returns the given file parsed into the requested output format
141 * @author Andreas Gohr <andi@splitbrain.org>
142 * @author Chris Smith <chris@jalakai.co.uk>
144 function p_cached_output($file, $format='xhtml', $id='') {
145 global $conf;
147 $cache = new cache_renderer($id, $file, $format);
148 if ($cache->useCache()) {
149 $parsed = $cache->retrieveCache(false);
150 if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n";
151 } else {
152 $parsed = p_render($format, p_cached_instructions($file,false,$id), $info);
154 if ($info['cache']) {
155 $cache->storeCache($parsed); //save cachefile
156 if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n";
157 }else{
158 $cache->removeCache(); //try to delete cachefile
159 if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
163 return $parsed;
167 * Returns the render instructions for a file
169 * Uses and creates a serialized cache file
171 * @author Andreas Gohr <andi@splitbrain.org>
173 function p_cached_instructions($file,$cacheonly=false,$id='') {
174 global $conf;
175 static $run = null;
176 if(is_null($run)) $run = array();
178 $cache = new cache_instructions($id, $file);
180 if ($cacheonly || $cache->useCache() || isset($run[$file])) {
181 return $cache->retrieveCache();
182 } else if (@file_exists($file)) {
183 // no cache - do some work
184 $ins = p_get_instructions(io_readWikiPage($file,$id));
185 if ($cache->storeCache($ins)) {
186 $run[$file] = true; // we won't rebuild these instructions in the same run again
187 } else {
188 msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.',-1);
190 return $ins;
193 return null;
197 * turns a page into a list of instructions
199 * @author Harry Fuecks <hfuecks@gmail.com>
200 * @author Andreas Gohr <andi@splitbrain.org>
202 function p_get_instructions($text){
204 $modes = p_get_parsermodes();
206 // Create the parser
207 $Parser = & new Doku_Parser();
209 // Add the Handler
210 $Parser->Handler = & new Doku_Handler();
212 //add modes to parser
213 foreach($modes as $mode){
214 $Parser->addMode($mode['mode'],$mode['obj']);
217 // Do the parsing
218 trigger_event('PARSER_WIKITEXT_PREPROCESS', $text);
219 $p = $Parser->parse($text);
220 // dbg($p);
221 return $p;
225 * returns the metadata of a page
227 * @author Esther Brunner <esther@kaffeehaus.ch>
229 function p_get_metadata($id, $key=false, $render=false){
230 global $ID, $INFO, $cache_metadata;
232 // cache the current page
233 // Benchmarking shows the current page's metadata is generally the only page metadata
234 // accessed several times. This may catch a few other pages, but that shouldn't be an issue.
235 $cache = ($ID == $id);
236 $meta = p_read_metadata($id, $cache);
238 // metadata has never been rendered before - do it! (but not for non-existent pages)
239 if ($render && !$meta['current']['description']['abstract'] && page_exists($id)){
240 $meta = p_render_metadata($id, $meta);
241 io_saveFile(metaFN($id, '.meta'), serialize($meta));
243 // sync cached copies, including $INFO metadata
244 if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta;
245 if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; }
248 // filter by $key
249 if ($key){
250 list($key, $subkey) = explode(' ', $key, 2);
251 $subkey = trim($subkey);
253 if ($subkey) {
254 return isset($meta['current'][$key][$subkey]) ? $meta['current'][$key][$subkey] : null;
257 return isset($meta['current'][$key]) ? $meta['current'][$key] : null;
260 return $meta['current'];
264 * sets metadata elements of a page
266 * @author Esther Brunner <esther@kaffeehaus.ch>
268 function p_set_metadata($id, $data, $render=false, $persistent=true){
269 if (!is_array($data)) return false;
271 global $ID;
273 // cache the current page
274 $cache = ($ID == $id);
275 $orig = p_read_metadata($id, $cache);
277 // render metadata first?
278 $meta = $render ? p_render_metadata($id, $orig) : $orig;
280 // now add the passed metadata
281 $protected = array('description', 'date', 'contributor');
282 foreach ($data as $key => $value){
284 // be careful with sub-arrays of $meta['relation']
285 if ($key == 'relation'){
287 foreach ($value as $subkey => $subvalue){
288 $meta['current'][$key][$subkey] = !empty($meta['current'][$key][$subkey]) ? array_merge($meta['current'][$key][$subkey], $subvalue) : $subvalue;
289 if ($persistent)
290 $meta['persistent'][$key][$subkey] = !empty($meta['persistent'][$key][$subkey]) ? array_merge($meta['persistent'][$key][$subkey], $subvalue) : $subvalue;
293 // be careful with some senisitive arrays of $meta
294 } elseif (in_array($key, $protected)){
296 // these keys, must have subkeys - a legitimate value must be an array
297 if (is_array($value)) {
298 $meta['current'][$key] = !empty($meta['current'][$key]) ? array_merge($meta['current'][$key],$value) : $value;
300 if ($persistent) {
301 $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ? array_merge($meta['persistent'][$key],$value) : $value;
305 // no special treatment for the rest
306 } else {
307 $meta['current'][$key] = $value;
308 if ($persistent) $meta['persistent'][$key] = $value;
312 // save only if metadata changed
313 if ($meta == $orig) return true;
315 // sync cached copies, including $INFO metadata
316 global $cache_metadata, $INFO;
318 if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta;
319 if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; }
321 return io_saveFile(metaFN($id, '.meta'), serialize($meta));
325 * Purges the non-persistant part of the meta data
326 * used on page deletion
328 * @author Michael Klier <chi@chimeric.de>
330 function p_purge_metadata($id) {
331 $metafn = metaFN('id', '.meta');
332 $meta = p_read_metadata($id);
333 foreach($meta['current'] as $key => $value) {
334 if(is_array($meta[$key])) {
335 $meta['current'][$key] = array();
336 } else {
337 $meta['current'][$key] = '';
340 return io_saveFile(metaFN($id, '.meta'), serialize($meta));
344 * read the metadata from source/cache for $id
345 * (internal use only - called by p_get_metadata & p_set_metadata)
347 * this function also converts the metadata from the original format to
348 * the current format ('current' & 'persistent' arrays)
350 * @author Christopher Smith <chris@jalakai.co.uk>
352 * @param string $id absolute wiki page id
353 * @param bool $cache whether or not to cache metadata in memory
354 * (only use for metadata likely to be accessed several times)
356 * @return array metadata
358 function p_read_metadata($id,$cache=false) {
359 global $cache_metadata;
361 if (isset($cache_metadata[(string)$id])) return $cache_metadata[(string)$id];
363 $file = metaFN($id, '.meta');
364 $meta = @file_exists($file) ? unserialize(io_readFile($file, false)) : array('current'=>array(),'persistent'=>array());
366 // convert $meta from old format to new (current+persistent) format
367 if (!isset($meta['current'])) {
368 $meta = array('current'=>$meta,'persistent'=>$meta);
370 // remove non-persistent keys
371 unset($meta['persistent']['title']);
372 unset($meta['persistent']['description']['abstract']);
373 unset($meta['persistent']['description']['tableofcontents']);
374 unset($meta['persistent']['relation']['haspart']);
375 unset($meta['persistent']['relation']['references']);
376 unset($meta['persistent']['date']['valid']);
378 if (empty($meta['persistent']['description'])) unset($meta['persistent']['description']);
379 if (empty($meta['persistent']['relation'])) unset($meta['persistent']['relation']);
380 if (empty($meta['persistent']['date'])) unset($meta['persistent']['date']);
382 // save converted metadata
383 io_saveFile($file, serialize($meta));
386 if ($cache) {
387 $cache_metadata[(string)$id] = $meta;
390 return $meta;
394 * renders the metadata of a page
396 * @author Esther Brunner <esther@kaffeehaus.ch>
398 function p_render_metadata($id, $orig){
399 // make sure the correct ID is in global ID
400 global $ID;
401 $keep = $ID;
402 $ID = $id;
405 // add an extra key for the event - to tell event handlers the page whose metadata this is
406 $orig['page'] = $id;
407 $evt = new Doku_Event('PARSER_METADATA_RENDER', $orig);
408 if ($evt->advise_before()) {
410 require_once DOKU_INC."inc/parser/metadata.php";
412 // get instructions
413 $instructions = p_cached_instructions(wikiFN($id),false,$id);
414 if(is_null($instructions)){
415 $ID = $keep;
416 return null; // something went wrong with the instructions
419 // set up the renderer
420 $renderer = & new Doku_Renderer_metadata();
421 $renderer->meta = $orig['current'];
422 $renderer->persistent = $orig['persistent'];
424 // loop through the instructions
425 foreach ($instructions as $instruction){
426 // execute the callback against the renderer
427 call_user_func_array(array(&$renderer, $instruction[0]), $instruction[1]);
430 $evt->result = array('current'=>$renderer->meta,'persistent'=>$renderer->persistent);
432 $evt->advise_after();
434 $ID = $keep;
435 return $evt->result;
439 * returns all available parser syntax modes in correct order
441 * @author Andreas Gohr <andi@splitbrain.org>
443 function p_get_parsermodes(){
444 global $conf;
446 //reuse old data
447 static $modes = null;
448 if($modes != null){
449 return $modes;
452 //import parser classes and mode definitions
453 require_once DOKU_INC . 'inc/parser/parser.php';
455 // we now collect all syntax modes and their objects, then they will
456 // be sorted and added to the parser in correct order
457 $modes = array();
459 // add syntax plugins
460 $pluginlist = plugin_list('syntax');
461 if(count($pluginlist)){
462 global $PARSER_MODES;
463 $obj = null;
464 foreach($pluginlist as $p){
465 if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj
466 $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
467 //add to modes
468 $modes[] = array(
469 'sort' => $obj->getSort(),
470 'mode' => "plugin_$p",
471 'obj' => $obj,
473 unset($obj); //remove the reference
477 // add default modes
478 $std_modes = array('listblock','preformatted','notoc','nocache',
479 'header','table','linebreak','footnote','hr',
480 'unformatted','php','html','code','file','quote',
481 'internallink','rss','media','externallink',
482 'emaillink','windowssharelink','eol');
483 if($conf['typography']){
484 $std_modes[] = 'quotes';
485 $std_modes[] = 'multiplyentity';
487 foreach($std_modes as $m){
488 $class = "Doku_Parser_Mode_$m";
489 $obj = new $class();
490 $modes[] = array(
491 'sort' => $obj->getSort(),
492 'mode' => $m,
493 'obj' => $obj
497 // add formatting modes
498 $fmt_modes = array('strong','emphasis','underline','monospace',
499 'subscript','superscript','deleted');
500 foreach($fmt_modes as $m){
501 $obj = new Doku_Parser_Mode_formatting($m);
502 $modes[] = array(
503 'sort' => $obj->getSort(),
504 'mode' => $m,
505 'obj' => $obj
509 // add modes which need files
510 $obj = new Doku_Parser_Mode_smiley(array_keys(getSmileys()));
511 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj' => $obj );
512 $obj = new Doku_Parser_Mode_acronym(array_keys(getAcronyms()));
513 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj' => $obj );
514 $obj = new Doku_Parser_Mode_entity(array_keys(getEntities()));
515 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj' => $obj );
518 // add optional camelcase mode
519 if($conf['camelcase']){
520 $obj = new Doku_Parser_Mode_camelcaselink();
521 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj' => $obj );
524 //sort modes
525 usort($modes,'p_sort_modes');
527 return $modes;
531 * Callback function for usort
533 * @author Andreas Gohr <andi@splitbrain.org>
535 function p_sort_modes($a, $b){
536 if($a['sort'] == $b['sort']) return 0;
537 return ($a['sort'] < $b['sort']) ? -1 : 1;
541 * Renders a list of instruction to the specified output mode
543 * In the $info array are informations from the renderer returned
545 * @author Harry Fuecks <hfuecks@gmail.com>
546 * @author Andreas Gohr <andi@splitbrain.org>
548 function p_render($mode,$instructions,&$info){
549 if(is_null($instructions)) return '';
551 $Renderer =& p_get_renderer($mode);
552 if (is_null($Renderer)) return null;
554 $Renderer->reset();
556 $Renderer->smileys = getSmileys();
557 $Renderer->entities = getEntities();
558 $Renderer->acronyms = getAcronyms();
559 $Renderer->interwiki = getInterwiki();
560 #$Renderer->badwords = getBadWords();
562 // Loop through the instructions
563 foreach ( $instructions as $instruction ) {
564 // Execute the callback against the Renderer
565 call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
568 //set info array
569 $info = $Renderer->info;
571 // Post process and return the output
572 $data = array($mode,& $Renderer->doc);
573 trigger_event('RENDERER_CONTENT_POSTPROCESS',$data);
574 return $Renderer->doc;
577 function & p_get_renderer($mode) {
578 global $conf;
580 $rname = !empty($conf['renderer_'.$mode]) ? $conf['renderer_'.$mode] : $mode;
582 // try default renderer first:
583 $file = DOKU_INC."inc/parser/$rname.php";
584 if(@file_exists($file)){
585 require_once $file;
586 $rclass = "Doku_Renderer_$rname";
588 if ( !class_exists($rclass) ) {
589 trigger_error("Unable to resolve render class $rclass",E_USER_WARNING);
590 msg("Renderer '$rname' for $mode not valid",-1);
591 return null;
593 $Renderer = & new $rclass();
594 }else{
595 // Maybe a plugin is available?
596 $Renderer =& plugin_load('renderer',$rname, true);
597 if(is_null($Renderer)){
598 msg("No renderer '$rname' found for mode '$mode'",-1);
599 return null;
603 return $Renderer;
607 * Gets the first heading from a file
609 * @param string $id dokuwiki page id
610 * @param bool $render rerender if first heading not known
611 * default: true -- must be set to false for calls from the metadata renderer to
612 * protects against loops and excessive resource usage when pages
613 * for which only a first heading is required will attempt to
614 * render metadata for all the pages for which they require first
615 * headings ... and so on.
617 * @author Andreas Gohr <andi@splitbrain.org>
619 function p_get_first_heading($id, $render=true){
620 return p_get_metadata($id,'title',$render);
624 * Wrapper for GeSHi Code Highlighter, provides caching of its output
626 * @param string $code source code to be highlighted
627 * @param string $language language to provide highlighting
628 * @param string $wrapper html element to wrap the returned highlighted text
630 * @author Christopher Smith <chris@jalakai.co.uk>
631 * @author Andreas Gohr <andi@splitbrain.org>
633 function p_xhtml_cached_geshi($code, $language, $wrapper='pre') {
634 global $conf, $config_cascade;
635 $language = strtolower($language);
637 // remove any leading or trailing blank lines
638 $code = preg_replace('/^\s*?\n|\s*?\n$/','',$code);
640 $cache = getCacheName($language.$code,".code");
641 $ctime = @filemtime($cache);
642 if($ctime && !$_REQUEST['purge'] &&
643 $ctime > filemtime(DOKU_INC.'inc/geshi.php') && // geshi changed
644 $ctime > @filemtime(DOKU_INC.'inc/geshi/'.$language.'.php') && // language syntax definition changed
645 $ctime > filemtime(reset($config_cascade['main']['default']))){ // dokuwiki changed
646 $highlighted_code = io_readFile($cache, false);
648 } else {
650 require_once(DOKU_INC . 'inc/geshi.php');
652 $geshi = new GeSHi($code, $language, DOKU_INC . 'inc/geshi');
653 $geshi->set_encoding('utf-8');
654 $geshi->enable_classes();
655 $geshi->set_header_type(GESHI_HEADER_PRE);
656 $geshi->set_link_target($conf['target']['extern']);
658 // remove GeSHi's wrapper element (we'll replace it with our own later)
659 // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text
660 $highlighted_code = preg_replace('!^<pre[^>]*>|</pre>$!','',$geshi->parse_code());
661 io_saveFile($cache,$highlighted_code);
664 // add a wrapper element if required
665 if ($wrapper) {
666 return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>";
667 } else {
668 return $highlighted_code;
672 //Setup VIM: ex: et ts=2 enc=utf-8 :