]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Template.php
improve comments
[SourceForge/phpwiki.git] / lib / plugin / Template.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Template.php,v 1.4 2005-09-11 13:30:22 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 /**
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" ?>
30  * Author:  Reini Urban
31  * See also: http://meta.wikimedia.org/wiki/Help:Template
32  *
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.
37  *
38  * The following predefined variables are automatically expanded if existing:
39  *   pagename
40  *   mtime     - last modified date + time
41  *   ctime     - creation date + time
42  *   author    - last author
43  *   owner     
44  *   creator   - first author
45  *   SERVER_URL, DATA_PATH, SCRIPT_NAME, PHPWIKI_BASE_URL and BASE_URL
46  *
47  * <noinclude> .. </noinclude> is stripped
48  *
49  * In work:
50  * - ENABLE_MARKUP_TEMPLATE = true: (lib/InlineParser.php)
51  *   Support a mediawiki-style syntax extension which maps 
52  *     {{TemplateFilm|title=Some Good Film|year=1999}}
53  *   to 
54  *     <?plugin Template page=TemplateFilm vars="title=Some Good Film&year=1999" ?>
55  */
56
57 class WikiPlugin_Template
58 extends WikiPlugin
59 {
60     function getName() {
61         return _("Template");
62     }
63
64     function getDescription() {
65         return _("Parametrized page inclusion.");
66     }
67
68     function getVersion() {
69         return preg_replace("/[Revision: $]/", '',
70                             "\$Revision: 1.4 $");
71     }
72
73     function getDefaultArguments() {
74         return array( 
75                      'page'    => false, // the page to include
76                      'vars'    => false,
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
80                      );
81     }
82
83     function getWikiPageLinks($argstr, $basepage) {
84         extract($this->getArgs($argstr));
85         if ($page) {
86             // Expand relative page names.
87             $page = new WikiPageName($page, $basepage);
88         }
89         if (!$page or !$page->name)
90             return false;
91         return array($page->name);
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         if (preg_match('/%%\w+%%/', $initial_content)) // need variable expansion
135         {
136             $var = array();
137             if (!empty($vars)) {
138                 foreach (split("&",$vars) as $pair) {
139                     list($key,$val) = split("=",$pair);
140                     $var[$key] = $val;
141                 }
142             }
143             $thispage = $dbi->getPage($basepage);
144             // pagename is not overridable
145             if (empty($var['pagename']))
146                 $var['pagename'] = $page;
147             // those are overridable
148             if (empty($var['mtime']) and preg_match('/%%mtime%%/', $initial_content)) {
149                 $thisrev  = $thispage->getCurrentRevision(false);
150                 $var['mtime'] = $GLOBALS['WikiTheme']->formatDateTime($thisrev->get('mtime'));
151             }
152             if (empty($var['ctime']) and preg_match('/%%ctime%%/', $initial_content)) {
153                 if ($first = $thispage->getRevision(1,false))
154                     $var['ctime'] = $GLOBALS['WikiTheme']->formatDateTime($first->get('mtime'));
155             }
156             if (empty($var['author']) and preg_match('/%%author%%/', $initial_content))
157                 $var['author'] = $thispage->getAuthor();
158             if (empty($var['owner']) and preg_match('/%%owner%%/', $initial_content))
159                 $var['owner'] = $thispage->getOwner();
160             if (empty($var['creator']) and preg_match('/%%creator%%/', $initial_content))
161                 $var['creator'] = $thispage->getCreator();
162             foreach (array("SERVER_URL", "DATA_PATH", "SCRIPT_NAME", "PHPWIKI_BASE_URL") as $c) {
163                 // constants are not overridable
164                 if (preg_match('/%%'.$c.'%%/', $initial_content))
165                     $var[$c] = constant($c);
166             }
167             if (preg_match('/%%BASE_URL%%/', $initial_content))
168                 $var['BASE_URL'] = PHPWIKI_BASE_URL;
169
170             foreach ($var as $key => $val) {
171                 $initial_content = preg_replace("/%%$key%%/", $val, $initial_content);
172             }
173         }
174
175         array_push($included_pages, $page);
176
177         include_once('lib/BlockParser.php');
178         $content = TransformText($initial_content, $r->get('markup'), $page);
179
180         array_pop($included_pages);
181
182         return HTML::div(array('class' => 'template'), $content);
183     }
184 };
185
186 // $Log: not supported by cvs2svn $
187 // Revision 1.3  2005/09/10 20:43:19  rurban
188 // support <noinclude>
189 //
190 // Revision 1.2  2005/09/10 20:07:16  rurban
191 // fix BASE_URL
192 //
193 // Revision 1.1  2005/09/10 19:59:38  rurban
194 // Parametrized page inclusion ala mediawiki
195 //
196
197 // For emacs users
198 // Local Variables:
199 // mode: php
200 // tab-width: 8
201 // c-basic-offset: 4
202 // c-hanging-comment-ender-p: nil
203 // indent-tabs-mode: nil
204 // End:
205 ?>