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