]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRename.php
reenable admin check on !ENABLE_PAGEPERM, honor s=Wildcard arg, fix warning after...
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRename.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminRename.php,v 1.14 2004-06-03 22:24:48 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  * Usage:   <?plugin WikiAdminRename ?> or called via WikiAdminSelect
25  * @author:  Reini Urban <rurban@x-ray.at>
26  *
27  * TODO: support case-insensitive checkbox
28  *       support regex checkbox and renaming
29  *
30  * KNOWN ISSUES:
31  * Enabled now PagePermissions.
32  * Requires PHP 4.2 so far.
33  */
34 require_once('lib/PageList.php');
35 require_once('lib/plugin/WikiAdminSelect.php');
36
37 class WikiPlugin_WikiAdminRename
38 extends WikiPlugin_WikiAdminSelect
39 {
40     function getName() {
41         return _("WikiAdminRename");
42     }
43
44     function getDescription() {
45         return _("Rename selected pages.");
46     }
47
48     function getVersion() {
49         return preg_replace("/[Revision: $]/", '',
50                             "\$Revision: 1.14 $");
51     }
52
53     function getDefaultArguments() {
54         return array(
55                      's'        => false,
56                      /* Pages to exclude in listing */
57                      'exclude'  => '',
58                      /* Columns to include in listing */
59                      'info'     => 'pagename,mtime',
60                      /* How to sort */
61                      'sortby'   => 'pagename',
62                      'limit'    => 0,
63                      'updatelinks' => 0
64                      );
65     }
66
67     //TODO: regex option
68     function renameHelper($name, $from, $to) {
69         return str_replace($from,$to,$name);
70     }
71
72     function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks=false) {
73         $ul = HTML::ul();
74         $count = 0;
75         foreach ($pages as $name) {
76             if ( ($newname = $this->renameHelper($name,$from,$to)) and 
77                  $newname != $name ) {
78                 if ($dbi->isWikiPage($newname))
79                     $ul->pushContent(HTML::li(fmt("Page %s already exists. Ignored.",WikiLink($newname))));
80                 elseif (!mayAccessPage('change',$name))
81                     $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",WikiLink($name))));
82                 elseif ( $dbi->renamePage($name,$newname,$updatelinks)) {
83                     /* not yet implemented for all backends */
84                     $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",$name,WikiLink($newname))));
85                     $count++;
86                 } else {
87                     $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", $name, $newname)));
88                 }
89             } else {
90                 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", $name, $newname)));
91             }
92         }
93         if ($count) {
94             $dbi->touch();
95             return HTML($ul, HTML::p(fmt("%s pages have been permanently renamed.",$count)));
96         } else {
97             return HTML($ul, HTML::p(fmt("No pages renamed.")));
98         }
99     }
100     
101     function run($dbi, $argstr, &$request, $basepage) {
102         if ($request->getArg('action') != 'browse')
103             return $this->disabled("(action != 'browse')");
104         
105         $args = $this->getArgs($argstr, $request);
106         $this->_args = $args;
107         if (!empty($args['exclude']))
108             $exclude = explodePageList($args['exclude']);
109         else
110             $exclude = false;
111         $this->preSelectS(&$args, &$request);
112
113         $p = $request->getArg('p');
114         if (!$p) $p = $this->_list;
115         $post_args = $request->getArg('admin_rename');
116         $next_action = 'select';
117         $pages = array();
118         if ($p && !$request->isPost())
119             $pages = $p;
120         if ($p && $request->isPost() &&
121             !empty($post_args['rename']) && empty($post_args['cancel'])) {
122             // DONE: check individual PagePermissions
123             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
124                 $request->_notAuthorized(WIKIAUTH_ADMIN);
125                 $this->disabled("! user->isAdmin");
126             }
127             // DONE: error message if not allowed.
128             if ($post_args['action'] == 'verify') {
129                 // Real action
130                 return $this->renamePages($dbi, $request, array_keys($p), 
131                                           $post_args['from'], $post_args['to'], !empty($post_args['updatelinks']));
132             }
133             if ($post_args['action'] == 'select') {
134                 if (!empty($post_args['from']))
135                     $next_action = 'verify';
136                 foreach ($p as $name => $c) {
137                     $pages[$name] = 1;
138                 }
139             }
140         }
141         if ($next_action == 'select' and empty($pages)) {
142             // List all pages to select from.
143             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit']);
144         }
145         if ($next_action == 'verify') {
146             $args['info'] = "checkbox,pagename,renamed_pagename";
147         }
148         $pagelist = new PageList_Selectable
149             (
150              $args['info'], $exclude, 
151              array('types' => 
152                    array('renamed_pagename'
153                          => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
154                          )));
155         $pagelist->addPageList($pages);
156
157         $header = HTML::p();
158         if ($next_action == 'verify') {
159             $button_label = _("Yes");
160             $header->pushContent(
161               HTML::p(HTML::strong(
162                                    _("Are you sure you want to permanently rename the selected files?"))));
163             $header = $this->renameForm($header, $post_args);
164         }
165         else {
166             $button_label = _("Rename selected pages");
167             $header->pushContent(HTML::p(_("Select the pages to rename:")));
168             $header = $this->renameForm($header, $post_args);
169         }
170
171
172         $buttons = HTML::p(Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
173                            Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
174
175         return HTML::form(array('action' => $request->getPostURL(),
176                                 'method' => 'post'),
177                           $header,
178                           $pagelist->getContent(),
179                           HiddenInputs($request->getArgs(),
180                                         false,
181                                         array('admin_rename')),
182                           HiddenInputs(array('admin_rename[action]' => $next_action,
183                                              /*'require_authority_for_post' => WIKIAUTH_ADMIN */)),
184                           $buttons);
185     }
186
187     function renameForm(&$header, $post_args) {
188         $header->pushContent(_("Rename")." "._("from").': ');
189         $header->pushContent(HTML::input(array('name' => 'admin_rename[from]',
190                                                'value' => $post_args['from'])));
191         $header->pushContent(' '._("to").': ');
192         $header->pushContent(HTML::input(array('name' => 'admin_rename[to]',
193                                                'value' => $post_args['to'])));
194         $header->pushContent(' '._("(no regex, case-sensitive)"));
195         if (DEBUG) { // not yet tested
196             $header->pushContent(HTML::br());
197             $header->pushContent(_("Change pagename in all linked pages also?"));
198             $header->pushContent(HTML::em(_("(Currently not working)")));
199             $checkbox = HTML::input(array('type' => 'checkbox',
200                                           'name' => 'admin_rename[updatelinks]',
201                                           'value' => 1));
202             if (!empty($post_args['updatelinks']))
203                 $checkbox->setAttr('checked','checked');
204             $header->pushContent($checkbox);
205         }
206         $header->pushContent(HTML::p());
207         return $header;
208     }
209 }
210
211 // moved from lib/PageList.php
212 class _PageList_Column_renamed_pagename extends _PageList_Column {
213     function _getValue ($page_handle, &$revision_handle) {
214         $post_args = $GLOBALS['request']->getArg('admin_rename');
215         $value = str_replace($post_args['from'], $post_args['to'],$page_handle->getName());
216         $div = HTML::div(" => ",HTML::input(array('type' => 'text',
217                                                   'name' => 'rename[]',
218                                                   'value' => $value)));
219         $new_page = $GLOBALS['request']->getPage($value);
220         if ($new_page->exists()) {
221             $div->setAttr('class','error');
222             $div->setAttr('title',_("This page already exists"));
223         }
224         return $div;
225     }
226 };
227
228 // $Log: not supported by cvs2svn $
229 // Revision 1.13  2004/06/03 12:59:41  rurban
230 // simplify translation
231 // NS4 wrap=virtual only
232 //
233 // Revision 1.12  2004/06/01 15:28:01  rurban
234 // AdminUser only ADMIN_USER not member of Administrators
235 // some RateIt improvements by dfrankow
236 // edit_toolbar buttons
237 //
238 // Revision 1.11  2004/05/24 17:34:53  rurban
239 // use ACLs
240 //
241 // Revision 1.10  2004/04/06 20:00:11  rurban
242 // Cleanup of special PageList column types
243 // Added support of plugin and theme specific Pagelist Types
244 // Added support for theme specific UserPreferences
245 // Added session support for ip-based throttling
246 //   sql table schema change: ALTER TABLE session ADD sess_ip CHAR(15);
247 // Enhanced postgres schema
248 // Added DB_Session_dba support
249 //
250 // Revision 1.9  2004/03/12 13:31:43  rurban
251 // enforce PagePermissions, errormsg if not Admin
252 //
253 // Revision 1.8  2004/03/01 13:48:46  rurban
254 // rename fix
255 // p[] consistency fix
256 //
257 // Revision 1.7  2004/02/22 23:20:33  rurban
258 // fixed DumpHtmlToDir,
259 // enhanced sortby handling in PageList
260 //   new button_heading th style (enabled),
261 // added sortby and limit support to the db backends and plugins
262 //   for paging support (<<prev, next>> links on long lists)
263 //
264 // Revision 1.6  2004/02/17 12:11:36  rurban
265 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
266 //
267 // Revision 1.5  2004/02/15 21:34:37  rurban
268 // PageList enhanced and improved.
269 // fixed new WikiAdmin... plugins
270 // editpage, Theme with exp. htmlarea framework
271 //   (htmlarea yet committed, this is really questionable)
272 // WikiUser... code with better session handling for prefs
273 // enhanced UserPreferences (again)
274 // RecentChanges for show_deleted: how should pages be deleted then?
275 //
276 // Revision 1.4  2004/02/12 17:05:39  rurban
277 // WikiAdminRename:
278 //   added "Change pagename in all linked pages also"
279 // PageList:
280 //   added javascript toggle for Select
281 // WikiAdminSearchReplace:
282 //   fixed another typo
283 //
284 // Revision 1.3  2004/02/12 13:05:50  rurban
285 // Rename functional for PearDB backend
286 // some other minor changes
287 // SiteMap comes with a not yet functional feature request: includepages (tbd)
288 //
289 // Revision 1.2  2004/02/12 11:45:11  rurban
290 // only WikiDB method missing
291 //
292 // Revision 1.1  2004/02/11 20:00:16  rurban
293 // WikiAdmin... series overhaul. Rename misses the db backend methods yet. Chmod + Chwon still missing.
294 //
295
296 // Local Variables:
297 // mode: php
298 // tab-width: 8
299 // c-basic-offset: 4
300 // c-hanging-comment-ender-p: nil
301 // indent-tabs-mode: nil
302 // End:
303 ?>