1<?php 2/** 3 * Utilities for accessing the parser 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 10use dokuwiki\Cache\CacheInstructions; 11use dokuwiki\Cache\CacheRenderer; 12use dokuwiki\ChangeLog\PageChangeLog; 13use dokuwiki\Extension\PluginController; 14use dokuwiki\Extension\Event; 15use dokuwiki\Parsing\Parser; 16 17/** 18 * How many pages shall be rendered for getting metadata during one request 19 * at maximum? Note that this limit isn't respected when METADATA_RENDER_UNLIMITED 20 * is passed as render parameter to p_get_metadata. 21 */ 22if (!defined('P_GET_METADATA_RENDER_LIMIT')) define('P_GET_METADATA_RENDER_LIMIT', 5); 23 24/** Don't render metadata even if it is outdated or doesn't exist */ 25define('METADATA_DONT_RENDER', 0); 26/** 27 * Render metadata when the page is really newer or the metadata doesn't exist. 28 * Uses just a simple check, but should work pretty well for loading simple 29 * metadata values like the page title and avoids rendering a lot of pages in 30 * one request. The P_GET_METADATA_RENDER_LIMIT is used in this mode. 31 * Use this if it is unlikely that the metadata value you are requesting 32 * does depend e.g. on pages that are included in the current page using 33 * the include plugin (this is very likely the case for the page title, but 34 * not for relation references). 35 */ 36define('METADATA_RENDER_USING_SIMPLE_CACHE', 1); 37/** 38 * Render metadata using the metadata cache logic. The P_GET_METADATA_RENDER_LIMIT 39 * is used in this mode. Use this mode when you are requesting more complex 40 * metadata. Although this will cause rendering more often it might actually have 41 * the effect that less current metadata is returned as it is more likely than in 42 * the simple cache mode that metadata needs to be rendered for all pages at once 43 * which means that when the metadata for the page is requested that actually needs 44 * to be updated the limit might have been reached already. 45 */ 46define('METADATA_RENDER_USING_CACHE', 2); 47/** 48 * Render metadata without limiting the number of pages for which metadata is 49 * rendered. Use this mode with care, normally it should only be used in places 50 * like the indexer or in cli scripts where the execution time normally isn't 51 * limited. This can be combined with the simple cache using 52 * METADATA_RENDER_USING_CACHE | METADATA_RENDER_UNLIMITED. 53 */ 54define('METADATA_RENDER_UNLIMITED', 4); 55 56/** 57 * Returns the parsed Wikitext in XHTML for the given id and revision. 58 * 59 * If $excuse is true an explanation is returned if the file 60 * wasn't found 61 * 62 * @author Andreas Gohr <andi@splitbrain.org> 63 * 64 * @param string $id page id 65 * @param string|int $rev revision timestamp or empty string 66 * @param bool $excuse 67 * @param string $date_at 68 * 69 * @return null|string 70 */ 71function p_wiki_xhtml($id, $rev='', $excuse=true,$date_at=''){ 72 $file = wikiFN($id,$rev); 73 $ret = ''; 74 75 //ensure $id is in global $ID (needed for parsing) 76 global $ID; 77 $keep = $ID; 78 $ID = $id; 79 80 if($rev || $date_at){ 81 if(file_exists($file)){ 82 //no caching on old revisions 83 $ret = p_render('xhtml',p_get_instructions(io_readWikiPage($file,$id,$rev)),$info,$date_at); 84 }elseif($excuse){ 85 $ret = p_locale_xhtml('norev'); 86 } 87 }else{ 88 if(file_exists($file)){ 89 $ret = p_cached_output($file,'xhtml',$id); 90 }elseif($excuse){ 91 //check if the page once existed 92 $changelog = new PageChangeLog($id); 93 if($changelog->hasRevisions()) { 94 $ret = p_locale_xhtml('onceexisted'); 95 } else { 96 $ret = p_locale_xhtml('newpage'); 97 } 98 } 99 } 100 101 //restore ID (just in case) 102 $ID = $keep; 103 104 return $ret; 105} 106 107/** 108 * Returns the specified local text in parsed format 109 * 110 * @author Andreas Gohr <andi@splitbrain.org> 111 * 112 * @param string $id page id 113 * @return null|string 114 */ 115function p_locale_xhtml($id){ 116 //fetch parsed locale 117 $html = p_cached_output(localeFN($id)); 118 Event::createAndTrigger('PARSER_LOCALE_XHTML', $html); 119 return $html; 120} 121 122/** 123 * Returns the given file parsed into the requested output format 124 * 125 * @author Andreas Gohr <andi@splitbrain.org> 126 * @author Chris Smith <chris@jalakai.co.uk> 127 * 128 * @param string $file filename, path to file 129 * @param string $format 130 * @param string $id page id 131 * @return null|string 132 */ 133function p_cached_output($file, $format='xhtml', $id='') { 134 global $conf; 135 136 $cache = new CacheRenderer($id, $file, $format); 137 if ($cache->useCache()) { 138 $parsed = $cache->retrieveCache(false); 139 if($conf['allowdebug'] && $format=='xhtml') { 140 $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n"; 141 } 142 } else { 143 $parsed = p_render($format, p_cached_instructions($file,false,$id), $info); 144 145 if ($info['cache'] && $cache->storeCache($parsed)) { // storeCache() attempts to save cachefile 146 if($conf['allowdebug'] && $format=='xhtml') { 147 $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n"; 148 } 149 }else{ 150 $cache->removeCache(); //try to delete cachefile 151 if($conf['allowdebug'] && $format=='xhtml') { 152 $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n"; 153 } 154 } 155 } 156 157 return $parsed; 158} 159 160/** 161 * Returns the render instructions for a file 162 * 163 * Uses and creates a serialized cache file 164 * 165 * @author Andreas Gohr <andi@splitbrain.org> 166 * 167 * @param string $file filename, path to file 168 * @param bool $cacheonly 169 * @param string $id page id 170 * @return array|null 171 */ 172function p_cached_instructions($file,$cacheonly=false,$id='') { 173 static $run = null; 174 if(is_null($run)) $run = array(); 175 176 $cache = new CacheInstructions($id, $file); 177 178 if ($cacheonly || $cache->useCache() || (isset($run[$file]) && !defined('DOKU_UNITTEST'))) { 179 return $cache->retrieveCache(); 180 } else if (file_exists($file)) { 181 // no cache - do some work 182 $ins = p_get_instructions(io_readWikiPage($file,$id)); 183 if ($cache->storeCache($ins)) { 184 $run[$file] = true; // we won't rebuild these instructions in the same run again 185 } else { 186 msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.',-1); 187 } 188 return $ins; 189 } 190 191 return null; 192} 193 194/** 195 * turns a page into a list of instructions 196 * 197 * @author Harry Fuecks <hfuecks@gmail.com> 198 * @author Andreas Gohr <andi@splitbrain.org> 199 * 200 * @param string $text raw wiki syntax text 201 * @return array a list of instruction arrays 202 */ 203function p_get_instructions($text){ 204 205 $modes = p_get_parsermodes(); 206 207 // Create the parser and handler 208 $Parser = new Parser(new Doku_Handler()); 209 210 //add modes to parser 211 foreach($modes as $mode){ 212 $Parser->addMode($mode['mode'],$mode['obj']); 213 } 214 215 // Do the parsing 216 Event::createAndTrigger('PARSER_WIKITEXT_PREPROCESS', $text); 217 $p = $Parser->parse($text); 218 // dbg($p); 219 return $p; 220} 221 222/** 223 * returns the metadata of a page 224 * 225 * @param string $id The id of the page the metadata should be returned from 226 * @param string $key The key of the metdata value that shall be read (by default everything) 227 * separate hierarchies by " " like "date created" 228 * @param int $render If the page should be rendererd - possible values: 229 * METADATA_DONT_RENDER, METADATA_RENDER_USING_SIMPLE_CACHE, METADATA_RENDER_USING_CACHE 230 * METADATA_RENDER_UNLIMITED (also combined with the previous two options), 231 * default: METADATA_RENDER_USING_CACHE 232 * @return mixed The requested metadata fields 233 * 234 * @author Esther Brunner <esther@kaffeehaus.ch> 235 * @author Michael Hamann <michael@content-space.de> 236 */ 237function p_get_metadata($id, $key='', $render=METADATA_RENDER_USING_CACHE){ 238 global $ID; 239 static $render_count = 0; 240 // track pages that have already been rendered in order to avoid rendering the same page 241 // again 242 static $rendered_pages = array(); 243 244 // cache the current page 245 // Benchmarking shows the current page's metadata is generally the only page metadata 246 // accessed several times. This may catch a few other pages, but that shouldn't be an issue. 247 $cache = ($ID == $id); 248 $meta = p_read_metadata($id, $cache); 249 250 if (!is_numeric($render)) { 251 if ($render) { 252 $render = METADATA_RENDER_USING_SIMPLE_CACHE; 253 } else { 254 $render = METADATA_DONT_RENDER; 255 } 256 } 257 258 // prevent recursive calls in the cache 259 static $recursion = false; 260 if (!$recursion && $render != METADATA_DONT_RENDER && !isset($rendered_pages[$id])&& page_exists($id)){ 261 $recursion = true; 262 263 $cachefile = new CacheRenderer($id, wikiFN($id), 'metadata'); 264 265 $do_render = false; 266 if ($render & METADATA_RENDER_UNLIMITED || $render_count < P_GET_METADATA_RENDER_LIMIT) { 267 if ($render & METADATA_RENDER_USING_SIMPLE_CACHE) { 268 $pagefn = wikiFN($id); 269 $metafn = metaFN($id, '.meta'); 270 if (!file_exists($metafn) || @filemtime($pagefn) > @filemtime($cachefile->cache)) { 271 $do_render = true; 272 } 273 } elseif (!$cachefile->useCache()){ 274 $do_render = true; 275 } 276 } 277 if ($do_render) { 278 if (!defined('DOKU_UNITTEST')) { 279 ++$render_count; 280 $rendered_pages[$id] = true; 281 } 282 $old_meta = $meta; 283 $meta = p_render_metadata($id, $meta); 284 // only update the file when the metadata has been changed 285 if ($meta == $old_meta || p_save_metadata($id, $meta)) { 286 // store a timestamp in order to make sure that the cachefile is touched 287 // this timestamp is also stored when the meta data is still the same 288 $cachefile->storeCache(time()); 289 } else { 290 msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.',-1); 291 } 292 } 293 294 $recursion = false; 295 } 296 297 $val = $meta['current']; 298 299 // filter by $key 300 foreach(preg_split('/\s+/', $key, 2, PREG_SPLIT_NO_EMPTY) as $cur_key) { 301 if (!isset($val[$cur_key])) { 302 return null; 303 } 304 $val = $val[$cur_key]; 305 } 306 return $val; 307} 308 309/** 310 * sets metadata elements of a page 311 * 312 * @see http://www.dokuwiki.org/devel:metadata#functions_to_get_and_set_metadata 313 * 314 * @param string $id is the ID of a wiki page 315 * @param array $data is an array with key ⇒ value pairs to be set in the metadata 316 * @param boolean $render whether or not the page metadata should be generated with the renderer 317 * @param boolean $persistent indicates whether or not the particular metadata value will persist through 318 * the next metadata rendering. 319 * @return boolean true on success 320 * 321 * @author Esther Brunner <esther@kaffeehaus.ch> 322 * @author Michael Hamann <michael@content-space.de> 323 */ 324function p_set_metadata($id, $data, $render=false, $persistent=true){ 325 if (!is_array($data)) return false; 326 327 global $ID, $METADATA_RENDERERS; 328 329 // if there is currently a renderer change the data in the renderer instead 330 if (isset($METADATA_RENDERERS[$id])) { 331 $orig =& $METADATA_RENDERERS[$id]; 332 $meta = $orig; 333 } else { 334 // cache the current page 335 $cache = ($ID == $id); 336 $orig = p_read_metadata($id, $cache); 337 338 // render metadata first? 339 $meta = $render ? p_render_metadata($id, $orig) : $orig; 340 } 341 342 // now add the passed metadata 343 $protected = array('description', 'date', 'contributor'); 344 foreach ($data as $key => $value){ 345 346 // be careful with sub-arrays of $meta['relation'] 347 if ($key == 'relation'){ 348 349 foreach ($value as $subkey => $subvalue){ 350 if(isset($meta['current'][$key][$subkey]) && is_array($meta['current'][$key][$subkey])) { 351 $meta['current'][$key][$subkey] = array_replace($meta['current'][$key][$subkey], (array)$subvalue); 352 } else { 353 $meta['current'][$key][$subkey] = $subvalue; 354 } 355 if($persistent) { 356 if(isset($meta['persistent'][$key][$subkey]) && is_array($meta['persistent'][$key][$subkey])) { 357 $meta['persistent'][$key][$subkey] = array_replace( 358 $meta['persistent'][$key][$subkey], 359 (array) $subvalue 360 ); 361 } else { 362 $meta['persistent'][$key][$subkey] = $subvalue; 363 } 364 } 365 } 366 367 // be careful with some senisitive arrays of $meta 368 } elseif (in_array($key, $protected)){ 369 370 // these keys, must have subkeys - a legitimate value must be an array 371 if (is_array($value)) { 372 $meta['current'][$key] = !empty($meta['current'][$key]) ? 373 array_replace((array)$meta['current'][$key],$value) : 374 $value; 375 376 if ($persistent) { 377 $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ? 378 array_replace((array)$meta['persistent'][$key],$value) : 379 $value; 380 } 381 } 382 383 // no special treatment for the rest 384 } else { 385 $meta['current'][$key] = $value; 386 if ($persistent) $meta['persistent'][$key] = $value; 387 } 388 } 389 390 // save only if metadata changed 391 if ($meta == $orig) return true; 392 393 if (isset($METADATA_RENDERERS[$id])) { 394 // set both keys individually as the renderer has references to the individual keys 395 $METADATA_RENDERERS[$id]['current'] = $meta['current']; 396 $METADATA_RENDERERS[$id]['persistent'] = $meta['persistent']; 397 return true; 398 } else { 399 return p_save_metadata($id, $meta); 400 } 401} 402 403/** 404 * Purges the non-persistant part of the meta data 405 * used on page deletion 406 * 407 * @author Michael Klier <chi@chimeric.de> 408 * 409 * @param string $id page id 410 * @return bool success / fail 411 */ 412function p_purge_metadata($id) { 413 $meta = p_read_metadata($id); 414 foreach($meta['current'] as $key => $value) { 415 if(isset($meta[$key]) && is_array($meta[$key])) { 416 $meta['current'][$key] = array(); 417 } else { 418 $meta['current'][$key] = ''; 419 } 420 421 } 422 return p_save_metadata($id, $meta); 423} 424 425/** 426 * read the metadata from source/cache for $id 427 * (internal use only - called by p_get_metadata & p_set_metadata) 428 * 429 * @author Christopher Smith <chris@jalakai.co.uk> 430 * 431 * @param string $id absolute wiki page id 432 * @param bool $cache whether or not to cache metadata in memory 433 * (only use for metadata likely to be accessed several times) 434 * 435 * @return array metadata 436 */ 437function p_read_metadata($id,$cache=false) { 438 global $cache_metadata; 439 440 if (isset($cache_metadata[(string)$id])) return $cache_metadata[(string)$id]; 441 442 $file = metaFN($id, '.meta'); 443 $meta = file_exists($file) ? 444 unserialize(io_readFile($file, false)) : 445 array('current'=>array(),'persistent'=>array()); 446 447 if ($cache) { 448 $cache_metadata[(string)$id] = $meta; 449 } 450 451 return $meta; 452} 453 454/** 455 * This is the backend function to save a metadata array to a file 456 * 457 * @param string $id absolute wiki page id 458 * @param array $meta metadata 459 * 460 * @return bool success / fail 461 */ 462function p_save_metadata($id, $meta) { 463 // sync cached copies, including $INFO metadata 464 global $cache_metadata, $INFO; 465 466 if (isset($cache_metadata[$id])) $cache_metadata[$id] = $meta; 467 if (!empty($INFO) && isset($INFO['id']) && ($id == $INFO['id'])) { 468 $INFO['meta'] = $meta['current']; 469 } 470 471 return io_saveFile(metaFN($id, '.meta'), serialize($meta)); 472} 473 474/** 475 * renders the metadata of a page 476 * 477 * @author Esther Brunner <esther@kaffeehaus.ch> 478 * 479 * @param string $id page id 480 * @param array $orig the original metadata 481 * @return array|null array('current'=> array,'persistent'=> array); 482 */ 483function p_render_metadata($id, $orig){ 484 // make sure the correct ID is in global ID 485 global $ID, $METADATA_RENDERERS; 486 487 // avoid recursive rendering processes for the same id 488 if (isset($METADATA_RENDERERS[$id])) { 489 return $orig; 490 } 491 492 // store the original metadata in the global $METADATA_RENDERERS so p_set_metadata can use it 493 $METADATA_RENDERERS[$id] =& $orig; 494 495 $keep = $ID; 496 $ID = $id; 497 498 // add an extra key for the event - to tell event handlers the page whose metadata this is 499 $orig['page'] = $id; 500 $evt = new Event('PARSER_METADATA_RENDER', $orig); 501 if ($evt->advise_before()) { 502 503 // get instructions 504 $instructions = p_cached_instructions(wikiFN($id),false,$id); 505 if(is_null($instructions)){ 506 $ID = $keep; 507 unset($METADATA_RENDERERS[$id]); 508 return null; // something went wrong with the instructions 509 } 510 511 // set up the renderer 512 $renderer = new Doku_Renderer_metadata(); 513 $renderer->meta =& $orig['current']; 514 $renderer->persistent =& $orig['persistent']; 515 516 // loop through the instructions 517 foreach ($instructions as $instruction){ 518 // execute the callback against the renderer 519 call_user_func_array(array(&$renderer, $instruction[0]), (array) $instruction[1]); 520 } 521 522 $evt->result = array('current'=>&$renderer->meta,'persistent'=>&$renderer->persistent); 523 } 524 $evt->advise_after(); 525 526 // clean up 527 $ID = $keep; 528 unset($METADATA_RENDERERS[$id]); 529 return $evt->result; 530} 531 532/** 533 * returns all available parser syntax modes in correct order 534 * 535 * @author Andreas Gohr <andi@splitbrain.org> 536 * 537 * @return array[] with for each plugin the array('sort' => sortnumber, 'mode' => mode string, 'obj' => plugin object) 538 */ 539function p_get_parsermodes(){ 540 global $conf; 541 542 //reuse old data 543 static $modes = null; 544 if($modes != null && !defined('DOKU_UNITTEST')){ 545 return $modes; 546 } 547 548 //import parser classes and mode definitions 549 require_once DOKU_INC . 'inc/parser/parser.php'; 550 551 // we now collect all syntax modes and their objects, then they will 552 // be sorted and added to the parser in correct order 553 $modes = array(); 554 555 // add syntax plugins 556 $pluginlist = plugin_list('syntax'); 557 if(count($pluginlist)){ 558 global $PARSER_MODES; 559 $obj = null; 560 foreach($pluginlist as $p){ 561 /** @var \dokuwiki\Extension\SyntaxPlugin $obj */ 562 if(!$obj = plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj 563 $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type 564 //add to modes 565 $modes[] = array( 566 'sort' => $obj->getSort(), 567 'mode' => "plugin_$p", 568 'obj' => $obj, 569 ); 570 unset($obj); //remove the reference 571 } 572 } 573 574 // add default modes 575 $std_modes = array('listblock','preformatted','notoc','nocache', 576 'header','table','linebreak','footnote','hr', 577 'unformatted','php','html','code','file','quote', 578 'internallink','rss','media','externallink', 579 'emaillink','windowssharelink','eol'); 580 if($conf['typography']){ 581 $std_modes[] = 'quotes'; 582 $std_modes[] = 'multiplyentity'; 583 } 584 foreach($std_modes as $m){ 585 $class = 'dokuwiki\\Parsing\\ParserMode\\'.ucfirst($m); 586 $obj = new $class(); 587 $modes[] = array( 588 'sort' => $obj->getSort(), 589 'mode' => $m, 590 'obj' => $obj 591 ); 592 } 593 594 // add formatting modes 595 $fmt_modes = array('strong','emphasis','underline','monospace', 596 'subscript','superscript','deleted'); 597 foreach($fmt_modes as $m){ 598 $obj = new \dokuwiki\Parsing\ParserMode\Formatting($m); 599 $modes[] = array( 600 'sort' => $obj->getSort(), 601 'mode' => $m, 602 'obj' => $obj 603 ); 604 } 605 606 // add modes which need files 607 $obj = new \dokuwiki\Parsing\ParserMode\Smiley(array_keys(getSmileys())); 608 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj' => $obj ); 609 $obj = new \dokuwiki\Parsing\ParserMode\Acronym(array_keys(getAcronyms())); 610 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj' => $obj ); 611 $obj = new \dokuwiki\Parsing\ParserMode\Entity(array_keys(getEntities())); 612 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj' => $obj ); 613 614 // add optional camelcase mode 615 if($conf['camelcase']){ 616 $obj = new \dokuwiki\Parsing\ParserMode\Camelcaselink(); 617 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj' => $obj ); 618 } 619 620 //sort modes 621 usort($modes,'p_sort_modes'); 622 623 return $modes; 624} 625 626/** 627 * Callback function for usort 628 * 629 * @author Andreas Gohr <andi@splitbrain.org> 630 * 631 * @param array $a 632 * @param array $b 633 * @return int $a is lower/equal/higher than $b 634 */ 635function p_sort_modes($a, $b){ 636 if($a['sort'] == $b['sort']) return 0; 637 return ($a['sort'] < $b['sort']) ? -1 : 1; 638} 639 640/** 641 * Renders a list of instruction to the specified output mode 642 * 643 * In the $info array is information from the renderer returned 644 * 645 * @author Harry Fuecks <hfuecks@gmail.com> 646 * @author Andreas Gohr <andi@splitbrain.org> 647 * 648 * @param string $mode 649 * @param array|null|false $instructions 650 * @param array $info returns render info like enabled toc and cache 651 * @param string $date_at 652 * @return null|string rendered output 653 */ 654function p_render($mode,$instructions,&$info,$date_at=''){ 655 if(is_null($instructions)) return ''; 656 if($instructions === false) return ''; 657 658 $Renderer = p_get_renderer($mode); 659 if (is_null($Renderer)) return null; 660 661 $Renderer->reset(); 662 663 if($date_at) { 664 $Renderer->date_at = $date_at; 665 } 666 667 $Renderer->smileys = getSmileys(); 668 $Renderer->entities = getEntities(); 669 $Renderer->acronyms = getAcronyms(); 670 $Renderer->interwiki = getInterwiki(); 671 672 // Loop through the instructions 673 foreach ( $instructions as $instruction ) { 674 // Execute the callback against the Renderer 675 if(method_exists($Renderer, $instruction[0])){ 676 call_user_func_array(array(&$Renderer, $instruction[0]), $instruction[1] ? $instruction[1] : array()); 677 } 678 } 679 680 //set info array 681 $info = $Renderer->info; 682 683 // Post process and return the output 684 $data = array($mode,& $Renderer->doc); 685 Event::createAndTrigger('RENDERER_CONTENT_POSTPROCESS',$data); 686 return $Renderer->doc; 687} 688 689/** 690 * Figure out the correct renderer class to use for $mode, 691 * instantiate and return it 692 * 693 * @param string $mode Mode of the renderer to get 694 * @return null|Doku_Renderer The renderer 695 * 696 * @author Christopher Smith <chris@jalakai.co.uk> 697 */ 698function p_get_renderer($mode) { 699 /** @var PluginController $plugin_controller */ 700 global $conf, $plugin_controller; 701 702 $rname = !empty($conf['renderer_'.$mode]) ? $conf['renderer_'.$mode] : $mode; 703 $rclass = "Doku_Renderer_$rname"; 704 705 // if requested earlier or a bundled renderer 706 if( class_exists($rclass) ) { 707 $Renderer = new $rclass(); 708 return $Renderer; 709 } 710 711 // not bundled, see if its an enabled renderer plugin & when $mode is 'xhtml', the renderer can supply that format. 712 /** @var Doku_Renderer $Renderer */ 713 $Renderer = $plugin_controller->load('renderer',$rname); 714 if ($Renderer && is_a($Renderer, 'Doku_Renderer') && ($mode != 'xhtml' || $mode == $Renderer->getFormat())) { 715 return $Renderer; 716 } 717 718 // there is a configuration error! 719 // not bundled, not a valid enabled plugin, use $mode to try to fallback to a bundled renderer 720 $rclass = "Doku_Renderer_$mode"; 721 if ( class_exists($rclass) ) { 722 // viewers should see renderered output, so restrict the warning to admins only 723 $msg = "No renderer '$rname' found for mode '$mode', check your plugins"; 724 if ($mode == 'xhtml') { 725 $msg .= " and the 'renderer_xhtml' config setting"; 726 } 727 $msg .= ".<br/>Attempting to fallback to the bundled renderer."; 728 msg($msg,-1,'','',MSG_ADMINS_ONLY); 729 730 $Renderer = new $rclass; 731 $Renderer->nocache(); // fallback only (and may include admin alerts), don't cache 732 return $Renderer; 733 } 734 735 // fallback failed, alert the world 736 msg("No renderer '$rname' found for mode '$mode'",-1); 737 return null; 738} 739 740/** 741 * Gets the first heading from a file 742 * 743 * @param string $id dokuwiki page id 744 * @param int $render rerender if first heading not known 745 * default: METADATA_RENDER_USING_SIMPLE_CACHE 746 * Possible values: METADATA_DONT_RENDER, 747 * METADATA_RENDER_USING_SIMPLE_CACHE, 748 * METADATA_RENDER_USING_CACHE, 749 * METADATA_RENDER_UNLIMITED 750 * @return string|null The first heading 751 * 752 * @author Andreas Gohr <andi@splitbrain.org> 753 * @author Michael Hamann <michael@content-space.de> 754 */ 755function p_get_first_heading($id, $render=METADATA_RENDER_USING_SIMPLE_CACHE){ 756 return p_get_metadata(cleanID($id),'title',$render); 757} 758 759/** 760 * Wrapper for GeSHi Code Highlighter, provides caching of its output 761 * 762 * @param string $code source code to be highlighted 763 * @param string $language language to provide highlighting 764 * @param string $wrapper html element to wrap the returned highlighted text 765 * @return string xhtml code 766 * 767 * @author Christopher Smith <chris@jalakai.co.uk> 768 * @author Andreas Gohr <andi@splitbrain.org> 769 */ 770function p_xhtml_cached_geshi($code, $language, $wrapper='pre', array $options=null) { 771 global $conf, $config_cascade, $INPUT; 772 $language = strtolower($language); 773 774 // remove any leading or trailing blank lines 775 $code = preg_replace('/^\s*?\n|\s*?\n$/','',$code); 776 777 $optionsmd5 = md5(serialize($options)); 778 $cache = getCacheName($language.$code.$optionsmd5,".code"); 779 $ctime = @filemtime($cache); 780 if($ctime && !$INPUT->bool('purge') && 781 $ctime > filemtime(DOKU_INC.'vendor/composer/installed.json') && // libraries changed 782 $ctime > filemtime(reset($config_cascade['main']['default']))){ // dokuwiki changed 783 $highlighted_code = io_readFile($cache, false); 784 } else { 785 786 $geshi = new GeSHi($code, $language); 787 $geshi->set_encoding('utf-8'); 788 $geshi->enable_classes(); 789 $geshi->set_header_type(GESHI_HEADER_PRE); 790 $geshi->set_link_target($conf['target']['extern']); 791 if($options !== null) { 792 foreach ($options as $function => $params) { 793 if(is_callable(array($geshi, $function))) { 794 $geshi->$function($params); 795 } 796 } 797 } 798 799 // remove GeSHi's wrapper element (we'll replace it with our own later) 800 // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text 801 $highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!','',$geshi->parse_code()),"\n\r"); 802 io_saveFile($cache,$highlighted_code); 803 } 804 805 // add a wrapper element if required 806 if ($wrapper) { 807 return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>"; 808 } else { 809 return $highlighted_code; 810 } 811} 812 813