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