]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRename.php
Add fieldset
[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.
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              PageList::supportedArgs(),
54              array(
55                    's'  => false,
56                    /* Columns to include in listing */
57                    'info'     => 'pagename,mtime',
58                    'updatelinks' => 0,
59                    'createredirect' => 0
60                    ));
61     }
62
63     function renameHelper($name, $from, $to, $options = false) {
64         if ($options['regex'])
65             return preg_replace('/'.$from.'/'.($options['icase']?'i':''), $to, $name);
66         elseif ($options['icase'])
67             return str_ireplace($from, $to, $name);
68         else
69             return str_replace($from, $to, $name);
70     }
71
72     function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks=false, $createredirect=false) {
73         $result = HTML::div();
74         $ul = HTML::ul();
75         $count = 0;
76         $post_args = $request->getArg('admin_rename');
77         $options = array('regex' => @$post_args['regex'],
78                          'icase' => @$post_args['icase']);
79         foreach ($pages as $name) {
80             if ( ($newname = $this->renameHelper($name, $from, $to, $options)) 
81                  and $newname != $name )
82             {
83                 if ($dbi->isWikiPage($newname))
84                     $ul->pushContent(HTML::li(fmt("Page %s already exists. Ignored.",
85                                                   WikiLink($newname))));
86                 elseif (! mayAccessPage('edit', $name))
87                     $ul->pushContent(HTML::li(fmt("Access denied to rename page '%s'.",
88                                                   WikiLink($name))));
89                 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'] = 0;
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'] = sprintf(_("Renaming created redirect page from '%s' to '%s'"), $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 '%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             $result->pushContent(HTML::p(fmt("%s pages have been permanently renamed:", $count)));
125             $result->pushContent($ul);
126             return $result;
127         } else {
128             $result->setAttr('class', 'error');
129             $result->pushContent(HTML::p(fmt("No pages renamed:")));
130             $result->pushContent($ul);
131             return $result;
132         }
133     }
134     
135     function run($dbi, $argstr, &$request, $basepage) {
136         $action = $request->getArg('action');
137         if ($action != 'browse' and $action != 'rename' 
138                                 and $action != _("PhpWikiAdministration")."/"._("Rename"))
139             return $this->disabled("(action != 'browse')");
140
141         if ($action == 'rename') {
142             // We rename a single page.
143             // No need to display "Regex?" and "Case insensitive?" boxes
144             // No need to confirm
145            $singlepage = true;
146         } else {
147            $singlepage = false;
148         }
149
150         $args = $this->getArgs($argstr, $request);
151         $this->_args = $args;
152         $this->preSelectS($args, $request);
153
154         $p = $request->getArg('p');
155         if (!$p) $p = $this->_list;
156         $post_args = $request->getArg('admin_rename');
157         $next_action = 'select';
158         $pages = array();
159         if ($p && !$request->isPost())
160             $pages = $p;
161         if ($p && $request->isPost() &&
162             !empty($post_args['rename']) && empty($post_args['cancel'])) {
163             // without individual PagePermissions:
164             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
165                 $request->_notAuthorized(WIKIAUTH_ADMIN);
166                 $this->disabled("! user->isAdmin");
167             }
168             // DONE: error message if not allowed.
169             if ($post_args['action'] == 'verify') {
170                 // Real action
171                 return $this->renamePages($dbi, $request, array_keys($p), 
172                                           $post_args['from'], $post_args['to'], 
173                                           !empty($post_args['updatelinks']),
174                                           !empty($post_args['createredirect']));
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         }
184         if ($next_action == 'select' and empty($pages)) {
185             // List all pages to select from.
186             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $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(Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
224                            HTML::Raw('&nbsp;&nbsp;'),
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' => WIKIAUTH_ADMIN))));
246     }
247
248     function checkBox (&$post_args, $name, $msg) {
249         $id = 'admin_rename-'.$name;
250         $checkbox = HTML::input(array('type' => 'checkbox',
251                                       'name' => 'admin_rename['.$name.']',
252                                       'id'   => $id,
253                                       'value' => 1));
254         if (!empty($post_args[$name]))
255             $checkbox->setAttr('checked', 'checked');
256         return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
257     }
258
259     function renameForm(&$header, $post_args, $singlepage) {
260         $table = HTML::table();
261         $this->_tablePush($table, _("Rename"). " ". _("from").': ',
262                           HTML::input(array('name' => 'admin_rename[from]',
263                                             'size' => 90,
264                                             'value' => $post_args['from'])));
265         $this->_tablePush($table, _("to").': ',
266                           HTML::input(array('name' => 'admin_rename[to]',
267                                             'size' => 90,
268                                             'value' => $post_args['to'])));
269         if ($singlepage === false) {
270             $this->_tablePush($table, '', $this->checkBox($post_args, 'regex', _("Regex?")));
271             $this->_tablePush($table, '', $this->checkBox($post_args, 'icase', _("Case insensitive?")));
272         }
273         if (DEBUG) // not yet stable
274             $this->_tablePush($table, '', $this->checkBox($post_args, 'updatelinks', 
275                                                       _("Change pagename in all linked pages also?")));
276         $this->_tablePush($table, '', $this->checkBox($post_args, 'createredirect', 
277                                                       _("Create redirect from old to new name?")));
278         $header->pushContent($table);
279         return $header;
280     }
281 }
282
283 // TODO: grey out unchangeble pages, even in the initial list also?
284 // TODO: autoselect by matching name javascript in admin_rename[from]
285 // TODO: update rename[] fields when case-sensitive and regex is changed
286
287 // moved from lib/PageList.php
288 class _PageList_Column_renamed_pagename extends _PageList_Column {
289     function _getValue ($page_handle, &$revision_handle) {
290         global $request;
291         $post_args = $request->getArg('admin_rename');
292         $options = array('regex' => @$post_args['regex'],
293                          'icase' => @$post_args['icase']);
294                          
295         $value = $post_args ? WikiPlugin_WikiAdminRename::renameHelper($page_handle->getName(), 
296                                                           $post_args['from'], $post_args['to'],
297                                                           $options)
298                             : $page_handle->getName();                              
299         $div = HTML::div(" => ",HTML::input(array('type' => 'text',
300                                                   'name' => 'rename[]',
301                                                   'value' => $value)));
302         $new_page = $request->getPage($value);
303         return $div;
304     }
305 };
306
307 // Local Variables:
308 // mode: php
309 // tab-width: 8
310 // c-basic-offset: 4
311 // c-hanging-comment-ender-p: nil
312 // indent-tabs-mode: nil
313 // End:
314 ?>