3 require_once 'lib/ErrorManager.php';
10 * name optionally of form "theme/template" to include parent templates in children
12 function Template($name, &$request, $args = false)
16 $this->_request =& $request;
17 $this->_basepage = $request->getArg('pagename');
19 if (strstr($name, "/")) {
20 $oldname = $WikiTheme->_name;
21 $oldtheme = $WikiTheme->_theme;
22 list($themename, $name) = explode("/", $name);
23 $WikiTheme->_theme = "themes/$themename";
24 $WikiTheme->_name = $name;
27 $file = $WikiTheme->findTemplate($name);
29 trigger_error("no template for $name found.", E_USER_WARNING);
32 if (isset($oldname)) {
33 $WikiTheme->_name = $oldname;
34 $WikiTheme->_theme = $oldtheme;
36 $fp = fopen($file, "rb");
38 trigger_error("$file not found", E_USER_WARNING);
41 $request->_TemplatesProcessed[$name] = 1;
42 $this->_tmpl = fread($fp, filesize($file));
44 //$userid = $request->_user->_userid;
46 $this->_locals = $args;
48 $this->_locals = array('CONTENT' => $args); else
49 $this->_locals = array();
52 function _munge_input($template)
55 // Convert < ?plugin expr ? > to < ?php $this->_printPluginPI("expr"); ? >
56 $orig[] = '/<\?plugin.*?\?>/se';
57 $repl[] = "\$this->_mungePlugin('\\0')";
59 // Convert < ?= expr ? > to < ?php $this->_print(expr); ? >
60 $orig[] = '/<\?=(.*?)\?>/s';
61 $repl[] = '<?php $this->_print(\1);?>';
63 // Convert < ?php echo expr ? > to < ?php $this->_print(expr); ? >
64 $orig[] = '/<\?php echo (.*?)\?>/s';
65 $repl[] = '<?php $this->_print(\1);?>';
67 return preg_replace($orig, $repl, $template);
70 function _mungePlugin($pi)
72 // HACK ALERT: PHP's preg_replace, with the /e option seems to
73 // escape both single and double quotes with backslashes.
74 // So we need to unescape the double quotes here...
76 $pi = preg_replace('/(?!<\\\\)\\\\"/x', '"', $pi);
77 return sprintf('<?php $this->_printPlugin(%s); ?>',
78 "'" . str_replace("'", "\'", $pi) . "'");
81 function _printPlugin($pi)
83 include_once 'lib/WikiPlugin.php';
87 $loader = new WikiPluginLoader;
89 $this->_print($loader->expandPI($pi, $this->_request, $this, $this->_basepage));
94 if (isa($val, 'Template')) {
95 $this->_expandSubtemplate($val);
101 function _expandSubtemplate(&$template)
104 //if (!$template->_request)
105 // $template->_request = &$this->_request;
107 echo "<!-- Begin $template->_name -->\n";
109 // Expand sub-template with defaults from this template.
110 $template->printExpansion($this->_vars);
112 echo "<!-- End $template->_name -->\n";
117 * Substitute HTML replacement text for tokens in template.
119 * Constructs a new WikiTemplate based upon the named template.
123 * @param $token string Name of token to substitute for.
125 * @param $replacement string Replacement HTML text.
127 function replace($varname, $value)
129 $this->_locals[$varname] = $value;
132 function printExpansion($defaults = false)
134 if (!is_array($defaults)) // HTML object or template object
135 $defaults = array('CONTENT' => $defaults);
136 $this->_vars = array_merge($defaults, $this->_locals);
137 extract($this->_vars);
141 $user = $request->getUser();
143 $page = $request->getPage();
144 // Speedup. I checked all templates
145 if (!isset($revision))
148 global $WikiTheme, $charset;
149 //$this->_dump_template();
150 $SEP = $WikiTheme->getButtonSeparator();
152 global $ErrorManager;
153 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_errorHandler'));
155 eval('?>' . $this->_munge_input($this->_tmpl));
157 $ErrorManager->popErrorHandler();
161 // Find a way to do template expansion less memory intensive and faster.
162 // 1.3.4 needed no memory at all for dumphtml, now it needs +15MB.
163 // Smarty? As before?
164 function getExpansion($defaults = false)
167 $this->printExpansion($defaults);
168 $xml = ob_get_contents();
169 ob_end_clean(); // PHP problem: Doesn't release its memory?
175 $this->printExpansion();
180 return $this->getExpansion();
184 function _dump_template()
186 $lines = explode("\n", $this->_munge_input($this->_tmpl));
189 foreach ($lines as $line)
190 $pre->pushContent(fmt("%4d %s\n", $n++, $line));
194 function _errorHandler($error)
196 //if (!preg_match('/: eval\(\)\'d code$/', $error->errfile))
199 if (preg_match('/: eval\(\)\'d code$/', $error->errfile)) {
200 $error->errfile = "In template '$this->_name'";
201 // Hack alert: Ignore 'undefined variable' messages for variables
202 // whose names are ALL_CAPS.
203 if (preg_match('/Undefined variable:\s*[_A-Z]+\s*$/', $error->errstr))
205 } // ignore recursively nested htmldump loop: browse -> body -> htmldump -> browse -> body ...
206 // FIXME for other possible loops also
207 elseif (strstr($error->errfile, "In template 'htmldump'")) {
209 } elseif (strstr($error->errfile, "In template '")) { // merge
210 $error->errfile = preg_replace("/'(\w+)'\)$/", "'\\1' < '$this->_name')",
213 $error->errfile .= " (In template '$this->_name')";
216 if (!empty($this->_tmpl)) {
217 $lines = explode("\n", $this->_tmpl);
218 if (isset($lines[$error->errline - 1]))
219 $error->errstr .= ":\n\t" . $lines[$error->errline - 1];
228 * This is a convenience function and is equivalent to:
233 function Template($name, $args = false)
236 return new Template($name, $request, $args);
239 function alreadyTemplateProcessed($name)
242 return !empty($request->_TemplatesProcessed[$name]) ? true : false;
246 * Make and expand the top-level template.
249 * @param $content mixed html content to put into the page
250 * @param $title string page title
251 * @param $page_revision object A WikiDB_PageRevision object or false
252 * @param $args hash Extract args for top-level template
254 * @return string HTML expansion of template.
256 function GeneratePage($content, $title, $page_revision = false, $args = false)
260 if (!is_array($args))
263 $args['CONTENT'] = $content;
264 $args['TITLE'] = $title;
265 $args['revision'] = $page_revision;
267 if (!isset($args['HEADER']))
268 $args['HEADER'] = $title;
270 printXML(new Template('html', $request, $args));
274 * For dumping pages as html to a file.
275 * Used for action=dumphtml,action=ziphtml,format=pdf,format=xml
277 function GeneratePageasXML($content, $title, $page_revision = false, $args = false)
281 if (!is_array($args))
284 $content->_basepage = $title;
285 $args['CONTENT'] = $content;
286 $args['TITLE'] = SplitPagename($title);
287 $args['revision'] = $page_revision;
289 if (!isset($args['HEADER']))
290 $args['HEADER'] = SplitPagename($title);
292 global $HIDE_TOOLBARS, $NO_BASEHREF, $WikiTheme;
293 $HIDE_TOOLBARS = true;
294 if (!$WikiTheme->DUMP_MODE)
295 $WikiTheme->DUMP_MODE = 'HTML';
297 // FIXME: unfatal errors and login requirements
298 $html = asXML(new Template('htmldump', $request, $args));
300 $HIDE_TOOLBARS = false;
301 //$WikiTheme->DUMP_MODE = false;
309 // c-hanging-comment-ender-p: nil
310 // indent-tabs-mode: nil