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