4 Copyright 2004,2007 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
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.
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.
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
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
29 * Put <?plugin-form CreatePage ?> at some page, browse this page,
30 * enter the name of the page to create, then click the button.
32 * Usage: <?plugin-form CreatePage template=SomeTemplatePage vars="year=2004&name=None" ?>
33 * @authors: Dan Frankowski, Reini Urban
36 include_once("lib/plugin/Template.php");
38 class WikiPlugin_CreatePage
39 extends WikiPlugin_Template
42 return _("CreatePage");
45 function getDescription() {
46 return _("Create a Wiki page by the provided name.");
49 function getVersion() {
50 return preg_replace("/[Revision: $]/", '',
54 function getDefaultArguments() {
55 return array('s' => false,
56 'initial_content' => '',
60 //'buttontext' => false,
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
70 return $this->error(_("Cannot create page with empty name!"));
72 // TODO: javascript warning if "/" or SUBPAGE_SEPARATOR in s
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;
81 // If the initial_content is too large, pre-save the content in the page
82 // and redirect without that argument.
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
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)));
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'];
109 if (preg_match('/<noinclude>.+<\/noinclude>/s', $initial_content)) {
110 $initial_content = preg_replace("/<noinclude>.+?<\/noinclude>/s", "",
114 $meta['summary'] = _("Created by CreatePage");
115 $content = $this->doVariableExpansion($initial_content, $vars, $s, $request);
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');
123 $page->save($content, $version+1, $meta);
126 return HTML($request->redirect($url, true));
130 // $Log: not supported by cvs2svn $
131 // Revision 1.12 2007/08/10 22:03:34 rurban
132 // Cannot create page with empty name
134 // Revision 1.11 2007/01/27 21:36:52 rurban
135 // Overwrite empty or deleted pages
137 // Revision 1.10 2007/01/25 07:42:16 rurban
138 // Changed doVariableExpansion API.
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%%
143 // Revision 1.8 2007/01/03 21:23:32 rurban
144 // Derive from Template. Use same variable expansion. Support <noinclude> as in Template.
146 // Revision 1.7 2004/09/06 10:22:15 rurban
147 // oops, forgot global request
149 // Revision 1.6 2004/09/06 08:35:32 rurban
150 // support template variables (not yet working)
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
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).
159 // Revision 1.3 2004/03/24 19:41:04 rurban
162 // Revision 1.2 2004/03/17 15:37:41 rurban
163 // properly support initial_content and template with URI length overflow workaround
165 // Revision 1.3 2004/03/16 16:25:05 dfrankow
166 // Support initial_content parameter
168 // Revision 1.2 2004/03/09 16:28:45 dfrankow
169 // Merge the RATING branch onto the main line
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.
176 // Revision 1.1.2.2 2004/02/23 21:22:29 dfrankow
179 // Revision 1.1.2.1 2004/02/21 15:29:19 dfrankow
180 // Allow a CreatePage edit box, as GUI syntactic sugar
182 // Revision 1.1.1.1 2004/01/29 14:30:28 dfrankow
183 // Right out of the 1.3.7 package
190 // c-hanging-comment-ender-p: nil
191 // indent-tabs-mode: nil