]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRename.php
Put buttons at the top so that they are always visible
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRename.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  Copyright 2004,2005,2007 $ThePhpWikiProgrammingTeam
5  Copyright 2008 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                    ));
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         $ul = HTML::ul();
73         $count = 0;
74         $post_args = $request->getArg('admin_rename');
75         $options = array('regex' => @$post_args['regex'],
76                          'icase' => @$post_args['icase']);
77         foreach ($pages as $name) {
78             if ( ($newname = $this->renameHelper($name, $from, $to, $options)) 
79                  and $newname != $name )
80             {
81                 if ($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                     $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",
90                                                   $name, WikiLink($newname))));
91                     $count++;
92                 } else {
93                     $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", 
94                                                   $name, $newname)));
95                 }
96             } else {
97                 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", 
98                                               $name, $newname)));
99             }
100         }
101         if ($count) {
102             $dbi->touch();
103             return HTML($ul, HTML::p(fmt("%s pages have been permanently renamed.",
104                                          $count)));
105         } else {
106             return HTML($ul, HTML::p(fmt("No pages renamed.")));
107         }
108     }
109     
110     function run($dbi, $argstr, &$request, $basepage) {
111         $action = $request->getArg('action');
112         if ($action != 'browse' and $action != 'rename' 
113                                 and $action != _("PhpWikiAdministration")."/"._("Rename"))
114             return $this->disabled("(action != 'browse')");
115         
116         $args = $this->getArgs($argstr, $request);
117         $this->_args = $args;
118         $this->preSelectS($args, $request);
119
120         $p = $request->getArg('p');
121         if (!$p) $p = $this->_list;
122         $post_args = $request->getArg('admin_rename');
123         $next_action = 'select';
124         $pages = array();
125         if ($p && !$request->isPost())
126             $pages = $p;
127         if ($p && $request->isPost() &&
128             !empty($post_args['rename']) && empty($post_args['cancel'])) {
129             // without individual PagePermissions:
130             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
131                 $request->_notAuthorized(WIKIAUTH_ADMIN);
132                 $this->disabled("! user->isAdmin");
133             }
134             // DONE: error message if not allowed.
135             if ($post_args['action'] == 'verify') {
136                 // Real action
137                 return $this->renamePages($dbi, $request, array_keys($p), 
138                                           $post_args['from'], $post_args['to'], 
139                                           !empty($post_args['updatelinks']));
140             }
141             if ($post_args['action'] == 'select') {
142                 if (!empty($post_args['from']))
143                     $next_action = 'verify';
144                 foreach ($p as $name => $c) {
145                     $pages[$name] = 1;
146                 }
147             }
148         }
149         if ($next_action == 'select' and empty($pages)) {
150             // List all pages to select from.
151             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
152         }
153         /*if ($next_action == 'verify') {
154             $args['info'] = "checkbox,pagename,renamed_pagename";
155         }*/
156         $pagelist = new PageList_Selectable
157             (
158              $args['info'], $args['exclude'],
159              array('types' => 
160                    array('renamed_pagename'
161                          => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
162                          )));
163         $pagelist->addPageList($pages);
164
165         $header = HTML::div();
166         if ($next_action == 'verify') {
167             $button_label = _("Yes");
168             $header->pushContent(
169               HTML::p(HTML::strong(
170                 _("Are you sure you want to permanently rename the selected pages?"))));
171             $header = $this->renameForm($header, $post_args);
172         }
173         else {
174             $button_label = _("Rename selected pages");
175             if (!$post_args and count($pages) == 1) {
176                 list($post_args['from'],) = array_keys($pages);
177                 $post_args['to'] = $post_args['from'];
178             }
179             $header = $this->renameForm($header, $post_args);
180             $header->pushContent(HTML::p(_("Select the pages to rename:")));
181         }
182
183         $buttons = HTML::p(Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
184                            HTML::Raw('&nbsp;&nbsp;'),
185                            Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
186
187         return HTML::form(array('action' => $request->getPostURL(),
188                                 'method' => 'post'),
189                           $header,
190                           $buttons,
191                           $pagelist->getContent(),
192                           HiddenInputs($request->getArgs(),
193                                         false,
194                                         array('admin_rename')),
195                           HiddenInputs(array('admin_rename[action]' => $next_action)),
196                           ENABLE_PAGEPERM
197                           ? ''
198                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
199     }
200
201     function checkBox (&$post_args, $name, $msg) {
202         $id = 'admin_rename-'.$name;
203         $checkbox = HTML::input(array('type' => 'checkbox',
204                                       'name' => 'admin_rename['.$name.']',
205                                       'id'   => $id,
206                                       'value' => 1));
207         if (!empty($post_args[$name]))
208             $checkbox->setAttr('checked', 'checked');
209         return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
210     }
211
212     function renameForm(&$header, $post_args) {
213         $table = HTML::table();
214         $this->_tablePush($table, _("Rename"). " ". _("from").': ',
215                           HTML::input(array('name' => 'admin_rename[from]',
216                                             'size' => 90,
217                                             'value' => $post_args['from'])));
218         $this->_tablePush($table, _("to").': ',
219                           HTML::input(array('name' => 'admin_rename[to]',
220                                             'size' => 90,
221                                             'value' => $post_args['to'])));
222         $this->_tablePush($table, '', $this->checkBox($post_args, 'regex', _("Regex?")));
223         $this->_tablePush($table, '', $this->checkBox($post_args, 'icase', _("Case insensitive?")));
224         if (DEBUG) // not yet stable
225             $this->_tablePush($table, '', $this->checkBox($post_args, 'updatelinks', 
226                                                       _("Change pagename in all linked pages also?")));
227         $header->pushContent($table);
228         return $header;
229     }
230 }
231
232 // TODO: grey out unchangeble pages, even in the initial list also?
233 // TODO: autoselect by matching name javascript in admin_rename[from]
234 // TODO: update rename[] fields when case-sensitive and regex is changed
235
236 // moved from lib/PageList.php
237 class _PageList_Column_renamed_pagename extends _PageList_Column {
238     function _getValue ($page_handle, &$revision_handle) {
239         global $request;
240         $post_args = $request->getArg('admin_rename');
241         $options = array('regex' => @$post_args['regex'],
242                          'icase' => @$post_args['icase']);
243                          
244         $value = $post_args ? WikiPlugin_WikiAdminRename::renameHelper($page_handle->getName(), 
245                                                           $post_args['from'], $post_args['to'],
246                                                           $options)
247                             : $page_handle->getName();                              
248         $div = HTML::div(" => ",HTML::input(array('type' => 'text',
249                                                   'name' => 'rename[]',
250                                                   'value' => $value)));
251         $new_page = $request->getPage($value);
252         if ($new_page->exists()) {
253             $div->setAttr('class','error');
254             $div->setAttr('title',_("This page already exists"));
255         }
256         return $div;
257     }
258 };
259
260 // Local Variables:
261 // mode: php
262 // tab-width: 8
263 // c-basic-offset: 4
264 // c-hanging-comment-ender-p: nil
265 // indent-tabs-mode: nil
266 // End:
267 ?>