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