xref: /dokuwiki/inc/parserutils.php (revision 9dc2c2af726890564ad8e83ab451fe55af98deef)
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');
14c112d578Sandi
15c112d578Sandi/**
16c112d578Sandi * Returns the parsed Wikitext in XHTML for the given id and revision.
17c112d578Sandi *
18c112d578Sandi * If $excuse is true an explanation is returned if the file
19c112d578Sandi * wasn't found
20c112d578Sandi *
21c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
22c112d578Sandi */
23c112d578Sandifunction p_wiki_xhtml($id, $rev='', $excuse=true){
24c112d578Sandi  $file = wikiFN($id,$rev);
25c112d578Sandi  $ret  = '';
26c112d578Sandi
27c112d578Sandi  //ensure $id is in global $ID (needed for parsing)
281e76272cSandi  global $ID;
291e76272cSandi  $ID = $id;
30c112d578Sandi
31c112d578Sandi  if($rev){
32c112d578Sandi    if(@file_exists($file)){
33*9dc2c2afSandi      $ret = p_render('xhtml',p_get_instructions(io_readfile($file)),$info); //no caching on old revisions
34c112d578Sandi    }elseif($excuse){
35c112d578Sandi      $ret = p_locale_xhtml('norev');
36c112d578Sandi    }
37c112d578Sandi  }else{
38c112d578Sandi    if(@file_exists($file)){
39c112d578Sandi      $ret = p_cached_xhtml($file);
40c112d578Sandi    }elseif($excuse){
41c112d578Sandi      $ret = p_locale_xhtml('newpage');
42c112d578Sandi    }
43c112d578Sandi  }
44c112d578Sandi
45c112d578Sandi  return $ret;
46c112d578Sandi}
47c112d578Sandi
48c112d578Sandi/**
49c112d578Sandi * Returns the specified local text in parsed format
50c112d578Sandi *
51c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
52c112d578Sandi */
53c112d578Sandifunction p_locale_xhtml($id){
54c112d578Sandi  //fetch parsed locale
55c112d578Sandi  $html = p_cached_xhtml(localeFN($id));
56c112d578Sandi  return $html;
57c112d578Sandi}
58c112d578Sandi
59c112d578Sandi/**
60c112d578Sandi * Returns the given file parsed to XHTML
61c112d578Sandi *
62c112d578Sandi * Uses and creates a cachefile
63c112d578Sandi *
64c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
65*9dc2c2afSandi * @todo   rewrite to use mode instead of hardcoded XHTML
66c112d578Sandi */
67c112d578Sandifunction p_cached_xhtml($file){
68c112d578Sandi  global $conf;
69c112d578Sandi  $cache  = $conf['datadir'].'/_cache/xhtml/';
70c112d578Sandi  $cache .= md5($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT']);
711094c798Sandi  $purge  = $conf['datadir'].'/_cache/purgefile';
72c112d578Sandi
73c112d578Sandi  // check if cache can be used
74c112d578Sandi  $cachetime = @filemtime($cache); // 0 if not exists
75c112d578Sandi
76c112d578Sandi  if( @file_exists($file)                                             // does the source exist
77c112d578Sandi      && $cachetime > @filemtime($file)                               // cache is fresh
78c112d578Sandi      && ((time() - $cachetime) < $conf['cachetime'])                 // and is cachefile young enough
79c112d578Sandi      && !isset($_REQUEST['purge'])                                   // no purge param was set
801094c798Sandi      && ($cachetime > @filemtime($purge))                            // and newer than the purgefile
81c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'conf/dokuwiki.php'))      // newer than the config file
82c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'conf/local.php'))         // newer than the local config file
83c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/xhtml.php'))   // newer than the renderer
84c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/parser.php'))  // newer than the parser
85c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/handler.php')))// newer than the handler
86c112d578Sandi  {
87c112d578Sandi    //well then use the cache
88c112d578Sandi    $parsed = io_readfile($cache);
89c112d578Sandi    $parsed .= "\n<!-- cachefile $cache used -->\n";
90c112d578Sandi  }else{
91*9dc2c2afSandi    $parsed = p_render('xhtml', p_cached_instructions($file),$info); //try to use cached instructions
92c112d578Sandi
93*9dc2c2afSandi    if($info['cache']){
94c112d578Sandi      io_saveFile($cache,$parsed); //save cachefile
95c112d578Sandi      $parsed .= "\n<!-- no cachefile used, but created -->\n";
96c112d578Sandi    }else{
97c112d578Sandi      @unlink($cache); //try to delete cachefile
98c112d578Sandi      $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
99c112d578Sandi    }
100c112d578Sandi  }
101c112d578Sandi
102c112d578Sandi  return $parsed;
103c112d578Sandi}
104c112d578Sandi
105c112d578Sandi/**
106c112d578Sandi * Returns the render instructions for a file
107c112d578Sandi *
108c112d578Sandi * Uses and creates a serialized cache file
109c112d578Sandi *
110c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
111c112d578Sandi */
11237e34a5eSandifunction p_cached_instructions($file,$cacheonly=false){
113c112d578Sandi  global $conf;
114c112d578Sandi  $cache  = $conf['datadir'].'/_cache/instructions/';
115c112d578Sandi  $cache .= md5($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT']);
116c112d578Sandi
117c112d578Sandi  // check if cache can be used
118c112d578Sandi  $cachetime = @filemtime($cache); // 0 if not exists
119c112d578Sandi
12037e34a5eSandi  // cache forced?
12137e34a5eSandi  if($cacheonly){
12237e34a5eSandi    if($cachetime){
12337e34a5eSandi      return unserialize(io_readfile($cache));
12437e34a5eSandi    }else{
12537e34a5eSandi      return NULL;
12637e34a5eSandi    }
12737e34a5eSandi  }
12837e34a5eSandi
129c112d578Sandi  if( @file_exists($file)                                             // does the source exist
130c112d578Sandi      && $cachetime > @filemtime($file)                               // cache is fresh
131c112d578Sandi      && !isset($_REQUEST['purge'])                                   // no purge param was set
132c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'conf/dokuwiki.php'))      // newer than the config file
133c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'conf/local.php'))         // newer than the local config file
134c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/parser.php'))  // newer than the parser
135c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/handler.php')))// newer than the handler
136c112d578Sandi  {
137c112d578Sandi    //well then use the cache
138c112d578Sandi    return unserialize(io_readfile($cache));
139c112d578Sandi  }elseif(@file_exists($file)){
140c112d578Sandi    // no cache - do some work
1416bbae538Sandi    $ins = p_get_instructions(io_readfile($file));
142c112d578Sandi    io_savefile($cache,serialize($ins));
143c112d578Sandi    return $ins;
144c112d578Sandi  }
145c112d578Sandi
146c112d578Sandi  return NULL;
147c112d578Sandi}
148c112d578Sandi
149c112d578Sandi/**
150c112d578Sandi * turns a page into a list of instructions
151c112d578Sandi *
152c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
153c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
154c112d578Sandi */
1556bbae538Sandifunction p_get_instructions($text){
156c112d578Sandi  global $conf;
157c112d578Sandi
158c112d578Sandi  require_once DOKU_INC . 'inc/parser/parser.php';
159c112d578Sandi
160c112d578Sandi  // Create the parser
161c112d578Sandi  $Parser = & new Doku_Parser();
162c112d578Sandi
163c112d578Sandi  // Add the Handler
164c112d578Sandi  $Parser->Handler = & new Doku_Handler();
165c112d578Sandi
166c112d578Sandi  // Load all the modes
167c112d578Sandi  $Parser->addMode('listblock',new Doku_Parser_Mode_ListBlock());
168c112d578Sandi  $Parser->addMode('preformatted',new Doku_Parser_Mode_Preformatted());
169c112d578Sandi  $Parser->addMode('notoc',new Doku_Parser_Mode_NoToc());
170*9dc2c2afSandi  $Parser->addMode('nocache',new Doku_Parser_Mode_NoCache());
171c112d578Sandi  $Parser->addMode('header',new Doku_Parser_Mode_Header());
172c112d578Sandi  $Parser->addMode('table',new Doku_Parser_Mode_Table());
173c112d578Sandi
174c112d578Sandi  $formats = array (
175c112d578Sandi      'strong', 'emphasis', 'underline', 'monospace',
176c112d578Sandi      'subscript', 'superscript', 'deleted',
177c112d578Sandi  );
178c112d578Sandi  foreach ( $formats as $format ) {
179c112d578Sandi      $Parser->addMode($format,new Doku_Parser_Mode_Formatting($format));
180c112d578Sandi  }
181c112d578Sandi
182c112d578Sandi  $Parser->addMode('linebreak',new Doku_Parser_Mode_Linebreak());
183c112d578Sandi  $Parser->addMode('footnote',new Doku_Parser_Mode_Footnote());
184c112d578Sandi  $Parser->addMode('hr',new Doku_Parser_Mode_HR());
185c112d578Sandi
186c112d578Sandi  $Parser->addMode('unformatted',new Doku_Parser_Mode_Unformatted());
187c112d578Sandi  $Parser->addMode('php',new Doku_Parser_Mode_PHP());
188c112d578Sandi  $Parser->addMode('html',new Doku_Parser_Mode_HTML());
189c112d578Sandi  $Parser->addMode('code',new Doku_Parser_Mode_Code());
190c112d578Sandi  $Parser->addMode('file',new Doku_Parser_Mode_File());
191c112d578Sandi  $Parser->addMode('quote',new Doku_Parser_Mode_Quote());
192c112d578Sandi
193c112d578Sandi  $Parser->addMode('smiley',new Doku_Parser_Mode_Smiley(array_keys(getSmileys())));
194c112d578Sandi  $Parser->addMode('acronym',new Doku_Parser_Mode_Acronym(array_keys(getAcronyms())));
195c112d578Sandi  #$Parser->addMode('wordblock',new Doku_Parser_Mode_Wordblock(getBadWords()));
196c112d578Sandi  $Parser->addMode('entity',new Doku_Parser_Mode_Entity(array_keys(getEntities())));
197c112d578Sandi
198c112d578Sandi  $Parser->addMode('multiplyentity',new Doku_Parser_Mode_MultiplyEntity());
199c112d578Sandi  $Parser->addMode('quotes',new Doku_Parser_Mode_Quotes());
200c112d578Sandi
201c112d578Sandi  if($conf['camelcase']){
202c112d578Sandi    $Parser->addMode('camelcaselink',new Doku_Parser_Mode_CamelCaseLink());
203c112d578Sandi  }
204c112d578Sandi
205c112d578Sandi  $Parser->addMode('internallink',new Doku_Parser_Mode_InternalLink());
206c112d578Sandi  $Parser->addMode('rss',new Doku_Parser_Mode_RSS());
207c112d578Sandi  $Parser->addMode('media',new Doku_Parser_Mode_Media());
208c112d578Sandi  $Parser->addMode('externallink',new Doku_Parser_Mode_ExternalLink());
20971352defSandi  $Parser->addMode('emaillink',new Doku_Parser_Mode_EmailLink());
210c112d578Sandi  $Parser->addMode('windowssharelink',new Doku_Parser_Mode_WindowsShareLink());
211c112d578Sandi  //$Parser->addMode('filelink',new Doku_Parser_Mode_FileLink()); //FIXME ???
212c112d578Sandi  $Parser->addMode('eol',new Doku_Parser_Mode_Eol());
213c112d578Sandi
214c112d578Sandi  // Do the parsing
215a2d649c4Sandi  $p    = $Parser->parse($text);
216a2d649c4Sandi#  dbg($p);
217a2d649c4Sandi  return $p;
218c112d578Sandi}
219c112d578Sandi
220c112d578Sandi/**
221ac83b9d8Sandi * Renders a list of instruction to the specified output mode
222c112d578Sandi *
223*9dc2c2afSandi * In the $info array are informations from the renderer returned
224*9dc2c2afSandi *
225c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
226c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
227c112d578Sandi */
228*9dc2c2afSandifunction p_render($mode,$instructions,& $info){
229c112d578Sandi  if(is_null($instructions)) return '';
230c112d578Sandi
231c112d578Sandi  // Create the renderer
232ac83b9d8Sandi  if(!@file_exists(DOKU_INC."inc/parser/$mode.php")){
233ac83b9d8Sandi    msg("No renderer for $mode found",-1);
234ac83b9d8Sandi    return null;
235ac83b9d8Sandi  }
236ac83b9d8Sandi
237ac83b9d8Sandi  require_once DOKU_INC."inc/parser/$mode.php";
238ac83b9d8Sandi  $rclass = "Doku_Renderer_$mode";
239ac83b9d8Sandi  $Renderer = & new $rclass(); #FIXME any way to check for class existance?
240c112d578Sandi
241c112d578Sandi  $Renderer->smileys = getSmileys();
242c112d578Sandi  $Renderer->entities = getEntities();
243c112d578Sandi  $Renderer->acronyms = getAcronyms();
244c112d578Sandi  $Renderer->interwiki = getInterwiki();
245c112d578Sandi  #$Renderer->badwords = getBadWords();
246c112d578Sandi
247c112d578Sandi  // Loop through the instructions
248c112d578Sandi  foreach ( $instructions as $instruction ) {
249c112d578Sandi      // Execute the callback against the Renderer
250c112d578Sandi      call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
251c112d578Sandi  }
252*9dc2c2afSandi
253*9dc2c2afSandi  //set info array
254*9dc2c2afSandi  $info = $Renderer->info;
255*9dc2c2afSandi
256c112d578Sandi  // Return the output
257c112d578Sandi  return $Renderer->doc;
258c112d578Sandi}
259c112d578Sandi
260bb0a59d4Sjan/**
261bb0a59d4Sjan * Gets the first heading from a file
262bb0a59d4Sjan *
263bb0a59d4Sjan * @author Jan Decaluwe <jan@jandecaluwe.com>
264bb0a59d4Sjan */
265bb0a59d4Sjanfunction p_get_first_heading($id){
266bb0a59d4Sjan  $file = wikiFN($id);
267bb0a59d4Sjan  if (@file_exists($file)) {
2686e38d921Sandi    $instructions = p_cached_instructions($file,true);
269bb0a59d4Sjan    foreach ( $instructions as $instruction ) {
270bb0a59d4Sjan      if ($instruction[0] == 'header') {
271bb0a59d4Sjan        return $instruction[1][0];
272bb0a59d4Sjan      }
273bb0a59d4Sjan    }
274bb0a59d4Sjan  }
275bb0a59d4Sjan  return NULL;
276bb0a59d4Sjan}
277bb0a59d4Sjan
278c112d578Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
279