]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CreatePage.php
support template variables (not yet working)
[SourceForge/phpwiki.git] / lib / plugin / CreatePage.php
1 <?php // -*-php-*-
2 rcs_id('$Id: CreatePage.php,v 1.6 2004-09-06 08:35:32 rurban Exp $');
3 /**
4  Copyright 2004 $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 geting 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 it <?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 class WikiPlugin_CreatePage
36 extends WikiPlugin
37 {
38     function getName() {
39         return _("CreatePage");
40     }
41
42     function getDescription() {
43         return _("Create a Wiki page by the provided name.");
44     }
45
46     function getVersion() {
47         return preg_replace("/[Revision: $]/", '',
48                             "\$Revision: 1.6 $");
49     }
50
51     function getDefaultArguments() {
52         return array('s'            => false,
53                      'initial_content' => '',
54                      'template'     => false,
55                      'vars'         => false,
56                      'overwrite'    => false,
57                      //'buttontext' => false,
58                      //'method'     => 'POST'
59                      );
60     }
61
62     function run($dbi, $argstr, &$request, $basepage) {
63         extract($this->getArgs($argstr, $request));
64         if (!$s)
65             return '';
66         // Prevent spaces at the start and end of a page name
67         $s = trim($s);
68
69         $param = array('action' => 'edit');
70         if ($template and $dbi->isWikiPage($template)) {
71             $param['template'] = $template;
72         } elseif (!empty($initial_content)) { 
73             // Warning! Potential URI overflow here on the GET redirect. Better use template.
74             $param['initial_content'] = $initial_content;
75         }
76         // If the initial_content is too large, pre-save the content in the page 
77         // and redirect without that argument.
78         // URI length limit:
79         //   http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.2.1
80         $url = WikiURL($s, $param, 'absurl');
81         if (strlen($url) > 255 
82             or preg_match('/%\w+%/',$initial_content)) // need variable expansion
83         {
84             unset($param['initial_content']);
85             $url = WikiURL($s, $param, 'absurl');
86             $page = $dbi->getPage($s);
87             $current = $page->getCurrentRevision();
88             $version = $current->getVersion();
89             if ($version and !$overwrite) {
90                 return $this->error(fmt("%s already exists", WikiLink($s)));
91             } else {
92                 $user = $request->getUser();
93                 $meta = array('markup' => 2.0,
94                               'author' => $user->getId());
95                 if ($param['template'] and !$initial_content) {
96                     $tmplpage = $dbi->getPage($template);
97                     $currenttmpl = $tmplpage->getCurrentRevision();
98                     $initial_content = $currenttmpl->getPackedContent();
99                     $meta['markup'] = $currenttmpl->_data['markup'];
100                 }
101                 $meta['summary'] = _("Created by CreatePage");
102                 // expand variables in $initial_content
103                 if (preg_match('/%\w+%/', $initial_content)) {
104                     $var = array();
105                     if (!empty($vars)) {
106                         foreach (split("&",$vars) as $pair) {
107                             list($key,$val) = split("=",$pair);
108                             $var[$key] = $val;
109                         }
110                     }
111                     if (empty($var['pagename']))
112                         $var['pagename'] = $s;
113                     if (empty($var['ctime']) and preg_match('/%ctime%/', $initial_content))
114                         $var['ctime'] = $GLOBALS['WikiTheme']->formatDateTime(time());
115                     if (empty($var['author']) and preg_match('/%author%/', $initial_content))
116                         $var['author'] = $user->getId();
117
118                     foreach ($var as $key => $val) {
119                         $initial_content = preg_replace("/%$key%/",$val,$initial_content);
120                     }
121                     // need to destroy the template so that editpage doesn't overwrite it.
122                     unset($param['template']);
123                     $url = WikiURL($s, $param, 'absurl');
124                 }
125                 $page->save($initial_content, $version+1, $meta);
126             }
127         }
128         return HTML($request->redirect($url, true));
129     }
130 };
131
132 // $Log: not supported by cvs2svn $
133 // Revision 1.5  2004/07/08 20:30:07  rurban
134 // plugin->run consistency: request as reference, added basepage.
135 // encountered strange bug in AllPages (and the test) which destroys ->_dbi
136 //
137 // Revision 1.4  2004/04/21 16:14:50  zorloc
138 // Prevent spaces at the start and end of a created page name -- submitted by Dan Frankowski (dfrankow).
139 //
140 // Revision 1.3  2004/03/24 19:41:04  rurban
141 // fixed the name
142 //
143 // Revision 1.2  2004/03/17 15:37:41  rurban
144 // properly support initial_content and template with URI length overflow workaround
145 //
146 // Revision 1.3  2004/03/16 16:25:05  dfrankow
147 // Support initial_content parameter
148 //
149 // Revision 1.2  2004/03/09 16:28:45  dfrankow
150 // Merge the RATING branch onto the main line
151 //
152 // Revision 1.1  2004/03/08 18:57:59  rurban
153 // Allow WikiForm overrides, such as method => POST, targetpage => [pagename]
154 // in the plugin definition.
155 // New simple CreatePage plugin by dfrankow.
156 //
157 // Revision 1.1.2.2  2004/02/23 21:22:29  dfrankow
158 // Add a little doc
159 //
160 // Revision 1.1.2.1  2004/02/21 15:29:19  dfrankow
161 // Allow a CreatePage edit box, as GUI syntactic sugar
162 //
163 // Revision 1.1.1.1  2004/01/29 14:30:28  dfrankow
164 // Right out of the 1.3.7 package
165 //
166
167 // Local Variables:
168 // mode: php
169 // tab-width: 8
170 // c-basic-offset: 4
171 // c-hanging-comment-ender-p: nil
172 // indent-tabs-mode: nil
173 // End:
174 ?>