1<?php 2/** 3 * Utilities for collecting data from config files 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',realpath(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(); 151 if($conf['allowdebug']) $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']) $parsed .= "\n<!-- no cachefile used, but created -->\n"; 158 }else{ 159 $cache->removeCache(); //try to delete cachefile 160 if($conf['allowdebug']) $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 177 $cache = new cache_instructions($id, $file); 178 179 if ($cacheonly || $cache->useCache()) { 180 return $cache->retrieveCache(); 181 } else if (@file_exists($file)) { 182 // no cache - do some work 183 $ins = p_get_instructions(io_readfile($file)); 184 $cache->storeCache($ins); 185 return $ins; 186 } 187 188 return NULL; 189} 190 191/** 192 * turns a page into a list of instructions 193 * 194 * @author Harry Fuecks <hfuecks@gmail.com> 195 * @author Andreas Gohr <andi@splitbrain.org> 196 */ 197function p_get_instructions($text){ 198 199 $modes = p_get_parsermodes(); 200 201 // Create the parser 202 $Parser = & new Doku_Parser(); 203 204 // Add the Handler 205 $Parser->Handler = & new Doku_Handler(); 206 207 //add modes to parser 208 foreach($modes as $mode){ 209 $Parser->addMode($mode['mode'],$mode['obj']); 210 } 211 212 // Do the parsing 213 trigger_event('PARSER_WIKITEXT_PREPROCESS', $text); 214 $p = $Parser->parse($text); 215// dbg($p); 216 return $p; 217} 218 219/** 220 * returns the metadata of a page 221 * 222 * @author Esther Brunner <esther@kaffeehaus.ch> 223 */ 224function p_get_metadata($id, $key=false, $render=false){ 225 global $ID, $INFO, $cache_metadata; 226 227 // cache the current page 228 // Benchmarking shows the current page's metadata is generally the only page metadata 229 // accessed several times. This may catch a few other pages, but that shouldn't be an issue. 230 $cache = ($ID == $id); 231 $meta = p_read_metadata($id, $cache); 232 233 // metadata has never been rendered before - do it! 234 if ($render && !$meta['description']['abstract']){ 235 $meta = p_render_metadata($id, $meta); 236 io_saveFile($file, serialize($meta)); 237 238 // sync cached copies, including $INFO metadata 239 if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta; 240 if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; } 241 } 242 243 // filter by $key 244 if ($key){ 245 list($key, $subkey) = explode(' ', $key, 2); 246 if (trim($subkey)) return $meta['current'][$key][$subkey]; 247 248 return $meta['current'][$key]; 249 } 250 251 return $meta['current']; 252} 253 254/** 255 * sets metadata elements of a page 256 * 257 * @author Esther Brunner <esther@kaffeehaus.ch> 258 */ 259function p_set_metadata($id, $data, $render=false, $persistent=true){ 260 if (!is_array($data)) return false; 261 262 global $ID; 263 264 // cache the current page 265 $cache = ($ID == $id); 266 $orig = p_read_metadata($id, $cache); 267 268 // render metadata first? 269 $meta = $render ? p_render_metadata($id, $orig) : $orig; 270 271 // now add the passed metadata 272 $protected = array('description', 'date', 'contributor'); 273 foreach ($data as $key => $value){ 274 275 // be careful with sub-arrays of $meta['relation'] 276 if ($key == 'relation'){ 277 278 foreach ($value as $subkey => $subvalue){ 279 $meta['current'][$key][$subkey] = array_merge($meta['current'][$key][$subkey], $subvalue); 280 if ($persistent) 281 $meta['persistent'][$key][$subkey] = array_merge($meta['persistent'][$key][$subkey], $subvalue); 282 } 283 284 // be careful with some senisitive arrays of $meta 285 } elseif (in_array($key, $protected)){ 286 287 if (is_array($value)){ 288 #FIXME not sure if this is the intended thing: 289 if(!is_array($meta['current'][$key])) $meta['current'][$key] = array($meta['current'][$key]); 290 $meta['current'][$key] = array_merge($meta['current'][$key], $value); 291 292 if ($persistent) { 293 if(!is_array($meta['persistent'][$key])) $meta['persistent'][$key] = array($meta['persistent'][$key]); 294 $meta['persistent'][$key] = array_merge($meta['persistent'][$key], $value); 295 } 296 } 297 298 // no special treatment for the rest 299 } else { 300 $meta['current'][$key] = $value; 301 if ($persistent) $meta['persistent'][$key] = $value; 302 } 303 } 304 305 // save only if metadata changed 306 if ($meta == $orig) return true; 307 308 // sync cached copies, including $INFO metadata 309 global $cache_metadata, $INFO; 310 311 if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta; 312 if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; } 313 314 return io_saveFile(metaFN($id, '.meta'), serialize($meta)); 315} 316 317/** 318 * read the metadata from source/cache for $id 319 * (internal use only - called by p_get_metadata & p_set_metadata) 320 * 321 * this function also converts the metadata from the original format to 322 * the current format ('current' & 'persistent' arrays) 323 * 324 * @author Christopher Smith <chris@jalakai.co.uk> 325 * 326 * @param string $id absolute wiki page id 327 * @param bool $cache whether or not to cache metadata in memory 328 * (only use for metadata likely to be accessed several times) 329 * 330 * @return array metadata 331 */ 332function p_read_metadata($id,$cache=false) { 333 global $cache_metadata; 334 335 if (isset($cache_metadata[$id])) return $cache_metadata[$id]; 336 337 $file = metaFN($id, '.meta'); 338 $meta = @file_exists($file) ? unserialize(io_readFile($file, false)) : array('current'=>array(),'persistent'=>array()); 339 340 // convert $meta from old format to new (current+persistent) format 341 if (!isset($meta['current'])) { 342 $meta = array('current'=>$meta,'persistent'=>$meta); 343 344 // remove non-persistent keys 345 unset($meta['persistent']['title']); 346 unset($meta['persistent']['description']['abstract']); 347 unset($meta['persistent']['description']['tableofcontents']); 348 unset($meta['persistent']['relation']['haspart']); 349 unset($meta['persistent']['relation']['references']); 350 unset($meta['persistent']['date']['valid']); 351 352 if (empty($meta['persistent']['description'])) unset($meta['persistent']['description']); 353 if (empty($meta['persistent']['relation'])) unset($meta['persistent']['relation']); 354 if (empty($meta['persistent']['date'])) unset($meta['persistent']['date']); 355 356 // save converted metadata 357 io_saveFile($file, serialize($meta)); 358 } 359 360 if ($cache) { 361 $cache_metadata[$id] = $meta; 362 } 363 364 return $meta; 365} 366 367/** 368 * renders the metadata of a page 369 * 370 * @author Esther Brunner <esther@kaffeehaus.ch> 371 */ 372function p_render_metadata($id, $orig){ 373 374 // add an extra key for the event - to tell event handlers the page whose metadata this is 375 $orig['page'] = $id; 376 $evt = new Doku_Event('PARSER_METADATA_RENDER', $orig); 377 if ($evt->advise_before()) { 378 379 require_once DOKU_INC."inc/parser/metadata.php"; 380 381 // get instructions 382 $instructions = p_cached_instructions(wikiFN($id),false,$id); 383 384 // set up the renderer 385 $renderer = & new Doku_Renderer_metadata(); 386 $renderer->meta = $orig['current']; 387 $renderer->persistent = $orig['persistent']; 388 389 // loop through the instructions 390 foreach ($instructions as $instruction){ 391 // execute the callback against the renderer 392 call_user_func_array(array(&$renderer, $instruction[0]), $instruction[1]); 393 } 394 395 $evt->result = array('current'=>$renderer->meta,'persistent'=>$renderer->persistent); 396 } 397 $evt->advise_after(); 398 399 return $evt->result; 400} 401 402/** 403 * returns all available parser syntax modes in correct order 404 * 405 * @author Andreas Gohr <andi@splitbrain.org> 406 */ 407function p_get_parsermodes(){ 408 global $conf; 409 410 //reuse old data 411 static $modes = null; 412 if($modes != null){ 413 return $modes; 414 } 415 416 //import parser classes and mode definitions 417 require_once DOKU_INC . 'inc/parser/parser.php'; 418 419 // we now collect all syntax modes and their objects, then they will 420 // be sorted and added to the parser in correct order 421 $modes = array(); 422 423 // add syntax plugins 424 $pluginlist = plugin_list('syntax'); 425 if(count($pluginlist)){ 426 global $PARSER_MODES; 427 $obj = null; 428 foreach($pluginlist as $p){ 429 if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj 430 $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type 431 //add to modes 432 $modes[] = array( 433 'sort' => $obj->getSort(), 434 'mode' => "plugin_$p", 435 'obj' => $obj, 436 ); 437 unset($obj); //remove the reference 438 } 439 } 440 441 // add default modes 442 $std_modes = array('listblock','preformatted','notoc','nocache', 443 'header','table','linebreak','footnote','hr', 444 'unformatted','php','html','code','file','quote', 445 'internallink','rss','media','externallink', 446 'emaillink','windowssharelink','eol'); 447 if($conf['typography']){ 448 $std_modes[] = 'quotes'; 449 $std_modes[] = 'multiplyentity'; 450 } 451 foreach($std_modes as $m){ 452 $class = "Doku_Parser_Mode_$m"; 453 $obj = new $class(); 454 $modes[] = array( 455 'sort' => $obj->getSort(), 456 'mode' => $m, 457 'obj' => $obj 458 ); 459 } 460 461 // add formatting modes 462 $fmt_modes = array('strong','emphasis','underline','monospace', 463 'subscript','superscript','deleted'); 464 foreach($fmt_modes as $m){ 465 $obj = new Doku_Parser_Mode_formatting($m); 466 $modes[] = array( 467 'sort' => $obj->getSort(), 468 'mode' => $m, 469 'obj' => $obj 470 ); 471 } 472 473 // add modes which need files 474 $obj = new Doku_Parser_Mode_smiley(array_keys(getSmileys())); 475 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj' => $obj ); 476 $obj = new Doku_Parser_Mode_acronym(array_keys(getAcronyms())); 477 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj' => $obj ); 478 $obj = new Doku_Parser_Mode_entity(array_keys(getEntities())); 479 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj' => $obj ); 480 481 482 // add optional camelcase mode 483 if($conf['camelcase']){ 484 $obj = new Doku_Parser_Mode_camelcaselink(); 485 $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj' => $obj ); 486 } 487 488 //sort modes 489 usort($modes,'p_sort_modes'); 490 491 return $modes; 492} 493 494/** 495 * Callback function for usort 496 * 497 * @author Andreas Gohr <andi@splitbrain.org> 498 */ 499function p_sort_modes($a, $b){ 500 if($a['sort'] == $b['sort']) return 0; 501 return ($a['sort'] < $b['sort']) ? -1 : 1; 502} 503 504/** 505 * Renders a list of instruction to the specified output mode 506 * 507 * In the $info array are informations from the renderer returned 508 * 509 * @author Harry Fuecks <hfuecks@gmail.com> 510 * @author Andreas Gohr <andi@splitbrain.org> 511 */ 512function p_render($mode,$instructions,& $info){ 513 if(is_null($instructions)) return ''; 514 515 if ($mode=='wiki') { msg("Renderer for $mode not valid",-1); return null; } //FIXME!! remove this line when inc/parser/wiki.php works. 516 517 // Create the renderer 518 if(!@file_exists(DOKU_INC."inc/parser/$mode.php")){ 519 msg("No renderer for $mode found",-1); 520 return null; 521 } 522 523 require_once DOKU_INC."inc/parser/$mode.php"; 524 $rclass = "Doku_Renderer_$mode"; 525 if ( !class_exists($rclass) ) { 526 trigger_error("Unable to resolve render class $rclass",E_USER_WARNING); 527 msg("Renderer for $mode not valid",-1); 528 return null; 529 } 530 $Renderer = & new $rclass(); #FIXME any way to check for class existance? 531 532 $Renderer->smileys = getSmileys(); 533 $Renderer->entities = getEntities(); 534 $Renderer->acronyms = getAcronyms(); 535 $Renderer->interwiki = getInterwiki(); 536 #$Renderer->badwords = getBadWords(); 537 538 // Loop through the instructions 539 foreach ( $instructions as $instruction ) { 540 // Execute the callback against the Renderer 541 call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]); 542 } 543 544 //set info array 545 $info = $Renderer->info; 546 547 // Post process and return the output 548 $data = array($mode,& $Renderer->doc); 549 trigger_event('RENDERER_CONTENT_POSTPROCESS',$data); 550 return $Renderer->doc; 551} 552 553/** 554 * Gets the first heading from a file 555 * 556 * @author Andreas Gohr <andi@splitbrain.org> 557 */ 558function p_get_first_heading($id){ 559 global $conf; 560 return $conf['useheading'] ? p_get_metadata($id,'title') : null; 561} 562 563/** 564 * Wrapper for GeSHi Code Highlighter, provides caching of its output 565 * 566 * @author Christopher Smith <chris@jalakai.co.uk> 567 */ 568function p_xhtml_cached_geshi($code, $language) { 569 $cache = getCacheName($language.$code,".code"); 570 571 if (@file_exists($cache) && !$_REQUEST['purge'] && 572 (filemtime($cache) > filemtime(DOKU_INC . 'inc/geshi.php'))) { 573 574 $highlighted_code = io_readFile($cache, false); 575 @touch($cache); 576 577 } else { 578 579 require_once(DOKU_INC . 'inc/geshi.php'); 580 581 $geshi = new GeSHi($code, strtolower($language), DOKU_INC . 'inc/geshi'); 582 $geshi->set_encoding('utf-8'); 583 $geshi->enable_classes(); 584 $geshi->set_header_type(GESHI_HEADER_PRE); 585 $geshi->set_overall_class("code $language"); 586 $geshi->set_link_target($conf['target']['extern']); 587 588 $highlighted_code = $geshi->parse_code(); 589 590 io_saveFile($cache,$highlighted_code); 591 } 592 593 return $highlighted_code; 594} 595 596//Setup VIM: ex: et ts=2 enc=utf-8 : 597