1 <?php rcs_id('$Id: Template.php,v 1.1 2001-09-18 19:16:23 dairiki Exp $');
3 require_once("lib/ErrorManager.php");
4 require_once("lib/WikiPlugin.php");
6 //FIXME: This is a mess and needs to be refactored.
7 // (In other words: this is all in a state of flux, so don't count on any
8 // of this being the same tomorrow...)
12 function Template($tmpl) {
13 //$this->_tmpl = $this->_munge_input($tmpl);
15 $this->_vars = array();
18 function _munge_input($template) {
19 // Expand "< ?plugin-* ...? >"
20 preg_match_all('/<\?plugin.*?\?>/s', $template, $m);
21 global $dbi, $request; // FIXME: no globals?
22 $pluginLoader = new WikiPluginLoader;
23 foreach (array_unique($m[0]) as $plugin_pi) {
24 $orig[] = '/' . preg_quote($plugin_pi, '/') . '/s';
25 $repl[] = $pluginLoader->expandPI($plugin_pi, $dbi, $request);
28 // Convert ${VAR} to < ?php echo "$VAR"; ? >
29 //$orig[] = '/\${(\w[\w\d]*)}/';
30 //$repl[] = '<?php echo "$\1"; ? >';
31 $orig[] = '/\${(\w[\w\d]*)}/e';
32 $repl[] = '$this->_getReplacement("\1")';
34 // Convert $VAR[ind] to < ?php echo "$VAR[ind]"; ? >
35 $orig[] = '/\$(\w[\w\d]*)\[([\w\d]+)\]/e';
36 $repl[] = '$this->_getReplacement("\1", "\2")';
38 return preg_replace($orig, $repl, $template);
41 function _getReplacement($varname, $index = false) {
42 // FIXME: report missing vars.
43 $vars = &$this->_vars;
44 if (isset($vars[$varname])) {
45 $value = $vars[$varname];
47 @$value = (string) $value[$index];
48 return str_replace('?', '?', $value);
54 * Substitute HTML replacement text for tokens in template.
56 * Constructs a new WikiTemplate based upon the named template.
60 * @param $token string Name of token to substitute for.
62 * @param $replacement string Replacement HTML text.
64 function replace($varname, $value) {
65 $this->_vars[$varname] = $value;
69 * Substitute text for tokens in template.
73 * @param $token string Name of token to substitute for.
75 * @param $replacement string Replacement text.
76 * The replacement text is run through htmlspecialchars()
77 * to escape any special characters.
79 function qreplace($varname, $value) {
80 $this->_vars[$varname] = htmlspecialchars($value);
85 * Include/remove conditional text in template.
89 * @param $token string Conditional token name.
90 * The text within any matching if blocks (or single line ifs) will
91 * be included in the template expansion, while the text in matching
92 * negated if blocks will be excluded.
95 function setConditional($token, $value = true) {
96 $this->_iftoken[$token] = $value;
100 function getExpansion($varhash = false) {
101 $savevars = $this->_vars;
102 if (is_array($varhash)) {
103 foreach ($varhash as $key => $val)
104 $this->_vars[$key] = $val;
106 extract($this->_vars);
107 if (isset($this->_iftoken))
108 $_iftoken = $this->_iftoken;
112 //$this->_dump_template();
114 global $ErrorManager;
115 $ErrorManager->pushErrorHandler(array($this, '_errorHandler'));
116 eval('?>' . $this->_munge_input($this->_tmpl));
117 $ErrorManager->popErrorHandler();
119 $html = ob_get_contents();
125 function printExpansion($args = false) {
126 echo $this->getExpansion($args);
130 function _dump_template () {
131 $lines = explode("\n", $this->_munge_input($this->_tmpl));
134 foreach ($lines as $line)
135 printf("%4d %s\n", $n++, htmlspecialchars($line));
139 function _errorHandler($error) {
140 if (!preg_match('/: eval\(\)\'d code$/', $error->errfile))
142 $error->errfile = "In template";
143 $lines = explode("\n", $this->_tmpl);
144 if (isset($lines[$error->errline - 1]))
145 $error->errstr .= ":\n\t" . $lines[$error->errline - 1];
153 function TemplateFile($filename) {
154 $this->_template_file = $filename;
155 $fp = fopen($filename, "rb");
156 $data = fread($fp, filesize($filename));
158 $this->Template($data);
168 * Constructs a new WikiTemplate based upon the named template.
172 * @param $template string Which template.
174 function WikiTemplate($template, $page_revision = false) {
177 $this->TemplateFile(FindLocalizedFile($templates[$template]));
178 $this->_template_name = $template;
180 $this->setGlobalTokens();
182 $this->setPageRevisionTokens($page_revision);
186 function setPageTokens(&$page) {
188 if ($page->get('locked'))
189 $this->setConditional('LOCK');
190 // HACK: note that EDITABLE may also be set in setWikiUserTokens.
191 if (!$page->get('locked'))
192 $this->setConditional('EDITABLE');
195 $pagename = $page->getName();
196 $this->replace('page', $page);
197 $this->qreplace('PAGE', $pagename);
198 $this->qreplace('PAGEURL', rawurlencode($pagename));
199 $this->qreplace('SPLIT_PAGE', split_pagename($pagename));
200 $this->qreplace('BROWSE_PAGE', WikiURL($pagename));
202 // FIXME: this is a bit of dangerous hackage.
203 $this->qreplace('ACTION', WikiURL($pagename, array('action' => '')));
206 //$this->replace_callback('HITS', array($page, 'getHitCount'));
207 //$this->replace_callback('RELATEDPAGES', array($page, 'getHitCount'));
208 //_dotoken('RELATEDPAGES', LinkRelatedPages($dbi, $name), $page);
211 function setPageRevisionTokens(&$revision) {
212 $page = & $revision->getPage();
214 $current = & $page->getCurrentRevision();
215 $previous = & $page->getRevisionBefore($revision->getVersion());
217 $this->replace('IS_CURRENT',
218 $current->getVersion() == $revision->getVersion());
221 if ($previous && $previous->getVersion() != 0)
222 $this->setConditional('COPY'); // FIXME: should rename HAVE_COPY?
225 global $datetimeformat;
227 $this->qreplace('LASTMODIFIED',
228 strftime($datetimeformat, $revision->get('mtime')));
229 $this->qreplace('LASTAUTHOR', $revision->get('author'));
230 $this->qreplace('VERSION', $revision->getVersion());
231 $this->qreplace('CURRENT_VERSION', $current->getVersion());
233 $this->setPageTokens($page);
236 function setWikiUserTokens(&$user) {
238 if ( $user->is_admin() ) {
239 $this->setConditional('ADMIN');
240 $this->setConditional('EDITABLE');
242 if ( ! $user->is_authenticated() )
243 $this->setConditional('ANONYMOUS');
245 $this->replace('user', $user);
246 $this->qreplace('USERID', $user->id());
247 $prefs = $user->getPreferences();
248 $this->qreplace('EDIT_AREA_WIDTH', $prefs['edit_area.width']);
249 $this->qreplace('EDIT_AREA_HEIGHT', $prefs['edit_area.height']);
252 function setGlobalTokens () {
253 global $user, $logo, $RCS_IDS;
255 // FIXME: This a a bit of dangerous hackage.
256 $this->qreplace('BROWSE', WikiURL(''));
257 $this->qreplace('CSS_URL', DataURL(CSS_URL));
260 $this->setWikiUserTokens($user);
262 $this->qreplace('LOGO', DataURL($logo));
264 $this->qreplace('RCS_IDS', $RCS_IDS);
266 $this->qreplace('BASE_URL',
268 WikiURL($GLOBALS['pagename'], false, 'absolute_url'));
274 * Generate page contents using a template.
276 * This is a convenience function for backwards compatibility with the old
279 * @param $template string name of the template (see config.php for list of names)
281 * @param $content string html content to put into the page
283 * @param $title string page title
285 * @param $page_revision object Current WikiDB_PageRevision, if available.
287 * @return string HTML expansion of template.
289 function GeneratePage($template, $content, $title, $page_revision = false) {
290 // require_once("lib/template.php");
291 $t = new WikiTemplate($template);
293 $t->setPageRevisionTokens($page_revision);
294 $t->replace('CONTENT', $content);
295 $t->replace('TITLE', $title);
296 return $t->getExpansion();
303 // c-hanging-comment-ender-p: nil
304 // indent-tabs-mode: nil33