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