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.');
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');
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
22 * @author Andreas Gohr <andi@splitbrain.org>
24 function p_wiki_xhtml($id, $rev='', $excuse=true){
25 $file = wikiFN($id,$rev);
28 //ensure $id is in global $ID (needed for parsing)
34 if(@file_exists
($file)){
35 $ret = p_render('xhtml',p_get_instructions(io_readWikiPage($file,$id,$rev)),$info); //no caching on old revisions
37 $ret = p_locale_xhtml('norev');
40 if(@file_exists
($file)){
41 $ret = p_cached_output($file,'xhtml',$id);
43 $ret = p_locale_xhtml('newpage');
47 //restore ID (just in case)
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
60 * @param string wiki page id
61 * @param reference populated with page title from heading or page id
63 * @author Harry Fuecks <hfuecks@gmail.com>
65 function p_wiki_xhtml_summary($id, &$title, $rev='', $excuse=true){
66 $file = wikiFN($id,$rev);
69 //ensure $id is in global $ID (needed for parsing)
75 if(@file_exists
($file)){
76 //no caching on old revisions
77 $ins = p_get_instructions(io_readWikiPage($file,$id,$rev));
79 $ret = p_locale_xhtml('norev');
80 //restore ID (just in case)
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);
91 $ret = p_locale_xhtml('newpage');
92 //restore ID (just in case)
98 $ret = p_render('xhtmlsummary',$ins,$info);
100 if ( $info['sum_pagetitle'] ) {
101 $title = $info['sum_pagetitle'];
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));
124 * use p_cached_output()
126 * Returns the given file parsed to XHTML
128 * Uses and creates a cachefile
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='') {
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";
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";
158 $cache->removeCache(); //try to delete cachefile
159 if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
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='') {
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
188 msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.',-1);
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();
207 $Parser = new Doku_Parser();
210 $Parser->Handler
= new Doku_Handler();
212 //add modes to parser
213 foreach($modes as $mode){
214 $Parser->addMode($mode['mode'],$mode['obj']);
218 trigger_event('PARSER_WIKITEXT_PREPROCESS', $text);
219 $p = $Parser->parse($text);
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 && !isset($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']; }
250 list($key, $subkey) = explode(' ', $key, 2);
251 $subkey = trim($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;
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;
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;
301 $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ?
array_merge($meta['persistent'][$key],$value) : $value;
305 // no special treatment for the rest
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();
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));
387 $cache_metadata[(string)$id] = $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
404 // add an extra key for the event - to tell event handlers the page whose metadata this is
406 $evt = new Doku_Event('PARSER_METADATA_RENDER', $orig);
407 if ($evt->advise_before()) {
409 require_once DOKU_INC
."inc/parser/metadata.php";
412 $instructions = p_cached_instructions(wikiFN($id),false,$id);
413 if(is_null($instructions)){
415 return null; // something went wrong with the instructions
418 // set up the renderer
419 $renderer = new Doku_Renderer_metadata();
420 $renderer->meta
= $orig['current'];
421 $renderer->persistent
= $orig['persistent'];
423 // loop through the instructions
424 foreach ($instructions as $instruction){
425 // execute the callback against the renderer
426 call_user_func_array(array(&$renderer, $instruction[0]), (array) $instruction[1]);
429 $evt->result
= array('current'=>$renderer->meta
,'persistent'=>$renderer->persistent
);
431 $evt->advise_after();
438 * returns all available parser syntax modes in correct order
440 * @author Andreas Gohr <andi@splitbrain.org>
442 function p_get_parsermodes(){
446 static $modes = null;
451 //import parser classes and mode definitions
452 require_once DOKU_INC
. 'inc/parser/parser.php';
454 // we now collect all syntax modes and their objects, then they will
455 // be sorted and added to the parser in correct order
458 // add syntax plugins
459 $pluginlist = plugin_list('syntax');
460 if(count($pluginlist)){
461 global $PARSER_MODES;
463 foreach($pluginlist as $p){
464 if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj
465 $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
468 'sort' => $obj->getSort(),
469 'mode' => "plugin_$p",
472 unset($obj); //remove the reference
477 $std_modes = array('listblock','preformatted','notoc','nocache',
478 'header','table','linebreak','footnote','hr',
479 'unformatted','php','html','code','file','quote',
480 'internallink','rss','media','externallink',
481 'emaillink','windowssharelink','eol');
482 if($conf['typography']){
483 $std_modes[] = 'quotes';
484 $std_modes[] = 'multiplyentity';
486 foreach($std_modes as $m){
487 $class = "Doku_Parser_Mode_$m";
490 'sort' => $obj->getSort(),
496 // add formatting modes
497 $fmt_modes = array('strong','emphasis','underline','monospace',
498 'subscript','superscript','deleted');
499 foreach($fmt_modes as $m){
500 $obj = new Doku_Parser_Mode_formatting($m);
502 'sort' => $obj->getSort(),
508 // add modes which need files
509 $obj = new Doku_Parser_Mode_smiley(array_keys(getSmileys()));
510 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj' => $obj );
511 $obj = new Doku_Parser_Mode_acronym(array_keys(getAcronyms()));
512 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj' => $obj );
513 $obj = new Doku_Parser_Mode_entity(array_keys(getEntities()));
514 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj' => $obj );
516 // add optional camelcase mode
517 if($conf['camelcase']){
518 $obj = new Doku_Parser_Mode_camelcaselink();
519 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj' => $obj );
523 usort($modes,'p_sort_modes');
529 * Callback function for usort
531 * @author Andreas Gohr <andi@splitbrain.org>
533 function p_sort_modes($a, $b){
534 if($a['sort'] == $b['sort']) return 0;
535 return ($a['sort'] < $b['sort']) ?
-1 : 1;
539 * Renders a list of instruction to the specified output mode
541 * In the $info array are informations from the renderer returned
543 * @author Harry Fuecks <hfuecks@gmail.com>
544 * @author Andreas Gohr <andi@splitbrain.org>
546 function p_render($mode,$instructions,&$info){
547 if(is_null($instructions)) return '';
549 $Renderer =& p_get_renderer($mode);
550 if (is_null($Renderer)) return null;
554 $Renderer->smileys
= getSmileys();
555 $Renderer->entities
= getEntities();
556 $Renderer->acronyms
= getAcronyms();
557 $Renderer->interwiki
= getInterwiki();
559 // Loop through the instructions
560 foreach ( $instructions as $instruction ) {
561 // Execute the callback against the Renderer
562 call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
566 $info = $Renderer->info
;
568 // Post process and return the output
569 $data = array($mode,& $Renderer->doc
);
570 trigger_event('RENDERER_CONTENT_POSTPROCESS',$data);
571 return $Renderer->doc
;
574 function & p_get_renderer($mode) {
575 global $conf, $plugin_controller;
577 $rname = !empty($conf['renderer_'.$mode]) ?
$conf['renderer_'.$mode] : $mode;
579 // try default renderer first:
580 $file = DOKU_INC
."inc/parser/$rname.php";
581 if(@file_exists
($file)){
583 $rclass = "Doku_Renderer_$rname";
585 if ( !class_exists($rclass) ) {
586 trigger_error("Unable to resolve render class $rclass",E_USER_WARNING
);
587 msg("Renderer '$rname' for $mode not valid",-1);
590 $Renderer = new $rclass();
592 // Maybe a plugin/component is available?
593 list($plugin, $component) = $plugin_controller->_splitName($rname);
594 if (!$plugin_controller->isdisabled($plugin)){
595 $Renderer =& $plugin_controller->load('renderer',$rname, true);
598 if(is_null($Renderer)){
599 msg("No renderer '$rname' found for mode '$mode'",-1);
608 * Gets the first heading from a file
610 * @param string $id dokuwiki page id
611 * @param bool $render rerender if first heading not known
612 * default: true -- must be set to false for calls from the metadata renderer to
613 * protects against loops and excessive resource usage when pages
614 * for which only a first heading is required will attempt to
615 * render metadata for all the pages for which they require first
616 * headings ... and so on.
618 * @author Andreas Gohr <andi@splitbrain.org>
620 function p_get_first_heading($id, $render=true){
621 return p_get_metadata($id,'title',$render);
625 * Wrapper for GeSHi Code Highlighter, provides caching of its output
627 * @param string $code source code to be highlighted
628 * @param string $language language to provide highlighting
629 * @param string $wrapper html element to wrap the returned highlighted text
631 * @author Christopher Smith <chris@jalakai.co.uk>
632 * @author Andreas Gohr <andi@splitbrain.org>
634 function p_xhtml_cached_geshi($code, $language, $wrapper='pre') {
635 global $conf, $config_cascade;
636 $language = strtolower($language);
638 // remove any leading or trailing blank lines
639 $code = preg_replace('/^\s*?\n|\s*?\n$/','',$code);
641 $cache = getCacheName($language.$code,".code");
642 $ctime = @filemtime
($cache);
643 if($ctime && !$_REQUEST['purge'] &&
644 $ctime > filemtime(DOKU_INC
.'inc/geshi.php') && // geshi changed
645 $ctime > @filemtime
(DOKU_INC
.'inc/geshi/'.$language.'.php') && // language syntax definition changed
646 $ctime > filemtime(reset($config_cascade['main']['default']))){ // dokuwiki changed
647 $highlighted_code = io_readFile($cache, false);
651 require_once(DOKU_INC
. 'inc/geshi.php');
653 $geshi = new GeSHi($code, $language, DOKU_INC
. 'inc/geshi');
654 $geshi->set_encoding('utf-8');
655 $geshi->enable_classes();
656 $geshi->set_header_type(GESHI_HEADER_PRE
);
657 $geshi->set_link_target($conf['target']['extern']);
659 // remove GeSHi's wrapper element (we'll replace it with our own later)
660 // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text
661 $highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!','',$geshi->parse_code()),"\n\r");
662 io_saveFile($cache,$highlighted_code);
665 // add a wrapper element if required
667 return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>";
669 return $highlighted_code;