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