]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRename.php
renamed global $Theme to $WikiTheme (gforge nameclash)
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRename.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminRename.php,v 1.19 2004-06-14 11:31:39 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.19 $");
52     }
53
54     function getDefaultArguments() {
55         return array_merge
56             (
57              PageList::supportedArgs(),
58              array(
59                      's'        => false,
60                      /* Columns to include in listing */
61                      'info'     => 'pagename,mtime',
62                      'updatelinks' => 0
63                      ));
64     }
65
66     //TODO: regex and case-inexact option
67     function renameHelper($name, $from, $to, $options=false) {
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         foreach ($pages as $name) {
75             if ($updatelinks) {
76                 $oldpage = $dbi->getPage($from);
77                 require_once('lib/plugin/WikiAdminSearchReplace.php');
78                 //$newpage = $dbi->getPage($to);
79                 $links = $oldpage->getBackLinks();
80                 while ($linked_page = $links->next()) {
81                     if (WikiPlugin_WikiAdminSearchReplace::replaceHelper($dbi,$linked_page->getName(),$from,$to))
82                         $ul->pushContent(HTML::li(fmt("Replaced link in %s.",
83                                                       WikiLink($linked_page->getName()))));
84                 }
85                 $links = $oldpage->getPageLinks();
86                 while ($linked_page = $links->next()) {
87                     WikiPlugin_WikiAdminSearchReplace::replaceHelper($dbi,$linked_page->getName(),$from,$to);
88                     $ul->pushContent(HTML::li(fmt("Replaced link in %s.",
89                                                   WikiLink($linked_page->getName()))));
90                 }
91             }
92             if ( ($newname = $this->renameHelper($name, $from, $to)) and 
93                  $newname != $name ) {
94                 if ($dbi->isWikiPage($newname))
95                     $ul->pushContent(HTML::li(fmt("Page %s already exists. Ignored.",
96                                                   WikiLink($newname))));
97                 elseif (!mayAccessPage('change', $name))
98                     $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",
99                                                   WikiLink($name))));
100                 elseif ( $dbi->renamePage($name, $newname, $updatelinks)) {
101                     /* not yet implemented for all backends */
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             return HTML($ul, HTML::p(fmt("%s pages have been permanently renamed.",
117                                          $count)));
118         } else {
119             return HTML($ul, HTML::p(fmt("No pages renamed.")));
120         }
121     }
122     
123     function run($dbi, $argstr, &$request, $basepage) {
124         if ($request->getArg('action') != 'browse')
125             if ($request->getArg('action') != _("PhpWikiAdministration/Rename"))
126                 return $this->disabled("(action != 'browse')");
127         
128         $args = $this->getArgs($argstr, $request);
129         $this->_args = $args;
130         if (!empty($args['exclude']))
131             $exclude = explodePageList($args['exclude']);
132         else
133             $exclude = false;
134         $this->preSelectS(&$args, &$request);
135
136         $p = $request->getArg('p');
137         if (!$p) $p = $this->_list;
138         $post_args = $request->getArg('admin_rename');
139         $next_action = 'select';
140         $pages = array();
141         if ($p && !$request->isPost())
142             $pages = $p;
143         if ($p && $request->isPost() &&
144             !empty($post_args['rename']) && empty($post_args['cancel'])) {
145             // without individual PagePermissions:
146             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
147                 $request->_notAuthorized(WIKIAUTH_ADMIN);
148                 $this->disabled("! user->isAdmin");
149             }
150             // DONE: error message if not allowed.
151             if ($post_args['action'] == 'verify') {
152                 // Real action
153                 return $this->renamePages($dbi, $request, array_keys($p), 
154                                           trim($post_args['from']), trim($post_args['to']), 
155                                           !empty($post_args['updatelinks']));
156             }
157             if ($post_args['action'] == 'select') {
158                 if (!empty($post_args['from']))
159                     $next_action = 'verify';
160                 foreach ($p as $name => $c) {
161                     $pages[$name] = 1;
162                 }
163             }
164         }
165         if ($next_action == 'select' and empty($pages)) {
166             // List all pages to select from.
167             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit']);
168         }
169         if ($next_action == 'verify') {
170             $args['info'] = "checkbox,pagename,renamed_pagename";
171         }
172         $pagelist = new PageList_Selectable
173             (
174              $args['info'], $exclude, 
175              array('types' => 
176                    array('renamed_pagename'
177                          => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
178                          )));
179         $pagelist->addPageList($pages);
180
181         $header = HTML::p();
182         if ($next_action == 'verify') {
183             $button_label = _("Yes");
184             $header->pushContent(
185               HTML::p(HTML::strong(
186                 _("Are you sure you want to permanently rename the selected files?"))));
187             $header = $this->renameForm($header, $post_args);
188         }
189         else {
190             $button_label = _("Rename selected pages");
191             $header->pushContent(HTML::p(_("Select the pages to rename:")));
192             $header = $this->renameForm($header, $post_args);
193         }
194
195         $buttons = HTML::p(Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
196                            Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
197
198         return HTML::form(array('action' => $request->getPostURL(),
199                                 'method' => 'post'),
200                           $header,
201                           $pagelist->getContent(),
202                           HiddenInputs($request->getArgs(),
203                                         false,
204                                         array('admin_rename')),
205                           HiddenInputs(array('admin_rename[action]' => $next_action)),
206                           ENABLE_PAGEPERM
207                           ? ''
208                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
209                           $buttons);
210     }
211
212     function renameForm(&$header, $post_args) {
213         $header->pushContent(_("Rename")." "._("from").': ');
214         $header->pushContent(HTML::input(array('name' => 'admin_rename[from]',
215                                                'value' => trim($post_args['from']))));
216         $header->pushContent(' '._("to").': ');
217         $header->pushContent(HTML::input(array('name' => 'admin_rename[to]',
218                                                'value' => trim($post_args['to']))));
219         $header->pushContent(' '._("(no regex, case-sensitive)"));
220         //if (DEBUG) { // not yet tested
221         $header->pushContent(HTML::br());
222         $checkbox = HTML::input(array('type' => 'checkbox',
223                                           'name' => 'admin_rename[updatelinks]',
224                                           'value' => 1));
225         if (!empty($post_args['updatelinks']))
226             $checkbox->setAttr('checked','checked');
227         $header->pushContent($checkbox);
228         $header->pushContent(_("Change pagename in all linked pages also?"));
229         //$header->pushContent(HTML::em(_("(Currently not working)")));
230         //}
231         $header->pushContent(HTML::p());
232         return $header;
233     }
234 }
235
236 // moved from lib/PageList.php
237 class _PageList_Column_renamed_pagename extends _PageList_Column {
238     function _getValue ($page_handle, &$revision_handle) {
239         $post_args = $GLOBALS['request']->getArg('admin_rename');
240         $value = str_replace($post_args['from'], $post_args['to'], $page_handle->getName());
241         $div = HTML::div(" => ",HTML::input(array('type' => 'text',
242                                                   'name' => 'rename[]',
243                                                   'value' => $value)));
244         $new_page = $GLOBALS['request']->getPage($value);
245         if ($new_page->exists()) {
246             $div->setAttr('class','error');
247             $div->setAttr('title',_("This page already exists"));
248         }
249         return $div;
250     }
251 };
252
253 // $Log: not supported by cvs2svn $
254 // Revision 1.18  2004/06/13 15:33:20  rurban
255 // new support for arguments owner, author, creator in most relevant
256 // PageList plugins. in WikiAdmin* via preSelectS()
257 //
258 // Revision 1.17  2004/06/08 10:05:12  rurban
259 // simplified admin action shortcuts
260 //
261 // Revision 1.16  2004/06/07 18:57:31  rurban
262 // fix rename: Change pagename in all linked pages
263 //
264 // Revision 1.15  2004/06/04 20:32:54  rurban
265 // Several locale related improvements suggested by Pierrick Meignen
266 // LDAP fix by John Cole
267 // reanable admin check without ENABLE_PAGEPERM in the admin plugins
268 //
269 // Revision 1.14  2004/06/03 22:24:48  rurban
270 // reenable admin check on !ENABLE_PAGEPERM, honor s=Wildcard arg, fix warning after Remove
271 //
272 // Revision 1.13  2004/06/03 12:59:41  rurban
273 // simplify translation
274 // NS4 wrap=virtual only
275 //
276 // Revision 1.12  2004/06/01 15:28:01  rurban
277 // AdminUser only ADMIN_USER not member of Administrators
278 // some RateIt improvements by dfrankow
279 // edit_toolbar buttons
280 //
281 // Revision 1.11  2004/05/24 17:34:53  rurban
282 // use ACLs
283 //
284 // Revision 1.10  2004/04/06 20:00:11  rurban
285 // Cleanup of special PageList column types
286 // Added support of plugin and theme specific Pagelist Types
287 // Added support for theme specific UserPreferences
288 // Added session support for ip-based throttling
289 //   sql table schema change: ALTER TABLE session ADD sess_ip CHAR(15);
290 // Enhanced postgres schema
291 // Added DB_Session_dba support
292 //
293 // Revision 1.9  2004/03/12 13:31:43  rurban
294 // enforce PagePermissions, errormsg if not Admin
295 //
296 // Revision 1.8  2004/03/01 13:48:46  rurban
297 // rename fix
298 // p[] consistency fix
299 //
300 // Revision 1.7  2004/02/22 23:20:33  rurban
301 // fixed DumpHtmlToDir,
302 // enhanced sortby handling in PageList
303 //   new button_heading th style (enabled),
304 // added sortby and limit support to the db backends and plugins
305 //   for paging support (<<prev, next>> links on long lists)
306 //
307 // Revision 1.6  2004/02/17 12:11:36  rurban
308 // 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, ...)
309 //
310 // Revision 1.5  2004/02/15 21:34:37  rurban
311 // PageList enhanced and improved.
312 // fixed new WikiAdmin... plugins
313 // editpage, Theme with exp. htmlarea framework
314 //   (htmlarea yet committed, this is really questionable)
315 // WikiUser... code with better session handling for prefs
316 // enhanced UserPreferences (again)
317 // RecentChanges for show_deleted: how should pages be deleted then?
318 //
319 // Revision 1.4  2004/02/12 17:05:39  rurban
320 // WikiAdminRename:
321 //   added "Change pagename in all linked pages also"
322 // PageList:
323 //   added javascript toggle for Select
324 // WikiAdminSearchReplace:
325 //   fixed another typo
326 //
327 // Revision 1.3  2004/02/12 13:05:50  rurban
328 // Rename functional for PearDB backend
329 // some other minor changes
330 // SiteMap comes with a not yet functional feature request: includepages (tbd)
331 //
332 // Revision 1.2  2004/02/12 11:45:11  rurban
333 // only WikiDB method missing
334 //
335 // Revision 1.1  2004/02/11 20:00:16  rurban
336 // WikiAdmin... series overhaul. Rename misses the db backend methods yet. Chmod + Chwon still missing.
337 //
338
339 // Local Variables:
340 // mode: php
341 // tab-width: 8
342 // c-basic-offset: 4
343 // c-hanging-comment-ender-p: nil
344 // indent-tabs-mode: nil
345 // End:
346 ?>