xref: /dokuwiki/inc/parserutils.php (revision f42d1c75316f76b00c3e1b4ae7f890048ae1b117)
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/**
54c112d578Sandi * Returns the specified local text in parsed format
55c112d578Sandi *
56c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
57c112d578Sandi */
58c112d578Sandifunction p_locale_xhtml($id){
59c112d578Sandi  //fetch parsed locale
60c112d578Sandi  $html = p_cached_xhtml(localeFN($id));
61c112d578Sandi  return $html;
62c112d578Sandi}
63c112d578Sandi
64c112d578Sandi/**
65c112d578Sandi * Returns the given file parsed to XHTML
66c112d578Sandi *
67c112d578Sandi * Uses and creates a cachefile
68c112d578Sandi *
69c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
709dc2c2afSandi * @todo   rewrite to use mode instead of hardcoded XHTML
71c112d578Sandi */
72c112d578Sandifunction p_cached_xhtml($file){
73c112d578Sandi  global $conf;
7498407a7aSandi  $cache  = getCacheName($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.xhtml');
75c7532350SDrew Amato  $purge  = $conf['cachedir'].'/purgefile';
76c112d578Sandi
77c112d578Sandi  // check if cache can be used
78c112d578Sandi  $cachetime = @filemtime($cache); // 0 if not exists
79c112d578Sandi
80c112d578Sandi  if( @file_exists($file)                                             // does the source exist
81c112d578Sandi      && $cachetime > @filemtime($file)                               // cache is fresh
82c112d578Sandi      && ((time() - $cachetime) < $conf['cachetime'])                 // and is cachefile young enough
83c112d578Sandi      && !isset($_REQUEST['purge'])                                   // no purge param was set
841094c798Sandi      && ($cachetime > @filemtime($purge))                            // and newer than the purgefile
85e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'dokuwiki.php'))      // newer than the config file
86e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'local.php'))         // newer than the local config file
87c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/xhtml.php'))   // newer than the renderer
88c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/parser.php'))  // newer than the parser
89c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/handler.php')))// newer than the handler
90c112d578Sandi  {
91c112d578Sandi    //well then use the cache
92c112d578Sandi    $parsed = io_readfile($cache);
93*f42d1c75SAndreas Gohr    if($conf['allowdebug']) $parsed .= "\n<!-- cachefile $cache used -->\n";
94c112d578Sandi  }else{
959dc2c2afSandi    $parsed = p_render('xhtml', p_cached_instructions($file),$info); //try to use cached instructions
96c112d578Sandi
979dc2c2afSandi    if($info['cache']){
98c112d578Sandi      io_saveFile($cache,$parsed); //save cachefile
99*f42d1c75SAndreas Gohr      if($conf['allowdebug']) $parsed .= "\n<!-- no cachefile used, but created -->\n";
100c112d578Sandi    }else{
101c112d578Sandi      @unlink($cache); //try to delete cachefile
102*f42d1c75SAndreas Gohr      if($conf['allowdebug']) $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
103c112d578Sandi    }
104c112d578Sandi  }
105c112d578Sandi
106c112d578Sandi  return $parsed;
107c112d578Sandi}
108c112d578Sandi
109c112d578Sandi/**
110c112d578Sandi * Returns the render instructions for a file
111c112d578Sandi *
112c112d578Sandi * Uses and creates a serialized cache file
113c112d578Sandi *
114c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
115c112d578Sandi */
11637e34a5eSandifunction p_cached_instructions($file,$cacheonly=false){
117c112d578Sandi  global $conf;
11898407a7aSandi  $cache  = getCacheName($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.i');
119c112d578Sandi
120c112d578Sandi  // check if cache can be used
121c112d578Sandi  $cachetime = @filemtime($cache); // 0 if not exists
122c112d578Sandi
12337e34a5eSandi  // cache forced?
12437e34a5eSandi  if($cacheonly){
12537e34a5eSandi    if($cachetime){
12637e34a5eSandi      return unserialize(io_readfile($cache));
12737e34a5eSandi    }else{
128fd198316Sandi      return array();
12937e34a5eSandi    }
13037e34a5eSandi  }
13137e34a5eSandi
132c112d578Sandi  if( @file_exists($file)                                             // does the source exist
133c112d578Sandi      && $cachetime > @filemtime($file)                               // cache is fresh
134c112d578Sandi      && !isset($_REQUEST['purge'])                                   // no purge param was set
135e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'dokuwiki.php'))      // newer than the config file
136e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'local.php'))         // newer than the local config file
137c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/parser.php'))  // newer than the parser
138c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/handler.php')))// newer than the handler
139c112d578Sandi  {
140c112d578Sandi    //well then use the cache
141c112d578Sandi    return unserialize(io_readfile($cache));
142c112d578Sandi  }elseif(@file_exists($file)){
143c112d578Sandi    // no cache - do some work
1446bbae538Sandi    $ins = p_get_instructions(io_readfile($file));
145c112d578Sandi    io_savefile($cache,serialize($ins));
146c112d578Sandi    return $ins;
147c112d578Sandi  }
148c112d578Sandi
149c112d578Sandi  return NULL;
150c112d578Sandi}
151c112d578Sandi
152c112d578Sandi/**
153c112d578Sandi * turns a page into a list of instructions
154c112d578Sandi *
155c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
156c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
157c112d578Sandi */
1586bbae538Sandifunction p_get_instructions($text){
159c112d578Sandi
160107b01d6Sandi  $modes = p_get_parsermodes();
161ee20e7d1Sandi
162c112d578Sandi  // Create the parser
163c112d578Sandi  $Parser = & new Doku_Parser();
164c112d578Sandi
165c112d578Sandi  // Add the Handler
166c112d578Sandi  $Parser->Handler = & new Doku_Handler();
167c112d578Sandi
168107b01d6Sandi  //add modes to parser
169107b01d6Sandi  foreach($modes as $mode){
170107b01d6Sandi    $Parser->addMode($mode['mode'],$mode['obj']);
171c112d578Sandi  }
172c112d578Sandi
173c112d578Sandi  // Do the parsing
174a2d649c4Sandi  $p    = $Parser->parse($text);
175ee20e7d1Sandi//  dbg($p);
176a2d649c4Sandi  return $p;
177c112d578Sandi}
178c112d578Sandi
179c112d578Sandi/**
180107b01d6Sandi * returns all available parser syntax modes in correct order
181107b01d6Sandi *
182107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
183107b01d6Sandi */
184107b01d6Sandifunction p_get_parsermodes(){
185107b01d6Sandi  global $conf;
186107b01d6Sandi
187107b01d6Sandi  //reuse old data
188107b01d6Sandi  static $modes = null;
189107b01d6Sandi  if($modes != null){
190107b01d6Sandi    return $modes;
191107b01d6Sandi  }
192107b01d6Sandi
193107b01d6Sandi  //import parser classes and mode definitions
194107b01d6Sandi  require_once DOKU_INC . 'inc/parser/parser.php';
195107b01d6Sandi
196107b01d6Sandi  // we now collect all syntax modes and their objects, then they will
197107b01d6Sandi  // be sorted and added to the parser in correct order
198107b01d6Sandi  $modes = array();
199107b01d6Sandi
200107b01d6Sandi  // add syntax plugins
201107b01d6Sandi  $pluginlist = plugin_list('syntax');
202107b01d6Sandi  if(count($pluginlist)){
203107b01d6Sandi    global $PARSER_MODES;
204107b01d6Sandi    $obj = null;
205107b01d6Sandi    foreach($pluginlist as $p){
206c90b2fb1Schris      if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj
207107b01d6Sandi      $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
208107b01d6Sandi      //add to modes
209107b01d6Sandi      $modes[] = array(
210107b01d6Sandi                   'sort' => $obj->getSort(),
211107b01d6Sandi                   'mode' => "plugin_$p",
212107b01d6Sandi                   'obj'  => $obj,
213107b01d6Sandi                 );
214a46d0d65SAndreas Gohr      unset($obj); //remove the reference
215107b01d6Sandi    }
216107b01d6Sandi  }
217107b01d6Sandi
218107b01d6Sandi  // add default modes
219107b01d6Sandi  $std_modes = array('listblock','preformatted','notoc','nocache',
220107b01d6Sandi                     'header','table','linebreak','footnote','hr',
221107b01d6Sandi                     'unformatted','php','html','code','file','quote',
222107b01d6Sandi                     'multiplyentity','quotes','internallink','rss',
223107b01d6Sandi                     'media','externallink','emaillink','windowssharelink',
224107b01d6Sandi                     'eol');
225107b01d6Sandi  foreach($std_modes as $m){
226107b01d6Sandi    $class = "Doku_Parser_Mode_$m";
227107b01d6Sandi    $obj   = new $class();
228107b01d6Sandi    $modes[] = array(
229107b01d6Sandi                 'sort' => $obj->getSort(),
230107b01d6Sandi                 'mode' => $m,
231107b01d6Sandi                 'obj'  => $obj
232107b01d6Sandi               );
233107b01d6Sandi  }
234107b01d6Sandi
235107b01d6Sandi  // add formatting modes
236107b01d6Sandi  $fmt_modes = array('strong','emphasis','underline','monospace',
237107b01d6Sandi                     'subscript','superscript','deleted');
238107b01d6Sandi  foreach($fmt_modes as $m){
239107b01d6Sandi    $obj   = new Doku_Parser_Mode_formatting($m);
240107b01d6Sandi    $modes[] = array(
241107b01d6Sandi                 'sort' => $obj->getSort(),
242107b01d6Sandi                 'mode' => $m,
243107b01d6Sandi                 'obj'  => $obj
244107b01d6Sandi               );
245107b01d6Sandi  }
246107b01d6Sandi
247107b01d6Sandi  // add modes which need files
248107b01d6Sandi  $obj     = new Doku_Parser_Mode_smiley(array_keys(getSmileys()));
249107b01d6Sandi  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj'  => $obj );
250107b01d6Sandi  $obj     = new Doku_Parser_Mode_acronym(array_keys(getAcronyms()));
251107b01d6Sandi  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj'  => $obj );
252107b01d6Sandi  $obj     = new Doku_Parser_Mode_entity(array_keys(getEntities()));
253107b01d6Sandi  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj'  => $obj );
254107b01d6Sandi
255107b01d6Sandi
256107b01d6Sandi  // add optional camelcase mode
257107b01d6Sandi  if($conf['camelcase']){
258107b01d6Sandi    $obj     = new Doku_Parser_Mode_camelcaselink();
259107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj'  => $obj );
260107b01d6Sandi  }
261107b01d6Sandi
262107b01d6Sandi  //sort modes
263107b01d6Sandi  usort($modes,'p_sort_modes');
264107b01d6Sandi
265107b01d6Sandi  return $modes;
266107b01d6Sandi}
267107b01d6Sandi
268107b01d6Sandi/**
269107b01d6Sandi * Callback function for usort
270107b01d6Sandi *
271107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
272107b01d6Sandi */
273107b01d6Sandifunction p_sort_modes($a, $b){
274107b01d6Sandi  if($a['sort'] == $b['sort']) return 0;
275107b01d6Sandi  return ($a['sort'] < $b['sort']) ? -1 : 1;
276107b01d6Sandi}
277107b01d6Sandi
278107b01d6Sandi/**
279ac83b9d8Sandi * Renders a list of instruction to the specified output mode
280c112d578Sandi *
2819dc2c2afSandi * In the $info array are informations from the renderer returned
2829dc2c2afSandi *
283c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
284c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
285c112d578Sandi */
2869dc2c2afSandifunction p_render($mode,$instructions,& $info){
287c112d578Sandi  if(is_null($instructions)) return '';
288c112d578Sandi
289c112d578Sandi  // Create the renderer
290ac83b9d8Sandi  if(!@file_exists(DOKU_INC."inc/parser/$mode.php")){
291ac83b9d8Sandi    msg("No renderer for $mode found",-1);
292ac83b9d8Sandi    return null;
293ac83b9d8Sandi  }
294ac83b9d8Sandi
295ac83b9d8Sandi  require_once DOKU_INC."inc/parser/$mode.php";
296ac83b9d8Sandi  $rclass = "Doku_Renderer_$mode";
297ac83b9d8Sandi  $Renderer = & new $rclass(); #FIXME any way to check for class existance?
298c112d578Sandi
299c112d578Sandi  $Renderer->smileys = getSmileys();
300c112d578Sandi  $Renderer->entities = getEntities();
301c112d578Sandi  $Renderer->acronyms = getAcronyms();
302c112d578Sandi  $Renderer->interwiki = getInterwiki();
303c112d578Sandi  #$Renderer->badwords = getBadWords();
304c112d578Sandi
305c112d578Sandi  // Loop through the instructions
306c112d578Sandi  foreach ( $instructions as $instruction ) {
307c112d578Sandi      // Execute the callback against the Renderer
308c112d578Sandi      call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
309c112d578Sandi  }
3109dc2c2afSandi
3119dc2c2afSandi  //set info array
3129dc2c2afSandi  $info = $Renderer->info;
3139dc2c2afSandi
314c112d578Sandi  // Return the output
315c112d578Sandi  return $Renderer->doc;
316c112d578Sandi}
317c112d578Sandi
318bb0a59d4Sjan/**
319bb0a59d4Sjan * Gets the first heading from a file
320bb0a59d4Sjan *
321bb0a59d4Sjan * @author Jan Decaluwe <jan@jandecaluwe.com>
322bb0a59d4Sjan */
323bb0a59d4Sjanfunction p_get_first_heading($id){
324bb0a59d4Sjan  $file = wikiFN($id);
325bb0a59d4Sjan  if (@file_exists($file)) {
3266e38d921Sandi    $instructions = p_cached_instructions($file,true);
327bb0a59d4Sjan    foreach ( $instructions as $instruction ) {
328bb0a59d4Sjan      if ($instruction[0] == 'header') {
32987c434ceSAndreas Gohr        return trim($instruction[1][0]);
330bb0a59d4Sjan      }
331bb0a59d4Sjan    }
332bb0a59d4Sjan  }
333bb0a59d4Sjan  return NULL;
334bb0a59d4Sjan}
335bb0a59d4Sjan
336c112d578Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
337