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