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