]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRename.php
Better user interface and feedback messages
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRename.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  Copyright 2004,2005,2007 $ThePhpWikiProgrammingTeam
5  Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
6
7  This file is part of PhpWiki.
8
9  PhpWiki is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  PhpWiki is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with PhpWiki; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /**
25  * Usage:   <?plugin WikiAdminRename ?> or called via WikiAdminSelect
26  * @author:  Reini Urban <rurban@x-ray.at>
27  *
28  * KNOWN ISSUES:
29  *   Requires PHP 4.2.
30  */
31 require_once('lib/PageList.php');
32 require_once('lib/plugin/WikiAdminSelect.php');
33
34 class WikiPlugin_WikiAdminRename
35 extends WikiPlugin_WikiAdminSelect
36 {
37     function getName() {
38         return _("WikiAdminRename");
39     }
40
41     function getDescription() {
42         return _("Rename selected pages");
43     }
44
45     function getVersion() {
46         return preg_replace("/[Revision: $]/", '',
47                             "\$Revision$");
48     }
49
50     function getDefaultArguments() {
51         return array_merge
52             (
53              PageList::supportedArgs(),
54              array(
55                    's'  => false,
56                    /* Columns to include in listing */
57                    'info'     => 'pagename,mtime',
58                    'updatelinks' => 0,
59                    'createredirect' => 0
60                    ));
61     }
62
63     function renameHelper($name, $from, $to, $options = false) {
64         if ($options['regex'])
65             return preg_replace('/'.$from.'/'.($options['icase']?'i':''), $to, $name);
66         elseif ($options['icase'])
67             return str_ireplace($from, $to, $name);
68         else
69             return str_replace($from, $to, $name);
70     }
71
72     function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks=false, $createredirect=false) {
73         $result = HTML::div();
74         $ul = HTML::ul();
75         $count = 0;
76         $post_args = $request->getArg('admin_rename');
77         $options = array('regex' => @$post_args['regex'],
78                          'icase' => @$post_args['icase']);
79         foreach ($pages as $name) {
80             if ( ($newname = $this->renameHelper($name, $from, $to, $options)) 
81                  and $newname != $name )
82             {
83                 if ($dbi->isWikiPage($newname))
84                     $ul->pushContent(HTML::li(fmt("Page '%s' already exists. Ignored.",
85                                                   WikiLink($newname))));
86                 elseif (! mayAccessPage('edit', $name))
87                     $ul->pushContent(HTML::li(fmt("Access denied to rename page '%s'.",
88                                                   WikiLink($name))));
89                 elseif ( $dbi->renamePage($name, $newname, $updatelinks)) {
90                     /* not yet implemented for all backends */
91                     $page = $dbi->getPage($newname);
92                     $current = $page->getCurrentRevision();
93                     $version = $current->getVersion();
94                     $meta = $current->_data;
95                     $text = $current->getPackedContent();
96                     $meta['summary'] = sprintf(_("Renamed page from '%s' to '%s'"), $name, $newname);
97                     $meta['is_minor_edit'] = 0;
98                     $meta['author'] = $request->_user->UserName();
99                     unset($meta['mtime']); // force new date
100                     $page->save($text, $version + 1, $meta);
101                     if ($createredirect) {
102                         $page = $dbi->getPage($name);
103                         $text = "<<RedirectTo page=\"" . $newname . "\">>";
104                         $meta['summary'] = sprintf(_("Renaming created redirect page from '%s' to '%s'"), $name, $newname);
105                         $meta['is_minor_edit'] = 0;
106                         $meta['author'] = $request->_user->UserName();
107                         $page->save($text, 1, $meta);
108                     }
109                     $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",
110                                                   $name, WikiLink($newname))));
111                     $count++;
112                 } else {
113                     $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", 
114                                                   $name, $newname)));
115                 }
116             } else {
117                 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", 
118                                               $name, $newname)));
119             }
120         }
121         if ($count) {
122             $dbi->touch();
123             $result->setAttr('class', 'feedback');
124             if ($count == 1) {
125                 $result->pushContent(HTML::p("One page has been permanently renamed:"));
126             } else {
127                 $result->pushContent(HTML::p(fmt("%s pages have been permanently renamed:", $count)));
128             }
129             $result->pushContent($ul);
130             return $result;
131         } else {
132             $result->setAttr('class', 'error');
133             $result->pushContent(HTML::p(fmt("No pages renamed.")));
134             $result->pushContent($ul);
135             return $result;
136         }
137     }
138
139     function run($dbi, $argstr, &$request, $basepage) {
140         $action = $request->getArg('action');
141         if ($action != 'browse' and $action != 'rename' 
142                                 and $action != _("PhpWikiAdministration")."/"._("Rename"))
143             return $this->disabled("(action != 'browse')");
144
145         if ($action == 'rename') {
146             // We rename a single page.
147             // No need to display "Regex?" and "Case insensitive?" boxes
148             // No need to confirm
149            $singlepage = true;
150         } else {
151            $singlepage = false;
152         }
153
154         $args = $this->getArgs($argstr, $request);
155         $this->_args = $args;
156         $this->preSelectS($args, $request);
157
158         $p = $request->getArg('p');
159         if (!$p) $p = $this->_list;
160         $post_args = $request->getArg('admin_rename');
161         $next_action = 'select';
162         $pages = array();
163         if ($p && !$request->isPost())
164             $pages = $p;
165         if ($p && $request->isPost() &&
166             !empty($post_args['rename']) && empty($post_args['cancel'])) {
167             // without individual PagePermissions:
168             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
169                 $request->_notAuthorized(WIKIAUTH_ADMIN);
170                 $this->disabled("! user->isAdmin");
171             }
172             // DONE: error message if not allowed.
173             if ($post_args['action'] == 'verify') {
174                 // Real action
175                 return $this->renamePages($dbi, $request, array_keys($p), 
176                                           $post_args['from'], $post_args['to'], 
177                                           !empty($post_args['updatelinks']),
178                                           !empty($post_args['createredirect']));
179             }
180             if ($post_args['action'] == 'select') {
181                 if (!empty($post_args['from']))
182                     $next_action = 'verify';
183                 foreach ($p as $name => $c) {
184                     $pages[$name] = 1;
185                 }
186             }
187         }
188         if ($next_action == 'select' and empty($pages)) {
189             // List all pages to select from.
190             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
191         }
192         /*if ($next_action == 'verify') {
193             $args['info'] = "checkbox,pagename,renamed_pagename";
194         }*/
195         $pagelist = new PageList_Selectable
196             (
197              $args['info'], $args['exclude'],
198              array('types' => 
199                    array('renamed_pagename'
200                          => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
201                          )));
202         $pagelist->addPageList($pages);
203
204         $header = HTML::div();
205         if ($next_action == 'verify') {
206             $button_label = _("Yes");
207             $header->pushContent(
208               HTML::p(HTML::strong(
209                 _("Are you sure you want to permanently rename the selected pages?"))));
210             $header = $this->renameForm($header, $post_args, $singlepage);
211         } else {
212             if ($singlepage === true) {
213                 $button_label = _("Rename page");
214             } else {
215                 $button_label = _("Rename selected pages");
216             }
217             if (!$post_args and count($pages) == 1) {
218                 list($post_args['from'],) = array_keys($pages);
219                 $post_args['to'] = $post_args['from'];
220             }
221             $header = $this->renameForm($header, $post_args, $singlepage);
222             if ($singlepage === false) {
223                 $header->pushContent(HTML::p(_("Select the pages to rename:")));
224             }
225         }
226
227         $buttons = HTML::p(Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
228                            HTML::Raw('&nbsp;&nbsp;'),
229                            Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
230
231         if ($singlepage === false) {
232             $list = $pagelist->getContent();
233         } else {
234             $list = ""; 
235         }
236         return HTML::form(array('action' => $request->getPostURL(),
237                                 'method' => 'post'),
238                           HTML::fieldset(
239                               HTML::legend("Rename page"),
240                               $header,
241                               $buttons,
242                               $list,
243                               HiddenInputs($request->getArgs(),
244                                             false,
245                                             array('admin_rename')),
246                               HiddenInputs(array('admin_rename[action]' => $next_action)),
247                               ENABLE_PAGEPERM
248                               ? ''
249                               : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN))));
250     }
251
252     function checkBox (&$post_args, $name, $msg) {
253         $id = 'admin_rename-'.$name;
254         $checkbox = HTML::input(array('type' => 'checkbox',
255                                       'name' => 'admin_rename['.$name.']',
256                                       'id'   => $id,
257                                       'value' => 1));
258         if (!empty($post_args[$name]))
259             $checkbox->setAttr('checked', 'checked');
260         return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
261     }
262
263     function renameForm(&$header, $post_args, $singlepage) {
264         $table = HTML::table();
265         $this->_tablePush($table, _("Rename"). " ". _("from").': ',
266                           HTML::input(array('name' => 'admin_rename[from]',
267                                             'size' => 90,
268                                             'value' => $post_args['from'])));
269         $this->_tablePush($table, _("to").': ',
270                           HTML::input(array('name' => 'admin_rename[to]',
271                                             'size' => 90,
272                                             'value' => $post_args['to'])));
273         if ($singlepage === false) {
274             $this->_tablePush($table, '', $this->checkBox($post_args, 'regex', _("Regex?")));
275             $this->_tablePush($table, '', $this->checkBox($post_args, 'icase', _("Case insensitive?")));
276         }
277         if (DEBUG) // not yet stable
278             $this->_tablePush($table, '', $this->checkBox($post_args, 'updatelinks', 
279                                                       _("Change pagename in all linked pages also?")));
280         $this->_tablePush($table, '', $this->checkBox($post_args, 'createredirect', 
281                                                       _("Create redirect from old to new name?")));
282         $header->pushContent($table);
283         return $header;
284     }
285 }
286
287 // TODO: grey out unchangeble pages, even in the initial list also?
288 // TODO: autoselect by matching name javascript in admin_rename[from]
289 // TODO: update rename[] fields when case-sensitive and regex is changed
290
291 // moved from lib/PageList.php
292 class _PageList_Column_renamed_pagename extends _PageList_Column {
293     function _getValue ($page_handle, &$revision_handle) {
294         global $request;
295         $post_args = $request->getArg('admin_rename');
296         $options = array('regex' => @$post_args['regex'],
297                          'icase' => @$post_args['icase']);
298                          
299         $value = $post_args ? WikiPlugin_WikiAdminRename::renameHelper($page_handle->getName(), 
300                                                           $post_args['from'], $post_args['to'],
301                                                           $options)
302                             : $page_handle->getName();                              
303         $div = HTML::div(" => ",HTML::input(array('type' => 'text',
304                                                   'name' => 'rename[]',
305                                                   'value' => $value)));
306         $new_page = $request->getPage($value);
307         return $div;
308     }
309 };
310
311 // Local Variables:
312 // mode: php
313 // tab-width: 8
314 // c-basic-offset: 4
315 // c-hanging-comment-ender-p: nil
316 // indent-tabs-mode: nil
317 // End:
318 ?>