]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRename.php
do not trim spaces
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRename.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminRename.php,v 1.26 2005-04-01 16:06:41 rurban Exp $');
3 /*
4  Copyright 2004,2005 $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  * KNOWN ISSUES:
28  *   Requires PHP 4.2.
29  */
30 require_once('lib/PageList.php');
31 require_once('lib/plugin/WikiAdminSelect.php');
32
33 class WikiPlugin_WikiAdminRename
34 extends WikiPlugin_WikiAdminSelect
35 {
36     function getName() {
37         return _("WikiAdminRename");
38     }
39
40     function getDescription() {
41         return _("Rename selected pages");
42     }
43
44     function getVersion() {
45         return preg_replace("/[Revision: $]/", '',
46                             "\$Revision: 1.26 $");
47     }
48
49     function getDefaultArguments() {
50         return array_merge
51             (
52              PageList::supportedArgs(),
53              array(
54                      's'        => false,
55                      /* Columns to include in listing */
56                      'info'     => 'pagename,mtime',
57                      'updatelinks' => 0
58                      ));
59     }
60
61     function renameHelper($name, $from, $to, $options = false) {
62         if ($options['regex'])
63             return preg_replace('/'.$from.'/'.($options['icase']?'i':''), $to, $name);
64         elseif ($options['icase'])
65             return str_ireplace($from, $to, $name);
66         else
67             return str_replace($from, $to, $name);
68     }
69
70     function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks=false) {
71         $ul = HTML::ul();
72         $count = 0;
73         $post_args = $request->getArg('admin_rename');
74         $options = array('regex' => @$post_args['regex'],
75                          'icase' => @$post_args['icase']);
76         foreach ($pages as $name) {
77             if ( ($newname = $this->renameHelper($name, $from, $to, $options)) 
78                  and $newname != $name )
79             {
80                 if ($dbi->isWikiPage($newname))
81                     $ul->pushContent(HTML::li(fmt("Page %s already exists. Ignored.",
82                                                   WikiLink($newname))));
83                 elseif (! mayAccessPage('change', $name))
84                     $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",
85                                                   WikiLink($name))));
86                 elseif ( $dbi->renamePage($name, $newname, $updatelinks)) {
87                     /* not yet implemented for all backends */
88                     $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",
89                                                   $name, WikiLink($newname))));
90                     $count++;
91                 } else {
92                     $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", 
93                                                   $name, $newname)));
94                 }
95             } else {
96                 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", 
97                                               $name, $newname)));
98             }
99         }
100         if ($count) {
101             $dbi->touch();
102             return HTML($ul, HTML::p(fmt("%s pages have been permanently renamed.",
103                                          $count)));
104         } else {
105             return HTML($ul, HTML::p(fmt("No pages renamed.")));
106         }
107     }
108     
109     function run($dbi, $argstr, &$request, $basepage) {
110         if ($request->getArg('action') != 'browse')
111             if ($request->getArg('action') != _("PhpWikiAdministration/Rename"))
112                 return $this->disabled("(action != 'browse')");
113         
114         $args = $this->getArgs($argstr, $request);
115         $this->_args = $args;
116         $this->preSelectS($args, $request);
117
118         $p = $request->getArg('p');
119         if (!$p) $p = $this->_list;
120         $post_args = $request->getArg('admin_rename');
121         $next_action = 'select';
122         $pages = array();
123         if ($p && !$request->isPost())
124             $pages = $p;
125         if ($p && $request->isPost() &&
126             !empty($post_args['rename']) && empty($post_args['cancel'])) {
127             // without individual PagePermissions:
128             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
129                 $request->_notAuthorized(WIKIAUTH_ADMIN);
130                 $this->disabled("! user->isAdmin");
131             }
132             // DONE: error message if not allowed.
133             if ($post_args['action'] == 'verify') {
134                 // Real action
135                 return $this->renamePages($dbi, $request, array_keys($p), 
136                                           $post_args['from'], $post_args['to'], 
137                                           !empty($post_args['updatelinks']));
138             }
139             if ($post_args['action'] == 'select') {
140                 if (!empty($post_args['from']))
141                     $next_action = 'verify';
142                 foreach ($p as $name => $c) {
143                     $pages[$name] = 1;
144                 }
145             }
146         }
147         if ($next_action == 'select' and empty($pages)) {
148             // List all pages to select from.
149             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
150         }
151         if ($next_action == 'verify') {
152             $args['info'] = "checkbox,pagename,renamed_pagename";
153         }
154         $pagelist = new PageList_Selectable
155             (
156              $args['info'], $args['exclude'],
157              array('types' => 
158                    array('renamed_pagename'
159                          => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
160                          )));
161         $pagelist->addPageList($pages);
162
163         $header = HTML::p();
164         if ($next_action == 'verify') {
165             $button_label = _("Yes");
166             $header->pushContent(
167               HTML::p(HTML::strong(
168                 _("Are you sure you want to permanently rename the selected files?"))));
169             $header = $this->renameForm($header, $post_args);
170         }
171         else {
172             $button_label = _("Rename selected pages");
173             $header->pushContent(HTML::p(_("Select the pages to rename:")));
174             if (!$post_args and count($pages) == 1) {
175                 list($post_args['from'],) = array_keys($pages);
176                 $post_args['to'] = $post_args['from'];
177             }
178             $header = $this->renameForm($header, $post_args);
179         }
180
181         $buttons = HTML::p(Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
182                            Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
183
184         return HTML::form(array('action' => $request->getPostURL(),
185                                 'method' => 'post'),
186                           $header,
187                           $pagelist->getContent(),
188                           HiddenInputs($request->getArgs(),
189                                         false,
190                                         array('admin_rename')),
191                           HiddenInputs(array('admin_rename[action]' => $next_action)),
192                           ENABLE_PAGEPERM
193                           ? ''
194                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
195                           $buttons);
196     }
197
198     function checkBox (&$post_args, $name, $msg) {
199         $checkbox = HTML::input(array('type' => 'checkbox',
200                                       'name' => 'admin_rename['.$name.']',
201                                       'value' => 1));
202         if (!empty($post_args[$name]))
203             $checkbox->setAttr('checked', 'checked');
204         return HTML::div($checkbox, ' ', HTML::span($msg));
205     }
206
207     function renameForm(&$header, $post_args) {
208         $header->pushContent(_("Rename")." "._("from").': ');
209         $header->pushContent(HTML::input(array('name' => 'admin_rename[from]',
210                                                'value' => $post_args['from'])));
211         $header->pushContent(' '._("to").': ');
212         $header->pushContent(HTML::input(array('name' => 'admin_rename[to]',
213                                                'value' => $post_args['to'])));
214         $header->pushContent($this->checkBox($post_args, 'regex', _("Regex?")));
215         $header->pushContent($this->checkBox($post_args, 'icase', _("Case insensitive?")));
216         $header->pushContent(HTML::br());
217         $header->pushContent($this->checkBox($post_args, 'updatelinks', 
218                                              _("Change pagename in all linked pages also?")));
219         $header->pushContent(HTML::p());
220         return $header;
221     }
222 }
223
224 // TODO: grey out unchangeble pages, even in the initial list also?
225 // TODO: autoselect by matching name javascript in admin_rename[from]
226 // TODO: update rename[] fields when case-sensitive and regex is changed
227
228 // moved from lib/PageList.php
229 class _PageList_Column_renamed_pagename extends _PageList_Column {
230     function _getValue ($page_handle, &$revision_handle) {
231         global $request;
232         $post_args = $request->getArg('admin_rename');
233         $options = array('regex' => @$post_args['regex'],
234                          'icase' => @$post_args['icase']);
235         $value = WikiPlugin_WikiAdminRename::renameHelper($page_handle->getName(), 
236                                                           $post_args['from'], $post_args['to'],
237                                                           $options);
238         $div = HTML::div(" => ",HTML::input(array('type' => 'text',
239                                                   'name' => 'rename[]',
240                                                   'value' => $value)));
241         $new_page = $request->getPage($value);
242         if ($new_page->exists()) {
243             $div->setAttr('class','error');
244             $div->setAttr('title',_("This page already exists"));
245         }
246         return $div;
247     }
248 };
249
250 // $Log: not supported by cvs2svn $
251 // Revision 1.25  2005/04/01 15:22:20  rurban
252 // Implement icase and regex options.
253 // Change checkbox case message from "Case-Sensitive" to "Case-Insensitive"
254 //
255 // Revision 1.24  2005/04/01 15:03:01  rurban
256 // Optimize rename UI with one selected pagename
257 //
258 // Revision 1.23  2005/02/12 17:24:24  rurban
259 // locale update: missing . : fixed. unified strings
260 // proper linebreaks
261 //
262 // Revision 1.22  2004/11/23 15:17:20  rurban
263 // better support for case_exact search (not caseexact for consistency),
264 // plugin args simplification:
265 //   handle and explode exclude and pages argument in WikiPlugin::getArgs
266 //     and exclude in advance (at the sql level if possible)
267 //   handle sortby and limit from request override in WikiPlugin::getArgs
268 // ListSubpages: renamed pages to maxpages
269 //
270 // Revision 1.21  2004/11/01 10:43:59  rurban
271 // seperate PassUser methods into seperate dir (memory usage)
272 // fix WikiUser (old) overlarge data session
273 // remove wikidb arg from various page class methods, use global ->_dbi instead
274 // ...
275 //
276 // Revision 1.20  2004/06/16 10:38:59  rurban
277 // Disallow refernces in calls if the declaration is a reference
278 // ("allow_call_time_pass_reference clean").
279 //   PhpWiki is now allow_call_time_pass_reference = Off clean,
280 //   but several external libraries may not.
281 //   In detail these libs look to be affected (not tested):
282 //   * Pear_DB odbc
283 //   * adodb oracle
284 //
285 // Revision 1.19  2004/06/14 11:31:39  rurban
286 // renamed global $Theme to $WikiTheme (gforge nameclash)
287 // inherit PageList default options from PageList
288 //   default sortby=pagename
289 // use options in PageList_Selectable (limit, sortby, ...)
290 // added action revert, with button at action=diff
291 // added option regex to WikiAdminSearchReplace
292 //
293 // Revision 1.18  2004/06/13 15:33:20  rurban
294 // new support for arguments owner, author, creator in most relevant
295 // PageList plugins. in WikiAdmin* via preSelectS()
296 //
297 // Revision 1.17  2004/06/08 10:05:12  rurban
298 // simplified admin action shortcuts
299 //
300 // Revision 1.16  2004/06/07 18:57:31  rurban
301 // fix rename: Change pagename in all linked pages
302 //
303 // Revision 1.15  2004/06/04 20:32:54  rurban
304 // Several locale related improvements suggested by Pierrick Meignen
305 // LDAP fix by John Cole
306 // reanable admin check without ENABLE_PAGEPERM in the admin plugins
307 //
308 // Revision 1.14  2004/06/03 22:24:48  rurban
309 // reenable admin check on !ENABLE_PAGEPERM, honor s=Wildcard arg, fix warning after Remove
310 //
311 // Revision 1.13  2004/06/03 12:59:41  rurban
312 // simplify translation
313 // NS4 wrap=virtual only
314 //
315 // Revision 1.12  2004/06/01 15:28:01  rurban
316 // AdminUser only ADMIN_USER not member of Administrators
317 // some RateIt improvements by dfrankow
318 // edit_toolbar buttons
319 //
320 // Revision 1.11  2004/05/24 17:34:53  rurban
321 // use ACLs
322 //
323 // Revision 1.10  2004/04/06 20:00:11  rurban
324 // Cleanup of special PageList column types
325 // Added support of plugin and theme specific Pagelist Types
326 // Added support for theme specific UserPreferences
327 // Added session support for ip-based throttling
328 //   sql table schema change: ALTER TABLE session ADD sess_ip CHAR(15);
329 // Enhanced postgres schema
330 // Added DB_Session_dba support
331 //
332 // Revision 1.9  2004/03/12 13:31:43  rurban
333 // enforce PagePermissions, errormsg if not Admin
334 //
335 // Revision 1.8  2004/03/01 13:48:46  rurban
336 // rename fix
337 // p[] consistency fix
338 //
339 // Revision 1.7  2004/02/22 23:20:33  rurban
340 // fixed DumpHtmlToDir,
341 // enhanced sortby handling in PageList
342 //   new button_heading th style (enabled),
343 // added sortby and limit support to the db backends and plugins
344 //   for paging support (<<prev, next>> links on long lists)
345 //
346 // Revision 1.6  2004/02/17 12:11:36  rurban
347 // 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, ...)
348 //
349 // Revision 1.5  2004/02/15 21:34:37  rurban
350 // PageList enhanced and improved.
351 // fixed new WikiAdmin... plugins
352 // editpage, Theme with exp. htmlarea framework
353 //   (htmlarea yet committed, this is really questionable)
354 // WikiUser... code with better session handling for prefs
355 // enhanced UserPreferences (again)
356 // RecentChanges for show_deleted: how should pages be deleted then?
357 //
358 // Revision 1.4  2004/02/12 17:05:39  rurban
359 // WikiAdminRename:
360 //   added "Change pagename in all linked pages also"
361 // PageList:
362 //   added javascript toggle for Select
363 // WikiAdminSearchReplace:
364 //   fixed another typo
365 //
366 // Revision 1.3  2004/02/12 13:05:50  rurban
367 // Rename functional for PearDB backend
368 // some other minor changes
369 // SiteMap comes with a not yet functional feature request: includepages (tbd)
370 //
371 // Revision 1.2  2004/02/12 11:45:11  rurban
372 // only WikiDB method missing
373 //
374 // Revision 1.1  2004/02/11 20:00:16  rurban
375 // WikiAdmin... series overhaul. Rename misses the db backend methods yet. Chmod + Chwon still missing.
376 //
377
378 // Local Variables:
379 // mode: php
380 // tab-width: 8
381 // c-basic-offset: 4
382 // c-hanging-comment-ender-p: nil
383 // indent-tabs-mode: nil
384 // End:
385 ?>