]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Template.php
protect page in links. new doVariableExpansion() for CreatePage. preg_quote custom...
[SourceForge/phpwiki.git] / lib / plugin / Template.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Template.php,v 1.6 2007-01-03 21:24:06 rurban Exp $');
3 /*
4  Copyright 2005 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
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.
12
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.
17
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
21  */
22
23 /**
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" ?>
29  * Author:  Reini Urban
30  * See also: http://meta.wikimedia.org/wiki/Help:Template
31  *
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.
36  *
37  * The following predefined variables are automatically expanded if existing:
38  *   pagename
39  *   mtime     - last modified date + time
40  *   ctime     - creation date + time
41  *   author    - last author
42  *   owner     
43  *   creator   - first author
44  *   SERVER_URL, DATA_PATH, SCRIPT_NAME, PHPWIKI_BASE_URL and BASE_URL
45  *
46  * <noinclude> .. </noinclude> is stripped
47  *
48  * See also:
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}}
52  *   to 
53  *     <?plugin Template page=TemplateFilm vars="title=Some Good Film&year=1999" ?>
54  */
55
56 class WikiPlugin_Template
57 extends WikiPlugin
58 {
59     function getName() {
60         return _("Template");
61     }
62
63     function getDescription() {
64         return _("Parametrized page inclusion.");
65     }
66
67     function getVersion() {
68         return preg_replace("/[Revision: $]/", '',
69                             "\$Revision: 1.6 $");
70     }
71
72     function getDefaultArguments() {
73         return array( 
74                      'page'    => false, // the page to include
75                      'vars'    => false,
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
79                      );
80     }
81
82     // TODO: check if page can really be pulled from the args, or if it is just the basepage. 
83     function getWikiPageLinks($argstr, $basepage) {
84         extract($this->getArgs($argstr));
85         if (isset($page)) {
86             // Expand relative page names.
87             $page = new WikiPageName($page, $basepage);
88         }
89         if (!isset($page) or !$page->name)
90             return false;
91         return array(array('linkto' => $page->name, 'relation' => 0));
92     }
93                 
94     function run($dbi, $argstr, &$request, $basepage) {
95         extract($this->getArgs($argstr, $request));
96         if ($page) {
97             // Expand relative page names.
98             $page = new WikiPageName($page, $basepage);
99             $page = $page->name;
100         }
101         if (!$page) {
102             return $this->error(_("no page specified"));
103         }
104
105         // Protect from recursive inclusion. A page can include itself once
106         static $included_pages = array();
107         if (in_array($page, $included_pages)) {
108             return $this->error(sprintf(_("recursive inclusion of page %s"),
109                                         $page));
110         }
111
112         $p = $dbi->getPage($page);
113         if ($rev) {
114             $r = $p->getRevision($rev);
115             if (!$r) {
116                 return $this->error(sprintf(_("%s(%d): no such revision"),
117                                             $page, $rev));
118             }
119         } else {
120             $r = $p->getCurrentRevision();
121         }
122         $initial_content = $r->getPackedContent();
123         $c = explode("\n", $initial_content);
124
125         if ($section) {
126             $c = extractSection($section, $c, $page, $quiet, $sectionhead);
127             $initial_content = implode("\n", $c);
128         }
129
130         if (preg_match('/<noinclude>.+<\/noinclude>/s', $initial_content)) {
131             $initial_content = preg_replace("/<noinclude>.+?<\/noinclude>/s", "", 
132                                             $initial_content);
133         }
134         $this->doVariableExpansion($initial_content, $vars, $basepage, $dbi);
135
136         array_push($included_pages, $page);
137
138         include_once('lib/BlockParser.php');
139         $content = TransformText($initial_content, $r->get('markup'), $page);
140
141         array_pop($included_pages);
142
143         return HTML::div(array('class' => 'template'), $content);
144     }
145
146     /**
147      * Expand template variables. Used by the TemplatePlugin and the CreatePagePlugin
148      */
149     function doVariableExpansion($content, $vars, $basepage, &$dbi) {
150         if (preg_match('/%%\w+%%/', $content)) // need variable expansion
151         {
152             $var = array();
153             if (!empty($vars)) {
154                 foreach (split("&",$vars) as $pair) {
155                     list($key,$val) = split("=",$pair);
156                     $var[$key] = $val;
157                 }
158             }
159             $thispage = $dbi->getPage($basepage);
160             // pagename is not overridable
161             if (empty($var['pagename']))
162                 $var['pagename'] = $page;
163             // those are overridable
164             if (empty($var['mtime']) and preg_match('/%%mtime%%/', $content)) {
165                 $thisrev  = $thispage->getCurrentRevision(false);
166                 $var['mtime'] = $GLOBALS['WikiTheme']->formatDateTime($thisrev->get('mtime'));
167             }
168             if (empty($var['ctime']) and preg_match('/%%ctime%%/', $content)) {
169                 if ($first = $thispage->getRevision(1,false))
170                     $var['ctime'] = $GLOBALS['WikiTheme']->formatDateTime($first->get('mtime'));
171             }
172             if (empty($var['author']) and preg_match('/%%author%%/', $content))
173                 $var['author'] = $thispage->getAuthor();
174             if (empty($var['owner']) and preg_match('/%%owner%%/', $content))
175                 $var['owner'] = $thispage->getOwner();
176             if (empty($var['creator']) and preg_match('/%%creator%%/', $content))
177                 $var['creator'] = $thispage->getCreator();
178             foreach (array("SERVER_URL", "DATA_PATH", "SCRIPT_NAME", "PHPWIKI_BASE_URL") as $c) {
179                 // constants are not overridable
180                 if (preg_match('/%%\b'.$c.'\b%%/', $content))
181                     $var[$c] = constant($c);
182             }
183             if (preg_match('/%%BASE_URL%%/', $content))
184                 $var['BASE_URL'] = PHPWIKI_BASE_URL;
185
186             foreach ($var as $key => $val) {
187                 $content = preg_replace("/%%".preg_quote($key,"/")."%%/", $val, $content);
188             }
189         }
190         return $content;
191     }
192 };
193
194 // $Log: not supported by cvs2svn $
195 // Revision 1.5  2006/04/17 17:28:21  rurban
196 // honor getWikiPageLinks change linkto=>relation
197 //
198 // Revision 1.4  2005/09/11 13:30:22  rurban
199 // improve comments
200 //
201 // Revision 1.3  2005/09/10 20:43:19  rurban
202 // support <noinclude>
203 //
204 // Revision 1.2  2005/09/10 20:07:16  rurban
205 // fix BASE_URL
206 //
207 // Revision 1.1  2005/09/10 19:59:38  rurban
208 // Parametrized page inclusion ala mediawiki
209 //
210
211 // For emacs users
212 // Local Variables:
213 // mode: php
214 // tab-width: 8
215 // c-basic-offset: 4
216 // c-hanging-comment-ender-p: nil
217 // indent-tabs-mode: nil
218 // End:
219 ?>