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