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: <?plugin 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 * <?plugin Template page=TemplateFilm vars="title=Some Good Film&year=1999" ?>
57 class WikiPlugin_Template
64 function getDescription() {
65 return _("Parametrized page inclusion.");
68 function getVersion() {
69 return preg_replace("/[Revision: $]/", '',
73 function getDefaultArguments() {
75 'page' => false, // the page to include
76 'vars' => false, // TODO: get rid of this, all remaining args should be vars
77 'rev' => false, // the revision (defaults to most recent)
78 'section' => false, // just include a named section
79 'sectionhead' => false // when including a named section show the heading
82 function allow_undeclared_arg($name, $value) {
83 // either just allow it or you can store it here away also.
84 $this->vars[$name] = $value;
85 return $name != 'action';
88 // TODO: check if page can really be pulled from the args, or if it is just the basepage.
89 function getWikiPageLinks($argstr, $basepage) {
90 $args = $this->getArgs($argstr);
91 $page = @$args['page'];
93 // Expand relative page names.
94 $page = new WikiPageName($page, $basepage);
96 if (!$page or !$page->name)
98 return array(array('linkto' => $page->name, 'relation' => 0));
101 function run($dbi, $argstr, &$request, $basepage) {
102 $this->vars = array();
103 $args = $this->getArgs($argstr, $request);
104 $vars = $args['vars'] ? $args['vars'] : $this->vars;
105 $page = $args['page'];
107 // Expand relative page names.
108 $page = new WikiPageName($page, $basepage);
112 return $this->error(_("no page specified"));
115 // If "Template:$page" exists, use it
116 // else if "Template/$page" exists, use it
118 if ($dbi->isWikiPage("Template:" . $page)) {
119 $page = "Template:" . $page;
120 } elseif ($dbi->isWikiPage("Template/" . $page)) {
121 $page = "Template/" . $page;
124 // Protect from recursive inclusion. A page can include itself once
125 static $included_pages = array();
126 if (in_array($page, $included_pages)) {
127 return $this->error(sprintf(_("recursive inclusion of page %s"),
131 // Check if user is allowed to get the Page.
132 if (!mayAccessPage ('view', $page)) {
133 return $this->error(sprintf(_("Illegal inclusion of page %s: no read access"),
137 $p = $dbi->getPage($page);
139 $r = $p->getRevision($args['rev']);
141 return $this->error(sprintf(_("%s(%d): no such revision"),
142 $page, $args['rev']));
145 $r = $p->getCurrentRevision();
147 $initial_content = $r->getPackedContent();
149 $content = $r->getContent();
151 if ((preg_match('/<'.'\?plugin\s+RedirectTo\s+page=(\S+)\s*\?'.'>/',
152 implode("\n", $content), $m))
153 or (preg_match('/<<\s*RedirectTo\s+page=(\S+)\s*>>/',
154 implode("\n", $content), $m)))
156 // Strip quotes (simple or double) from page name if any
157 if ((string_starts_with($m[1], "'"))
158 or (string_starts_with($m[1], "\""))) {
159 $m[1] = substr($m[1], 1, -1);
161 // trap recursive redirects
162 if (in_array($m[1], $included_pages)) {
163 return $this->error(sprintf(_("recursive inclusion of page %s ignored"),
164 $page.' => '.$m[1]));
167 $p = $dbi->getPage($page);
168 $r = $p->getCurrentRevision();
169 $initial_content = $r->getPackedContent();
172 if ($args['section']) {
173 $c = explode("\n", $initial_content);
174 $c = extractSection($args['section'], $c, $page, $quiet, $args['sectionhead']);
175 $initial_content = implode("\n", $c);
177 // exclude from expansion
178 if (preg_match('/<noinclude>.+<\/noinclude>/s', $initial_content)) {
179 $initial_content = preg_replace("/<noinclude>.+?<\/noinclude>/s", "",
183 $initial_content = preg_replace("/<includeonly>(.+)<\/includeonly>/s", "\\1",
185 $this->doVariableExpansion($initial_content, $vars, $basepage, $request);
187 array_push($included_pages, $page);
189 // If content is single-line, call TransformInline, else call TransformText
190 $initial_content = trim($initial_content, "\n");
191 if (preg_match("/\n/", $initial_content)) {
192 include_once('lib/BlockParser.php');
193 $content = TransformText($initial_content, $r->get('markup'), $page);
195 include_once('lib/InlineParser.php');
196 $content = TransformInline($initial_content, $r->get('markup'), $page);
199 array_pop($included_pages);
205 * Expand template variables. Used by the TemplatePlugin and the CreatePagePlugin
207 function doVariableExpansion(&$content, $vars, $basepage, &$request) {
208 if (preg_match('/%%\w+%%/', $content)) // need variable expansion
210 $dbi =& $request->_dbi;
212 if (is_string($vars) and !empty($vars)) {
213 foreach (split("&",$vars) as $pair) {
214 list($key,$val) = split("=",$pair);
217 } elseif (is_array($vars)) {
220 $thispage = $dbi->getPage($basepage);
221 // pagename and userid are not overridable
222 $var['PAGENAME'] = $thispage->getName();
223 if (preg_match('/%%USERID%%/', $content))
224 $var['USERID'] = $request->_user->getId();
225 if (empty($var['MTIME']) and preg_match('/%%MTIME%%/', $content)) {
226 $thisrev = $thispage->getCurrentRevision(false);
227 $var['MTIME'] = $GLOBALS['WikiTheme']->formatDateTime($thisrev->get('mtime'));
229 if (empty($var['CTIME']) and preg_match('/%%CTIME%%/', $content)) {
230 if ($first = $thispage->getRevision(1,false))
231 $var['CTIME'] = $GLOBALS['WikiTheme']->formatDateTime($first->get('mtime'));
233 if (empty($var['AUTHOR']) and preg_match('/%%AUTHOR%%/', $content))
234 $var['AUTHOR'] = $thispage->getAuthor();
235 if (empty($var['OWNER']) and preg_match('/%%OWNER%%/', $content))
236 $var['OWNER'] = $thispage->getOwner();
237 if (empty($var['CREATOR']) and preg_match('/%%CREATOR%%/', $content))
238 $var['CREATOR'] = $thispage->getCreator();
239 foreach (array("SERVER_URL", "DATA_PATH", "SCRIPT_NAME", "PHPWIKI_BASE_URL") as $c) {
240 // constants are not overridable
241 if (preg_match('/%%'.$c.'%%/', $content))
242 $var[$c] = constant($c);
244 if (preg_match('/%%BASE_URL%%/', $content))
245 $var['BASE_URL'] = PHPWIKI_BASE_URL;
247 foreach ($var as $key => $val) {
248 //$content = preg_replace("/%%".preg_quote($key,"/")."%%/", $val, $content);
249 $content = str_replace("%%".$key."%%", $val, $content);
261 // c-hanging-comment-ender-p: nil
262 // indent-tabs-mode: nil