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