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