2 rcs_id('$Id: Template.php,v 1.10 2007-06-07 17:03:38 rurban Exp $');
4 Copyright 2005 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Template: Parametrized blocks.
25 * Include text from a wiki page and replace certain placeholders by parameters.
26 * Similiar to CreatePage with the template argument, but at run-time.
27 * Similiar to the mediawiki templates but not with the "|" parameter seperator.
28 * Usage: <?plugin Template page=TemplateFilm vars="title=rurban&year=1999" ?>
30 * See also: http://meta.wikimedia.org/wiki/Help:Template
32 * Parameter expansion:
33 * vars="var1=value1&var2=value2"
34 * We only support named parameters, not numbered ones as in mediawiki, and
35 * the placeholder is %%var%% and not {{{var}}} as in mediawiki.
37 * The following predefined variables are automatically expanded if existing:
39 * mtime - last modified date + time
40 * ctime - creation date + time
41 * author - last author
43 * creator - first author
44 * SERVER_URL, DATA_PATH, SCRIPT_NAME, PHPWIKI_BASE_URL and BASE_URL
46 * <noinclude> .. </noinclude> is stripped
49 * - ENABLE_MARKUP_TEMPLATE = true: (lib/InlineParser.php)
50 * Support a mediawiki-style syntax extension which maps
51 * {{TemplateFilm|title=Some Good Film|year=1999}}
53 * <?plugin Template page=TemplateFilm vars="title=Some Good Film&year=1999" ?>
56 class WikiPlugin_Template
63 function getDescription() {
64 return _("Parametrized page inclusion.");
67 function getVersion() {
68 return preg_replace("/[Revision: $]/", '',
69 "\$Revision: 1.10 $");
72 function getDefaultArguments() {
74 'page' => false, // the page to include
75 'vars' => false, // TODO: get rid of this, all remaining args should be vars
76 'rev' => false, // the revision (defaults to most recent)
77 'section' => false, // just include a named section
78 'sectionhead' => false // when including a named section show the heading
81 function allow_undeclared_arg($name, $value) {
82 // either just allow it or you can store it here away also.
83 $this->vars[$name] = $value;
84 return $name != 'action';
86 // TODO: check if page can really be pulled from the args, or if it is just the basepage.
87 function getWikiPageLinks($argstr, $basepage) {
88 $args = $this->getArgs($argstr);
89 $page = @$args['page'];
91 // Expand relative page names.
92 $page = new WikiPageName($page, $basepage);
94 if (!$page or !$page->name)
96 return array(array('linkto' => $page->name, 'relation' => 0));
99 function run($dbi, $argstr, &$request, $basepage) {
100 $this->vars = array();
101 $args = $this->getArgs($argstr, $request);
102 $vars = $args['vars'] ? $args['vars'] : $this->vars;
103 $page = $args['page'];
105 // Expand relative page names.
106 $page = new WikiPageName($page, $basepage);
110 return $this->error(_("no page specified"));
113 // Protect from recursive inclusion. A page can include itself once
114 static $included_pages = array();
115 if (in_array($page, $included_pages)) {
116 return $this->error(sprintf(_("recursive inclusion of page %s"),
120 $p = $dbi->getPage($page);
122 $r = $p->getRevision($args['rev']);
124 return $this->error(sprintf(_("%s(%d): no such revision"),
125 $page, $args['rev']));
128 $r = $p->getCurrentRevision();
130 $initial_content = $r->getPackedContent();
132 if ($args['section']) {
133 $c = explode("\n", $initial_content);
134 $c = extractSection($args['section'], $c, $page, $quiet, $args['sectionhead']);
135 $initial_content = implode("\n", $c);
138 if (preg_match('/<noinclude>.+<\/noinclude>/s', $initial_content)) {
139 $initial_content = preg_replace("/<noinclude>.+?<\/noinclude>/s", "",
142 $this->doVariableExpansion($initial_content, $vars, $basepage, $request);
144 array_push($included_pages, $page);
146 include_once('lib/BlockParser.php');
147 $content = TransformText($initial_content, $r->get('markup'), $page);
149 array_pop($included_pages);
151 return HTML::div(array('class' => 'template'), $content);
155 * Expand template variables. Used by the TemplatePlugin and the CreatePagePlugin
157 function doVariableExpansion(&$content, $vars, $basepage, &$request) {
158 if (preg_match('/%%\w+%%/', $content)) // need variable expansion
160 $dbi =& $request->_dbi;
162 if (is_string($vars) and !empty($vars)) {
163 foreach (split("&",$vars) as $pair) {
164 list($key,$val) = split("=",$pair);
167 } elseif (is_array($vars)) {
170 $thispage = $dbi->getPage($basepage);
171 // pagename and userid are not overridable
172 $var['PAGENAME'] = $thispage->getName();
173 if (preg_match('/%%USERID%%/', $content))
174 $var['USERID'] = $request->_user->getId();
175 if (empty($var['MTIME']) and preg_match('/%%MTIME%%/', $content)) {
176 $thisrev = $thispage->getCurrentRevision(false);
177 $var['MTIME'] = $GLOBALS['WikiTheme']->formatDateTime($thisrev->get('mtime'));
179 if (empty($var['CTIME']) and preg_match('/%%CTIME%%/', $content)) {
180 if ($first = $thispage->getRevision(1,false))
181 $var['CTIME'] = $GLOBALS['WikiTheme']->formatDateTime($first->get('mtime'));
183 if (empty($var['AUTHOR']) and preg_match('/%%AUTHOR%%/', $content))
184 $var['AUTHOR'] = $thispage->getAuthor();
185 if (empty($var['OWNER']) and preg_match('/%%OWNER%%/', $content))
186 $var['OWNER'] = $thispage->getOwner();
187 if (empty($var['CREATOR']) and preg_match('/%%CREATOR%%/', $content))
188 $var['CREATOR'] = $thispage->getCreator();
189 foreach (array("SERVER_URL", "DATA_PATH", "SCRIPT_NAME", "PHPWIKI_BASE_URL") as $c) {
190 // constants are not overridable
191 if (preg_match('/%%'.$c.'%%/', $content))
192 $var[$c] = constant($c);
194 if (preg_match('/%%BASE_URL%%/', $content))
195 $var['BASE_URL'] = PHPWIKI_BASE_URL;
197 foreach ($var as $key => $val) {
198 //$content = preg_replace("/%%".preg_quote($key,"/")."%%/", $val, $content);
199 $content = str_replace("%%".$key."%%", $val, $content);
206 // $Log: not supported by cvs2svn $
207 // Revision 1.9 2007/03/04 14:09:13 rurban
208 // silence missing page warning
210 // Revision 1.8 2007/01/25 07:42:29 rurban
211 // Changed doVariableExpansion API. Uppercase default vars. Use str_replace.
213 // Revision 1.7 2007/01/04 16:42:41 rurban
214 // Improve vars passing. Use new method allow_undeclared_arg to allow arbitrary args for the template. Fix doVariableExpansion: use a ref. Fix pagename. Put away \b in regex.
216 // Revision 1.6 2007/01/03 21:24:06 rurban
217 // protect page in links. new doVariableExpansion() for CreatePage. preg_quote custom vars.
219 // Revision 1.5 2006/04/17 17:28:21 rurban
220 // honor getWikiPageLinks change linkto=>relation
222 // Revision 1.4 2005/09/11 13:30:22 rurban
225 // Revision 1.3 2005/09/10 20:43:19 rurban
226 // support <noinclude>
228 // Revision 1.2 2005/09/10 20:07:16 rurban
231 // Revision 1.1 2005/09/10 19:59:38 rurban
232 // Parametrized page inclusion ala mediawiki
240 // c-hanging-comment-ender-p: nil
241 // indent-tabs-mode: nil