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 return preg_replace($orig, $repl, $template);
67 function _mungePlugin($pi) {
68 // HACK ALERT: PHP's preg_replace, with the /e option seems to
69 // escape both single and double quotes with backslashes.
70 // So we need to unescape the double quotes here...
72 $pi = preg_replace('/(?!<\\\\)\\\\"/x', '"', $pi);
73 return sprintf('<?php $this->_printPlugin(%s); ?>',
74 "'" . str_replace("'", "\'", $pi) . "'");
77 function _printPlugin ($pi) {
78 include_once("lib/WikiPlugin.php");
82 $loader = new WikiPluginLoader;
84 $this->_print($loader->expandPI($pi, $this->_request, $this, $this->_basepage));
87 function _print ($val) {
88 if (isa($val, 'Template')) {
89 $this->_expandSubtemplate($val);
95 function _expandSubtemplate (&$template) {
97 //if (!$template->_request)
98 // $template->_request = &$this->_request;
100 echo "<!-- Begin $template->_name -->\n";
102 // Expand sub-template with defaults from this template.
103 $template->printExpansion($this->_vars);
105 echo "<!-- End $template->_name -->\n";
110 * Substitute HTML replacement text for tokens in template.
112 * Constructs a new WikiTemplate based upon the named template.
116 * @param $token string Name of token to substitute for.
118 * @param $replacement string Replacement HTML text.
120 function replace($varname, $value) {
121 $this->_locals[$varname] = $value;
125 function printExpansion ($defaults = false) {
126 if (!is_array($defaults)) // HTML object or template object
127 $defaults = array('CONTENT' => $defaults);
128 $this->_vars = array_merge($defaults, $this->_locals);
129 extract($this->_vars);
133 $user = $request->getUser();
135 $page = $request->getPage();
136 // Speedup. I checked all templates
137 if (!isset($revision))
140 global $WikiTheme, $RCS_IDS, $charset;
141 //$this->_dump_template();
142 $SEP = $WikiTheme->getButtonSeparator();
144 global $ErrorManager;
145 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_errorHandler'));
147 eval('?>' . $this->_munge_input($this->_tmpl));
149 $ErrorManager->popErrorHandler();
153 // Find a way to do template expansion less memory intensive and faster.
154 // 1.3.4 needed no memory at all for dumphtml, now it needs +15MB.
155 // Smarty? As before?
156 function getExpansion ($defaults = false) {
158 $this->printExpansion($defaults);
159 $xml = ob_get_contents();
160 ob_end_clean(); // PHP problem: Doesn't release its memory?
164 function printXML () {
165 $this->printExpansion();
169 return $this->getExpansion();
174 function _dump_template () {
175 $lines = explode("\n", $this->_munge_input($this->_tmpl));
178 foreach ($lines as $line)
179 $pre->pushContent(fmt("%4d %s\n", $n++, $line));
183 function _errorHandler($error) {
184 //if (!preg_match('/: eval\(\)\'d code$/', $error->errfile))
187 if (preg_match('/: eval\(\)\'d code$/', $error->errfile)) {
188 $error->errfile = "In template '$this->_name'";
189 // Hack alert: Ignore 'undefined variable' messages for variables
190 // whose names are ALL_CAPS.
191 if (preg_match('/Undefined variable:\s*[_A-Z]+\s*$/', $error->errstr))
194 // ignore recursively nested htmldump loop: browse -> body -> htmldump -> browse -> body ...
195 // FIXME for other possible loops also
196 elseif (strstr($error->errfile, "In template 'htmldump'")) {
199 elseif (strstr($error->errfile, "In template '")) { // merge
200 $error->errfile = preg_replace("/'(\w+)'\)$/", "'\\1' < '$this->_name')", $error->errfile);
203 $error->errfile .= " (In template '$this->_name')";
206 if (!empty($this->_tmpl)) {
207 $lines = explode("\n", $this->_tmpl);
208 if (isset($lines[$error->errline - 1]))
209 $error->errstr .= ":\n\t" . $lines[$error->errline - 1];
218 * This is a convenience function and is equivalent to:
223 function Template($name, $args = false) {
225 return new Template($name, $request, $args);
228 function alreadyTemplateProcessed($name) {
230 return !empty($request->_TemplatesProcessed[$name]) ? true : false;
233 * Make and expand the top-level template.
236 * @param $content mixed html content to put into the page
237 * @param $title string page title
238 * @param $page_revision object A WikiDB_PageRevision object or false
239 * @param $args hash Extract args for top-level template
241 * @return string HTML expansion of template.
243 function GeneratePage($content, $title, $page_revision = false, $args = false) {
246 if (!is_array($args))
249 $args['CONTENT'] = $content;
250 $args['TITLE'] = $title;
251 $args['revision'] = $page_revision;
253 if (!isset($args['HEADER']))
254 $args['HEADER'] = $title;
256 printXML(new Template('html', $request, $args));
261 * For dumping pages as html to a file.
262 * Used for action=dumphtml,action=ziphtml,format=pdf,format=xml
264 function GeneratePageasXML($content, $title, $page_revision = false, $args = false) {
267 if (!is_array($args))
270 $content->_basepage = $title;
271 $args['CONTENT'] = $content;
272 $args['TITLE'] = SplitPagename($title);
273 $args['revision'] = $page_revision;
275 if (!isset($args['HEADER']))
276 $args['HEADER'] = SplitPagename($title);
278 global $HIDE_TOOLBARS, $NO_BASEHREF, $WikiTheme;
279 $HIDE_TOOLBARS = true;
280 if (!$WikiTheme->DUMP_MODE)
281 $WikiTheme->DUMP_MODE = 'HTML';
283 // FIXME: unfatal errors and login requirements
284 $html = asXML(new Template('htmldump', $request, $args));
286 $HIDE_TOOLBARS = false;
287 //$WikiTheme->DUMP_MODE = false;
295 // c-hanging-comment-ender-p: nil
296 // indent-tabs-mode: nil