]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRename.php
Better use "%d" for numbers; remove unneeded "permanently"
[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:   <<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 ($options['regex'])
57                 return preg_replace('/'.$from.'/'.($options['icase']?'i':''), $to, $name);
58             elseif ($options['icase'])
59                 return str_ireplace($from, $to, $name);
60             else
61             return str_replace($from, $to, $name);
62     }
63
64     function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks=false,
65                          $createredirect=false)
66     {
67         $result = HTML::div();
68         $ul = HTML::ul();
69         $count = 0;
70         $post_args = $request->getArg('admin_rename');
71         $options =
72           array('regex' => isset($post_args['regex']) ? $post_args['regex'] : null,
73                 'icase' => isset($post_args['icase']) ? $post_args['icase'] : null);
74         foreach ($pages as $name) {
75             if ( ($newname = $this->renameHelper($name, $from, $to, $options))
76                  and $newname != $name )
77             {
78                 if ($dbi->isWikiPage($newname))
79                     $ul->pushContent(HTML::li(fmt("Page '%s' already exists. Ignored.",
80                                                   WikiLink($newname))));
81                 elseif (! mayAccessPage('edit', $name))
82                     $ul->pushContent(HTML::li(fmt("Access denied to rename page '%s'.",
83                                                   WikiLink($name))));
84                 elseif ( $dbi->renamePage($name, $newname, $updatelinks)) {
85                     /* not yet implemented for all backends */
86                     $page = $dbi->getPage($newname);
87                     $current = $page->getCurrentRevision();
88                     $version = $current->getVersion();
89                     $meta = $current->_data;
90                     $text = $current->getPackedContent();
91                     $meta['summary'] = sprintf(_("Renamed page from '%s' to '%s'"), $name, $newname);
92                     $meta['is_minor_edit'] = 1;
93                     $meta['author'] = $request->_user->UserName();
94                     unset($meta['mtime']); // force new date
95                     $page->save($text, $version + 1, $meta);
96                     if ($createredirect) {
97                         $page = $dbi->getPage($name);
98                         $text = "<<RedirectTo page=\"" . $newname . "\">>";
99                         $meta['summary'] =
100                             sprintf(_("Renaming created redirect page from '%s' to '%s'"),
101                                     $name, $newname);
102                         $meta['is_minor_edit'] = 0;
103                         $meta['author'] = $request->_user->UserName();
104                         $page->save($text, 1, $meta);
105                     }
106                     $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",
107                                                   $name, WikiLink($newname))));
108                     $count++;
109                 } else {
110                     $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
111                                                   $name, $newname)));
112                 }
113             } else {
114                 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
115                                               $name, $newname)));
116             }
117         }
118         if ($count) {
119             $dbi->touch();
120             $result->setAttr('class', 'feedback');
121             if ($count == 1) {
122                 $result->pushContent(HTML::p(
123                   "One page has been renamed:"));
124             } else {
125                 $result->pushContent(HTML::p(
126                   fmt("%d pages have been renamed:", $count)));
127             }
128             $result->pushContent($ul);
129             return $result;
130         } else {
131             $result->setAttr('class', 'error');
132             $result->pushContent(HTML::p(fmt("No pages renamed.")));
133             $result->pushContent($ul);
134             return $result;
135         }
136     }
137
138     function run($dbi, $argstr, &$request, $basepage) {
139             $action = $request->getArg('action');
140         if ($action != 'browse' and $action != 'rename'
141                                 and $action != _("PhpWikiAdministration")."/"._("Rename"))
142             return $this->disabled("(action != 'browse')");
143
144         if ($action == 'rename') {
145             // We rename a single page.
146             // No need to display "Regex?" and "Case insensitive?" boxes
147             // No need to confirm
148            $singlepage = true;
149         } else {
150            $singlepage = false;
151         }
152
153         $args = $this->getArgs($argstr, $request);
154         $this->_args = $args;
155         $this->preSelectS($args, $request);
156
157         $p = $request->getArg('p');
158         if (!$p) $p = $this->_list;
159         $post_args = $request->getArg('admin_rename');
160         $next_action = 'select';
161         $pages = array();
162         if ($p && !$request->isPost())
163             $pages = $p;
164         if ($p && $request->isPost() &&
165             !empty($post_args['rename']) && empty($post_args['cancel'])) {
166             // without individual PagePermissions:
167             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
168                 $request->_notAuthorized(WIKIAUTH_ADMIN);
169                 $this->disabled("! user->isAdmin");
170             }
171             // DONE: error message if not allowed.
172             if ($post_args['action'] == 'verify') {
173                 // Real action
174                 return $this->renamePages($dbi, $request, array_keys($p),
175                                           $post_args['from'], $post_args['to'],
176                                           !empty($post_args['updatelinks']),
177                                           !empty($post_args['createredirect']));
178             }
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         if ($next_action == 'select' and empty($pages)) {
188             // List all pages to select from.
189             $pages = $this->collectPages($pages, $dbi, $args['sortby'],
190                                          $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 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
228             (Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
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'
250                                                    => WIKIAUTH_ADMIN))));
251     }
252
253     function checkBox (&$post_args, $name, $msg) {
254             $id = 'admin_rename-'.$name;
255             $checkbox = HTML::input(array('type' => 'checkbox',
256                                       'name' => 'admin_rename['.$name.']',
257                                       'id'   => $id,
258                                       'value' => 1));
259         if (!empty($post_args[$name]))
260             $checkbox->setAttr('checked', 'checked');
261         return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
262     }
263
264     function renameForm(&$header, $post_args, $singlepage) {
265         $table = HTML::table();
266         $this->_tablePush($table, _("Rename"). " ". _("from")._(": "),
267                           HTML::input(array('name' => 'admin_rename[from]',
268                                             'size' => 90,
269                                             'value' => $post_args['from'])));
270         $this->_tablePush($table, _("to")._(": "),
271                           HTML::input(array('name' => 'admin_rename[to]',
272                                             'size' => 90,
273                                             'value' => $post_args['to'])));
274         if ($singlepage === false) {
275             $this->_tablePush($table, '',
276                               $this->checkBox($post_args, 'regex', _("Regex?")));
277             $this->_tablePush($table, '',
278                               $this->checkBox($post_args, 'icase', _("Case insensitive?")));
279         }
280         if (defined('EXPERIMENTAL') and EXPERIMENTAL) // not yet stable
281             $this->_tablePush($table, '',
282                               $this->checkBox($post_args, 'updatelinks',
283                                 _("Change pagename in all linked pages also?")));
284         $this->_tablePush($table, '',
285                           $this->checkBox($post_args, 'createredirect',
286                                           _("Create redirect from old to new name?")));
287         $header->pushContent($table);
288         return $header;
289     }
290 }
291
292 // TODO: grey out unchangeble pages, even in the initial list also?
293 // TODO: autoselect by matching name javascript in admin_rename[from]
294 // TODO: update rename[] fields when case-sensitive and regex is changed
295
296 // moved from lib/PageList.php
297 class _PageList_Column_renamed_pagename extends _PageList_Column {
298     function _getValue ($page_handle, &$revision_handle) {
299         global $request;
300         $post_args = $request->getArg('admin_rename');
301         $options = array('regex' => @$post_args['regex'],
302                          'icase' => @$post_args['icase']);
303
304         $value = $post_args
305             ? WikiPlugin_WikiAdminRename::renameHelper
306                 ($page_handle->getName(),
307                  $post_args['from'], $post_args['to'],
308                  $options)
309             : $page_handle->getName();
310         $div = HTML::div(" => ",HTML::input(array('type' => 'text',
311                                                   'name' => 'rename[]',
312                                                   'value' => $value)));
313         $new_page = $request->getPage($value);
314         return $div;
315     }
316 };
317
318 // Local Variables:
319 // mode: php
320 // tab-width: 8
321 // c-basic-offset: 4
322 // c-hanging-comment-ender-p: nil
323 // indent-tabs-mode: nil
324 // End:
325 ?>