xref: /dokuwiki/inc/parserutils.php (revision 39a89382d5e693e2b9c9f4e10d73081d1311aeee)
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/**
236*39a89382SEsther Brunner * returns the metadata of a page
237*39a89382SEsther Brunner *
238*39a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
239*39a89382SEsther Brunner */
240*39a89382SEsther Brunnerfunction p_get_metadata($id, $key=false, $render=false){
241*39a89382SEsther Brunner  $file = metaFN($id, '.meta');
242*39a89382SEsther Brunner
243*39a89382SEsther Brunner  if (@file_exists($file)) $meta = unserialize(io_readFile($file, false));
244*39a89382SEsther Brunner  else $meta = array();
245*39a89382SEsther Brunner
246*39a89382SEsther Brunner  // metadata has never been rendered before - do it!
247*39a89382SEsther Brunner  if ($render && !$meta['description']['abstract']){
248*39a89382SEsther Brunner    $meta = p_render_metadata($id, $meta);
249*39a89382SEsther Brunner    io_saveFile($file, serialize($meta));
250*39a89382SEsther Brunner  }
251*39a89382SEsther Brunner
252*39a89382SEsther Brunner  // filter by $key
253*39a89382SEsther Brunner  if ($key){
254*39a89382SEsther Brunner    list($key, $subkey) = explode(' ', $key, 2);
255*39a89382SEsther Brunner    if (trim($subkey)) return $meta[$key][$subkey];
256*39a89382SEsther Brunner    else return $meta[$key];
257*39a89382SEsther Brunner  }
258*39a89382SEsther Brunner
259*39a89382SEsther Brunner  return $meta;
260*39a89382SEsther Brunner}
261*39a89382SEsther Brunner
262*39a89382SEsther Brunner/**
263*39a89382SEsther Brunner * sets metadata elements of a page
264*39a89382SEsther Brunner *
265*39a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
266*39a89382SEsther Brunner */
267*39a89382SEsther Brunnerfunction p_set_metadata($id, $data, $render=false){
268*39a89382SEsther Brunner  if (!is_array($data)) return false;
269*39a89382SEsther Brunner
270*39a89382SEsther Brunner  $orig = p_get_metadata($id);
271*39a89382SEsther Brunner
272*39a89382SEsther Brunner  // render metadata first?
273*39a89382SEsther Brunner  if ($render) $meta = p_render_metadata($id, $orig);
274*39a89382SEsther Brunner  else $meta = $orig;
275*39a89382SEsther Brunner
276*39a89382SEsther Brunner  // now add the passed metadata
277*39a89382SEsther Brunner  $protected = array('description', 'date', 'contributor');
278*39a89382SEsther Brunner  foreach ($data as $key => $value){
279*39a89382SEsther Brunner
280*39a89382SEsther Brunner    // be careful with sub-arrays of $meta['relation']
281*39a89382SEsther Brunner    if ($key == 'relation'){
282*39a89382SEsther Brunner      foreach ($value as $subkey => $subvalue){
283*39a89382SEsther Brunner        $meta[$key][$subkey] = array_merge($meta[$key][$subkey], $subvalue);
284*39a89382SEsther Brunner      }
285*39a89382SEsther Brunner
286*39a89382SEsther Brunner    // be careful with some senisitive arrays of $meta
287*39a89382SEsther Brunner    } elseif (in_array($key, $protected)){
288*39a89382SEsther Brunner      if (is_array($value)){
289*39a89382SEsther Brunner        $meta[$key] = array_merge($meta[$key], $value);
290*39a89382SEsther Brunner      }
291*39a89382SEsther Brunner
292*39a89382SEsther Brunner    // no special treatment for the rest
293*39a89382SEsther Brunner    } else {
294*39a89382SEsther Brunner      $meta[$key] = $value;
295*39a89382SEsther Brunner    }
296*39a89382SEsther Brunner  }
297*39a89382SEsther Brunner
298*39a89382SEsther Brunner  // save only if metadata changed
299*39a89382SEsther Brunner  if ($meta == $orig) return true;
300*39a89382SEsther Brunner  return io_saveFile(metaFN($id, '.meta'), serialize($meta));
301*39a89382SEsther Brunner}
302*39a89382SEsther Brunner
303*39a89382SEsther Brunner/**
304*39a89382SEsther Brunner * renders the metadata of a page
305*39a89382SEsther Brunner *
306*39a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
307*39a89382SEsther Brunner */
308*39a89382SEsther Brunnerfunction p_render_metadata($id, $orig){
309*39a89382SEsther Brunner  require_once DOKU_INC."inc/parser/metadata.php";
310*39a89382SEsther Brunner
311*39a89382SEsther Brunner  // get instructions
312*39a89382SEsther Brunner  $instructions = p_cached_instructions(wikiFN($id));
313*39a89382SEsther Brunner
314*39a89382SEsther Brunner  // set up the renderer
315*39a89382SEsther Brunner  $renderer = & new Doku_Renderer_metadata();
316*39a89382SEsther Brunner  $renderer->meta = $orig;
317*39a89382SEsther Brunner
318*39a89382SEsther Brunner  // loop through the instructions
319*39a89382SEsther Brunner  foreach ($instructions as $instruction){
320*39a89382SEsther Brunner    // execute the callback against the renderer
321*39a89382SEsther Brunner    call_user_func_array(array(&$renderer, $instruction[0]), $instruction[1]);
322*39a89382SEsther Brunner  }
323*39a89382SEsther Brunner
324*39a89382SEsther Brunner  return $renderer->meta;
325*39a89382SEsther Brunner}
326*39a89382SEsther Brunner
327*39a89382SEsther Brunner/**
328107b01d6Sandi * returns all available parser syntax modes in correct order
329107b01d6Sandi *
330107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
331107b01d6Sandi */
332107b01d6Sandifunction p_get_parsermodes(){
333107b01d6Sandi  global $conf;
334107b01d6Sandi
335107b01d6Sandi  //reuse old data
336107b01d6Sandi  static $modes = null;
337107b01d6Sandi  if($modes != null){
338107b01d6Sandi    return $modes;
339107b01d6Sandi  }
340107b01d6Sandi
341107b01d6Sandi  //import parser classes and mode definitions
342107b01d6Sandi  require_once DOKU_INC . 'inc/parser/parser.php';
343107b01d6Sandi
344107b01d6Sandi  // we now collect all syntax modes and their objects, then they will
345107b01d6Sandi  // be sorted and added to the parser in correct order
346107b01d6Sandi  $modes = array();
347107b01d6Sandi
348107b01d6Sandi  // add syntax plugins
349107b01d6Sandi  $pluginlist = plugin_list('syntax');
350107b01d6Sandi  if(count($pluginlist)){
351107b01d6Sandi    global $PARSER_MODES;
352107b01d6Sandi    $obj = null;
353107b01d6Sandi    foreach($pluginlist as $p){
354c90b2fb1Schris      if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj
355107b01d6Sandi      $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
356107b01d6Sandi      //add to modes
357107b01d6Sandi      $modes[] = array(
358107b01d6Sandi                   'sort' => $obj->getSort(),
359107b01d6Sandi                   'mode' => "plugin_$p",
360107b01d6Sandi                   'obj'  => $obj,
361107b01d6Sandi                 );
362a46d0d65SAndreas Gohr      unset($obj); //remove the reference
363107b01d6Sandi    }
364107b01d6Sandi  }
365107b01d6Sandi
366107b01d6Sandi  // add default modes
367107b01d6Sandi  $std_modes = array('listblock','preformatted','notoc','nocache',
368107b01d6Sandi                     'header','table','linebreak','footnote','hr',
369107b01d6Sandi                     'unformatted','php','html','code','file','quote',
370e77ea1bcSAndreas Gohr                     'internallink','rss','media','externallink',
371e77ea1bcSAndreas Gohr                     'emaillink','windowssharelink','eol');
372e77ea1bcSAndreas Gohr  if($conf['typography']){
373e77ea1bcSAndreas Gohr    $std_modes[] = 'quotes';
374e77ea1bcSAndreas Gohr    $std_modes[] = 'multiplyentity';
375e77ea1bcSAndreas Gohr  }
376107b01d6Sandi  foreach($std_modes as $m){
377107b01d6Sandi    $class = "Doku_Parser_Mode_$m";
378107b01d6Sandi    $obj   = new $class();
379107b01d6Sandi    $modes[] = array(
380107b01d6Sandi                 'sort' => $obj->getSort(),
381107b01d6Sandi                 'mode' => $m,
382107b01d6Sandi                 'obj'  => $obj
383107b01d6Sandi               );
384107b01d6Sandi  }
385107b01d6Sandi
386107b01d6Sandi  // add formatting modes
387107b01d6Sandi  $fmt_modes = array('strong','emphasis','underline','monospace',
388107b01d6Sandi                     'subscript','superscript','deleted');
389107b01d6Sandi  foreach($fmt_modes as $m){
390107b01d6Sandi    $obj   = new Doku_Parser_Mode_formatting($m);
391107b01d6Sandi    $modes[] = array(
392107b01d6Sandi                 'sort' => $obj->getSort(),
393107b01d6Sandi                 'mode' => $m,
394107b01d6Sandi                 'obj'  => $obj
395107b01d6Sandi               );
396107b01d6Sandi  }
397107b01d6Sandi
398107b01d6Sandi  // add modes which need files
399107b01d6Sandi  $obj     = new Doku_Parser_Mode_smiley(array_keys(getSmileys()));
400107b01d6Sandi  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj'  => $obj );
401107b01d6Sandi  $obj     = new Doku_Parser_Mode_acronym(array_keys(getAcronyms()));
402107b01d6Sandi  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj'  => $obj );
403107b01d6Sandi  $obj     = new Doku_Parser_Mode_entity(array_keys(getEntities()));
404107b01d6Sandi  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj'  => $obj );
405107b01d6Sandi
406107b01d6Sandi
407107b01d6Sandi  // add optional camelcase mode
408107b01d6Sandi  if($conf['camelcase']){
409107b01d6Sandi    $obj     = new Doku_Parser_Mode_camelcaselink();
410107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj'  => $obj );
411107b01d6Sandi  }
412107b01d6Sandi
413107b01d6Sandi  //sort modes
414107b01d6Sandi  usort($modes,'p_sort_modes');
415107b01d6Sandi
416107b01d6Sandi  return $modes;
417107b01d6Sandi}
418107b01d6Sandi
419107b01d6Sandi/**
420107b01d6Sandi * Callback function for usort
421107b01d6Sandi *
422107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
423107b01d6Sandi */
424107b01d6Sandifunction p_sort_modes($a, $b){
425107b01d6Sandi  if($a['sort'] == $b['sort']) return 0;
426107b01d6Sandi  return ($a['sort'] < $b['sort']) ? -1 : 1;
427107b01d6Sandi}
428107b01d6Sandi
429107b01d6Sandi/**
430ac83b9d8Sandi * Renders a list of instruction to the specified output mode
431c112d578Sandi *
4329dc2c2afSandi * In the $info array are informations from the renderer returned
4339dc2c2afSandi *
434c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
435c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
436c112d578Sandi */
4379dc2c2afSandifunction p_render($mode,$instructions,& $info){
438c112d578Sandi  if(is_null($instructions)) return '';
439c112d578Sandi
440c19c9173SBen Coburn  if ($mode=='wiki') { msg("Renderer for $mode not valid",-1); return null; } //FIXME!! remove this line when inc/parser/wiki.php works.
441c19c9173SBen Coburn
442c112d578Sandi  // Create the renderer
443ac83b9d8Sandi  if(!@file_exists(DOKU_INC."inc/parser/$mode.php")){
444ac83b9d8Sandi    msg("No renderer for $mode found",-1);
445ac83b9d8Sandi    return null;
446ac83b9d8Sandi  }
447ac83b9d8Sandi
448ac83b9d8Sandi  require_once DOKU_INC."inc/parser/$mode.php";
449ac83b9d8Sandi  $rclass = "Doku_Renderer_$mode";
4506b7b33dcShfuecks  if ( !class_exists($rclass) ) {
451c19c9173SBen Coburn    trigger_error("Unable to resolve render class $rclass",E_USER_WARNING);
452c19c9173SBen Coburn    msg("Renderer for $mode not valid",-1);
453c19c9173SBen Coburn    return null;
4546b7b33dcShfuecks  }
455ac83b9d8Sandi  $Renderer = & new $rclass(); #FIXME any way to check for class existance?
456c112d578Sandi
457c112d578Sandi  $Renderer->smileys = getSmileys();
458c112d578Sandi  $Renderer->entities = getEntities();
459c112d578Sandi  $Renderer->acronyms = getAcronyms();
460c112d578Sandi  $Renderer->interwiki = getInterwiki();
461c112d578Sandi  #$Renderer->badwords = getBadWords();
462c112d578Sandi
463c112d578Sandi  // Loop through the instructions
464c112d578Sandi  foreach ( $instructions as $instruction ) {
465c112d578Sandi      // Execute the callback against the Renderer
466c112d578Sandi      call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
467c112d578Sandi  }
4689dc2c2afSandi
4699dc2c2afSandi  //set info array
4709dc2c2afSandi  $info = $Renderer->info;
4719dc2c2afSandi
472c112d578Sandi  // Return the output
473c112d578Sandi  return $Renderer->doc;
474c112d578Sandi}
475c112d578Sandi
476bb0a59d4Sjan/**
477bb0a59d4Sjan * Gets the first heading from a file
478bb0a59d4Sjan *
479bb0a59d4Sjan * @author Jan Decaluwe <jan@jandecaluwe.com>
480bb0a59d4Sjan */
481bb0a59d4Sjanfunction p_get_first_heading($id){
482bb0a59d4Sjan  $file = wikiFN($id);
483bb0a59d4Sjan  if (@file_exists($file)) {
4846e38d921Sandi    $instructions = p_cached_instructions($file,true);
485e1c10e4dSchris		$meta = $instructions[0][1];
486e1c10e4dSchris		return isset($meta[0]['first_heading']) ? trim($meta[0]['first_heading']) : NULL;
487bb0a59d4Sjan  }
488bb0a59d4Sjan  return NULL;
489bb0a59d4Sjan}
490bb0a59d4Sjan
491c112d578Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
492