]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CreatePage.php
Replace tabs by spaces; remove EOL spaces
[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                      'verify'       => false, // true or a pagename
61                      //'buttontext' => false,
62                      //'method'     => 'POST'
63                      );
64     }
65
66     function run($dbi, $argstr, &$request, $basepage) {
67         extract($this->getArgs($argstr, $request));
68         // Prevent spaces at the start and end of a page name
69         $s = trim($s);
70         if (!$s) {
71             return $this->error(_("Cannot create page with empty name!"));
72         }
73         // TODO: javascript warning if "/" or SUBPAGE_SEPARATOR in s
74         if ($verify) {
75             $head = _("CreatePage failed");
76             if ($dbi->isWikiPage($verify)) {
77                 $msg = _("Do you really want to create the page '%s'?");
78             } else {
79                 $msg = _("Do you really want to create the page '%s'?");
80             }
81             if (isSubPage($s)) {
82                 $main = subPageSlice(0);
83                 if (!$dbi->isWikiPage(subPageSlice(0))) {
84                     $msg .= "\n" . _("The new page you want to create will be a subpage.")
85                          .  "\n" . _("Subpages cannot be created unless the parent page exists.");
86                     return alert($head, $msg);
87                 } else {
88                     $msg .= "\n" . _("The new page you want to create will be a subpage.");
89                 }
90             }
91             if (strpos($s, " \/")) {
92                 $msg .= "\n" . _("Subpages with ending space are not allowed as directory name on Windows.");
93                 return alert($head, $msg);
94             }
95         }
96
97         $param = array('action' => 'edit');
98         if ($template and $dbi->isWikiPage($template)) {
99             $param['template'] = $template;
100         } elseif (!empty($initial_content)) {
101             // Warning! Potential URI overflow here on the GET redirect. Better use template.
102             $param['initial_content'] = $initial_content;
103         }
104         // If the initial_content is too large, pre-save the content in the page
105         // and redirect without that argument.
106         // URI length limit:
107         //   http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.2.1
108         $url = WikiURL($s, $param, 'absurl');
109         // FIXME: expand vars in templates here.
110         if (strlen($url) > 255
111             or ($param['template'])
112             or preg_match('/%%\w+%%/', $initial_content)) // need variable expansion
113         {
114             unset($param['initial_content']);
115             $url = WikiURL($s, $param, 'absurl');
116             $page = $dbi->getPage($s);
117             $current = $page->getCurrentRevision();
118             $version = $current->getVersion();
119             // overwrite empty (deleted) pages
120             if ($version and !$current->hasDefaultContents() and !$overwrite) {
121                 return $this->error(fmt("%s already exists", WikiLink($s)));
122             } else {
123                 $user = $request->getUser();
124                 $meta = array('markup' => 2.0,
125                               'author' => $user->getId());
126                 if (!empty($param['template']) and !$initial_content) {
127                     $tmplpage = $dbi->getPage($template);
128                     $currenttmpl = $tmplpage->getCurrentRevision();
129                     $initial_content = $currenttmpl->getPackedContent();
130                     $meta['markup'] = $currenttmpl->_data['markup'];
131
132                     if (preg_match('/<noinclude>.+<\/noinclude>/s', $initial_content)) {
133                         $initial_content = preg_replace("/<noinclude>.+?<\/noinclude>/s", "",
134                                                         $initial_content);
135                     }
136                 }
137                 $meta['summary'] = _("Created by CreatePage");
138                 $content = $this->doVariableExpansion($initial_content, $vars, $s, $request);
139
140                 if ($content !== $initial_content) {
141                     // need to destroy the template so that editpage doesn't overwrite it.
142                     unset($param['template']);
143                     $url = WikiURL($s, $param, 'absurl');
144                 }
145
146                 $page->save($content, $version+1, $meta);
147             }
148         }
149         return HTML($request->redirect($url, true));
150     }
151 };
152
153 // Local Variables:
154 // mode: php
155 // tab-width: 8
156 // c-basic-offset: 4
157 // c-hanging-comment-ender-p: nil
158 // indent-tabs-mode: nil
159 // End:
160 ?>