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