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