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