]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRename.php
Put private function after public ones
[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     public 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     function run($dbi, $argstr, &$request, $basepage)
70     {
71         $action = $request->getArg('action');
72         if ($action != 'browse' and $action != 'rename'
73             and $action != _("PhpWikiAdministration") . "/" . _("Rename")
74         ) {
75             return $this->disabled(_("Plugin not run: not in browse mode"));
76         }
77
78         if ($action == 'rename') {
79             // We rename a single page.
80             // No need to display "Regex?" and "Case insensitive?" boxes
81             // No need to confirm
82             $singlepage = true;
83         } else {
84             $singlepage = false;
85         }
86
87         $args = $this->getArgs($argstr, $request);
88         $this->_args = $args;
89         $this->preSelectS($args, $request);
90
91         $p = $request->getArg('p');
92         if (!$p) $p = $this->_list;
93         $post_args = $request->getArg('admin_rename');
94         $next_action = 'select';
95         $pages = array();
96         if ($p && !$request->isPost())
97             $pages = $p;
98         if ($p && $request->isPost() &&
99             !empty($post_args['rename']) && empty($post_args['cancel'])
100         ) {
101             // without individual PagePermissions:
102             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
103                 $request->_notAuthorized(WIKIAUTH_ADMIN);
104                 $this->disabled("! user->isAdmin");
105             }
106             // DONE: error message if not allowed.
107             if ($post_args['action'] == 'verify') {
108                 // Real action
109                 return $this->renamePages($dbi, $request, array_keys($p),
110                     $post_args['from'], $post_args['to'],
111                     !empty($post_args['updatelinks']),
112                     !empty($post_args['createredirect']));
113             }
114         }
115         if ($post_args['action'] == 'select') {
116             if (!empty($post_args['from']))
117                 $next_action = 'verify';
118             foreach ($p as $name => $c) {
119                 $pages[$name] = 1;
120             }
121         }
122         if ($next_action == 'select' and empty($pages)) {
123             // List all pages to select from.
124             $pages = $this->collectPages($pages, $dbi, $args['sortby'],
125                 $args['limit'], $args['exclude']);
126         }
127         /*if ($next_action == 'verify') {
128             $args['info'] = "checkbox,pagename,renamed_pagename";
129         }*/
130         $pagelist = new PageList_Selectable
131         (
132             $args['info'], $args['exclude'],
133             array('types' =>
134             array('renamed_pagename'
135             => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
136             )));
137         $pagelist->addPageList($pages);
138
139         $header = HTML::div();
140         if ($next_action == 'verify') {
141             $button_label = _("Yes");
142             $header->pushContent(
143                 HTML::p(HTML::strong(
144                     _("Are you sure you want to rename the selected pages?"))));
145             $header = $this->renameForm($header, $post_args, $singlepage);
146         } else {
147             if ($singlepage === true) {
148                 $button_label = _("Rename Page");
149             } else {
150                 $button_label = _("Rename selected pages");
151             }
152             if (!$post_args and count($pages) == 1) {
153                 list($post_args['from'],) = array_keys($pages);
154                 $post_args['to'] = $post_args['from'];
155             }
156             $header = $this->renameForm($header, $post_args, $singlepage);
157             if ($singlepage === false) {
158                 $header->pushContent(HTML::p(_("Select the pages to rename:")));
159             }
160         }
161
162         $buttons = HTML::p
163         (Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
164             Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
165
166         if ($singlepage === false) {
167             $list = $pagelist->getContent();
168         } else {
169             $list = "";
170         }
171         return HTML::form(array('action' => $request->getPostURL(),
172                 'method' => 'post'),
173             HTML::fieldset(
174                 HTML::legend(_("Rename Page")),
175                 $header,
176                 $buttons,
177                 $list,
178                 HiddenInputs($request->getArgs(),
179                     false,
180                     array('admin_rename')),
181                 HiddenInputs(array('admin_rename[action]' => $next_action)),
182                 ENABLE_PAGEPERM
183                     ? ''
184                     : HiddenInputs(array('require_authority_for_post'
185                 => WIKIAUTH_ADMIN))));
186     }
187
188     private function checkBox(&$post_args, $name, $msg)
189     {
190         $id = 'admin_rename-' . $name;
191         $checkbox = HTML::input(array('type' => 'checkbox',
192             'name' => 'admin_rename[' . $name . ']',
193             'id' => $id,
194             'value' => 1));
195         if (!empty($post_args[$name]))
196             $checkbox->setAttr('checked', 'checked');
197         return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
198     }
199
200     private function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks = false,
201                                  $createredirect = false)
202     {
203         $result = HTML::div();
204         $ul = HTML::ul();
205         $count = 0;
206         $post_args = $request->getArg('admin_rename');
207         $options =
208             array('regex' => isset($post_args['regex']) ? $post_args['regex'] : null,
209                 'icase' => isset($post_args['icase']) ? $post_args['icase'] : null);
210         foreach ($pages as $name) {
211             if (($newname = $this->renameHelper($name, $from, $to, $options))
212                 and $newname != $name
213             ) {
214                 if (strlen($newname) > MAX_PAGENAME_LENGTH)
215                     $ul->pushContent(HTML::li(_("Cannot rename. New page name too long.")));
216                 elseif ($dbi->isWikiPage($newname))
217                     $ul->pushContent(HTML::li(fmt("Page “%s” already exists. Ignored.",
218                         WikiLink($newname)))); elseif (!mayAccessPage('edit', $name))
219                     $ul->pushContent(HTML::li(fmt("Access denied to rename page “%s”.",
220                         WikiLink($name)))); elseif ($dbi->renamePage($name, $newname, $updatelinks)) {
221                     /* not yet implemented for all backends */
222                     $page = $dbi->getPage($newname);
223                     $current = $page->getCurrentRevision();
224                     $version = $current->getVersion();
225                     $meta = $current->_data;
226                     $text = $current->getPackedContent();
227                     $meta['summary'] = sprintf(_("Renamed page from “%s” to “%s”."), $name, $newname);
228                     $meta['is_minor_edit'] = 1;
229                     $meta['author'] = $request->_user->UserName();
230                     unset($meta['mtime']); // force new date
231                     $page->save($text, $version + 1, $meta);
232                     if ($createredirect) {
233                         $page = $dbi->getPage($name);
234                         $text = "<<RedirectTo page=\"" . $newname . "\">>";
235                         $meta['summary'] =
236                             sprintf(_("Renaming created redirect page from “%s” to “%s”"),
237                                 $name, $newname);
238                         $meta['is_minor_edit'] = 0;
239                         $meta['author'] = $request->_user->UserName();
240                         $page->save($text, 1, $meta);
241                     }
242                     $ul->pushContent(HTML::li(fmt("Renamed page from “%s” to “%s”.",
243                         $name, WikiLink($newname))));
244                     $count++;
245                 } else {
246                     $ul->pushContent(HTML::li(fmt("Couldn't rename page “%s” to “%s”.",
247                         $name, $newname)));
248                 }
249             } else {
250                 $ul->pushContent(HTML::li(fmt("Couldn't rename page “%s” to “%s”.",
251                     $name, $newname)));
252             }
253         }
254         if ($count) {
255             $dbi->touch();
256             $result->setAttr('class', 'feedback');
257             if ($count == 1) {
258                 $result->pushContent(HTML::p(
259                     _("One page has been renamed:")));
260             } else {
261                 $result->pushContent(HTML::p(
262                     fmt("%d pages have been renamed:", $count)));
263             }
264             $result->pushContent($ul);
265             return $result;
266         } else {
267             $result->setAttr('class', 'error');
268             $result->pushContent(HTML::p(fmt("No pages renamed.")));
269             $result->pushContent($ul);
270             return $result;
271         }
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: