4 * Copyright 2005,2007 $ThePhpWikiProgrammingTeam
5 * Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
9 * PhpWiki is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * PhpWiki is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with PhpWiki; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Template: Parametrized blocks.
26 * Include text from a wiki page and replace certain placeholders by parameters.
27 * Similiar to CreatePage with the template argument, but at run-time.
28 * Similiar to the mediawiki templates but not with the "|" parameter seperator.
29 * Usage: <<Template page=TemplateFilm vars="title=rurban&year=1999" >>
31 * See also: http://meta.wikimedia.org/wiki/Help:Template
33 * Parameter expansion:
34 * vars="var1=value1&var2=value2"
35 * We only support named parameters, not numbered ones as in mediawiki, and
36 * the placeholder is %%var%% and not {{{var}}} as in mediawiki.
38 * The following predefined uppercase variables are automatically expanded if existing:
40 * MTIME - last modified date + time
41 * CTIME - creation date + time
42 * AUTHOR - last author
44 * CREATOR - first author
45 * SERVER_URL, DATA_PATH, SCRIPT_NAME, PHPWIKI_BASE_URL and BASE_URL
47 * <noinclude> .. </noinclude> is stripped from the template expansion.
48 * <includeonly> .. </includeonly> is only expanded in pages using the template,
49 * not in the template itself.
51 * We support a mediawiki-style syntax extension which maps
52 * {{TemplateFilm|title=Some Good Film|year=1999}}
54 * <<Template page=TemplateFilm vars="title=Some Good Film&year=1999" >>
57 class WikiPlugin_Template
64 function getDescription() {
65 return _("Parametrized page inclusion.");
68 function getDefaultArguments() {
70 'page' => false, // the page to include
71 'vars' => false, // TODO: get rid of this, all remaining args should be vars
72 'rev' => false, // the revision (defaults to most recent)
73 'version' => false, // same as "rev"
74 'section' => false, // just include a named section
75 'sectionhead' => false // when including a named section show the heading
78 function allow_undeclared_arg($name, $value) {
79 // either just allow it or you can store it here away also.
80 $this->vars[$name] = $value;
81 return $name != 'action';
84 // TODO: check if page can really be pulled from the args, or if it is just the basepage.
85 function getWikiPageLinks($argstr, $basepage) {
86 $args = $this->getArgs($argstr);
87 $page = @$args['page'];
89 // Expand relative page names.
90 $page = new WikiPageName($page, $basepage);
92 if (!$page or !$page->name)
94 return array(array('linkto' => $page->name, 'relation' => 0));
97 function run($dbi, $argstr, &$request, $basepage) {
98 $this->vars = array();
99 $args = $this->getArgs($argstr, $request);
100 $vars = $args['vars'] ? $args['vars'] : $this->vars;
101 $page = $args['page'];
103 if ($args['version'] && $args['rev']) {
104 return $this->error(_("Choose only one of 'version' or 'rev' parameters."));
105 } elseif ($args['version']) {
106 $args['rev'] = $args['version'];
110 // Expand relative page names.
111 $page = new WikiPageName($page, $basepage);
115 return $this->error(_("No page specified."));
118 // If "Template:$page" exists, use it
119 // else if "Template/$page" exists, use it
121 if ($dbi->isWikiPage("Template:" . $page)) {
122 $page = "Template:" . $page;
123 } elseif ($dbi->isWikiPage("Template/" . $page)) {
124 $page = "Template/" . $page;
127 // Protect from recursive inclusion. A page can include itself once
128 static $included_pages = array();
129 if (in_array($page, $included_pages)) {
130 return $this->error(sprintf(_("recursive inclusion of page %s"),
134 // Check if page exists
135 if (!($dbi->isWikiPage($page))) {
136 return $this->error(sprintf(_("Page '%s' does not exist."), $page));
139 // Check if user is allowed to get the Page.
140 if (!mayAccessPage ('view', $page)) {
141 return $this->error(sprintf(_("Illegal inclusion of page %s: no read access."),
145 $p = $dbi->getPage($page);
147 $r = $p->getRevision($args['rev']);
149 return $this->error(sprintf(_("%s(%d): no such revision."),
150 $page, $args['rev']));
153 $r = $p->getCurrentRevision();
155 $initial_content = $r->getPackedContent();
157 $content = $r->getContent();
159 if ((preg_match('/<'.'\?plugin\s+RedirectTo\s+page=(\S+)\s*\?'.'>/', implode("\n", $content), $m))
160 or (preg_match('/<'.'\?plugin\s+RedirectTo\s+page=(.*?)\s*\?'.'>/', implode("\n", $content), $m))
161 or (preg_match('/<<\s*RedirectTo\s+page=(\S+)\s*>>/', implode("\n", $content), $m))
162 or (preg_match('/<<\s*RedirectTo\s+page="(.*?)"\s*>>/', implode("\n", $content), $m)))
164 // Strip quotes (simple or double) from page name if any
165 if ((string_starts_with($m[1], "'"))
166 or (string_starts_with($m[1], "\""))) {
167 $m[1] = substr($m[1], 1, -1);
169 // trap recursive redirects
170 if (in_array($m[1], $included_pages)) {
171 return $this->error(sprintf(_("recursive inclusion of page %s ignored"),
172 $page.' => '.$m[1]));
175 $p = $dbi->getPage($page);
176 $r = $p->getCurrentRevision();
177 $initial_content = $r->getPackedContent();
180 if ($args['section']) {
181 $c = explode("\n", $initial_content);
182 $c = extractSection($args['section'], $c, $page, $quiet, $args['sectionhead']);
183 $initial_content = implode("\n", $c);
185 // exclude from expansion
186 if (preg_match('/<noinclude>.+<\/noinclude>/s', $initial_content)) {
187 $initial_content = preg_replace("/<noinclude>.+?<\/noinclude>/s", "",
191 $initial_content = preg_replace("/<includeonly>(.+)<\/includeonly>/s", "\\1",
193 $this->doVariableExpansion($initial_content, $vars, $basepage, $request);
195 array_push($included_pages, $page);
197 // If content is single-line, call TransformInline, else call TransformText
198 $initial_content = trim($initial_content, "\n");
199 if (preg_match("/\n/", $initial_content)) {
200 include_once('lib/BlockParser.php');
201 $content = TransformText($initial_content, $r->get('markup'), $page);
203 include_once('lib/InlineParser.php');
204 $content = TransformInline($initial_content, $r->get('markup'), $page);
207 array_pop($included_pages);
213 * Expand template variables. Used by the TemplatePlugin and the CreatePagePlugin
215 function doVariableExpansion(&$content, $vars, $basepage, &$request) {
216 if (preg_match('/%%\w+%%/', $content)) // need variable expansion
218 $dbi =& $request->_dbi;
220 if (is_string($vars) and !empty($vars)) {
221 foreach (explode("&", $vars) as $pair) {
222 list($key,$val) = explode("=", $pair);
225 } elseif (is_array($vars)) {
228 $thispage = $dbi->getPage($basepage);
229 // pagename and userid are not overridable
230 $var['PAGENAME'] = $thispage->getName();
231 if (preg_match('/%%USERID%%/', $content))
232 $var['USERID'] = $request->_user->getId();
233 if (empty($var['MTIME']) and preg_match('/%%MTIME%%/', $content)) {
234 $thisrev = $thispage->getCurrentRevision(false);
235 $var['MTIME'] = $GLOBALS['WikiTheme']->formatDateTime($thisrev->get('mtime'));
237 if (empty($var['CTIME']) and preg_match('/%%CTIME%%/', $content)) {
238 if ($first = $thispage->getRevision(1,false))
239 $var['CTIME'] = $GLOBALS['WikiTheme']->formatDateTime($first->get('mtime'));
241 if (empty($var['AUTHOR']) and preg_match('/%%AUTHOR%%/', $content))
242 $var['AUTHOR'] = $thispage->getAuthor();
243 if (empty($var['OWNER']) and preg_match('/%%OWNER%%/', $content))
244 $var['OWNER'] = $thispage->getOwner();
245 if (empty($var['CREATOR']) and preg_match('/%%CREATOR%%/', $content))
246 $var['CREATOR'] = $thispage->getCreator();
247 foreach (array("SERVER_URL", "DATA_PATH", "SCRIPT_NAME", "PHPWIKI_BASE_URL") as $c) {
248 // constants are not overridable
249 if (preg_match('/%%'.$c.'%%/', $content))
250 $var[$c] = constant($c);
252 if (preg_match('/%%BASE_URL%%/', $content))
253 $var['BASE_URL'] = PHPWIKI_BASE_URL;
255 foreach ($var as $key => $val) {
256 //$content = preg_replace("/%%".preg_quote($key,"/")."%%/", $val, $content);
257 $content = str_replace("%%".$key."%%", $val, $content);
268 // c-hanging-comment-ender-p: nil
269 // indent-tabs-mode: nil