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])) { 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 ++$render_count; 315 $rendered_pages[$id] = true; 316 $old_meta = $meta; 317 $meta = p_render_metadata($id, $meta); 318 // only update the file when the metadata has been changed 319 if ($meta == $old_meta || p_save_metadata($id, $meta)) { 320 // store a timestamp in order to make sure that the cachefile is touched 321 $cachefile->storeCache(time()); 322 } elseif ($meta != $old_meta) { 323 msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.',-1); 324 } 325 } 326 327 $recursion = false; 328 } 329 330 $val = $meta['current']; 331 332 // filter by $key 333 foreach(preg_split('/\s+/', $key, 2, PREG_SPLIT_NO_EMPTY) as $cur_key) { 334 if (!isset($val[$cur_key])) { 335 return null; 336 } 337 $val = $val[$cur_key]; 338 } 339 return $val; 340} 341 342/** 343 * sets metadata elements of a page 344 * 345 * @see http://www.dokuwiki.org/devel:metadata#functions_to_get_and_set_metadata 346 * 347 * @param String $id is the ID of a wiki page 348 * @param Array $data is an array with key ⇒ value pairs to be set in the metadata 349 * @param Boolean $render whether or not the page metadata should be generated with the renderer 350 * @param Boolean $persistent indicates whether or not the particular metadata value will persist through 351 * the next metadata rendering. 352 * @return boolean true on success 353 * 354 * @author Esther Brunner <esther@kaffeehaus.ch> 355 * @author Michael Hamann <michael@content-space.de> 356 */ 357function p_set_metadata($id, $data, $render=false, $persistent=true){ 358 if (!is_array($data)) return false; 359 360 global $ID, $METADATA_RENDERERS; 361 362 // if there is currently a renderer change the data in the renderer instead 363 if (isset($METADATA_RENDERERS[$id])) { 364 $orig =& $METADATA_RENDERERS[$id]; 365 $meta = $orig; 366 } else { 367 // cache the current page 368 $cache = ($ID == $id); 369 $orig = p_read_metadata($id, $cache); 370 371 // render metadata first? 372 $meta = $render ? p_render_metadata($id, $orig) : $orig; 373 } 374 375 // now add the passed metadata 376 $protected = array('description', 'date', 'contributor'); 377 foreach ($data as $key => $value){ 378 379 // be careful with sub-arrays of $meta['relation'] 380 if ($key == 'relation'){ 381 382 foreach ($value as $subkey => $subvalue){ 383 $meta['current'][$key][$subkey] = !empty($meta['current'][$key][$subkey]) ? array_merge($meta['current'][$key][$subkey], $subvalue) : $subvalue; 384 if ($persistent) 385 $meta['persistent'][$key][$subkey] = !empty($meta['persistent'][$key][$subkey]) ? array_merge($meta['persistent'][$key][$subkey], $subvalue) : $subvalue; 386 } 387 388 // be careful with some senisitive arrays of $meta 389 } elseif (in_array($key, $protected)){ 390 391 // these keys, must have subkeys - a legitimate value must be an array 392 if (is_array($value)) { 393 $meta['current'][$key] = !empty($meta['current'][$key]) ? array_merge($meta['current'][$key],$value) : $value; 394 395 if ($persistent) { 396 $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ? array_merge($meta['persistent'][$key],$value) : $value; 397 } 398 } 399 400 // no special treatment for the rest 401 } else { 402 $meta['current'][$key] = $value; 403 if ($persistent) $meta['persistent'][$key] = $value; 404 } 405 } 406 407 // save only if metadata changed 408 if ($meta == $orig) return true; 409 410 if (isset($METADATA_RENDERERS[$id])) { 411 // set both keys individually as the renderer has references to the individual keys 412 $METADATA_RENDERERS[$id]['current'] = $meta['current']; 413 $METADATA_RENDERERS[$id]['persistent'] = $meta['persistent']; 414 } else { 415 return p_save_metadata($id, $meta); 416 } 417} 418 419/** 420 * Purges the non-persistant part of the meta data 421 * used on page deletion 422 * 423 * @author Michael Klier <chi@chimeric.de> 424 */ 425function p_purge_metadata($id) { 426 $meta = p_read_metadata($id); 427 foreach($meta['current'] as $key => $value) { 428 if(is_array($meta[$key])) { 429 $meta['current'][$key] = array(); 430 } else { 431 $meta['current'][$key] = ''; 432 } 433 434 } 435 return p_save_metadata($id, $meta); 436} 437 438/** 439 * read the metadata from source/cache for $id 440 * (internal use only - called by p_get_metadata & p_set_metadata) 441 * 442 * @author Christopher Smith <chris@jalakai.co.uk> 443 * 444 * @param string $id absolute wiki page id 445 * @param bool $cache whether or not to cache metadata in memory 446 * (only use for metadata likely to be accessed several times) 447 * 448 * @return array metadata 449 */ 450function p_read_metadata($id,$cache=false) { 451 global $cache_metadata; 452 453 if (isset($cache_metadata[(string)$id])) return $cache_metadata[(string)$id]; 454 455 $file = metaFN($id, '.meta'); 456 $meta = @file_exists($file) ? unserialize(io_readFile($file, false)) : array('current'=>array(),'persistent'=>array()); 457 458 if ($cache) { 459 $cache_metadata[(string)$id] = $meta; 460 } 461 462 return $meta; 463} 464 465/** 466 * This is the backend function to save a metadata array to a file 467 * 468 * @param string $id absolute wiki page id 469 * @param array $meta metadata 470 * 471 * @return bool success / fail 472 */ 473function p_save_metadata($id, $meta) { 474 // sync cached copies, including $INFO metadata 475 global $cache_metadata, $INFO; 476 477 if (isset($cache_metadata[$id])) $cache_metadata[$id] = $meta; 478 if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; } 479 480 return io_saveFile(metaFN($id, '.meta'), serialize($meta)); 481} 482 483/** 484 * renders the metadata of a page 485 * 486 * @author Esther Brunner <esther@kaffeehaus.ch> 487 */ 488function p_render_metadata($id, $orig){ 489 // make sure the correct ID is in global ID 490 global $ID, $METADATA_RENDERERS; 491 492 // avoid recursive rendering processes for the same id 493 if (isset($METADATA_RENDERERS[$id])) 494 return $orig; 495 496 // store the original metadata in the global $METADATA_RENDERERS so p_set_metadata can use it 497 $METADATA_RENDERERS[$id] =& $orig; 498 499 $keep = $ID; 500 $ID = $id; 501 502 // add an extra key for the event - to tell event handlers the page whose metadata this is 503 $orig['page'] = $id; 504 $evt = new Doku_Event('PARSER_METADATA_RENDER', $orig); 505 if ($evt->advise_before()) { 506 507 require_once DOKU_INC."inc/parser/metadata.php"; 508 509 // get instructions 510 $instructions = p_cached_instructions(wikiFN($id),false,$id); 511 if(is_null($instructions)){ 512 $ID = $keep; 513 unset($METADATA_RENDERERS[$id]); 514 return null; // something went wrong with the instructions 515 } 516 517 // set up the renderer 518 $renderer = new Doku_Renderer_metadata(); 519 $renderer->meta =& $orig['current']; 520 $renderer->persistent =& $orig['persistent']; 521 522 // loop through the instructions 523 foreach ($instructions as $instruction){ 524 // execute the callback against the renderer 525 call_user_func_array(array(&$renderer, $instruction[0]), (array) $instruction[1]); 526 } 527 528 $evt->result = array('current'=>&$renderer->meta,'persistent'=>&$renderer->persistent); 529 } 530 $evt->advise_after(); 531 532 // clean up 533 $ID = $keep; 534 unset($METADATA_RENDERERS[$id]); 535 return $evt->result; 536} 537 538/** 539 * returns all available parser syntax modes in correct order 540 * 541 * @author Andreas Gohr <andi@splitbrain.org> 542 */ 543function p_get_parsermodes(){ 544 global $conf; 545 546 //reuse old data 547 static $modes = null; 548 if($modes != null){ 549 return $modes; 550 } 551 552 //import parser classes and mode definitions 553 require_once DOKU_INC . 'inc/parser/parser.php'; 554 555 // we now collect all syntax modes and their objects, then they will 556 // be sorted and added to the parser in correct order 557 $modes = array(); 558 559 // add syntax plugins 560 $pluginlist = plugin_list('syntax'); 561 if(count($pluginlist)){ 562 global $PARSER_MODES; 563 $obj = null; 564 foreach($pluginlist as $p){ 565 if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj 566 $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type 567 //add to modes 568 $modes[] = array( 569 'sort' => $obj->getSort(), 570 'mode' => "plugin_$p", 571 'obj' => $obj, 572 ); 573 unset($obj); //remove the reference 574 } 575 } 576 577 // add default modes 578 $std_modes = array('listblock','preformatted','notoc','nocache', 579 'header','table','linebreak','footnote','hr', 580 'unformatted','php','html','code','file','quote', 581 'internallink','rss','media','externallink', 582 'emaillink','windowssharelink','eol'); 583 if($conf['typography']){ 584 $std_modes[] = 'quotes'; 585 $std_modes[] = 'multiplyentity'; 586 } 587 foreach($std_modes as $m){ 588 $class = "Doku_Parser_Mode_$m"; 589 $obj = new $class(); 590 $modes[] = array( 591 'sort' => $obj->getSort(), 592 'mode' => $m, 593 'obj' => $obj 594 ); 595 } 596 597 // add formatting modes 598 $fmt_modes = array('strong','emphasis','underline','monospace', 599 'subscript','superscript','deleted'); 600 foreach($fmt_modes as $m){ 601 $obj = new Doku_Parser_Mode_formatting($m); 602 $modes[] = array( 603 'sort' => $obj->getSort(), 604 'mode' => $m, 605 'obj' => $obj 606 ); 607 } 608 609 // add modes which need files 610 $obj = new Doku_Parser_Mode_smiley(array_keys(getSmileys())); 611 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj' => $obj ); 612 $obj = new Doku_Parser_Mode_acronym(array_keys(getAcronyms())); 613 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj' => $obj ); 614 $obj = new Doku_Parser_Mode_entity(array_keys(getEntities())); 615 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj' => $obj ); 616 617 // add optional camelcase mode 618 if($conf['camelcase']){ 619 $obj = new Doku_Parser_Mode_camelcaselink(); 620 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj' => $obj ); 621 } 622 623 //sort modes 624 usort($modes,'p_sort_modes'); 625 626 return $modes; 627} 628 629/** 630 * Callback function for usort 631 * 632 * @author Andreas Gohr <andi@splitbrain.org> 633 */ 634function p_sort_modes($a, $b){ 635 if($a['sort'] == $b['sort']) return 0; 636 return ($a['sort'] < $b['sort']) ? -1 : 1; 637} 638 639/** 640 * Renders a list of instruction to the specified output mode 641 * 642 * In the $info array is information from the renderer returned 643 * 644 * @author Harry Fuecks <hfuecks@gmail.com> 645 * @author Andreas Gohr <andi@splitbrain.org> 646 */ 647function p_render($mode,$instructions,&$info){ 648 if(is_null($instructions)) return ''; 649 650 $Renderer =& p_get_renderer($mode); 651 if (is_null($Renderer)) return null; 652 653 $Renderer->reset(); 654 655 $Renderer->smileys = getSmileys(); 656 $Renderer->entities = getEntities(); 657 $Renderer->acronyms = getAcronyms(); 658 $Renderer->interwiki = getInterwiki(); 659 660 // Loop through the instructions 661 foreach ( $instructions as $instruction ) { 662 // Execute the callback against the Renderer 663 call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]); 664 } 665 666 //set info array 667 $info = $Renderer->info; 668 669 // Post process and return the output 670 $data = array($mode,& $Renderer->doc); 671 trigger_event('RENDERER_CONTENT_POSTPROCESS',$data); 672 return $Renderer->doc; 673} 674 675function & p_get_renderer($mode) { 676 global $conf, $plugin_controller; 677 678 $rname = !empty($conf['renderer_'.$mode]) ? $conf['renderer_'.$mode] : $mode; 679 680 // try default renderer first: 681 $file = DOKU_INC."inc/parser/$rname.php"; 682 if(@file_exists($file)){ 683 require_once $file; 684 $rclass = "Doku_Renderer_$rname"; 685 686 if ( !class_exists($rclass) ) { 687 trigger_error("Unable to resolve render class $rclass",E_USER_WARNING); 688 msg("Renderer '$rname' for $mode not valid",-1); 689 return null; 690 } 691 $Renderer = new $rclass(); 692 }else{ 693 // Maybe a plugin/component is available? 694 list($plugin, $component) = $plugin_controller->_splitName($rname); 695 if (!$plugin_controller->isdisabled($plugin)){ 696 $Renderer =& $plugin_controller->load('renderer',$rname); 697 } 698 699 if(is_null($Renderer)){ 700 msg("No renderer '$rname' found for mode '$mode'",-1); 701 return null; 702 } 703 } 704 705 return $Renderer; 706} 707 708/** 709 * Gets the first heading from a file 710 * 711 * @param string $id dokuwiki page id 712 * @param int $render rerender if first heading not known 713 * default: METADATA_RENDER_USING_SIMPLE_CACHE 714 * Possible values: METADATA_DONT_RENDER, 715 * METADATA_RENDER_USING_SIMPLE_CACHE, 716 * METADATA_RENDER_USING_CACHE, 717 * METADATA_RENDER_UNLIMITED 718 * 719 * @author Andreas Gohr <andi@splitbrain.org> 720 * @author Michael Hamann <michael@content-space.de> 721 */ 722function p_get_first_heading($id, $render=METADATA_RENDER_USING_SIMPLE_CACHE){ 723 return p_get_metadata(cleanID($id),'title',$render); 724} 725 726/** 727 * Wrapper for GeSHi Code Highlighter, provides caching of its output 728 * 729 * @param string $code source code to be highlighted 730 * @param string $language language to provide highlighting 731 * @param string $wrapper html element to wrap the returned highlighted text 732 * 733 * @author Christopher Smith <chris@jalakai.co.uk> 734 * @author Andreas Gohr <andi@splitbrain.org> 735 */ 736function p_xhtml_cached_geshi($code, $language, $wrapper='pre') { 737 global $conf, $config_cascade; 738 $language = strtolower($language); 739 740 // remove any leading or trailing blank lines 741 $code = preg_replace('/^\s*?\n|\s*?\n$/','',$code); 742 743 $cache = getCacheName($language.$code,".code"); 744 $ctime = @filemtime($cache); 745 if($ctime && !$_REQUEST['purge'] && 746 $ctime > filemtime(DOKU_INC.'inc/geshi.php') && // geshi changed 747 $ctime > @filemtime(DOKU_INC.'inc/geshi/'.$language.'.php') && // language syntax definition changed 748 $ctime > filemtime(reset($config_cascade['main']['default']))){ // dokuwiki changed 749 $highlighted_code = io_readFile($cache, false); 750 751 } else { 752 753 $geshi = new GeSHi($code, $language, DOKU_INC . 'inc/geshi'); 754 $geshi->set_encoding('utf-8'); 755 $geshi->enable_classes(); 756 $geshi->set_header_type(GESHI_HEADER_PRE); 757 $geshi->set_link_target($conf['target']['extern']); 758 759 // remove GeSHi's wrapper element (we'll replace it with our own later) 760 // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text 761 $highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!','',$geshi->parse_code()),"\n\r"); 762 io_saveFile($cache,$highlighted_code); 763 } 764 765 // add a wrapper element if required 766 if ($wrapper) { 767 return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>"; 768 } else { 769 return $highlighted_code; 770 } 771} 772 773