4 * Copyright 2005,2007 $ThePhpWikiProgrammingTeam
5 * Copyright 2008-2011 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 along
20 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 separator.
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
62 function getDescription()
64 return _("Parametrized page inclusion.");
67 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 'section' => false, // just include a named section
74 'sectionhead' => false // when including a named section show the heading
78 function allow_undeclared_arg($name, $value)
80 // either just allow it or you can store it here away also.
81 $this->vars[$name] = $value;
82 return $name != 'action';
85 // TODO: check if page can really be pulled from the args, or if it is just the basepage.
86 function getWikiPageLinks($argstr, $basepage)
88 $args = $this->getArgs($argstr);
89 $page = isset($args['page']) ? $args['page'] : '';
91 // Expand relative page names.
92 $page = new WikiPageName($page, $basepage);
94 if (!$page or !$page->name)
98 if (empty($backlinks)) {
100 $this->run($request->_dbi, $argstr, $request, $basepage);
103 $backlinks[] = array('linkto' => $page->name);
109 * @param string $argstr
110 * @param WikiRequest $request
111 * @param string $basepage
114 function run($dbi, $argstr, &$request, $basepage)
116 $this->vars = array();
117 $args = $this->getArgs($argstr, $request);
118 $vars = $args['vars'] ? $args['vars'] : $this->vars;
119 $page = $args['page'];
122 // Expand relative page names.
123 $page = new WikiPageName($page, $basepage);
127 return $this->error(sprintf(_("A required argument ā%sā is missing."), 'page'));
130 // If "Template:$page" exists, use it
131 // else if "Template/$page" exists, use it
133 if ($dbi->isWikiPage("Template:" . $page)) {
134 $page = "Template:" . $page;
135 } elseif ($dbi->isWikiPage("Template/" . $page)) {
136 $page = "Template/" . $page;
139 // Protect from recursive inclusion.
141 if ($rootpage == '') {
142 $rootpage = $basepage;
144 if ($page == $rootpage) {
145 return $this->error(sprintf(_("Recursive inclusion of page %s ignored"),
149 // Check if page exists
150 if (!($dbi->isWikiPage($page))) {
151 return $this->error(sprintf(_("Page ā%sā does not exist."), $page));
154 // Check if user is allowed to get the Page.
155 if (!mayAccessPage('view', $page)) {
156 return $this->error(sprintf(_("Illegal inclusion of page %s: no read access."),
160 $p = $dbi->getPage($page);
162 if (!is_whole_number($args['rev']) or !($args['rev'] > 0)) {
163 return $this->error(_("Error: rev must be a positive integer."));
165 $r = $p->getRevision($args['rev']);
166 if ((!$r) || ($r->hasDefaultContents())) {
167 return $this->error(sprintf(_("%s: no such revision %d."),
168 $page, $args['rev']));
171 $r = $p->getCurrentRevision();
173 $initial_content = $r->getPackedContent();
175 $content = $r->getContent();
177 if ((preg_match('/<' . '\?plugin\s+RedirectTo\s+page=(\S+)\s*\?' . '>/', implode("\n", $content), $m))
178 or (preg_match('/<' . '\?plugin\s+RedirectTo\s+page=(.*?)\s*\?' . '>/', implode("\n", $content), $m))
179 or (preg_match('/<<\s*RedirectTo\s+page=(\S+)\s*>>/', implode("\n", $content), $m))
180 or (preg_match('/<<\s*RedirectTo\s+page="(.*?)"\s*>>/', implode("\n", $content), $m))
182 // Strip quotes (simple or double) from page name if any
183 if ((string_starts_with($m[1], "'"))
184 or (string_starts_with($m[1], "\""))
186 $m[1] = substr($m[1], 1, -1);
188 // trap recursive redirects
189 if ($m[1] == $rootpage) {
190 return $this->error(sprintf(_("Recursive inclusion of page %s ignored"),
191 $page . ' => ' . $m[1]));
194 $p = $dbi->getPage($page);
195 $r = $p->getCurrentRevision();
196 $initial_content = $r->getPackedContent();
199 if ($args['section']) {
200 $c = explode("\n", $initial_content);
201 $c = extractSection($args['section'], $c, $page, $quiet, $args['sectionhead']);
202 $initial_content = implode("\n", $c);
204 // exclude from expansion
205 if (preg_match('/<noinclude>.+<\/noinclude>/s', $initial_content)) {
206 $initial_content = preg_replace("/<noinclude>.+?<\/noinclude>/s", "",
210 $initial_content = preg_replace("/<includeonly>(.+)<\/includeonly>/s", "\\1",
212 $this->doVariableExpansion($initial_content, $vars, $basepage, $request);
214 // If content is single-line, call TransformInline, else call TransformText
215 $initial_content = trim($initial_content, "\n");
216 if (preg_match("/\n/", $initial_content)) {
217 include_once 'lib/BlockParser.php';
218 $content = TransformText($initial_content, $page);
220 include_once 'lib/InlineParser.php';
221 $content = TransformInline($initial_content, $page);
228 * Expand template variables. Used by the TemplatePlugin and the CreatePagePlugin
230 function doVariableExpansion(&$content, $vars, $basepage, &$request)
232 if (preg_match('/%%\w+%%/', $content)) // need variable expansion
234 $dbi =& $request->_dbi;
236 if (is_string($vars) and !empty($vars)) {
237 foreach (explode("&", $vars) as $pair) {
238 list($key, $val) = explode("=", $pair);
241 } elseif (is_array($vars)) {
244 $thispage = $dbi->getPage($basepage);
245 // pagename and userid are not overridable
246 $var['PAGENAME'] = $thispage->getName();
247 if (preg_match('/%%USERID%%/', $content))
248 $var['USERID'] = $request->_user->getId();
249 if (empty($var['MTIME']) and preg_match('/%%MTIME%%/', $content)) {
250 $thisrev = $thispage->getCurrentRevision(false);
251 $var['MTIME'] = $GLOBALS['WikiTheme']->formatDateTime($thisrev->get('mtime'));
253 if (empty($var['CTIME']) and preg_match('/%%CTIME%%/', $content)) {
254 if ($first = $thispage->getRevision(1, false))
255 $var['CTIME'] = $GLOBALS['WikiTheme']->formatDateTime($first->get('mtime'));
257 if (empty($var['AUTHOR']) and preg_match('/%%AUTHOR%%/', $content))
258 $var['AUTHOR'] = $thispage->getAuthor();
259 if (empty($var['OWNER']) and preg_match('/%%OWNER%%/', $content))
260 $var['OWNER'] = $thispage->getOwner();
261 if (empty($var['CREATOR']) and preg_match('/%%CREATOR%%/', $content))
262 $var['CREATOR'] = $thispage->getCreator();
263 foreach (array("SERVER_URL", "DATA_PATH", "SCRIPT_NAME", "PHPWIKI_BASE_URL") as $c) {
264 // constants are not overridable
265 if (preg_match('/%%' . $c . '%%/', $content))
266 $var[$c] = constant($c);
268 if (preg_match('/%%BASE_URL%%/', $content))
269 $var['BASE_URL'] = PHPWIKI_BASE_URL;
271 foreach ($var as $key => $val) {
272 // We have to decode the double quotes that have been encoded
273 // in inline or block parser.
274 $content = str_replace("%%" . $key . "%%", htmlspecialchars_decode($val), $content);
285 // c-hanging-comment-ender-p: nil
286 // indent-tabs-mode: nil