xref: /dokuwiki/inc/parserutils.php (revision c19c91735c84cbf928b8a7717a4701fdc22b27ce)
1c112d578Sandi<?php
2c112d578Sandi/**
3c112d578Sandi * Utilities for collecting data from config files
4c112d578Sandi *
5c112d578Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6c112d578Sandi * @author     Harry Fuecks <hfuecks@gmail.com>
7c112d578Sandi * @author     Andreas Gohr <andi@splitbrain.org>
8c112d578Sandi */
9c112d578Sandi
10c112d578Sandi  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
11c112d578Sandi
12c112d578Sandi  require_once(DOKU_INC.'inc/confutils.php');
13c112d578Sandi  require_once(DOKU_INC.'inc/pageutils.php');
14ee20e7d1Sandi  require_once(DOKU_INC.'inc/pluginutils.php');
15c112d578Sandi
16c112d578Sandi/**
17c112d578Sandi * Returns the parsed Wikitext in XHTML for the given id and revision.
18c112d578Sandi *
19c112d578Sandi * If $excuse is true an explanation is returned if the file
20c112d578Sandi * wasn't found
21c112d578Sandi *
22c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
23c112d578Sandi */
24c112d578Sandifunction p_wiki_xhtml($id, $rev='', $excuse=true){
25c112d578Sandi  $file = wikiFN($id,$rev);
26c112d578Sandi  $ret  = '';
27c112d578Sandi
28c112d578Sandi  //ensure $id is in global $ID (needed for parsing)
291e76272cSandi  global $ID;
303ff8773bSAndreas Gohr  $keep = $ID;
311e76272cSandi  $ID   = $id;
32c112d578Sandi
33c112d578Sandi  if($rev){
34c112d578Sandi    if(@file_exists($file)){
359dc2c2afSandi      $ret = p_render('xhtml',p_get_instructions(io_readfile($file)),$info); //no caching on old revisions
36c112d578Sandi    }elseif($excuse){
37c112d578Sandi      $ret = p_locale_xhtml('norev');
38c112d578Sandi    }
39c112d578Sandi  }else{
40c112d578Sandi    if(@file_exists($file)){
41c112d578Sandi      $ret = p_cached_xhtml($file);
42c112d578Sandi    }elseif($excuse){
43c112d578Sandi      $ret = p_locale_xhtml('newpage');
44c112d578Sandi    }
45c112d578Sandi  }
46c112d578Sandi
473ff8773bSAndreas Gohr  //restore ID (just in case)
483ff8773bSAndreas Gohr  $ID = $keep;
493ff8773bSAndreas Gohr
50c112d578Sandi  return $ret;
51c112d578Sandi}
52c112d578Sandi
53c112d578Sandi/**
546b7b33dcShfuecks * Returns starting summary for a page (e.g. the first few
556b7b33dcShfuecks * paragraphs), marked up in XHTML.
566b7b33dcShfuecks *
576b7b33dcShfuecks * If $excuse is true an explanation is returned if the file
586b7b33dcShfuecks * wasn't found
596b7b33dcShfuecks *
606b7b33dcShfuecks * @param string wiki page id
616b7b33dcShfuecks * @param reference populated with page title from heading or page id
626b7b33dcShfuecks * @author Andreas Gohr <hfuecks@gmail.com>
636b7b33dcShfuecks */
646b7b33dcShfuecksfunction p_wiki_xhtml_summary($id, &$title, $rev='', $excuse=true){
656b7b33dcShfuecks  $file = wikiFN($id,$rev);
666b7b33dcShfuecks  $ret  = '';
676b7b33dcShfuecks
686b7b33dcShfuecks  //ensure $id is in global $ID (needed for parsing)
696b7b33dcShfuecks  global $ID;
706b7b33dcShfuecks  $keep = $ID;
716b7b33dcShfuecks  $ID   = $id;
726b7b33dcShfuecks
736b7b33dcShfuecks  if($rev){
746b7b33dcShfuecks    if(@file_exists($file)){
756b7b33dcShfuecks      //no caching on old revisions
766b7b33dcShfuecks      $ins = p_get_instructions(io_readfile($file));
776b7b33dcShfuecks    }elseif($excuse){
786b7b33dcShfuecks      $ret = p_locale_xhtml('norev');
796b7b33dcShfuecks      //restore ID (just in case)
806b7b33dcShfuecks      $ID = $keep;
816b7b33dcShfuecks      return $ret;
826b7b33dcShfuecks    }
836b7b33dcShfuecks
846b7b33dcShfuecks  }else{
856b7b33dcShfuecks
866b7b33dcShfuecks    if(@file_exists($file)){
876b7b33dcShfuecks      // The XHTML for a summary is not cached so use the instruction cache
886b7b33dcShfuecks      $ins = p_cached_instructions($file);
896b7b33dcShfuecks    }elseif($excuse){
906b7b33dcShfuecks      $ret = p_locale_xhtml('newpage');
916b7b33dcShfuecks      //restore ID (just in case)
926b7b33dcShfuecks      $ID = $keep;
936b7b33dcShfuecks      return $ret;
946b7b33dcShfuecks    }
956b7b33dcShfuecks  }
966b7b33dcShfuecks
976b7b33dcShfuecks  $ret = p_render('xhtmlsummary',$ins,$info);
986b7b33dcShfuecks
996b7b33dcShfuecks  if ( $info['sum_pagetitle'] ) {
1006b7b33dcShfuecks    $title = $info['sum_pagetitle'];
1016b7b33dcShfuecks  } else {
1026b7b33dcShfuecks    $title = $id;
1036b7b33dcShfuecks  }
1046b7b33dcShfuecks
1056b7b33dcShfuecks  $ID = $keep;
1066b7b33dcShfuecks  return $ret;
1076b7b33dcShfuecks}
1086b7b33dcShfuecks
1096b7b33dcShfuecks/**
110c112d578Sandi * Returns the specified local text in parsed format
111c112d578Sandi *
112c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
113c112d578Sandi */
114c112d578Sandifunction p_locale_xhtml($id){
115c112d578Sandi  //fetch parsed locale
116c112d578Sandi  $html = p_cached_xhtml(localeFN($id));
117c112d578Sandi  return $html;
118c112d578Sandi}
119c112d578Sandi
120c112d578Sandi/**
121c112d578Sandi * Returns the given file parsed to XHTML
122c112d578Sandi *
123c112d578Sandi * Uses and creates a cachefile
124c112d578Sandi *
125c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
1269dc2c2afSandi * @todo   rewrite to use mode instead of hardcoded XHTML
127c112d578Sandi */
128c112d578Sandifunction p_cached_xhtml($file){
129c112d578Sandi  global $conf;
13098407a7aSandi  $cache  = getCacheName($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.xhtml');
131c7532350SDrew Amato  $purge  = $conf['cachedir'].'/purgefile';
132c112d578Sandi
133c112d578Sandi  // check if cache can be used
134c112d578Sandi  $cachetime = @filemtime($cache); // 0 if not exists
135c112d578Sandi
136c112d578Sandi  if( @file_exists($file)                                             // does the source exist
137c112d578Sandi      && $cachetime > @filemtime($file)                               // cache is fresh
138c112d578Sandi      && ((time() - $cachetime) < $conf['cachetime'])                 // and is cachefile young enough
139c112d578Sandi      && !isset($_REQUEST['purge'])                                   // no purge param was set
1401094c798Sandi      && ($cachetime > @filemtime($purge))                            // and newer than the purgefile
141e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'dokuwiki.php'))      // newer than the config file
142e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'local.php'))         // newer than the local config file
143c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/xhtml.php'))   // newer than the renderer
144c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/parser.php'))  // newer than the parser
145c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/handler.php')))// newer than the handler
146c112d578Sandi  {
147c112d578Sandi    //well then use the cache
148c112d578Sandi    $parsed = io_readfile($cache);
149f42d1c75SAndreas Gohr    if($conf['allowdebug']) $parsed .= "\n<!-- cachefile $cache used -->\n";
150c112d578Sandi  }else{
1519dc2c2afSandi    $parsed = p_render('xhtml', p_cached_instructions($file),$info); //try to use cached instructions
152c112d578Sandi
1539dc2c2afSandi    if($info['cache']){
154c112d578Sandi      io_saveFile($cache,$parsed); //save cachefile
155f42d1c75SAndreas Gohr      if($conf['allowdebug']) $parsed .= "\n<!-- no cachefile used, but created -->\n";
156c112d578Sandi    }else{
157c112d578Sandi      @unlink($cache); //try to delete cachefile
158f42d1c75SAndreas Gohr      if($conf['allowdebug']) $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
159c112d578Sandi    }
160c112d578Sandi  }
161c112d578Sandi
162c112d578Sandi  return $parsed;
163c112d578Sandi}
164c112d578Sandi
165c112d578Sandi/**
166c112d578Sandi * Returns the render instructions for a file
167c112d578Sandi *
168c112d578Sandi * Uses and creates a serialized cache file
169c112d578Sandi *
170c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
171c112d578Sandi */
17237e34a5eSandifunction p_cached_instructions($file,$cacheonly=false){
173c112d578Sandi  global $conf;
17498407a7aSandi  $cache  = getCacheName($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.i');
175c112d578Sandi
176c112d578Sandi  // check if cache can be used
177c112d578Sandi  $cachetime = @filemtime($cache); // 0 if not exists
178c112d578Sandi
17937e34a5eSandi  // cache forced?
18037e34a5eSandi  if($cacheonly){
18137e34a5eSandi    if($cachetime){
182e34c0709SAndreas Gohr      return unserialize(io_readfile($cache,false));
18337e34a5eSandi    }else{
184fd198316Sandi      return array();
18537e34a5eSandi    }
18637e34a5eSandi  }
18737e34a5eSandi
188c112d578Sandi  if( @file_exists($file)                                             // does the source exist
189c112d578Sandi      && $cachetime > @filemtime($file)                               // cache is fresh
190c112d578Sandi      && !isset($_REQUEST['purge'])                                   // no purge param was set
191e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'dokuwiki.php'))      // newer than the config file
192e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'local.php'))         // newer than the local config file
193c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/parser.php'))  // newer than the parser
194c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/handler.php')))// newer than the handler
195c112d578Sandi  {
196c112d578Sandi    //well then use the cache
197e34c0709SAndreas Gohr    return unserialize(io_readfile($cache,false));
198c112d578Sandi  }elseif(@file_exists($file)){
199c112d578Sandi    // no cache - do some work
2006bbae538Sandi    $ins = p_get_instructions(io_readfile($file));
201c112d578Sandi    io_savefile($cache,serialize($ins));
202c112d578Sandi    return $ins;
203c112d578Sandi  }
204c112d578Sandi
205c112d578Sandi  return NULL;
206c112d578Sandi}
207c112d578Sandi
208c112d578Sandi/**
209c112d578Sandi * turns a page into a list of instructions
210c112d578Sandi *
211c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
212c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
213c112d578Sandi */
2146bbae538Sandifunction p_get_instructions($text){
215c112d578Sandi
216107b01d6Sandi  $modes = p_get_parsermodes();
217ee20e7d1Sandi
218c112d578Sandi  // Create the parser
219c112d578Sandi  $Parser = & new Doku_Parser();
220c112d578Sandi
221c112d578Sandi  // Add the Handler
222c112d578Sandi  $Parser->Handler = & new Doku_Handler();
223c112d578Sandi
224107b01d6Sandi  //add modes to parser
225107b01d6Sandi  foreach($modes as $mode){
226107b01d6Sandi    $Parser->addMode($mode['mode'],$mode['obj']);
227c112d578Sandi  }
228c112d578Sandi
229c112d578Sandi  // Do the parsing
230a2d649c4Sandi  $p    = $Parser->parse($text);
231ee20e7d1Sandi//  dbg($p);
232a2d649c4Sandi  return $p;
233c112d578Sandi}
234c112d578Sandi
235c112d578Sandi/**
236107b01d6Sandi * returns all available parser syntax modes in correct order
237107b01d6Sandi *
238107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
239107b01d6Sandi */
240107b01d6Sandifunction p_get_parsermodes(){
241107b01d6Sandi  global $conf;
242107b01d6Sandi
243107b01d6Sandi  //reuse old data
244107b01d6Sandi  static $modes = null;
245107b01d6Sandi  if($modes != null){
246107b01d6Sandi    return $modes;
247107b01d6Sandi  }
248107b01d6Sandi
249107b01d6Sandi  //import parser classes and mode definitions
250107b01d6Sandi  require_once DOKU_INC . 'inc/parser/parser.php';
251107b01d6Sandi
252107b01d6Sandi  // we now collect all syntax modes and their objects, then they will
253107b01d6Sandi  // be sorted and added to the parser in correct order
254107b01d6Sandi  $modes = array();
255107b01d6Sandi
256107b01d6Sandi  // add syntax plugins
257107b01d6Sandi  $pluginlist = plugin_list('syntax');
258107b01d6Sandi  if(count($pluginlist)){
259107b01d6Sandi    global $PARSER_MODES;
260107b01d6Sandi    $obj = null;
261107b01d6Sandi    foreach($pluginlist as $p){
262c90b2fb1Schris      if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj
263107b01d6Sandi      $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
264107b01d6Sandi      //add to modes
265107b01d6Sandi      $modes[] = array(
266107b01d6Sandi                   'sort' => $obj->getSort(),
267107b01d6Sandi                   'mode' => "plugin_$p",
268107b01d6Sandi                   'obj'  => $obj,
269107b01d6Sandi                 );
270a46d0d65SAndreas Gohr      unset($obj); //remove the reference
271107b01d6Sandi    }
272107b01d6Sandi  }
273107b01d6Sandi
274107b01d6Sandi  // add default modes
275107b01d6Sandi  $std_modes = array('listblock','preformatted','notoc','nocache',
276107b01d6Sandi                     'header','table','linebreak','footnote','hr',
277107b01d6Sandi                     'unformatted','php','html','code','file','quote',
278e77ea1bcSAndreas Gohr                     'internallink','rss','media','externallink',
279e77ea1bcSAndreas Gohr                     'emaillink','windowssharelink','eol');
280e77ea1bcSAndreas Gohr  if($conf['typography']){
281e77ea1bcSAndreas Gohr    $std_modes[] = 'quotes';
282e77ea1bcSAndreas Gohr    $std_modes[] = 'multiplyentity';
283e77ea1bcSAndreas Gohr  }
284107b01d6Sandi  foreach($std_modes as $m){
285107b01d6Sandi    $class = "Doku_Parser_Mode_$m";
286107b01d6Sandi    $obj   = new $class();
287107b01d6Sandi    $modes[] = array(
288107b01d6Sandi                 'sort' => $obj->getSort(),
289107b01d6Sandi                 'mode' => $m,
290107b01d6Sandi                 'obj'  => $obj
291107b01d6Sandi               );
292107b01d6Sandi  }
293107b01d6Sandi
294107b01d6Sandi  // add formatting modes
295107b01d6Sandi  $fmt_modes = array('strong','emphasis','underline','monospace',
296107b01d6Sandi                     'subscript','superscript','deleted');
297107b01d6Sandi  foreach($fmt_modes as $m){
298107b01d6Sandi    $obj   = new Doku_Parser_Mode_formatting($m);
299107b01d6Sandi    $modes[] = array(
300107b01d6Sandi                 'sort' => $obj->getSort(),
301107b01d6Sandi                 'mode' => $m,
302107b01d6Sandi                 'obj'  => $obj
303107b01d6Sandi               );
304107b01d6Sandi  }
305107b01d6Sandi
306107b01d6Sandi  // add modes which need files
307107b01d6Sandi  $obj     = new Doku_Parser_Mode_smiley(array_keys(getSmileys()));
308107b01d6Sandi  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj'  => $obj );
309107b01d6Sandi  $obj     = new Doku_Parser_Mode_acronym(array_keys(getAcronyms()));
310107b01d6Sandi  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj'  => $obj );
311107b01d6Sandi  $obj     = new Doku_Parser_Mode_entity(array_keys(getEntities()));
312107b01d6Sandi  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj'  => $obj );
313107b01d6Sandi
314107b01d6Sandi
315107b01d6Sandi  // add optional camelcase mode
316107b01d6Sandi  if($conf['camelcase']){
317107b01d6Sandi    $obj     = new Doku_Parser_Mode_camelcaselink();
318107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj'  => $obj );
319107b01d6Sandi  }
320107b01d6Sandi
321107b01d6Sandi  //sort modes
322107b01d6Sandi  usort($modes,'p_sort_modes');
323107b01d6Sandi
324107b01d6Sandi  return $modes;
325107b01d6Sandi}
326107b01d6Sandi
327107b01d6Sandi/**
328107b01d6Sandi * Callback function for usort
329107b01d6Sandi *
330107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
331107b01d6Sandi */
332107b01d6Sandifunction p_sort_modes($a, $b){
333107b01d6Sandi  if($a['sort'] == $b['sort']) return 0;
334107b01d6Sandi  return ($a['sort'] < $b['sort']) ? -1 : 1;
335107b01d6Sandi}
336107b01d6Sandi
337107b01d6Sandi/**
338ac83b9d8Sandi * Renders a list of instruction to the specified output mode
339c112d578Sandi *
3409dc2c2afSandi * In the $info array are informations from the renderer returned
3419dc2c2afSandi *
342c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
343c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
344c112d578Sandi */
3459dc2c2afSandifunction p_render($mode,$instructions,& $info){
346c112d578Sandi  if(is_null($instructions)) return '';
347c112d578Sandi
348*c19c9173SBen Coburn  if ($mode=='wiki') { msg("Renderer for $mode not valid",-1); return null; } //FIXME!! remove this line when inc/parser/wiki.php works.
349*c19c9173SBen Coburn
350c112d578Sandi  // Create the renderer
351ac83b9d8Sandi  if(!@file_exists(DOKU_INC."inc/parser/$mode.php")){
352ac83b9d8Sandi    msg("No renderer for $mode found",-1);
353ac83b9d8Sandi    return null;
354ac83b9d8Sandi  }
355ac83b9d8Sandi
356ac83b9d8Sandi  require_once DOKU_INC."inc/parser/$mode.php";
357ac83b9d8Sandi  $rclass = "Doku_Renderer_$mode";
3586b7b33dcShfuecks  if ( !class_exists($rclass) ) {
359*c19c9173SBen Coburn    trigger_error("Unable to resolve render class $rclass",E_USER_WARNING);
360*c19c9173SBen Coburn    msg("Renderer for $mode not valid",-1);
361*c19c9173SBen Coburn    return null;
3626b7b33dcShfuecks  }
363ac83b9d8Sandi  $Renderer = & new $rclass(); #FIXME any way to check for class existance?
364c112d578Sandi
365c112d578Sandi  $Renderer->smileys = getSmileys();
366c112d578Sandi  $Renderer->entities = getEntities();
367c112d578Sandi  $Renderer->acronyms = getAcronyms();
368c112d578Sandi  $Renderer->interwiki = getInterwiki();
369c112d578Sandi  #$Renderer->badwords = getBadWords();
370c112d578Sandi
371c112d578Sandi  // Loop through the instructions
372c112d578Sandi  foreach ( $instructions as $instruction ) {
373c112d578Sandi      // Execute the callback against the Renderer
374c112d578Sandi      call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
375c112d578Sandi  }
3769dc2c2afSandi
3779dc2c2afSandi  //set info array
3789dc2c2afSandi  $info = $Renderer->info;
3799dc2c2afSandi
380c112d578Sandi  // Return the output
381c112d578Sandi  return $Renderer->doc;
382c112d578Sandi}
383c112d578Sandi
384bb0a59d4Sjan/**
385bb0a59d4Sjan * Gets the first heading from a file
386bb0a59d4Sjan *
387bb0a59d4Sjan * @author Jan Decaluwe <jan@jandecaluwe.com>
388bb0a59d4Sjan */
389bb0a59d4Sjanfunction p_get_first_heading($id){
390bb0a59d4Sjan  $file = wikiFN($id);
391bb0a59d4Sjan  if (@file_exists($file)) {
3926e38d921Sandi    $instructions = p_cached_instructions($file,true);
393bb0a59d4Sjan    foreach ( $instructions as $instruction ) {
394bb0a59d4Sjan      if ($instruction[0] == 'header') {
39587c434ceSAndreas Gohr        return trim($instruction[1][0]);
396bb0a59d4Sjan      }
397bb0a59d4Sjan    }
398bb0a59d4Sjan  }
399bb0a59d4Sjan  return NULL;
400bb0a59d4Sjan}
401bb0a59d4Sjan
402c112d578Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
403