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