]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CreatePage.php
Changed doVariableExpansion API.
[SourceForge/phpwiki.git] / lib / plugin / CreatePage.php
1 <?php // -*-php-*-
2 rcs_id('$Id: CreatePage.php,v 1.10 2007-01-25 07:42:16 rurban Exp $');
3 /**
4  Copyright 2004,2007 $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  * This allows you to create a page getting the new pagename from a 
25  * forms-based interface, and optionally with the initial content from 
26  * some template, plus expansion of some variables via %%variable%% statements 
27  * in the template.
28  *
29  * Put <?plugin-form CreatePage ?> at some page, browse this page, 
30  * enter the name of the page to create, then click the button.
31  *
32  * Usage: <?plugin-form CreatePage template=SomeTemplatePage vars="year=2004&name=None" ?>
33  * @authors: Dan Frankowski, Reini Urban
34  */
35
36 include_once("lib/plugin/Template.php");
37
38 class WikiPlugin_CreatePage
39 extends WikiPlugin_Template
40 {
41     function getName() {
42         return _("CreatePage");
43     }
44
45     function getDescription() {
46         return _("Create a Wiki page by the provided name.");
47     }
48
49     function getVersion() {
50         return preg_replace("/[Revision: $]/", '',
51                             "\$Revision: 1.10 $");
52     }
53
54     function getDefaultArguments() {
55         return array('s'            => false,
56                      'initial_content' => '',
57                      'template'     => false,
58                      'vars'         => false,
59                      'overwrite'    => false,
60                      //'buttontext' => false,
61                      //'method'     => 'POST'
62                      );
63     }
64
65     function run($dbi, $argstr, &$request, $basepage) {
66         extract($this->getArgs($argstr, $request));
67         if (!$s)
68             return '';
69         // Prevent spaces at the start and end of a page name
70         $s = trim($s);
71
72         $param = array('action' => 'edit');
73         if ($template and $dbi->isWikiPage($template)) {
74             $param['template'] = $template;
75         } elseif (!empty($initial_content)) { 
76             // Warning! Potential URI overflow here on the GET redirect. Better use template.
77             $param['initial_content'] = $initial_content;
78         }
79         // If the initial_content is too large, pre-save the content in the page 
80         // and redirect without that argument.
81         // URI length limit:
82         //   http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.2.1
83         $url = WikiURL($s, $param, 'absurl');
84         // FIXME: expand vars in templates here.
85         if (strlen($url) > 255 
86             or ($param['template'])
87             or preg_match('/%%\w+%%/', $initial_content)) // need variable expansion
88         {
89             unset($param['initial_content']);
90             $url = WikiURL($s, $param, 'absurl');
91             $page = $dbi->getPage($s);
92             $current = $page->getCurrentRevision();
93             $version = $current->getVersion();
94             if ($version and !$overwrite) {
95                 return $this->error(fmt("%s already exists", WikiLink($s)));
96             } else {
97                 $user = $request->getUser();
98                 $meta = array('markup' => 2.0,
99                               'author' => $user->getId());
100                 if (!empty($param['template']) and !$initial_content) {
101                     $tmplpage = $dbi->getPage($template);
102                     $currenttmpl = $tmplpage->getCurrentRevision();
103                     $initial_content = $currenttmpl->getPackedContent();
104                     $meta['markup'] = $currenttmpl->_data['markup'];
105
106                     if (preg_match('/<noinclude>.+<\/noinclude>/s', $initial_content)) {
107                         $initial_content = preg_replace("/<noinclude>.+?<\/noinclude>/s", "", 
108                                                         $initial_content);
109                     }
110                 }
111                 $meta['summary'] = _("Created by CreatePage");
112                 $content = $this->doVariableExpansion($initial_content, $vars, $s, $request);
113
114                 if ($content !== $initial_content) {
115                     // need to destroy the template so that editpage doesn't overwrite it.
116                     unset($param['template']);
117                     $url = WikiURL($s, $param, 'absurl');
118                 }
119
120                 $page->save($content, $version+1, $meta);
121             }
122         }
123         return HTML($request->redirect($url, true));
124     }
125 };
126
127 // $Log: not supported by cvs2svn $
128 // Revision 1.9  2007/01/04 16:42:23  rurban
129 // Expand even if no vars are given. They may be defaults, i.e %%pagename%%
130 //
131 // Revision 1.8  2007/01/03 21:23:32  rurban
132 // Derive from Template. Use same variable expansion. Support <noinclude> as in Template.
133 //
134 // Revision 1.7  2004/09/06 10:22:15  rurban
135 // oops, forgot global request
136 //
137 // Revision 1.6  2004/09/06 08:35:32  rurban
138 // support template variables (not yet working)
139 //
140 // Revision 1.5  2004/07/08 20:30:07  rurban
141 // plugin->run consistency: request as reference, added basepage.
142 // encountered strange bug in AllPages (and the test) which destroys ->_dbi
143 //
144 // Revision 1.4  2004/04/21 16:14:50  zorloc
145 // Prevent spaces at the start and end of a created page name -- submitted by Dan Frankowski (dfrankow).
146 //
147 // Revision 1.3  2004/03/24 19:41:04  rurban
148 // fixed the name
149 //
150 // Revision 1.2  2004/03/17 15:37:41  rurban
151 // properly support initial_content and template with URI length overflow workaround
152 //
153 // Revision 1.3  2004/03/16 16:25:05  dfrankow
154 // Support initial_content parameter
155 //
156 // Revision 1.2  2004/03/09 16:28:45  dfrankow
157 // Merge the RATING branch onto the main line
158 //
159 // Revision 1.1  2004/03/08 18:57:59  rurban
160 // Allow WikiForm overrides, such as method => POST, targetpage => [pagename]
161 // in the plugin definition.
162 // New simple CreatePage plugin by dfrankow.
163 //
164 // Revision 1.1.2.2  2004/02/23 21:22:29  dfrankow
165 // Add a little doc
166 //
167 // Revision 1.1.2.1  2004/02/21 15:29:19  dfrankow
168 // Allow a CreatePage edit box, as GUI syntactic sugar
169 //
170 // Revision 1.1.1.1  2004/01/29 14:30:28  dfrankow
171 // Right out of the 1.3.7 package
172 //
173
174 // Local Variables:
175 // mode: php
176 // tab-width: 8
177 // c-basic-offset: 4
178 // c-hanging-comment-ender-p: nil
179 // indent-tabs-mode: nil
180 // End:
181 ?>