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