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