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