]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Template.php
support <noinclude>
[SourceForge/phpwiki.git] / lib / plugin / Template.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Template.php,v 1.3 2005-09-10 20:43:19 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  * 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  * In work:
49  * - ENABLE_MARKUP_TEMPLATE = true: (lib/InlineParser.php)
50  *   Support a mediawiki-style syntax extension which maps 
51  *     {{TemplateFilm|title=rurban|year=1999}}
52  *   to 
53  *     <?plugin Template page=TemplateFilm vars="title=rurban&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.3 $");
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     function getWikiPageLinks($argstr, $basepage) {
83         extract($this->getArgs($argstr));
84         if ($page) {
85             // Expand relative page names.
86             $page = new WikiPageName($page, $basepage);
87         }
88         if (!$page or !$page->name)
89             return false;
90         return array($page->name);
91     }
92                 
93     function run($dbi, $argstr, &$request, $basepage) {
94         extract($this->getArgs($argstr, $request));
95         if ($page) {
96             // Expand relative page names.
97             $page = new WikiPageName($page, $basepage);
98             $page = $page->name;
99         }
100         if (!$page) {
101             return $this->error(_("no page specified"));
102         }
103
104         // Protect from recursive inclusion. A page can include itself once
105         static $included_pages = array();
106         if (in_array($page, $included_pages)) {
107             return $this->error(sprintf(_("recursive inclusion of page %s"),
108                                         $page));
109         }
110
111         $p = $dbi->getPage($page);
112         if ($rev) {
113             $r = $p->getRevision($rev);
114             if (!$r) {
115                 return $this->error(sprintf(_("%s(%d): no such revision"),
116                                             $page, $rev));
117             }
118         } else {
119             $r = $p->getCurrentRevision();
120         }
121         $initial_content = $r->getPackedContent();
122         $c = explode("\n", $initial_content);
123
124         if ($section) {
125             $c = extractSection($section, $c, $page, $quiet, $sectionhead);
126             $initial_content = implode("\n", $c);
127         }
128
129         if (preg_match('/<noinclude>.+<\/noinclude>/s', $initial_content)) {
130             $initial_content = preg_replace("/<noinclude>.+?<\/noinclude>/s", "", 
131                                             $initial_content);
132         }
133         if (preg_match('/%%\w+%%/', $initial_content)) // need variable expansion
134         {
135             $var = array();
136             if (!empty($vars)) {
137                 foreach (split("&",$vars) as $pair) {
138                     list($key,$val) = split("=",$pair);
139                     $var[$key] = $val;
140                 }
141             }
142             $thispage = $dbi->getPage($basepage);
143             // pagename is not overridable
144             if (empty($var['pagename']))
145                 $var['pagename'] = $page;
146             // those are overridable
147             if (empty($var['mtime']) and preg_match('/%%mtime%%/', $initial_content)) {
148                 $thisrev  = $thispage->getCurrentRevision(false);
149                 $var['mtime'] = $GLOBALS['WikiTheme']->formatDateTime($thisrev->get('mtime'));
150             }
151             if (empty($var['ctime']) and preg_match('/%%ctime%%/', $initial_content)) {
152                 if ($first = $thispage->getRevision(1,false))
153                     $var['ctime'] = $GLOBALS['WikiTheme']->formatDateTime($first->get('mtime'));
154             }
155             if (empty($var['author']) and preg_match('/%%author%%/', $initial_content))
156                 $var['author'] = $thispage->getAuthor();
157             if (empty($var['owner']) and preg_match('/%%owner%%/', $initial_content))
158                 $var['owner'] = $thispage->getOwner();
159             if (empty($var['creator']) and preg_match('/%%creator%%/', $initial_content))
160                 $var['creator'] = $thispage->getCreator();
161             foreach (array("SERVER_URL", "DATA_PATH", "SCRIPT_NAME", "PHPWIKI_BASE_URL") as $c) {
162                 // constants are not overridable
163                 if (preg_match('/%%'.$c.'%%/', $initial_content))
164                     $var[$c] = constant($c);
165             }
166             if (preg_match('/%%BASE_URL%%/', $initial_content))
167                 $var['BASE_URL'] = PHPWIKI_BASE_URL;
168
169             foreach ($var as $key => $val) {
170                 $initial_content = preg_replace("/%%$key%%/", $val, $initial_content);
171             }
172         }
173
174         array_push($included_pages, $page);
175
176         include_once('lib/BlockParser.php');
177         $content = TransformText($initial_content, $r->get('markup'), $page);
178
179         array_pop($included_pages);
180
181         return HTML::div(array('class' => 'template'), $content);
182     }
183 };
184
185 // $Log: not supported by cvs2svn $
186 // Revision 1.2  2005/09/10 20:07:16  rurban
187 // fix BASE_URL
188 //
189 // Revision 1.1  2005/09/10 19:59:38  rurban
190 // Parametrized page inclusion ala mediawiki
191 //
192
193 // For emacs users
194 // Local Variables:
195 // mode: php
196 // tab-width: 8
197 // c-basic-offset: 4
198 // c-hanging-comment-ender-p: nil
199 // indent-tabs-mode: nil
200 // End:
201 ?>