4 require_once("lib/ErrorManager.php");
12 * name optionally of form "theme/template" to include parent templates in children
14 function Template ($name, &$request, $args = false) {
17 $this->_request =& $request;
18 $this->_basepage = $request->getArg('pagename');
20 if (strstr($name, "/")) {
21 $oldname = $WikiTheme->_name;
22 $oldtheme = $WikiTheme->_theme;
23 list($themename, $name) = explode("/", $name);
24 $WikiTheme->_theme = "themes/$themename";
25 $WikiTheme->_name = $name;
28 $file = $WikiTheme->findTemplate($name);
30 trigger_error("no template for $name found.", E_USER_WARNING);
33 if (isset($oldname)) {
34 $WikiTheme->_name = $oldname;
35 $WikiTheme->_theme = $oldtheme;
37 $fp = fopen($file, "rb");
39 trigger_error("$file not found", E_USER_WARNING);
42 $request->_TemplatesProcessed[$name] = 1;
43 $this->_tmpl = fread($fp, filesize($file));
45 //$userid = $request->_user->_userid;
47 $this->_locals = $args;
49 $this->_locals = array('CONTENT' => $args);
51 $this->_locals = array();
54 function _munge_input($template) {
56 // Convert < ?plugin expr ? > to < ?php $this->_printPluginPI("expr"); ? >
57 $orig[] = '/<\?plugin.*?\?>/se';
58 $repl[] = "\$this->_mungePlugin('\\0')";
60 // Convert < ?= expr ? > to < ?php $this->_print(expr); ? >
61 $orig[] = '/<\?=(.*?)\?>/s';
62 $repl[] = '<?php $this->_print(\1);?>';
64 // Convert < ?php echo expr ? > to < ?php $this->_print(expr); ? >
65 $orig[] = '/<\?php echo (.*?)\?>/s';
66 $repl[] = '<?php $this->_print(\1);?>';
68 return preg_replace($orig, $repl, $template);
71 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) {
82 include_once("lib/WikiPlugin.php");
86 $loader = new WikiPluginLoader;
88 $this->_print($loader->expandPI($pi, $this->_request, $this, $this->_basepage));
91 function _print ($val) {
92 if (isa($val, 'Template')) {
93 $this->_expandSubtemplate($val);
99 function _expandSubtemplate (&$template) {
101 //if (!$template->_request)
102 // $template->_request = &$this->_request;
104 echo "<!-- Begin $template->_name -->\n";
106 // Expand sub-template with defaults from this template.
107 $template->printExpansion($this->_vars);
109 echo "<!-- End $template->_name -->\n";
114 * Substitute HTML replacement text for tokens in template.
116 * Constructs a new WikiTemplate based upon the named template.
120 * @param $token string Name of token to substitute for.
122 * @param $replacement string Replacement HTML text.
124 function replace($varname, $value) {
125 $this->_locals[$varname] = $value;
129 function printExpansion ($defaults = false) {
130 if (!is_array($defaults)) // HTML object or template object
131 $defaults = array('CONTENT' => $defaults);
132 $this->_vars = array_merge($defaults, $this->_locals);
133 extract($this->_vars);
137 $user = $request->getUser();
139 $page = $request->getPage();
140 // Speedup. I checked all templates
141 if (!isset($revision))
144 global $WikiTheme, $charset;
145 //$this->_dump_template();
146 $SEP = $WikiTheme->getButtonSeparator();
148 global $ErrorManager;
149 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_errorHandler'));
151 eval('?>' . $this->_munge_input($this->_tmpl));
153 $ErrorManager->popErrorHandler();
157 // Find a way to do template expansion less memory intensive and faster.
158 // 1.3.4 needed no memory at all for dumphtml, now it needs +15MB.
159 // Smarty? As before?
160 function getExpansion ($defaults = false) {
162 $this->printExpansion($defaults);
163 $xml = ob_get_contents();
164 ob_end_clean(); // PHP problem: Doesn't release its memory?
168 function printXML () {
169 $this->printExpansion();
173 return $this->getExpansion();
178 function _dump_template () {
179 $lines = explode("\n", $this->_munge_input($this->_tmpl));
182 foreach ($lines as $line)
183 $pre->pushContent(fmt("%4d %s\n", $n++, $line));
187 function _errorHandler($error) {
188 //if (!preg_match('/: eval\(\)\'d code$/', $error->errfile))
191 if (preg_match('/: eval\(\)\'d code$/', $error->errfile)) {
192 $error->errfile = "In template '$this->_name'";
193 // Hack alert: Ignore 'undefined variable' messages for variables
194 // whose names are ALL_CAPS.
195 if (preg_match('/Undefined variable:\s*[_A-Z]+\s*$/', $error->errstr))
198 // ignore recursively nested htmldump loop: browse -> body -> htmldump -> browse -> body ...
199 // FIXME for other possible loops also
200 elseif (strstr($error->errfile, "In template 'htmldump'")) {
203 elseif (strstr($error->errfile, "In template '")) { // merge
204 $error->errfile = preg_replace("/'(\w+)'\)$/", "'\\1' < '$this->_name')",
208 $error->errfile .= " (In template '$this->_name')";
211 if (!empty($this->_tmpl)) {
212 $lines = explode("\n", $this->_tmpl);
213 if (isset($lines[$error->errline - 1]))
214 $error->errstr .= ":\n\t" . $lines[$error->errline - 1];
223 * This is a convenience function and is equivalent to:
228 function Template($name, $args = false) {
230 return new Template($name, $request, $args);
233 function alreadyTemplateProcessed($name) {
235 return !empty($request->_TemplatesProcessed[$name]) ? true : false;
238 * Make and expand the top-level template.
241 * @param $content mixed html content to put into the page
242 * @param $title string page title
243 * @param $page_revision object A WikiDB_PageRevision object or false
244 * @param $args hash Extract args for top-level template
246 * @return string HTML expansion of template.
248 function GeneratePage($content, $title, $page_revision = false, $args = false) {
251 if (!is_array($args))
254 $args['CONTENT'] = $content;
255 $args['TITLE'] = $title;
256 $args['revision'] = $page_revision;
258 if (!isset($args['HEADER']))
259 $args['HEADER'] = $title;
261 printXML(new Template('html', $request, $args));
266 * For dumping pages as html to a file.
267 * Used for action=dumphtml,action=ziphtml,format=pdf,format=xml
269 function GeneratePageasXML($content, $title, $page_revision = false, $args = false) {
272 if (!is_array($args))
275 $content->_basepage = $title;
276 $args['CONTENT'] = $content;
277 $args['TITLE'] = SplitPagename($title);
278 $args['revision'] = $page_revision;
280 if (!isset($args['HEADER']))
281 $args['HEADER'] = SplitPagename($title);
283 global $HIDE_TOOLBARS, $NO_BASEHREF, $WikiTheme;
284 $HIDE_TOOLBARS = true;
285 if (!$WikiTheme->DUMP_MODE)
286 $WikiTheme->DUMP_MODE = 'HTML';
288 // FIXME: unfatal errors and login requirements
289 $html = asXML(new Template('htmldump', $request, $args));
291 $HIDE_TOOLBARS = false;
292 //$WikiTheme->DUMP_MODE = false;
300 // c-hanging-comment-ender-p: nil
301 // indent-tabs-mode: nil