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 along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
41 function getDescription()
43 return _("Create a wiki page by the provided name.");
46 function getDefaultArguments()
48 return array('s' => false,
49 'initial_content' => '',
53 'verify' => false, // true or a pagename
54 //'buttontext' => false,
59 function run($dbi, $argstr, &$request, $basepage)
61 extract($this->getArgs($argstr, $request));
62 // Prevent spaces at the start and end of a page name
65 return $this->error(_("Cannot create page with empty name!"));
67 // TODO: javascript warning if "/" or SUBPAGE_SEPARATOR in s
69 $head = _("CreatePage failed");
70 if ($dbi->isWikiPage($verify)) {
71 $msg = _("Do you really want to create the page ā%sā?");
73 $msg = _("Do you really want to create the page ā%sā?");
76 if (!$dbi->isWikiPage(subPageSlice(0))) {
77 $msg .= "\n" . _("The new page you want to create will be a subpage.")
78 . "\n" . _("Subpages cannot be created unless the parent page exists.");
79 return alert($head, $msg);
81 $msg .= "\n" . _("The new page you want to create will be a subpage.");
84 if (strpos($s, " \/")) {
85 $msg .= "\n" . _("Subpages with ending space are not allowed as directory name on Windows.");
86 return alert($head, $msg);
90 $param = array('action' => 'edit');
91 if ($template and $dbi->isWikiPage($template)) {
92 $param['template'] = $template;
93 } elseif (!empty($initial_content)) {
94 // Warning! Potential URI overflow here on the GET redirect. Better use template.
95 $param['initial_content'] = $initial_content;
97 // If the initial_content is too large, pre-save the content in the page
98 // and redirect without that argument.
100 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.2.1
101 $url = WikiURL($s, $param, 'absurl');
102 // FIXME: expand vars in templates here.
103 if (strlen($url) > 255
104 or ($param['template'])
105 or preg_match('/%%\w+%%/', $initial_content)
106 ) // need variable expansion
108 unset($param['initial_content']);
109 $url = WikiURL($s, $param, 'absurl');
110 $page = $dbi->getPage($s);
111 $current = $page->getCurrentRevision();
112 $version = $current->getVersion();
113 // overwrite empty (deleted) pages
114 if ($version and !$current->hasDefaultContents() and !$overwrite) {
115 return $this->error(fmt("%s already exists", WikiLink($s)));
117 $user = $request->getUser();
118 $meta = array('author' => $user->getId());
119 if (!empty($param['template']) and !$initial_content) {
120 $tmplpage = $dbi->getPage($template);
121 $currenttmpl = $tmplpage->getCurrentRevision();
122 $initial_content = $currenttmpl->getPackedContent();
124 if (preg_match('/<noinclude>.+<\/noinclude>/s', $initial_content)) {
125 $initial_content = preg_replace("/<noinclude>.+?<\/noinclude>/s", "",
129 $meta['summary'] = _("Created by CreatePage");
130 $content = $this->doVariableExpansion($initial_content, $vars, $s, $request);
132 if ($content !== $initial_content) {
133 // need to destroy the template so that editpage doesn't overwrite it.
134 unset($param['template']);
135 $url = WikiURL($s, $param, 'absurl');
138 $page->save($content, $version + 1, $meta);
141 return HTML($request->redirect($url, true));
149 // c-hanging-comment-ender-p: nil
150 // indent-tabs-mode: nil