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