]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRename.php
Partial revert: put renaming in page history
[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:   <?plugin WikiAdminRename ?> or called via WikiAdminSelect
26  * @author:  Reini Urban <rurban@x-ray.at>
27  *
28  * KNOWN ISSUES:
29  *   Requires PHP 4.2 at least.
30  */
31 require_once('lib/PageList.php');
32 require_once('lib/plugin/WikiAdminSelect.php');
33
34 class WikiPlugin_WikiAdminRename
35 extends WikiPlugin_WikiAdminSelect
36 {
37     function getName() {
38         return _("WikiAdminRename");
39     }
40
41     function getDescription() {
42         return _("Rename selected pages");
43     }
44
45     function getVersion() {
46         return preg_replace("/[Revision: $]/", '',
47                             "\$Revision$");
48     }
49
50     function getDefaultArguments() {
51         return array_merge
52             (
53              WikiPlugin_WikiAdminSelect::getDefaultArguments(),
54              array(
55                    /* Columns to include in listing */
56                    'info'     => 'pagename,mtime',
57                    'updatelinks' => 0,
58                    'createredirect' => 0
59                    ));
60     }
61
62     function renameHelper($name, $from, $to, $options = false) {
63         if ($options['regex'])
64             return preg_replace('/'.$from.'/'.($options['icase']?'i':''), $to, $name);
65         elseif ($options['icase'])
66             return str_ireplace($from, $to, $name);
67         else
68             return str_replace($from, $to, $name);
69     }
70
71     function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks=false, 
72                          $createredirect=false) 
73     {
74         $result = HTML::div();
75         $ul = HTML::ul();
76         $count = 0;
77         $post_args = $request->getArg('admin_rename');
78         $options = 
79           array('regex' => isset($post_args['regex']) ? $post_args['regex'] : null,
80                 'icase' => isset($post_args['icase']) ? $post_args['icase'] : null);
81         foreach ($pages as $name) {
82             if ( ($newname = $this->renameHelper($name, $from, $to, $options)) 
83                  and $newname != $name )
84             {
85                 if ($dbi->isWikiPage($newname))
86                     $ul->pushContent(HTML::li(fmt("Page '%s' already exists. Ignored.",
87                                                   WikiLink($newname))));
88                 elseif (! mayAccessPage('edit', $name))
89                     $ul->pushContent(HTML::li(fmt("Access denied to rename page '%s'.",
90                                                   WikiLink($name))));
91                 elseif ( $dbi->renamePage($name, $newname, $updatelinks)) {
92                     /* not yet implemented for all backends */
93                     $page = $dbi->getPage($newname);
94                     $current = $page->getCurrentRevision();
95                     $version = $current->getVersion();
96                     $meta = $current->_data;
97                     $text = $current->getPackedContent();
98                     $meta['summary'] = sprintf(_("Renamed page from '%s' to '%s'"), $name, $newname);
99                     $meta['is_minor_edit'] = 1;
100                     $meta['author'] = $request->_user->UserName();
101                     unset($meta['mtime']); // force new date
102                     $page->save($text, $version + 1, $meta);
103                     if ($createredirect) {
104                         $page = $dbi->getPage($name);
105                         $text = "<<RedirectTo page=\"" . $newname . "\">>";
106                         $meta['summary'] = 
107                             sprintf(_("Renaming created redirect page from '%s' to '%s'"),
108                                     $name, $newname);
109                         $meta['is_minor_edit'] = 0;
110                         $meta['author'] = $request->_user->UserName();
111                         $page->save($text, 1, $meta);
112                     }
113                     $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",
114                                                   $name, WikiLink($newname))));
115                     $count++;
116                 } else {
117                     $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", 
118                                                   $name, $newname)));
119                 }
120             } else {
121                 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", 
122                                               $name, $newname)));
123             }
124         }
125         if ($count) {
126             $dbi->touch();
127             $result->setAttr('class', 'feedback');
128             if ($count == 1) {
129                 $result->pushContent(HTML::p(
130                   "One page has been permanently renamed:"));
131             } else {
132                 $result->pushContent(HTML::p(
133                   fmt("%s pages have been permanently renamed:", $count)));
134             }
135             $result->pushContent($ul);
136             return $result;
137         } else {
138             $result->setAttr('class', 'error');
139             $result->pushContent(HTML::p(fmt("No pages renamed.")));
140             $result->pushContent($ul);
141             return $result;
142         }
143     }
144
145     function run($dbi, $argstr, &$request, $basepage) {
146         $action = $request->getArg('action');
147         if ($action != 'browse' and $action != 'rename' 
148                                 and $action != _("PhpWikiAdministration")."/"._("Rename"))
149             return $this->disabled("(action != 'browse')");
150
151         if ($action == 'rename') {
152             // We rename a single page.
153             // No need to display "Regex?" and "Case insensitive?" boxes
154             // No need to confirm
155            $singlepage = true;
156         } else {
157            $singlepage = false;
158         }
159
160         $args = $this->getArgs($argstr, $request);
161         $this->_args = $args;
162         $this->preSelectS($args, $request);
163
164         $p = $request->getArg('p');
165         if (!$p) $p = $this->_list;
166         $post_args = $request->getArg('admin_rename');
167         $next_action = 'select';
168         $pages = array();
169         if ($p && !$request->isPost())
170             $pages = $p;
171         if ($p && $request->isPost() &&
172             !empty($post_args['rename']) && empty($post_args['cancel'])) {
173             // without individual PagePermissions:
174             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
175                 $request->_notAuthorized(WIKIAUTH_ADMIN);
176                 $this->disabled("! user->isAdmin");
177             }
178             // DONE: error message if not allowed.
179             if ($post_args['action'] == 'verify') {
180                 // Real action
181                 return $this->renamePages($dbi, $request, array_keys($p), 
182                                           $post_args['from'], $post_args['to'], 
183                                           !empty($post_args['updatelinks']),
184                                           !empty($post_args['createredirect']));
185             }
186         }
187         if ($post_args['action'] == 'select') {
188             if (!empty($post_args['from']))
189                 $next_action = 'verify';
190             foreach ($p as $name => $c) {
191                 $pages[$name] = 1;
192             }
193         }
194         if ($next_action == 'select' and empty($pages)) {
195             // List all pages to select from.
196             $pages = $this->collectPages($pages, $dbi, $args['sortby'],
197                                          $args['limit'], $args['exclude']);
198         }
199         /*if ($next_action == 'verify') {
200             $args['info'] = "checkbox,pagename,renamed_pagename";
201         }*/
202         $pagelist = new PageList_Selectable
203             (
204              $args['info'], $args['exclude'],
205              array('types' => 
206                    array('renamed_pagename'
207                          => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
208                          )));
209         $pagelist->addPageList($pages);
210
211         $header = HTML::div();
212         if ($next_action == 'verify') {
213             $button_label = _("Yes");
214             $header->pushContent(
215               HTML::p(HTML::strong(
216                 _("Are you sure you want to permanently rename the selected pages?"))));
217             $header = $this->renameForm($header, $post_args, $singlepage);
218         } else {
219             if ($singlepage === true) {
220                 $button_label = _("Rename page");
221             } else {
222                 $button_label = _("Rename selected pages");
223             }
224             if (!$post_args and count($pages) == 1) {
225                 list($post_args['from'],) = array_keys($pages);
226                 $post_args['to'] = $post_args['from'];
227             }
228             $header = $this->renameForm($header, $post_args, $singlepage);
229             if ($singlepage === false) {
230                 $header->pushContent(HTML::p(_("Select the pages to rename:")));
231             }
232         }
233
234         $buttons = HTML::p
235             (Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
236              Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
237
238         if ($singlepage === false) {
239             $list = $pagelist->getContent();
240         } else {
241             $list = ""; 
242         }
243         return HTML::form(array('action' => $request->getPostURL(),
244                                 'method' => 'post'),
245                           HTML::fieldset(
246                               HTML::legend("Rename page"),
247                               $header,
248                               $buttons,
249                               $list,
250                               HiddenInputs($request->getArgs(),
251                                             false,
252                                             array('admin_rename')),
253                               HiddenInputs(array('admin_rename[action]' => $next_action)),
254                               ENABLE_PAGEPERM
255                               ? ''
256                               : HiddenInputs(array('require_authority_for_post'
257                                                    => WIKIAUTH_ADMIN))));
258     }
259
260     function checkBox (&$post_args, $name, $msg) {
261         $id = 'admin_rename-'.$name;
262         $checkbox = HTML::input(array('type' => 'checkbox',
263                                       'name' => 'admin_rename['.$name.']',
264                                       'id'   => $id,
265                                       'value' => 1));
266         if (!empty($post_args[$name]))
267             $checkbox->setAttr('checked', 'checked');
268         return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
269     }
270
271     function renameForm(&$header, $post_args, $singlepage) {
272         $table = HTML::table();
273         $this->_tablePush($table, _("Rename"). " ". _("from").': ',
274                           HTML::input(array('name' => 'admin_rename[from]',
275                                             'size' => 90,
276                                             'value' => $post_args['from'])));
277         $this->_tablePush($table, _("to").': ',
278                           HTML::input(array('name' => 'admin_rename[to]',
279                                             'size' => 90,
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 (DEBUG) // 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 = array('regex' => @$post_args['regex'],
309                          'icase' => @$post_args['icase']);
310                          
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:
332 ?>