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