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>
10 if(!defined('DOKU_INC')) die('meh.');
13 * How many pages shall be rendered for getting metadata during one request
14 * at maximum? Note that this limit isn't respected when METADATA_RENDER_UNLIMITED
15 * is passed as render parameter to p_get_metadata.
17 if (!defined('P_GET_METADATA_RENDER_LIMIT')) define('P_GET_METADATA_RENDER_LIMIT', 5);
19 /** Don't render metadata even if it is outdated or doesn't exist */
20 define('METADATA_DONT_RENDER', 0);
22 * Render metadata when the page is really newer or the metadata doesn't exist.
23 * Uses just a simple check, but should work pretty well for loading simple
24 * metadata values like the page title and avoids rendering a lot of pages in
25 * one request. The P_GET_METADATA_RENDER_LIMIT is used in this mode.
26 * Use this if it is unlikely that the metadata value you are requesting
27 * does depend e.g. on pages that are included in the current page using
28 * the include plugin (this is very likely the case for the page title, but
29 * not for relation references).
31 define('METADATA_RENDER_USING_SIMPLE_CACHE', 1);
33 * Render metadata using the metadata cache logic. The P_GET_METADATA_RENDER_LIMIT
34 * is used in this mode. Use this mode when you are requesting more complex
35 * metadata. Although this will cause rendering more often it might actually have
36 * the effect that less current metadata is returned as it is more likely than in
37 * the simple cache mode that metadata needs to be rendered for all pages at once
38 * which means that when the metadata for the page is requested that actually needs
39 * to be updated the limit might have been reached already.
41 define('METADATA_RENDER_USING_CACHE', 2);
43 * Render metadata without limiting the number of pages for which metadata is
44 * rendered. Use this mode with care, normally it should only be used in places
45 * like the indexer or in cli scripts where the execution time normally isn't
46 * limited. This can be combined with the simple cache using
47 * METADATA_RENDER_USING_CACHE | METADATA_RENDER_UNLIMITED.
49 define('METADATA_RENDER_UNLIMITED', 4);
52 * Returns the parsed Wikitext in XHTML for the given id and revision.
54 * If $excuse is true an explanation is returned if the file
57 * @author Andreas Gohr <andi@splitbrain.org>
59 function p_wiki_xhtml($id, $rev='', $excuse=true){
60 $file = wikiFN($id,$rev);
63 //ensure $id is in global $ID (needed for parsing)
69 if(@file_exists($file)){
70 $ret = p_render('xhtml',p_get_instructions(io_readWikiPage($file,$id,$rev)),$info); //no caching on old revisions
72 $ret = p_locale_xhtml('norev');
75 if(@file_exists($file)){
76 $ret = p_cached_output($file,'xhtml',$id);
78 $ret = p_locale_xhtml('newpage');
82 //restore ID (just in case)
89 * Returns starting summary for a page (e.g. the first few
90 * paragraphs), marked up in XHTML.
92 * If $excuse is true an explanation is returned if the file
95 * @param string $id wiki page id
96 * @param string $title populated with page title from heading or page id
97 * @param string $rev revision string
98 * @param bool $excuse if an excuse shall be renderer when no content is found
99 * @return string xhtml code
101 * @author Harry Fuecks <hfuecks@gmail.com>
103 function p_wiki_xhtml_summary($id, &$title, $rev='', $excuse=true){
104 $file = wikiFN($id,$rev);
108 //ensure $id is in global $ID (needed for parsing)
114 if(@file_exists($file)){
115 //no caching on old revisions
116 $ins = p_get_instructions(io_readWikiPage($file,$id,$rev));
118 $ret = p_locale_xhtml('norev');
119 //restore ID (just in case)
126 if(@file_exists($file)){
127 // The XHTML for a summary is not cached so use the instruction cache
128 $ins = p_cached_instructions($file);
130 $ret = p_locale_xhtml('newpage');
131 //restore ID (just in case)
137 $ret = p_render('xhtmlsummary',$ins,$info);
139 if ( $info['sum_pagetitle'] ) {
140 $title = $info['sum_pagetitle'];
150 * Returns the specified local text in parsed format
152 * @author Andreas Gohr <andi@splitbrain.org>
154 function p_locale_xhtml($id){
155 //fetch parsed locale
156 $html = p_cached_output(localeFN($id));
163 * use p_cached_output()
165 * Returns the given file parsed to XHTML
167 * Uses and creates a cachefile
170 * @author Andreas Gohr <andi@splitbrain.org>
171 * @todo rewrite to use mode instead of hardcoded XHTML
173 function p_cached_xhtml($file){
174 return p_cached_output($file);
178 * Returns the given file parsed into the requested output format
180 * @author Andreas Gohr <andi@splitbrain.org>
181 * @author Chris Smith <chris@jalakai.co.uk>
183 function p_cached_output($file, $format='xhtml', $id='') {
186 $cache = new cache_renderer($id, $file, $format);
187 if ($cache->useCache()) {
188 $parsed = $cache->retrieveCache(false);
189 if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n";
191 $parsed = p_render($format, p_cached_instructions($file,false,$id), $info);
193 if ($info['cache']) {
194 $cache->storeCache($parsed); //save cachefile
195 if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n";
197 $cache->removeCache(); //try to delete cachefile
198 if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
206 * Returns the render instructions for a file
208 * Uses and creates a serialized cache file
210 * @author Andreas Gohr <andi@splitbrain.org>
212 function p_cached_instructions($file,$cacheonly=false,$id='') {
214 if(is_null($run)) $run = array();
216 $cache = new cache_instructions($id, $file);
218 if ($cacheonly || $cache->useCache() || (isset($run[$file]) && !defined('DOKU_UNITTEST'))) {
219 return $cache->retrieveCache();
220 } else if (@file_exists($file)) {
221 // no cache - do some work
222 $ins = p_get_instructions(io_readWikiPage($file,$id));
223 if ($cache->storeCache($ins)) {
224 $run[$file] = true; // we won't rebuild these instructions in the same run again
226 msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.',-1);
235 * turns a page into a list of instructions
237 * @author Harry Fuecks <hfuecks@gmail.com>
238 * @author Andreas Gohr <andi@splitbrain.org>
240 function p_get_instructions($text){
242 $modes = p_get_parsermodes();
245 $Parser = new Doku_Parser();
248 $Parser->Handler = new Doku_Handler();
250 //add modes to parser
251 foreach($modes as $mode){
252 $Parser->addMode($mode['mode'],$mode['obj']);
256 trigger_event('PARSER_WIKITEXT_PREPROCESS', $text);
257 $p = $Parser->parse($text);
263 * returns the metadata of a page
265 * @param string $id The id of the page the metadata should be returned from
266 * @param string $key The key of the metdata value that shall be read (by default everything) - separate hierarchies by " " like "date created"
267 * @param int $render If the page should be rendererd - possible values:
268 * METADATA_DONT_RENDER, METADATA_RENDER_USING_SIMPLE_CACHE, METADATA_RENDER_USING_CACHE
269 * METADATA_RENDER_UNLIMITED (also combined with the previous two options),
270 * default: METADATA_RENDER_USING_CACHE
271 * @return mixed The requested metadata fields
273 * @author Esther Brunner <esther@kaffeehaus.ch>
274 * @author Michael Hamann <michael@content-space.de>
276 function p_get_metadata($id, $key='', $render=METADATA_RENDER_USING_CACHE){
278 static $render_count = 0;
279 // track pages that have already been rendered in order to avoid rendering the same page
281 static $rendered_pages = array();
283 // cache the current page
284 // Benchmarking shows the current page's metadata is generally the only page metadata
285 // accessed several times. This may catch a few other pages, but that shouldn't be an issue.
286 $cache = ($ID == $id);
287 $meta = p_read_metadata($id, $cache);
289 if (!is_numeric($render)) {
291 $render = METADATA_RENDER_USING_SIMPLE_CACHE;
293 $render = METADATA_DONT_RENDER;
297 // prevent recursive calls in the cache
298 static $recursion = false;
299 if (!$recursion && $render != METADATA_DONT_RENDER && !isset($rendered_pages[$id])&& page_exists($id)){
302 $cachefile = new cache_renderer($id, wikiFN($id), 'metadata');
305 if ($render & METADATA_RENDER_UNLIMITED || $render_count < P_GET_METADATA_RENDER_LIMIT) {
306 if ($render & METADATA_RENDER_USING_SIMPLE_CACHE) {
307 $pagefn = wikiFN($id);
308 $metafn = metaFN($id, '.meta');
309 if (!@file_exists($metafn) || @filemtime($pagefn) > @filemtime($cachefile->cache)) {
312 } elseif (!$cachefile->useCache()){
317 if (!defined('DOKU_UNITTEST')) {
319 $rendered_pages[$id] = true;
322 $meta = p_render_metadata($id, $meta);
323 // only update the file when the metadata has been changed
324 if ($meta == $old_meta || p_save_metadata($id, $meta)) {
325 // store a timestamp in order to make sure that the cachefile is touched
326 // this timestamp is also stored when the meta data is still the same
327 $cachefile->storeCache(time());
329 msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.',-1);
336 $val = $meta['current'];
339 foreach(preg_split('/\s+/', $key, 2, PREG_SPLIT_NO_EMPTY) as $cur_key) {
340 if (!isset($val[$cur_key])) {
343 $val = $val[$cur_key];
349 * sets metadata elements of a page
351 * @see http://www.dokuwiki.org/devel:metadata#functions_to_get_and_set_metadata
353 * @param String $id is the ID of a wiki page
354 * @param Array $data is an array with key ⇒ value pairs to be set in the metadata
355 * @param Boolean $render whether or not the page metadata should be generated with the renderer
356 * @param Boolean $persistent indicates whether or not the particular metadata value will persist through
357 * the next metadata rendering.
358 * @return boolean true on success
360 * @author Esther Brunner <esther@kaffeehaus.ch>
361 * @author Michael Hamann <michael@content-space.de>
363 function p_set_metadata($id, $data, $render=false, $persistent=true){
364 if (!is_array($data)) return false;
366 global $ID, $METADATA_RENDERERS;
368 // if there is currently a renderer change the data in the renderer instead
369 if (isset($METADATA_RENDERERS[$id])) {
370 $orig =& $METADATA_RENDERERS[$id];
373 // cache the current page
374 $cache = ($ID == $id);
375 $orig = p_read_metadata($id, $cache);
377 // render metadata first?
378 $meta = $render ? p_render_metadata($id, $orig) : $orig;
381 // now add the passed metadata
382 $protected = array('description', 'date', 'contributor');
383 foreach ($data as $key => $value){
385 // be careful with sub-arrays of $meta['relation']
386 if ($key == 'relation'){
388 foreach ($value as $subkey => $subvalue){
389 if(isset($meta['current'][$key][$subkey]) && is_array($meta['current'][$key][$subkey])) {
390 $meta['current'][$key][$subkey] = array_merge($meta['current'][$key][$subkey], (array)$subvalue);
392 $meta['current'][$key][$subkey] = $subvalue;
395 if(isset($meta['persistent'][$key][$subkey]) && is_array($meta['persistent'][$key][$subkey])) {
396 $meta['persistent'][$key][$subkey] = array_merge($meta['persistent'][$key][$subkey], (array)$subvalue);
398 $meta['persistent'][$key][$subkey] = $subvalue;
403 // be careful with some senisitive arrays of $meta
404 } elseif (in_array($key, $protected)){
406 // these keys, must have subkeys - a legitimate value must be an array
407 if (is_array($value)) {
408 $meta['current'][$key] = !empty($meta['current'][$key]) ? array_merge((array)$meta['current'][$key],$value) : $value;
411 $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ? array_merge((array)$meta['persistent'][$key],$value) : $value;
415 // no special treatment for the rest
417 $meta['current'][$key] = $value;
418 if ($persistent) $meta['persistent'][$key] = $value;
422 // save only if metadata changed
423 if ($meta == $orig) return true;
425 if (isset($METADATA_RENDERERS[$id])) {
426 // set both keys individually as the renderer has references to the individual keys
427 $METADATA_RENDERERS[$id]['current'] = $meta['current'];
428 $METADATA_RENDERERS[$id]['persistent'] = $meta['persistent'];
431 return p_save_metadata($id, $meta);
436 * Purges the non-persistant part of the meta data
437 * used on page deletion
439 * @author Michael Klier <chi@chimeric.de>
441 function p_purge_metadata($id) {
442 $meta = p_read_metadata($id);
443 foreach($meta['current'] as $key => $value) {
444 if(is_array($meta[$key])) {
445 $meta['current'][$key] = array();
447 $meta['current'][$key] = '';
451 return p_save_metadata($id, $meta);
455 * read the metadata from source/cache for $id
456 * (internal use only - called by p_get_metadata & p_set_metadata)
458 * @author Christopher Smith <chris@jalakai.co.uk>
460 * @param string $id absolute wiki page id
461 * @param bool $cache whether or not to cache metadata in memory
462 * (only use for metadata likely to be accessed several times)
464 * @return array metadata
466 function p_read_metadata($id,$cache=false) {
467 global $cache_metadata;
469 if (isset($cache_metadata[(string)$id])) return $cache_metadata[(string)$id];
471 $file = metaFN($id, '.meta');
472 $meta = @file_exists($file) ? unserialize(io_readFile($file, false)) : array('current'=>array(),'persistent'=>array());
475 $cache_metadata[(string)$id] = $meta;
482 * This is the backend function to save a metadata array to a file
484 * @param string $id absolute wiki page id
485 * @param array $meta metadata
487 * @return bool success / fail
489 function p_save_metadata($id, $meta) {
490 // sync cached copies, including $INFO metadata
491 global $cache_metadata, $INFO;
493 if (isset($cache_metadata[$id])) $cache_metadata[$id] = $meta;
494 if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; }
496 return io_saveFile(metaFN($id, '.meta'), serialize($meta));
500 * renders the metadata of a page
502 * @author Esther Brunner <esther@kaffeehaus.ch>
504 function p_render_metadata($id, $orig){
505 // make sure the correct ID is in global ID
506 global $ID, $METADATA_RENDERERS;
508 // avoid recursive rendering processes for the same id
509 if (isset($METADATA_RENDERERS[$id]))
512 // store the original metadata in the global $METADATA_RENDERERS so p_set_metadata can use it
513 $METADATA_RENDERERS[$id] =& $orig;
518 // add an extra key for the event - to tell event handlers the page whose metadata this is
520 $evt = new Doku_Event('PARSER_METADATA_RENDER', $orig);
521 if ($evt->advise_before()) {
523 require_once DOKU_INC."inc/parser/metadata.php";
526 $instructions = p_cached_instructions(wikiFN($id),false,$id);
527 if(is_null($instructions)){
529 unset($METADATA_RENDERERS[$id]);
530 return null; // something went wrong with the instructions
533 // set up the renderer
534 $renderer = new Doku_Renderer_metadata();
535 $renderer->meta =& $orig['current'];
536 $renderer->persistent =& $orig['persistent'];
538 // loop through the instructions
539 foreach ($instructions as $instruction){
540 // execute the callback against the renderer
541 call_user_func_array(array(&$renderer, $instruction[0]), (array) $instruction[1]);
544 $evt->result = array('current'=>&$renderer->meta,'persistent'=>&$renderer->persistent);
546 $evt->advise_after();
550 unset($METADATA_RENDERERS[$id]);
555 * returns all available parser syntax modes in correct order
557 * @author Andreas Gohr <andi@splitbrain.org>
559 function p_get_parsermodes(){
563 static $modes = null;
564 if($modes != null && !defined('DOKU_UNITTEST')){
568 //import parser classes and mode definitions
569 require_once DOKU_INC . 'inc/parser/parser.php';
571 // we now collect all syntax modes and their objects, then they will
572 // be sorted and added to the parser in correct order
575 // add syntax plugins
576 $pluginlist = plugin_list('syntax');
577 if(count($pluginlist)){
578 global $PARSER_MODES;
580 foreach($pluginlist as $p){
581 /** @var DokuWiki_Syntax_Plugin $obj */
582 if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj
583 $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
586 'sort' => $obj->getSort(),
587 'mode' => "plugin_$p",
590 unset($obj); //remove the reference
595 $std_modes = array('listblock','preformatted','notoc','nocache',
596 'header','table','linebreak','footnote','hr',
597 'unformatted','php','html','code','file','quote',
598 'internallink','rss','media','externallink',
599 'emaillink','windowssharelink','eol');
600 if($conf['typography']){
601 $std_modes[] = 'quotes';
602 $std_modes[] = 'multiplyentity';
604 foreach($std_modes as $m){
605 $class = "Doku_Parser_Mode_$m";
608 'sort' => $obj->getSort(),
614 // add formatting modes
615 $fmt_modes = array('strong','emphasis','underline','monospace',
616 'subscript','superscript','deleted');
617 foreach($fmt_modes as $m){
618 $obj = new Doku_Parser_Mode_formatting($m);
620 'sort' => $obj->getSort(),
626 // add modes which need files
627 $obj = new Doku_Parser_Mode_smiley(array_keys(getSmileys()));
628 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj' => $obj );
629 $obj = new Doku_Parser_Mode_acronym(array_keys(getAcronyms()));
630 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj' => $obj );
631 $obj = new Doku_Parser_Mode_entity(array_keys(getEntities()));
632 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj' => $obj );
634 // add optional camelcase mode
635 if($conf['camelcase']){
636 $obj = new Doku_Parser_Mode_camelcaselink();
637 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj' => $obj );
641 usort($modes,'p_sort_modes');
647 * Callback function for usort
649 * @author Andreas Gohr <andi@splitbrain.org>
651 function p_sort_modes($a, $b){
652 if($a['sort'] == $b['sort']) return 0;
653 return ($a['sort'] < $b['sort']) ? -1 : 1;
657 * Renders a list of instruction to the specified output mode
659 * In the $info array is information from the renderer returned
661 * @author Harry Fuecks <hfuecks@gmail.com>
662 * @author Andreas Gohr <andi@splitbrain.org>
664 function p_render($mode,$instructions,&$info){
665 if(is_null($instructions)) return '';
667 $Renderer =& p_get_renderer($mode);
668 if (is_null($Renderer)) return null;
672 $Renderer->smileys = getSmileys();
673 $Renderer->entities = getEntities();
674 $Renderer->acronyms = getAcronyms();
675 $Renderer->interwiki = getInterwiki();
677 // Loop through the instructions
678 foreach ( $instructions as $instruction ) {
679 // Execute the callback against the Renderer
680 if(method_exists($Renderer, $instruction[0])){
681 call_user_func_array(array(&$Renderer, $instruction[0]), $instruction[1] ? $instruction[1] : array());
686 $info = $Renderer->info;
688 // Post process and return the output
689 $data = array($mode,& $Renderer->doc);
690 trigger_event('RENDERER_CONTENT_POSTPROCESS',$data);
691 return $Renderer->doc;
695 * @param $mode string Mode of the renderer to get
696 * @return null|Doku_Renderer The renderer
698 function & p_get_renderer($mode) {
699 /** @var Doku_Plugin_Controller $plugin_controller */
700 global $conf, $plugin_controller;
702 $rname = !empty($conf['renderer_'.$mode]) ? $conf['renderer_'.$mode] : $mode;
703 $rclass = "Doku_Renderer_$rname";
705 if( class_exists($rclass) ) {
706 return new $rclass();
709 // try default renderer first:
710 $file = DOKU_INC."inc/parser/$rname.php";
711 if(@file_exists($file)){
714 if ( !class_exists($rclass) ) {
715 trigger_error("Unable to resolve render class $rclass",E_USER_WARNING);
716 msg("Renderer '$rname' for $mode not valid",-1);
719 $Renderer = new $rclass();
721 // Maybe a plugin/component is available?
722 list($plugin, $component) = $plugin_controller->_splitName($rname);
723 if (!$plugin_controller->isdisabled($plugin)){
724 $Renderer =& $plugin_controller->load('renderer',$rname);
727 if(!isset($Renderer) || is_null($Renderer)){
728 msg("No renderer '$rname' found for mode '$mode'",-1);
737 * Gets the first heading from a file
739 * @param string $id dokuwiki page id
740 * @param int $render rerender if first heading not known
741 * default: METADATA_RENDER_USING_SIMPLE_CACHE
742 * Possible values: METADATA_DONT_RENDER,
743 * METADATA_RENDER_USING_SIMPLE_CACHE,
744 * METADATA_RENDER_USING_CACHE,
745 * METADATA_RENDER_UNLIMITED
747 * @return string|null The first heading
748 * @author Andreas Gohr <andi@splitbrain.org>
749 * @author Michael Hamann <michael@content-space.de>
751 function p_get_first_heading($id, $render=METADATA_RENDER_USING_SIMPLE_CACHE){
752 return p_get_metadata(cleanID($id),'title',$render);
756 * Wrapper for GeSHi Code Highlighter, provides caching of its output
758 * @param string $code source code to be highlighted
759 * @param string $language language to provide highlighting
760 * @param string $wrapper html element to wrap the returned highlighted text
762 * @return string xhtml code
763 * @author Christopher Smith <chris@jalakai.co.uk>
764 * @author Andreas Gohr <andi@splitbrain.org>
766 function p_xhtml_cached_geshi($code, $language, $wrapper='pre') {
767 global $conf, $config_cascade, $INPUT;
768 $language = strtolower($language);
770 // remove any leading or trailing blank lines
771 $code = preg_replace('/^\s*?\n|\s*?\n$/','',$code);
773 $cache = getCacheName($language.$code,".code");
774 $ctime = @filemtime($cache);
775 if($ctime && !$INPUT->bool('purge') &&
776 $ctime > filemtime(DOKU_INC.'inc/geshi.php') && // geshi changed
777 $ctime > @filemtime(DOKU_INC.'inc/geshi/'.$language.'.php') && // language syntax definition changed
778 $ctime > filemtime(reset($config_cascade['main']['default']))){ // dokuwiki changed
779 $highlighted_code = io_readFile($cache, false);
783 $geshi = new GeSHi($code, $language, DOKU_INC . 'inc/geshi');
784 $geshi->set_encoding('utf-8');
785 $geshi->enable_classes();
786 $geshi->set_overall_class('geshi');
787 $geshi->set_header_type(GESHI_HEADER_PRE_VALID);
788 $geshi->set_link_target($conf['target']['extern']);
790 if(in_array($language, array('cli'))) $geshi->enable_line_numbers(GESHI_NO_LINE_NUMBERS);
791 else $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
793 // remove GeSHi's wrapper element (we'll replace it with our own later)
794 // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text
795 //$highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!','',$geshi->parse_code()),"\n\r");
796 $highlighted_code = $geshi->parse_code() . '<style type="text/css">' . $geshi->get_stylesheet() . '</style>';
797 io_saveFile($cache,$highlighted_code);
800 // add a wrapper element if required
802 //return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>";
804 return $highlighted_code;