]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CreatePage.php
Remove unused variables
[SourceForge/phpwiki.git] / lib / plugin / CreatePage.php
1 <?php
2
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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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     {
43         return _("CreatePage");
44     }
45
46     function getDescription()
47     {
48         return _("Create a wiki page by the provided name.");
49     }
50
51     function getDefaultArguments()
52     {
53         return array('s' => false,
54             'initial_content' => '',
55             'template' => false,
56             'vars' => false,
57             'overwrite' => false,
58             'verify' => false, // true or a pagename
59             //'buttontext' => false,
60             //'method'     => 'POST'
61         );
62     }
63
64     function run($dbi, $argstr, &$request, $basepage)
65     {
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         if ($verify) {
74             $head = _("CreatePage failed");
75             if ($dbi->isWikiPage($verify)) {
76                 $msg = _("Do you really want to create the page ā€œ%sā€?");
77             } else {
78                 $msg = _("Do you really want to create the page ā€œ%sā€?");
79             }
80             if (isSubPage($s)) {
81                 if (!$dbi->isWikiPage(subPageSlice(0))) {
82                     $msg .= "\n" . _("The new page you want to create will be a subpage.")
83                         . "\n" . _("Subpages cannot be created unless the parent page exists.");
84                     return alert($head, $msg);
85                 } else {
86                     $msg .= "\n" . _("The new page you want to create will be a subpage.");
87                 }
88             }
89             if (strpos($s, " \/")) {
90                 $msg .= "\n" . _("Subpages with ending space are not allowed as directory name on Windows.");
91                 return alert($head, $msg);
92             }
93         }
94
95         $param = array('action' => 'edit');
96         if ($template and $dbi->isWikiPage($template)) {
97             $param['template'] = $template;
98         } elseif (!empty($initial_content)) {
99             // Warning! Potential URI overflow here on the GET redirect. Better use template.
100             $param['initial_content'] = $initial_content;
101         }
102         // If the initial_content is too large, pre-save the content in the page
103         // and redirect without that argument.
104         // URI length limit:
105         //   http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.2.1
106         $url = WikiURL($s, $param, 'absurl');
107         // FIXME: expand vars in templates here.
108         if (strlen($url) > 255
109             or ($param['template'])
110             or preg_match('/%%\w+%%/', $initial_content)
111         ) // need variable expansion
112         {
113             unset($param['initial_content']);
114             $url = WikiURL($s, $param, 'absurl');
115             $page = $dbi->getPage($s);
116             $current = $page->getCurrentRevision();
117             $version = $current->getVersion();
118             // overwrite empty (deleted) pages
119             if ($version and !$current->hasDefaultContents() and !$overwrite) {
120                 return $this->error(fmt("%s already exists", WikiLink($s)));
121             } else {
122                 $user = $request->getUser();
123                 $meta = array('markup' => 2.0,
124                     'author' => $user->getId());
125                 if (!empty($param['template']) and !$initial_content) {
126                     $tmplpage = $dbi->getPage($template);
127                     $currenttmpl = $tmplpage->getCurrentRevision();
128                     $initial_content = $currenttmpl->getPackedContent();
129                     $meta['markup'] = $currenttmpl->_data['markup'];
130
131                     if (preg_match('/<noinclude>.+<\/noinclude>/s', $initial_content)) {
132                         $initial_content = preg_replace("/<noinclude>.+?<\/noinclude>/s", "",
133                             $initial_content);
134                     }
135                 }
136                 $meta['summary'] = _("Created by CreatePage");
137                 $content = $this->doVariableExpansion($initial_content, $vars, $s, $request);
138
139                 if ($content !== $initial_content) {
140                     // need to destroy the template so that editpage doesn't overwrite it.
141                     unset($param['template']);
142                     $url = WikiURL($s, $param, 'absurl');
143                 }
144
145                 $page->save($content, $version + 1, $meta);
146             }
147         }
148         return HTML($request->redirect($url, true));
149     }
150 }
151
152 // Local Variables:
153 // mode: php
154 // tab-width: 8
155 // c-basic-offset: 4
156 // c-hanging-comment-ender-p: nil
157 // indent-tabs-mode: nil
158 // End: