]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRename.php
Several locale related improvements suggested by Pierrick Meignen
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRename.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminRename.php,v 1.15 2004-06-04 20:32:54 rurban Exp $');
3 /*
4  Copyright 2004 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * Usage:   <?plugin WikiAdminRename ?> or called via WikiAdminSelect
25  * @author:  Reini Urban <rurban@x-ray.at>
26  *
27  * TODO: support case-insensitive checkbox
28  *       support regex checkbox and renaming
29  *       fix updatelinks
30  *
31  * KNOWN ISSUES:
32  * Enabled now PagePermissions.
33  * Requires PHP 4.2 so far.
34  */
35 require_once('lib/PageList.php');
36 require_once('lib/plugin/WikiAdminSelect.php');
37
38 class WikiPlugin_WikiAdminRename
39 extends WikiPlugin_WikiAdminSelect
40 {
41     function getName() {
42         return _("WikiAdminRename");
43     }
44
45     function getDescription() {
46         return _("Rename selected pages.");
47     }
48
49     function getVersion() {
50         return preg_replace("/[Revision: $]/", '',
51                             "\$Revision: 1.15 $");
52     }
53
54     function getDefaultArguments() {
55         return array(
56                      's'        => false,
57                      /* Pages to exclude in listing */
58                      'exclude'  => '',
59                      /* Columns to include in listing */
60                      'info'     => 'pagename,mtime',
61                      /* How to sort */
62                      'sortby'   => 'pagename',
63                      'limit'    => 0,
64                      'updatelinks' => 0 // not yet working
65                      );
66     }
67
68     //TODO: regex and case-inexact option
69     function renameHelper($name, $from, $to, $options=false) {
70         return str_replace($from, $to, $name);
71     }
72
73     function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks=false) {
74         $ul = HTML::ul();
75         $count = 0;
76         foreach ($pages as $name) {
77             if ( ($newname = $this->renameHelper($name, $from, $to)) and 
78                  $newname != $name ) {
79                 if ($dbi->isWikiPage($newname))
80                     $ul->pushContent(HTML::li(fmt("Page %s already exists. Ignored.",
81                                                   WikiLink($newname))));
82                 elseif (!mayAccessPage('change', $name))
83                     $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",
84                                                   WikiLink($name))));
85                 elseif ( $dbi->renamePage($name, $newname, $updatelinks)) {
86                     /* not yet implemented for all backends */
87                     $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",
88                                                   $name, WikiLink($newname))));
89                     $count++;
90                 } else {
91                     $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", 
92                                                   $name, $newname)));
93                 }
94             } else {
95                 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", 
96                                               $name, $newname)));
97             }
98         }
99         if ($count) {
100             $dbi->touch();
101             return HTML($ul, HTML::p(fmt("%s pages have been permanently renamed.",
102                                          $count)));
103         } else {
104             return HTML($ul, HTML::p(fmt("No pages renamed.")));
105         }
106     }
107     
108     function run($dbi, $argstr, &$request, $basepage) {
109         if ($request->getArg('action') != 'browse')
110             return $this->disabled("(action != 'browse')");
111         
112         $args = $this->getArgs($argstr, $request);
113         $this->_args = $args;
114         if (!empty($args['exclude']))
115             $exclude = explodePageList($args['exclude']);
116         else
117             $exclude = false;
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']);
152         }
153         if ($next_action == 'verify') {
154             $args['info'] = "checkbox,pagename,renamed_pagename";
155         }
156         $pagelist = new PageList_Selectable
157             (
158              $args['info'], $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::p();
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 files?"))));
171             $header = $this->renameForm($header, $post_args);
172         }
173         else {
174             $button_label = _("Rename selected pages");
175             $header->pushContent(HTML::p(_("Select the pages to rename:")));
176             $header = $this->renameForm($header, $post_args);
177         }
178
179         $buttons = HTML::p(Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
180                            Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
181
182         return HTML::form(array('action' => $request->getPostURL(),
183                                 'method' => 'post'),
184                           $header,
185                           $pagelist->getContent(),
186                           HiddenInputs($request->getArgs(),
187                                         false,
188                                         array('admin_rename')),
189                           HiddenInputs(array('admin_rename[action]' => $next_action)),
190                           ENABLE_PAGEPERM
191                           ? ''
192                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
193                           $buttons);
194     }
195
196     function renameForm(&$header, $post_args) {
197         $header->pushContent(_("Rename")." "._("from").': ');
198         $header->pushContent(HTML::input(array('name' => 'admin_rename[from]',
199                                                'value' => $post_args['from'])));
200         $header->pushContent(' '._("to").': ');
201         $header->pushContent(HTML::input(array('name' => 'admin_rename[to]',
202                                                'value' => $post_args['to'])));
203         $header->pushContent(' '._("(no regex, case-sensitive)"));
204         if (DEBUG) { // not yet tested
205             $header->pushContent(HTML::br());
206             $header->pushContent(_("Change pagename in all linked pages also?"));
207             $header->pushContent(HTML::em(_("(Currently not working)")));
208             $checkbox = HTML::input(array('type' => 'checkbox',
209                                           'name' => 'admin_rename[updatelinks]',
210                                           'value' => 1));
211             if (!empty($post_args['updatelinks']))
212                 $checkbox->setAttr('checked','checked');
213             $header->pushContent($checkbox);
214         }
215         $header->pushContent(HTML::p());
216         return $header;
217     }
218 }
219
220 // moved from lib/PageList.php
221 class _PageList_Column_renamed_pagename extends _PageList_Column {
222     function _getValue ($page_handle, &$revision_handle) {
223         $post_args = $GLOBALS['request']->getArg('admin_rename');
224         $value = str_replace($post_args['from'], $post_args['to'], $page_handle->getName());
225         $div = HTML::div(" => ",HTML::input(array('type' => 'text',
226                                                   'name' => 'rename[]',
227                                                   'value' => $value)));
228         $new_page = $GLOBALS['request']->getPage($value);
229         if ($new_page->exists()) {
230             $div->setAttr('class','error');
231             $div->setAttr('title',_("This page already exists"));
232         }
233         return $div;
234     }
235 };
236
237 // $Log: not supported by cvs2svn $
238 // Revision 1.14  2004/06/03 22:24:48  rurban
239 // reenable admin check on !ENABLE_PAGEPERM, honor s=Wildcard arg, fix warning after Remove
240 //
241 // Revision 1.13  2004/06/03 12:59:41  rurban
242 // simplify translation
243 // NS4 wrap=virtual only
244 //
245 // Revision 1.12  2004/06/01 15:28:01  rurban
246 // AdminUser only ADMIN_USER not member of Administrators
247 // some RateIt improvements by dfrankow
248 // edit_toolbar buttons
249 //
250 // Revision 1.11  2004/05/24 17:34:53  rurban
251 // use ACLs
252 //
253 // Revision 1.10  2004/04/06 20:00:11  rurban
254 // Cleanup of special PageList column types
255 // Added support of plugin and theme specific Pagelist Types
256 // Added support for theme specific UserPreferences
257 // Added session support for ip-based throttling
258 //   sql table schema change: ALTER TABLE session ADD sess_ip CHAR(15);
259 // Enhanced postgres schema
260 // Added DB_Session_dba support
261 //
262 // Revision 1.9  2004/03/12 13:31:43  rurban
263 // enforce PagePermissions, errormsg if not Admin
264 //
265 // Revision 1.8  2004/03/01 13:48:46  rurban
266 // rename fix
267 // p[] consistency fix
268 //
269 // Revision 1.7  2004/02/22 23:20:33  rurban
270 // fixed DumpHtmlToDir,
271 // enhanced sortby handling in PageList
272 //   new button_heading th style (enabled),
273 // added sortby and limit support to the db backends and plugins
274 //   for paging support (<<prev, next>> links on long lists)
275 //
276 // Revision 1.6  2004/02/17 12:11:36  rurban
277 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
278 //
279 // Revision 1.5  2004/02/15 21:34:37  rurban
280 // PageList enhanced and improved.
281 // fixed new WikiAdmin... plugins
282 // editpage, Theme with exp. htmlarea framework
283 //   (htmlarea yet committed, this is really questionable)
284 // WikiUser... code with better session handling for prefs
285 // enhanced UserPreferences (again)
286 // RecentChanges for show_deleted: how should pages be deleted then?
287 //
288 // Revision 1.4  2004/02/12 17:05:39  rurban
289 // WikiAdminRename:
290 //   added "Change pagename in all linked pages also"
291 // PageList:
292 //   added javascript toggle for Select
293 // WikiAdminSearchReplace:
294 //   fixed another typo
295 //
296 // Revision 1.3  2004/02/12 13:05:50  rurban
297 // Rename functional for PearDB backend
298 // some other minor changes
299 // SiteMap comes with a not yet functional feature request: includepages (tbd)
300 //
301 // Revision 1.2  2004/02/12 11:45:11  rurban
302 // only WikiDB method missing
303 //
304 // Revision 1.1  2004/02/11 20:00:16  rurban
305 // WikiAdmin... series overhaul. Rename misses the db backend methods yet. Chmod + Chwon still missing.
306 //
307
308 // Local Variables:
309 // mode: php
310 // tab-width: 8
311 // c-basic-offset: 4
312 // c-hanging-comment-ender-p: nil
313 // indent-tabs-mode: nil
314 // End:
315 ?>