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