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