1<?php 2 3/** 4 * Utilities for accessing the parser 5 * 6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 7 * @author Harry Fuecks <hfuecks@gmail.com> 8 * @author Andreas Gohr <andi@splitbrain.org> 9 */ 10 11use dokuwiki\Cache\CacheInstructions; 12use dokuwiki\Cache\CacheRenderer; 13use dokuwiki\ChangeLog\PageChangeLog; 14use dokuwiki\Extension\PluginController; 15use dokuwiki\Extension\Event; 16use dokuwiki\Parsing\Handler; 17use dokuwiki\Parsing\ModeRegistry; 18use dokuwiki\Parsing\Parser; 19 20/** 21 * How many pages shall be rendered for getting metadata during one request 22 * at maximum? Note that this limit isn't respected when METADATA_RENDER_UNLIMITED 23 * is passed as render parameter to p_get_metadata. 24 */ 25if (!defined('P_GET_METADATA_RENDER_LIMIT')) define('P_GET_METADATA_RENDER_LIMIT', 5); 26 27/** Don't render metadata even if it is outdated or doesn't exist */ 28define('METADATA_DONT_RENDER', 0); 29/** 30 * Render metadata when the page is really newer or the metadata doesn't exist. 31 * Uses just a simple check, but should work pretty well for loading simple 32 * metadata values like the page title and avoids rendering a lot of pages in 33 * one request. The P_GET_METADATA_RENDER_LIMIT is used in this mode. 34 * Use this if it is unlikely that the metadata value you are requesting 35 * does depend e.g. on pages that are included in the current page using 36 * the include plugin (this is very likely the case for the page title, but 37 * not for relation references). 38 */ 39define('METADATA_RENDER_USING_SIMPLE_CACHE', 1); 40/** 41 * Render metadata using the metadata cache logic. The P_GET_METADATA_RENDER_LIMIT 42 * is used in this mode. Use this mode when you are requesting more complex 43 * metadata. Although this will cause rendering more often it might actually have 44 * the effect that less current metadata is returned as it is more likely than in 45 * the simple cache mode that metadata needs to be rendered for all pages at once 46 * which means that when the metadata for the page is requested that actually needs 47 * to be updated the limit might have been reached already. 48 */ 49define('METADATA_RENDER_USING_CACHE', 2); 50/** 51 * Render metadata without limiting the number of pages for which metadata is 52 * rendered. Use this mode with care, normally it should only be used in places 53 * like the indexer or in cli scripts where the execution time normally isn't 54 * limited. This can be combined with the simple cache using 55 * METADATA_RENDER_USING_CACHE | METADATA_RENDER_UNLIMITED. 56 */ 57define('METADATA_RENDER_UNLIMITED', 4); 58 59/** 60 * Returns the parsed Wikitext in XHTML for the given id and revision. 61 * 62 * If $excuse is true an explanation is returned if the file 63 * wasn't found 64 * 65 * @param string $id page id 66 * @param string|int $rev revision timestamp or empty string 67 * @param bool $excuse 68 * @param string $date_at 69 * 70 * @return null|string 71 * @author Andreas Gohr <andi@splitbrain.org> 72 * 73 */ 74function p_wiki_xhtml($id, $rev = '', $excuse = true, $date_at = '') 75{ 76 $file = wikiFN($id, $rev); 77 $ret = ''; 78 79 //ensure $id is in global $ID (needed for parsing) 80 global $ID; 81 $keep = $ID; 82 $ID = $id; 83 84 if ($rev || $date_at) { 85 if (file_exists($file)) { 86 //no caching on old revisions 87 $ret = p_render('xhtml', p_get_instructions(io_readWikiPage($file, $id, $rev)), $info, $date_at); 88 } elseif ($excuse) { 89 $ret = p_locale_xhtml('norev'); 90 } 91 } elseif (file_exists($file)) { 92 $ret = p_cached_output($file, 'xhtml', $id); 93 } elseif ($excuse) { 94 //check if the page once existed 95 $changelog = new PageChangeLog($id); 96 if ($changelog->hasRevisions()) { 97 $ret = p_locale_xhtml('onceexisted'); 98 } else { 99 $ret = p_locale_xhtml('newpage'); 100 } 101 } 102 103 //restore ID (just in case) 104 $ID = $keep; 105 106 return $ret; 107} 108 109/** 110 * Returns the specified local text in parsed format 111 * 112 * @param string $id page id 113 * @return null|string 114 * @author Andreas Gohr <andi@splitbrain.org> 115 * 116 */ 117function p_locale_xhtml($id) 118{ 119 //fetch parsed locale 120 $data = ['id' => $id, 'html' => '']; 121 122 $event = new Event('PARSER_LOCALE_XHTML', $data); 123 if ($event->advise_before()) { 124 $data['html'] = p_cached_output(localeFN($data['id'])); 125 } 126 $event->advise_after(); 127 128 return $data['html']; 129} 130 131/** 132 * Returns the given file parsed into the requested output format 133 * 134 * @param string $file filename, path to file 135 * @param string $format 136 * @param string $id page id 137 * @param string|null $syntax syntax flavour to parse under; null uses the 138 * configured $conf['syntax']. When passed explicitly it also enters the 139 * cache key so the same file rendered under two syntaxes never collides. 140 * See p_get_instructions(). 141 * @return null|string 142 * @author Andreas Gohr <andi@splitbrain.org> 143 * @author Chris Smith <chris@jalakai.co.uk> 144 * 145 */ 146function p_cached_output($file, $format = 'xhtml', $id = '', $syntax = null) 147{ 148 global $conf; 149 150 $cache = new CacheRenderer($id, $file, $format, $syntax); 151 if ($cache->useCache()) { 152 $parsed = $cache->retrieveCache(false); 153 if ($conf['allowdebug'] && $format == 'xhtml') { 154 $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n"; 155 } 156 } else { 157 $parsed = p_render($format, p_cached_instructions($file, false, $id, $syntax), $info); 158 159 if (!empty($info['cache']) && $cache->storeCache($parsed)) { // storeCache() attempts to save cachefile 160 if ($conf['allowdebug'] && $format == 'xhtml') { 161 $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n"; 162 } 163 } else { 164 $cache->removeCache(); //try to delete cachefile 165 if ($conf['allowdebug'] && $format == 'xhtml') { 166 $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n"; 167 } 168 } 169 } 170 171 return $parsed; 172} 173 174/** 175 * Returns the render instructions for a file 176 * 177 * Uses and creates a serialized cache file 178 * 179 * @param string $file filename, path to file 180 * @param bool $cacheonly 181 * @param string $id page id 182 * @param string|null $syntax syntax flavour to parse under; null uses the 183 * configured $conf['syntax']. See p_get_instructions(). 184 * @return array|null 185 * @author Andreas Gohr <andi@splitbrain.org> 186 * 187 */ 188function p_cached_instructions($file, $cacheonly = false, $id = '', $syntax = null) 189{ 190 static $run = null; 191 if (is_null($run)) $run = []; 192 193 // The in-request memo and the on-disk cache are both keyed on $syntax so 194 // the same file rendered under two syntaxes in one request (e.g. a plugin 195 // forcing 'dw' on a doc whose configured syntax is 'md') does not collide. 196 $runKey = $file . '|' . ($syntax ?? ''); 197 198 $cache = new CacheInstructions($id, $file, $syntax); 199 200 if ($cacheonly || $cache->useCache() || (isset($run[$runKey]) && !defined('DOKU_UNITTEST'))) { 201 return $cache->retrieveCache(); 202 } elseif (file_exists($file)) { 203 // no cache - do some work 204 $ins = p_get_instructions(io_readWikiPage($file, $id), $syntax); 205 if ($cache->storeCache($ins)) { 206 $run[$runKey] = true; // we won't rebuild these instructions in the same run again 207 } else { 208 msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.', -1); 209 } 210 return $ins; 211 } 212 213 return null; 214} 215 216/** 217 * turns a page into a list of instructions 218 * 219 * This is the one place in the parser pipeline where the configured wiki 220 * syntax preference ($conf['syntax']) is read. From here on the syntax is 221 * a parameter carried by the ModeRegistry, never a global lookup. 222 * 223 * @param string $text raw wiki syntax text 224 * @param string|null $syntax syntax flavour to parse under: 'dw', 'md', 225 * 'dw+md' or 'md+dw'. null (the default) means "use the configured 226 * $conf['syntax']" — appropriate for user content. Locale/bundled 227 * callers pass 'dw' explicitly. 228 * @return array a list of instruction arrays 229 * @author Harry Fuecks <hfuecks@gmail.com> 230 * @author Andreas Gohr <andi@splitbrain.org> 231 * 232 */ 233function p_get_instructions($text, $syntax = null) 234{ 235 global $conf; 236 237 $registry = new ModeRegistry($syntax ?? $conf['syntax']); 238 239 // Create the parser and handler 240 $Handler = new Handler($registry); 241 $Parser = new Parser($Handler, $registry); 242 243 //add modes to parser 244 foreach ($registry->getModes() as $mode) { 245 $Parser->addMode($mode['mode'], $mode['obj']); 246 } 247 248 // Do the parsing 249 Event::createAndTrigger('PARSER_WIKITEXT_PREPROCESS', $text); 250 return $Parser->parse($text); 251} 252 253/** 254 * returns the metadata of a page 255 * 256 * @param string $id The id of the page the metadata should be returned from 257 * @param string $key The key of the metdata value that shall be read (by default everything) 258 * separate hierarchies by " " like "date created" 259 * @param int $render If the page should be rendererd - possible values: 260 * METADATA_DONT_RENDER, METADATA_RENDER_USING_SIMPLE_CACHE, METADATA_RENDER_USING_CACHE 261 * METADATA_RENDER_UNLIMITED (also combined with the previous two options), 262 * default: METADATA_RENDER_USING_CACHE 263 * @return mixed The requested metadata fields 264 * 265 * @author Esther Brunner <esther@kaffeehaus.ch> 266 * @author Michael Hamann <michael@content-space.de> 267 */ 268function p_get_metadata($id, $key = '', $render = METADATA_RENDER_USING_CACHE) 269{ 270 global $ID; 271 static $render_count = 0; 272 // track pages that have already been rendered in order to avoid rendering the same page 273 // again 274 static $rendered_pages = []; 275 276 // cache the current page 277 // Benchmarking shows the current page's metadata is generally the only page metadata 278 // accessed several times. This may catch a few other pages, but that shouldn't be an issue. 279 $cache = ($ID == $id); 280 $meta = p_read_metadata($id, $cache); 281 282 if (!is_numeric($render)) { 283 if ($render) { 284 $render = METADATA_RENDER_USING_SIMPLE_CACHE; 285 } else { 286 $render = METADATA_DONT_RENDER; 287 } 288 } 289 290 // prevent recursive calls in the cache 291 static $recursion = false; 292 if (!$recursion && $render != METADATA_DONT_RENDER && !isset($rendered_pages[$id]) && page_exists($id)) { 293 $recursion = true; 294 295 $cachefile = new CacheRenderer($id, wikiFN($id), 'metadata'); 296 297 $do_render = false; 298 if ($render & METADATA_RENDER_UNLIMITED || $render_count < P_GET_METADATA_RENDER_LIMIT) { 299 if ($render & METADATA_RENDER_USING_SIMPLE_CACHE) { 300 $pagefn = wikiFN($id); 301 $metafn = metaFN($id, '.meta'); 302 if (!file_exists($metafn) || @filemtime($pagefn) > @filemtime($cachefile->cache)) { 303 $do_render = true; 304 } 305 } elseif (!$cachefile->useCache()) { 306 $do_render = true; 307 } 308 } 309 if ($do_render) { 310 if (!defined('DOKU_UNITTEST')) { 311 ++$render_count; 312 $rendered_pages[$id] = true; 313 } 314 $old_meta = $meta; 315 $meta = p_render_metadata($id, $meta); 316 // only update the file when the metadata has been changed 317 if ($meta == $old_meta || p_save_metadata($id, $meta)) { 318 // store a timestamp in order to make sure that the cachefile is touched 319 // this timestamp is also stored when the meta data is still the same 320 $cachefile->storeCache(time()); 321 } else { 322 msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.', -1); 323 } 324 } 325 326 $recursion = false; 327 } 328 329 $val = $meta['current'] ?? null; 330 331 // filter by $key 332 foreach (preg_split('/\s+/', $key, 2, PREG_SPLIT_NO_EMPTY) as $cur_key) { 333 if (!isset($val[$cur_key])) { 334 return null; 335 } 336 $val = $val[$cur_key]; 337 } 338 return $val; 339} 340 341/** 342 * sets metadata elements of a page 343 * 344 * @see http://www.dokuwiki.org/devel:metadata#functions_to_get_and_set_metadata 345 * 346 * @param string $id is the ID of a wiki page 347 * @param array $data is an array with key ⇒ value pairs to be set in the metadata 348 * @param boolean $render whether or not the page metadata should be generated with the renderer 349 * @param boolean $persistent indicates whether or not the particular metadata value will persist through 350 * the next metadata rendering. 351 * @return boolean true on success 352 * 353 * @author Esther Brunner <esther@kaffeehaus.ch> 354 * @author Michael Hamann <michael@content-space.de> 355 */ 356function p_set_metadata($id, $data, $render = false, $persistent = true) 357{ 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 = ['description', 'date', 'contributor']; 377 foreach ($data as $key => $value) { 378 // be careful with sub-arrays of $meta['relation'] 379 if ($key == 'relation') { 380 foreach ($value as $subkey => $subvalue) { 381 if (isset($meta['current'][$key][$subkey]) && is_array($meta['current'][$key][$subkey])) { 382 $meta['current'][$key][$subkey] = array_replace($meta['current'][$key][$subkey], (array)$subvalue); 383 } else { 384 $meta['current'][$key][$subkey] = $subvalue; 385 } 386 if ($persistent) { 387 if (isset($meta['persistent'][$key][$subkey]) && is_array($meta['persistent'][$key][$subkey])) { 388 $meta['persistent'][$key][$subkey] = array_replace( 389 $meta['persistent'][$key][$subkey], 390 (array)$subvalue 391 ); 392 } else { 393 $meta['persistent'][$key][$subkey] = $subvalue; 394 } 395 } 396 } 397 398 // be careful with some senisitive arrays of $meta 399 } elseif (in_array($key, $protected, true)) { 400 // these keys, must have subkeys - a legitimate value must be an array 401 if (is_array($value)) { 402 $meta['current'][$key] = empty($meta['current'][$key]) ? 403 $value : 404 array_replace((array)$meta['current'][$key], $value); 405 406 if ($persistent) { 407 $meta['persistent'][$key] = empty($meta['persistent'][$key]) ? 408 $value : 409 array_replace((array)$meta['persistent'][$key], $value); 410 } 411 } 412 413 // no special treatment for the rest 414 } else { 415 $meta['current'][$key] = $value; 416 if ($persistent) $meta['persistent'][$key] = $value; 417 } 418 } 419 420 // save only if metadata changed 421 if ($meta == $orig) return true; 422 423 if (isset($METADATA_RENDERERS[$id])) { 424 // set both keys individually as the renderer has references to the individual keys 425 $METADATA_RENDERERS[$id]['current'] = $meta['current']; 426 $METADATA_RENDERERS[$id]['persistent'] = $meta['persistent']; 427 return true; 428 } else { 429 return p_save_metadata($id, $meta); 430 } 431} 432 433/** 434 * Purges the non-persistant part of the meta data 435 * used on page deletion 436 * 437 * @param string $id page id 438 * @return bool success / fail 439 * @author Michael Klier <chi@chimeric.de> 440 * 441 */ 442function p_purge_metadata($id) 443{ 444 $meta = p_read_metadata($id); 445 foreach ($meta['current'] as $key => $value) { 446 if (isset($meta[$key]) && is_array($meta[$key])) { 447 $meta['current'][$key] = []; 448 } else { 449 $meta['current'][$key] = ''; 450 } 451 } 452 return p_save_metadata($id, $meta); 453} 454 455/** 456 * read the metadata from source/cache for $id 457 * (internal use only - called by p_get_metadata & p_set_metadata) 458 * 459 * @param string $id absolute wiki page id 460 * @param bool $cache whether or not to cache metadata in memory 461 * (only use for metadata likely to be accessed several times) 462 * 463 * @return array metadata 464 * @author Christopher Smith <chris@jalakai.co.uk> 465 * 466 */ 467function p_read_metadata($id, $cache = false) 468{ 469 global $cache_metadata; 470 471 if (isset($cache_metadata[(string)$id])) return $cache_metadata[(string)$id]; 472 473 $file = metaFN($id, '.meta'); 474 $meta = file_exists($file) ? 475 unserialize(io_readFile($file, false)) : 476 ['current' => [], 'persistent' => []]; 477 478 if ($cache) { 479 $cache_metadata[(string)$id] = $meta; 480 } 481 482 return $meta; 483} 484 485/** 486 * This is the backend function to save a metadata array to a file 487 * 488 * @param string $id absolute wiki page id 489 * @param array $meta metadata 490 * 491 * @return bool success / fail 492 */ 493function p_save_metadata($id, $meta) 494{ 495 // sync cached copies, including $INFO metadata 496 global $cache_metadata, $INFO; 497 498 if (isset($cache_metadata[$id])) $cache_metadata[$id] = $meta; 499 if (!empty($INFO) && isset($INFO['id']) && ($id == $INFO['id'])) { 500 $INFO['meta'] = $meta['current']; 501 } 502 503 return io_saveFile(metaFN($id, '.meta'), serialize($meta)); 504} 505 506/** 507 * renders the metadata of a page 508 * 509 * @param string $id page id 510 * @param array $orig the original metadata 511 * @return array|null array('current'=> array,'persistent'=> array); 512 * @author Esther Brunner <esther@kaffeehaus.ch> 513 * 514 */ 515function p_render_metadata($id, $orig) 516{ 517 // make sure the correct ID is in global ID 518 global $ID, $METADATA_RENDERERS; 519 520 // avoid recursive rendering processes for the same id 521 if (isset($METADATA_RENDERERS[$id])) { 522 return $orig; 523 } 524 525 // store the original metadata in the global $METADATA_RENDERERS so p_set_metadata can use it 526 $METADATA_RENDERERS[$id] =& $orig; 527 528 $keep = $ID; 529 $ID = $id; 530 531 // add an extra key for the event - to tell event handlers the page whose metadata this is 532 $orig['page'] = $id; 533 $evt = new Event('PARSER_METADATA_RENDER', $orig); 534 if ($evt->advise_before()) { 535 // get instructions 536 $instructions = p_cached_instructions(wikiFN($id), false, $id); 537 if (is_null($instructions)) { 538 $ID = $keep; 539 unset($METADATA_RENDERERS[$id]); 540 return null; // something went wrong with the instructions 541 } 542 543 // set up the renderer 544 $renderer = new Doku_Renderer_metadata(); 545 $renderer->meta =& $orig['current']; 546 $renderer->persistent =& $orig['persistent']; 547 548 // loop through the instructions 549 foreach ($instructions as $instruction) { 550 // execute the callback against the renderer 551 call_user_func_array([&$renderer, $instruction[0]], (array)$instruction[1]); 552 } 553 554 $evt->result = ['current' => &$renderer->meta, 'persistent' => &$renderer->persistent]; 555 } 556 $evt->advise_after(); 557 558 // clean up 559 $ID = $keep; 560 unset($METADATA_RENDERERS[$id]); 561 return $evt->result; 562} 563 564/** 565 * Renders a list of instruction to the specified output mode 566 * 567 * In the $info array is information from the renderer returned 568 * 569 * @param string $mode 570 * @param array|null|false $instructions 571 * @param array $info returns render info like enabled toc and cache 572 * @param string $date_at 573 * @return null|string rendered output 574 * @author Andreas Gohr <andi@splitbrain.org> 575 * 576 * @author Harry Fuecks <hfuecks@gmail.com> 577 */ 578function p_render($mode, $instructions, &$info, $date_at = '') 579{ 580 if (is_null($instructions)) return ''; 581 if ($instructions === false) return ''; 582 583 $Renderer = p_get_renderer($mode); 584 if (is_null($Renderer)) return null; 585 586 $Renderer->reset(); 587 588 if ($date_at) { 589 $Renderer->date_at = $date_at; 590 } 591 592 $Renderer->smileys = getSmileys(); 593 $Renderer->entities = getEntities(); 594 $Renderer->acronyms = getAcronyms(); 595 $Renderer->interwiki = getInterwiki(); 596 597 // Loop through the instructions 598 foreach ($instructions as $instruction) { 599 // Execute the callback against the Renderer 600 if (method_exists($Renderer, $instruction[0])) { 601 call_user_func_array([&$Renderer, $instruction[0]], $instruction[1] ?: []); 602 } 603 } 604 605 //set info array 606 $info = $Renderer->info; 607 608 // Post process and return the output 609 $data = [$mode, & $Renderer->doc]; 610 Event::createAndTrigger('RENDERER_CONTENT_POSTPROCESS', $data); 611 return $Renderer->doc; 612} 613 614/** 615 * Figure out the correct renderer class to use for $mode, 616 * instantiate and return it 617 * 618 * @param string $mode Mode of the renderer to get 619 * @return null|Doku_Renderer The renderer 620 * 621 * @author Christopher Smith <chris@jalakai.co.uk> 622 */ 623function p_get_renderer($mode) 624{ 625 /** @var PluginController $plugin_controller */ 626 global $conf, $plugin_controller; 627 628 $rname = empty($conf['renderer_' . $mode]) ? $mode : $conf['renderer_' . $mode]; 629 $rclass = "Doku_Renderer_$rname"; 630 631 // if requested earlier or a bundled renderer 632 if (class_exists($rclass)) { 633 return new $rclass(); 634 } 635 636 // not bundled, see if its an enabled renderer plugin & when $mode is 'xhtml', the renderer can supply that format. 637 /** @var Doku_Renderer $Renderer */ 638 $Renderer = $plugin_controller->load('renderer', $rname); 639 if ($Renderer && is_a($Renderer, 'Doku_Renderer') && ($mode != 'xhtml' || $mode == $Renderer->getFormat())) { 640 return $Renderer; 641 } 642 643 // there is a configuration error! 644 // not bundled, not a valid enabled plugin, use $mode to try to fallback to a bundled renderer 645 $rclass = "Doku_Renderer_$mode"; 646 if (class_exists($rclass)) { 647 // viewers should see renderered output, so restrict the warning to admins only 648 $msg = "No renderer '$rname' found for mode '$mode', check your plugins"; 649 if ($mode == 'xhtml') { 650 $msg .= " and the 'renderer_xhtml' config setting"; 651 } 652 $msg .= ".<br/>Attempting to fallback to the bundled renderer."; 653 msg($msg, -1, '', '', MSG_ADMINS_ONLY); 654 655 $Renderer = new $rclass(); 656 $Renderer->nocache(); // fallback only (and may include admin alerts), don't cache 657 return $Renderer; 658 } 659 660 // fallback failed, alert the world 661 msg("No renderer '$rname' found for mode '$mode'", -1); 662 return null; 663} 664 665/** 666 * Gets the first heading from a file 667 * 668 * @param string $id dokuwiki page id 669 * @param int $render rerender if first heading not known 670 * default: METADATA_RENDER_USING_SIMPLE_CACHE 671 * Possible values: METADATA_DONT_RENDER, 672 * METADATA_RENDER_USING_SIMPLE_CACHE, 673 * METADATA_RENDER_USING_CACHE, 674 * METADATA_RENDER_UNLIMITED 675 * @return string|null The first heading 676 * 677 * @author Andreas Gohr <andi@splitbrain.org> 678 * @author Michael Hamann <michael@content-space.de> 679 */ 680function p_get_first_heading($id, $render = METADATA_RENDER_USING_SIMPLE_CACHE) 681{ 682 return p_get_metadata(cleanID($id), 'title', $render); 683} 684 685/** 686 * Wrapper for GeSHi Code Highlighter, provides caching of its output 687 * 688 * @param string $code source code to be highlighted 689 * @param string $language language to provide highlighting 690 * @param string $wrapper html element to wrap the returned highlighted text 691 * @return string xhtml code 692 * 693 * @author Christopher Smith <chris@jalakai.co.uk> 694 * @author Andreas Gohr <andi@splitbrain.org> 695 */ 696function p_xhtml_cached_geshi($code, $language, $wrapper = 'pre', ?array $options = null) 697{ 698 global $conf, $config_cascade, $INPUT; 699 $language = strtolower($language); 700 701 // remove any leading or trailing blank lines 702 $code = preg_replace('/^\s*?\n|\s*?\n$/', '', $code); 703 704 $optionsmd5 = md5(serialize($options)); 705 $cache = getCacheName($language . $code . $optionsmd5, ".code"); 706 $ctime = @filemtime($cache); 707 if ( 708 $ctime && !$INPUT->bool('purge') && 709 $ctime > filemtime(DOKU_INC . 'vendor/composer/installed.json') && // libraries changed 710 $ctime > filemtime(reset($config_cascade['main']['default'])) 711 ) { // dokuwiki changed 712 $highlighted_code = io_readFile($cache, false); 713 } else { 714 $geshi = new GeSHi($code, $language); 715 $geshi->set_encoding('utf-8'); 716 $geshi->enable_classes(); 717 $geshi->set_header_type(GESHI_HEADER_PRE); 718 $geshi->set_link_target($conf['target']['extern']); 719 if ($options !== null) { 720 foreach ($options as $function => $params) { 721 if (is_callable([$geshi, $function])) { 722 $geshi->$function($params); 723 } 724 } 725 } 726 727 // remove GeSHi's wrapper element (we'll replace it with our own later) 728 // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text 729 $highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!', '', $geshi->parse_code()), "\n\r"); 730 io_saveFile($cache, $highlighted_code); 731 } 732 733 // add a wrapper element if required 734 if ($wrapper) { 735 return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>"; 736 } else { 737 return $highlighted_code; 738 } 739} 740