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 10 if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/'); 11 12 require_once(DOKU_INC.'inc/confutils.php'); 13 require_once(DOKU_INC.'inc/pageutils.php'); 14 require_once(DOKU_INC.'inc/pluginutils.php'); 15 require_once(DOKU_INC.'inc/cache.php'); 16 17/** 18 * Returns the parsed Wikitext in XHTML for the given id and revision. 19 * 20 * If $excuse is true an explanation is returned if the file 21 * wasn't found 22 * 23 * @author Andreas Gohr <andi@splitbrain.org> 24 */ 25function p_wiki_xhtml($id, $rev='', $excuse=true){ 26 $file = wikiFN($id,$rev); 27 $ret = ''; 28 29 //ensure $id is in global $ID (needed for parsing) 30 global $ID; 31 $keep = $ID; 32 $ID = $id; 33 34 if($rev){ 35 if(@file_exists($file)){ 36 $ret = p_render('xhtml',p_get_instructions(io_readfile($file)),$info); //no caching on old revisions 37 }elseif($excuse){ 38 $ret = p_locale_xhtml('norev'); 39 } 40 }else{ 41 if(@file_exists($file)){ 42 $ret = p_cached_output($file,'xhtml',$id); 43 }elseif($excuse){ 44 $ret = p_locale_xhtml('newpage'); 45 } 46 } 47 48 //restore ID (just in case) 49 $ID = $keep; 50 51 return $ret; 52} 53 54/** 55 * Returns starting summary for a page (e.g. the first few 56 * paragraphs), marked up in XHTML. 57 * 58 * If $excuse is true an explanation is returned if the file 59 * wasn't found 60 * 61 * @param string wiki page id 62 * @param reference populated with page title from heading or page id 63 * @deprecated 64 * @author Harry Fuecks <hfuecks@gmail.com> 65 */ 66function p_wiki_xhtml_summary($id, &$title, $rev='', $excuse=true){ 67 $file = wikiFN($id,$rev); 68 $ret = ''; 69 70 //ensure $id is in global $ID (needed for parsing) 71 global $ID; 72 $keep = $ID; 73 $ID = $id; 74 75 if($rev){ 76 if(@file_exists($file)){ 77 //no caching on old revisions 78 $ins = p_get_instructions(io_readfile($file)); 79 }elseif($excuse){ 80 $ret = p_locale_xhtml('norev'); 81 //restore ID (just in case) 82 $ID = $keep; 83 return $ret; 84 } 85 86 }else{ 87 88 if(@file_exists($file)){ 89 // The XHTML for a summary is not cached so use the instruction cache 90 $ins = p_cached_instructions($file); 91 }elseif($excuse){ 92 $ret = p_locale_xhtml('newpage'); 93 //restore ID (just in case) 94 $ID = $keep; 95 return $ret; 96 } 97 } 98 99 $ret = p_render('xhtmlsummary',$ins,$info); 100 101 if ( $info['sum_pagetitle'] ) { 102 $title = $info['sum_pagetitle']; 103 } else { 104 $title = $id; 105 } 106 107 $ID = $keep; 108 return $ret; 109} 110 111/** 112 * Returns the specified local text in parsed format 113 * 114 * @author Andreas Gohr <andi@splitbrain.org> 115 */ 116function p_locale_xhtml($id){ 117 //fetch parsed locale 118 $html = p_cached_output(localeFN($id)); 119 return $html; 120} 121 122/** 123 * *** DEPRECATED *** 124 * 125 * use p_cached_output() 126 * 127 * Returns the given file parsed to XHTML 128 * 129 * Uses and creates a cachefile 130 * 131 * @deprecated 132 * @author Andreas Gohr <andi@splitbrain.org> 133 * @todo rewrite to use mode instead of hardcoded XHTML 134 */ 135function p_cached_xhtml($file){ 136 return p_cached_output($file); 137} 138 139/** 140 * Returns the given file parsed into the requested output format 141 * 142 * @author Andreas Gohr <andi@splitbrain.org> 143 * @author Chris Smith <chris@jalakai.co.uk> 144 */ 145function p_cached_output($file, $format='xhtml', $id='') { 146 global $conf; 147 148 $cache = new cache_renderer($id, $file, $format); 149 if ($cache->useCache()) { 150 $parsed = $cache->retrieveCache(false); 151 if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n"; 152 } else { 153 $parsed = p_render($format, p_cached_instructions($file,false,$id), $info); 154 155 if ($info['cache']) { 156 $cache->storeCache($parsed); //save cachefile 157 if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n"; 158 }else{ 159 $cache->removeCache(); //try to delete cachefile 160 if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n"; 161 } 162 } 163 164 return $parsed; 165} 166 167/** 168 * Returns the render instructions for a file 169 * 170 * Uses and creates a serialized cache file 171 * 172 * @author Andreas Gohr <andi@splitbrain.org> 173 */ 174function p_cached_instructions($file,$cacheonly=false,$id='') { 175 global $conf; 176 static $run = null; 177 if(is_null($run)) $run = array(); 178 179 $cache = new cache_instructions($id, $file); 180 181 if ($cacheonly || $cache->useCache() || isset($run[$file])) { 182 return $cache->retrieveCache(); 183 } else if (@file_exists($file)) { 184 // no cache - do some work 185 $ins = p_get_instructions(io_readfile($file)); 186 $cache->storeCache($ins); 187 $run[$file] = true; // we won't rebuild these instructions in the same run again 188 return $ins; 189 } 190 191 return null; 192} 193 194/** 195 * turns a page into a list of instructions 196 * 197 * @author Harry Fuecks <hfuecks@gmail.com> 198 * @author Andreas Gohr <andi@splitbrain.org> 199 */ 200function p_get_instructions($text){ 201 202 $modes = p_get_parsermodes(); 203 204 // Create the parser 205 $Parser = & new Doku_Parser(); 206 207 // Add the Handler 208 $Parser->Handler = & new Doku_Handler(); 209 210 //add modes to parser 211 foreach($modes as $mode){ 212 $Parser->addMode($mode['mode'],$mode['obj']); 213 } 214 215 // Do the parsing 216 trigger_event('PARSER_WIKITEXT_PREPROCESS', $text); 217 $p = $Parser->parse($text); 218// dbg($p); 219 return $p; 220} 221 222/** 223 * returns the metadata of a page 224 * 225 * @author Esther Brunner <esther@kaffeehaus.ch> 226 */ 227function p_get_metadata($id, $key=false, $render=false){ 228 global $ID, $INFO, $cache_metadata; 229 230 // cache the current page 231 // Benchmarking shows the current page's metadata is generally the only page metadata 232 // accessed several times. This may catch a few other pages, but that shouldn't be an issue. 233 $cache = ($ID == $id); 234 $meta = p_read_metadata($id, $cache); 235 236 // metadata has never been rendered before - do it! (but not for non-existent pages) 237 if ($render && !$meta['current']['description']['abstract'] && page_exists($id)){ 238 $meta = p_render_metadata($id, $meta); 239 io_saveFile(metaFN($id, '.meta'), serialize($meta)); 240 241 // sync cached copies, including $INFO metadata 242 if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta; 243 if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; } 244 } 245 246 // filter by $key 247 if ($key){ 248 list($key, $subkey) = explode(' ', $key, 2); 249 $subkey = trim($subkey); 250 251 if ($subkey) { 252 return isset($meta['current'][$key][$subkey]) ? $meta['current'][$key][$subkey] : null; 253 } 254 255 return isset($meta['current'][$key]) ? $meta['current'][$key] : null; 256 } 257 258 return $meta['current']; 259} 260 261/** 262 * sets metadata elements of a page 263 * 264 * @author Esther Brunner <esther@kaffeehaus.ch> 265 */ 266function p_set_metadata($id, $data, $render=false, $persistent=true){ 267 if (!is_array($data)) return false; 268 269 global $ID; 270 271 // cache the current page 272 $cache = ($ID == $id); 273 $orig = p_read_metadata($id, $cache); 274 275 // render metadata first? 276 $meta = $render ? p_render_metadata($id, $orig) : $orig; 277 278 // now add the passed metadata 279 $protected = array('description', 'date', 'contributor'); 280 foreach ($data as $key => $value){ 281 282 // be careful with sub-arrays of $meta['relation'] 283 if ($key == 'relation'){ 284 285 foreach ($value as $subkey => $subvalue){ 286 $meta['current'][$key][$subkey] = !empty($meta['current'][$key][$subkey]) ? array_merge($meta['current'][$key][$subkey], $subvalue) : $subvalue; 287 if ($persistent) 288 $meta['persistent'][$key][$subkey] = !empty($meta['persistent'][$key][$subkey]) ? array_merge($meta['persistent'][$key][$subkey], $subvalue) : $subvalue; 289 } 290 291 // be careful with some senisitive arrays of $meta 292 } elseif (in_array($key, $protected)){ 293 294 // these keys, must have subkeys - a legitimate value must be an array 295 if (is_array($value)) { 296 $meta['current'][$key] = !empty($meta['current'][$key]) ? array_merge($meta['current'][$key],$value) : $value; 297 298 if ($persistent) { 299 $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ? array_merge($meta['persistent'][$key],$value) : $value; 300 } 301 } 302 303 // no special treatment for the rest 304 } else { 305 $meta['current'][$key] = $value; 306 if ($persistent) $meta['persistent'][$key] = $value; 307 } 308 } 309 310 // save only if metadata changed 311 if ($meta == $orig) return true; 312 313 // sync cached copies, including $INFO metadata 314 global $cache_metadata, $INFO; 315 316 if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta; 317 if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; } 318 319 return io_saveFile(metaFN($id, '.meta'), serialize($meta)); 320} 321 322/** 323 * read the metadata from source/cache for $id 324 * (internal use only - called by p_get_metadata & p_set_metadata) 325 * 326 * this function also converts the metadata from the original format to 327 * the current format ('current' & 'persistent' arrays) 328 * 329 * @author Christopher Smith <chris@jalakai.co.uk> 330 * 331 * @param string $id absolute wiki page id 332 * @param bool $cache whether or not to cache metadata in memory 333 * (only use for metadata likely to be accessed several times) 334 * 335 * @return array metadata 336 */ 337function p_read_metadata($id,$cache=false) { 338 global $cache_metadata; 339 340 if (isset($cache_metadata[$id])) return $cache_metadata[$id]; 341 342 $file = metaFN($id, '.meta'); 343 $meta = @file_exists($file) ? unserialize(io_readFile($file, false)) : array('current'=>array(),'persistent'=>array()); 344 345 // convert $meta from old format to new (current+persistent) format 346 if (!isset($meta['current'])) { 347 $meta = array('current'=>$meta,'persistent'=>$meta); 348 349 // remove non-persistent keys 350 unset($meta['persistent']['title']); 351 unset($meta['persistent']['description']['abstract']); 352 unset($meta['persistent']['description']['tableofcontents']); 353 unset($meta['persistent']['relation']['haspart']); 354 unset($meta['persistent']['relation']['references']); 355 unset($meta['persistent']['date']['valid']); 356 357 if (empty($meta['persistent']['description'])) unset($meta['persistent']['description']); 358 if (empty($meta['persistent']['relation'])) unset($meta['persistent']['relation']); 359 if (empty($meta['persistent']['date'])) unset($meta['persistent']['date']); 360 361 // save converted metadata 362 io_saveFile($file, serialize($meta)); 363 } 364 365 if ($cache) { 366 $cache_metadata[$id] = $meta; 367 } 368 369 return $meta; 370} 371 372/** 373 * renders the metadata of a page 374 * 375 * @author Esther Brunner <esther@kaffeehaus.ch> 376 */ 377function p_render_metadata($id, $orig){ 378 // make sure the correct ID is in global ID 379 global $ID; 380 $keep = $ID; 381 $ID = $id; 382 383 384 // add an extra key for the event - to tell event handlers the page whose metadata this is 385 $orig['page'] = $id; 386 $evt = new Doku_Event('PARSER_METADATA_RENDER', $orig); 387 if ($evt->advise_before()) { 388 389 require_once DOKU_INC."inc/parser/metadata.php"; 390 391 // get instructions 392 $instructions = p_cached_instructions(wikiFN($id),false,$id); 393 if(is_null($instructions)){ 394 $ID = $keep; 395 return null; // something went wrong with the instructions 396 } 397 398 // set up the renderer 399 $renderer = & new Doku_Renderer_metadata(); 400 $renderer->meta = $orig['current']; 401 $renderer->persistent = $orig['persistent']; 402 403 // loop through the instructions 404 foreach ($instructions as $instruction){ 405 // execute the callback against the renderer 406 call_user_func_array(array(&$renderer, $instruction[0]), $instruction[1]); 407 } 408 409 $evt->result = array('current'=>$renderer->meta,'persistent'=>$renderer->persistent); 410 } 411 $evt->advise_after(); 412 413 $ID = $keep; 414 return $evt->result; 415} 416 417/** 418 * returns all available parser syntax modes in correct order 419 * 420 * @author Andreas Gohr <andi@splitbrain.org> 421 */ 422function p_get_parsermodes(){ 423 global $conf; 424 425 //reuse old data 426 static $modes = null; 427 if($modes != null){ 428 return $modes; 429 } 430 431 //import parser classes and mode definitions 432 require_once DOKU_INC . 'inc/parser/parser.php'; 433 434 // we now collect all syntax modes and their objects, then they will 435 // be sorted and added to the parser in correct order 436 $modes = array(); 437 438 // add syntax plugins 439 $pluginlist = plugin_list('syntax'); 440 if(count($pluginlist)){ 441 global $PARSER_MODES; 442 $obj = null; 443 foreach($pluginlist as $p){ 444 if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj 445 $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type 446 //add to modes 447 $modes[] = array( 448 'sort' => $obj->getSort(), 449 'mode' => "plugin_$p", 450 'obj' => $obj, 451 ); 452 unset($obj); //remove the reference 453 } 454 } 455 456 // add default modes 457 $std_modes = array('listblock','preformatted','notoc','nocache', 458 'header','table','linebreak','footnote','hr', 459 'unformatted','php','html','code','file','quote', 460 'internallink','rss','media','externallink', 461 'emaillink','windowssharelink','eol'); 462 if($conf['typography']){ 463 $std_modes[] = 'quotes'; 464 $std_modes[] = 'multiplyentity'; 465 } 466 foreach($std_modes as $m){ 467 $class = "Doku_Parser_Mode_$m"; 468 $obj = new $class(); 469 $modes[] = array( 470 'sort' => $obj->getSort(), 471 'mode' => $m, 472 'obj' => $obj 473 ); 474 } 475 476 // add formatting modes 477 $fmt_modes = array('strong','emphasis','underline','monospace', 478 'subscript','superscript','deleted'); 479 foreach($fmt_modes as $m){ 480 $obj = new Doku_Parser_Mode_formatting($m); 481 $modes[] = array( 482 'sort' => $obj->getSort(), 483 'mode' => $m, 484 'obj' => $obj 485 ); 486 } 487 488 // add modes which need files 489 $obj = new Doku_Parser_Mode_smiley(array_keys(getSmileys())); 490 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj' => $obj ); 491 $obj = new Doku_Parser_Mode_acronym(array_keys(getAcronyms())); 492 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj' => $obj ); 493 $obj = new Doku_Parser_Mode_entity(array_keys(getEntities())); 494 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj' => $obj ); 495 496 497 // add optional camelcase mode 498 if($conf['camelcase']){ 499 $obj = new Doku_Parser_Mode_camelcaselink(); 500 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj' => $obj ); 501 } 502 503 //sort modes 504 usort($modes,'p_sort_modes'); 505 506 return $modes; 507} 508 509/** 510 * Callback function for usort 511 * 512 * @author Andreas Gohr <andi@splitbrain.org> 513 */ 514function p_sort_modes($a, $b){ 515 if($a['sort'] == $b['sort']) return 0; 516 return ($a['sort'] < $b['sort']) ? -1 : 1; 517} 518 519/** 520 * Renders a list of instruction to the specified output mode 521 * 522 * In the $info array are informations from the renderer returned 523 * 524 * @author Harry Fuecks <hfuecks@gmail.com> 525 * @author Andreas Gohr <andi@splitbrain.org> 526 */ 527function p_render($mode,$instructions,&$info){ 528 if(is_null($instructions)) return ''; 529 530 // try default renderer first: 531 $file = DOKU_INC."inc/parser/$mode.php"; 532 if(@file_exists($file)){ 533 require_once $file; 534 $rclass = "Doku_Renderer_$mode"; 535 536 if ( !class_exists($rclass) ) { 537 trigger_error("Unable to resolve render class $rclass",E_USER_WARNING); 538 msg("Renderer for $mode not valid",-1); 539 return null; 540 } 541 $Renderer = & new $rclass(); 542 }else{ 543 // Maybe a plugin is available? 544 $Renderer =& plugin_load('renderer',$mode); 545 if(is_null($Renderer)){ 546 msg("No renderer for $mode found",-1); 547 return null; 548 } 549 } 550 551 $Renderer->smileys = getSmileys(); 552 $Renderer->entities = getEntities(); 553 $Renderer->acronyms = getAcronyms(); 554 $Renderer->interwiki = getInterwiki(); 555 #$Renderer->badwords = getBadWords(); 556 557 // Loop through the instructions 558 foreach ( $instructions as $instruction ) { 559 // Execute the callback against the Renderer 560 call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]); 561 } 562 563 //set info array 564 $info = $Renderer->info; 565 566 // Post process and return the output 567 $data = array($mode,& $Renderer->doc); 568 trigger_event('RENDERER_CONTENT_POSTPROCESS',$data); 569 return $Renderer->doc; 570} 571 572/** 573 * Gets the first heading from a file 574 * 575 * @param string $id dokuwiki page id 576 * @param bool $render rerender if first heading not known 577 * default: true -- must be set to false for calls from the metadata renderer to 578 * protects against loops and excessive resource usage when pages 579 * for which only a first heading is required will attempt to 580 * render metadata for all the pages for which they require first 581 * headings ... and so on. 582 * 583 * @author Andreas Gohr <andi@splitbrain.org> 584 */ 585function p_get_first_heading($id, $render=true){ 586 global $conf; 587 return $conf['useheading'] ? p_get_metadata($id,'title',$render) : null; 588} 589 590/** 591 * Wrapper for GeSHi Code Highlighter, provides caching of its output 592 * 593 * @author Christopher Smith <chris@jalakai.co.uk> 594 */ 595function p_xhtml_cached_geshi($code, $language) { 596 $cache = getCacheName($language.$code,".code"); 597 598 if (@file_exists($cache) && !$_REQUEST['purge'] && 599 (filemtime($cache) > filemtime(DOKU_INC . 'inc/geshi.php'))) { 600 601 $highlighted_code = io_readFile($cache, false); 602 @touch($cache); 603 604 } else { 605 606 require_once(DOKU_INC . 'inc/geshi.php'); 607 608 $geshi = new GeSHi($code, strtolower($language), DOKU_INC . 'inc/geshi'); 609 $geshi->set_encoding('utf-8'); 610 $geshi->enable_classes(); 611 $geshi->set_header_type(GESHI_HEADER_PRE); 612 $geshi->set_overall_class("code $language"); 613 $geshi->set_link_target($conf['target']['extern']); 614 615 $highlighted_code = $geshi->parse_code(); 616 617 io_saveFile($cache,$highlighted_code); 618 } 619 620 return $highlighted_code; 621} 622 623//Setup VIM: ex: et ts=2 enc=utf-8 : 624