xref: /dokuwiki/inc/parserutils.php (revision c112d57805d6740c6e0a200fd5fd3f2717f86c69)
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($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($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($file){
149  global $conf;
150
151  $text = io_readfile($file);
152
153  require_once DOKU_INC . 'inc/parser/parser.php';
154
155  // Create the parser
156  $Parser = & new Doku_Parser();
157
158  // Add the Handler
159  $Parser->Handler = & new Doku_Handler();
160
161  // Load all the modes
162  $Parser->addMode('listblock',new Doku_Parser_Mode_ListBlock());
163  $Parser->addMode('preformatted',new Doku_Parser_Mode_Preformatted());
164  $Parser->addMode('notoc',new Doku_Parser_Mode_NoToc());
165  $Parser->addMode('header',new Doku_Parser_Mode_Header());
166  $Parser->addMode('table',new Doku_Parser_Mode_Table());
167
168  $formats = array (
169      'strong', 'emphasis', 'underline', 'monospace',
170      'subscript', 'superscript', 'deleted',
171  );
172  foreach ( $formats as $format ) {
173      $Parser->addMode($format,new Doku_Parser_Mode_Formatting($format));
174  }
175
176  $Parser->addMode('linebreak',new Doku_Parser_Mode_Linebreak());
177  $Parser->addMode('footnote',new Doku_Parser_Mode_Footnote());
178  $Parser->addMode('hr',new Doku_Parser_Mode_HR());
179
180  $Parser->addMode('unformatted',new Doku_Parser_Mode_Unformatted());
181  $Parser->addMode('php',new Doku_Parser_Mode_PHP());
182  $Parser->addMode('html',new Doku_Parser_Mode_HTML());
183  $Parser->addMode('code',new Doku_Parser_Mode_Code());
184  $Parser->addMode('file',new Doku_Parser_Mode_File());
185  $Parser->addMode('quote',new Doku_Parser_Mode_Quote());
186
187  $Parser->addMode('smiley',new Doku_Parser_Mode_Smiley(array_keys(getSmileys())));
188  $Parser->addMode('acronym',new Doku_Parser_Mode_Acronym(array_keys(getAcronyms())));
189  #$Parser->addMode('wordblock',new Doku_Parser_Mode_Wordblock(getBadWords()));
190  $Parser->addMode('entity',new Doku_Parser_Mode_Entity(array_keys(getEntities())));
191
192  $Parser->addMode('multiplyentity',new Doku_Parser_Mode_MultiplyEntity());
193  $Parser->addMode('quotes',new Doku_Parser_Mode_Quotes());
194
195  if($conf['camelcase']){
196    $Parser->addMode('camelcaselink',new Doku_Parser_Mode_CamelCaseLink());
197  }
198
199  $Parser->addMode('internallink',new Doku_Parser_Mode_InternalLink());
200  $Parser->addMode('rss',new Doku_Parser_Mode_RSS());
201  $Parser->addMode('media',new Doku_Parser_Mode_Media());
202  $Parser->addMode('externallink',new Doku_Parser_Mode_ExternalLink());
203  $Parser->addMode('email',new Doku_Parser_Mode_Email());
204  $Parser->addMode('windowssharelink',new Doku_Parser_Mode_WindowsShareLink());
205  //$Parser->addMode('filelink',new Doku_Parser_Mode_FileLink()); //FIXME ???
206  $Parser->addMode('eol',new Doku_Parser_Mode_Eol());
207
208  // Do the parsing
209  return $Parser->parse($text);
210}
211
212/**
213 * Renders a list of instruction to XHTML
214 *
215 * @author Harry Fuecks <hfuecks@gmail.com>
216 * @author Andreas Gohr <andi@splitbrain.org>
217 */
218function p_render_xhtml($instructions){
219  if(is_null($instructions)) return '';
220
221  // Create the renderer
222  require_once DOKU_INC . 'inc/parser/xhtml.php';
223  $Renderer = & new Doku_Renderer_XHTML();
224
225  $Renderer->smileys = getSmileys();
226  $Renderer->entities = getEntities();
227  $Renderer->acronyms = getAcronyms();
228  $Renderer->interwiki = getInterwiki();
229  #$Renderer->badwords = getBadWords();
230
231  // Loop through the instructions
232  foreach ( $instructions as $instruction ) {
233      // Execute the callback against the Renderer
234      call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
235  }
236  // Return the output
237  return $Renderer->doc;
238}
239
240//Setup VIM: ex: et ts=2 enc=utf-8 :
241