]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRename.php
rename fix
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRename.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminRename.php,v 1.8 2004-03-01 13:48:46 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  * KNOWN ISSUES:
28  * Currently we must be Admin.
29  * Future versions will support PagePermissions.
30  * requires PHP 4.2 so far.
31  */
32 require_once('lib/PageList.php');
33 require_once('lib/plugin/WikiAdminSelect.php');
34
35 class WikiPlugin_WikiAdminRename
36 extends WikiPlugin_WikiAdminSelect
37 {
38     function getName() {
39         return _("WikiAdminRename");
40     }
41
42     function getDescription() {
43         return _("Rename selected pages.");
44     }
45
46     function getVersion() {
47         return preg_replace("/[Revision: $]/", '',
48                             "\$Revision: 1.8 $");
49     }
50
51     function getDefaultArguments() {
52         return array(
53                      /* Pages to exclude in listing */
54                      'exclude'  => '',
55                      /* Columns to include in listing */
56                      'info'     => 'pagename,mtime',
57                      /* How to sort */
58                      'sortby'   => 'pagename',
59                      'limit'    => 0,
60                      'updatelinks' => 0
61                      );
62     }
63
64     function renameHelper($name, $from, $to) {
65         return str_replace($from,$to,$name);
66     }
67
68     function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks=false) {
69         $ul = HTML::ul();
70         $count = 0;
71         foreach ($pages as $name) {
72             if ( ($newname = $this->renameHelper($name,$from,$to)) and 
73                   $newname != $name and
74                  $dbi->renamePage($name,$newname,$updatelinks) ) {
75                 /* not yet implemented for all backends */
76                 $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",$name,WikiLink($newname))));
77                 $count++;
78             } else {
79                 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", $name, $newname)));
80             }
81         }
82         if ($count) {
83             $dbi->touch();
84             return HTML($ul,
85                         HTML::p(fmt("%s pages have been permanently renamed.",$count)));
86         } else {
87             return HTML($ul,
88                         HTML::p(fmt("No pages renamed.")));
89         }
90     }
91     
92     function run($dbi, $argstr, &$request, $basepage) {
93         if ($request->getArg('action') != 'browse')
94             return $this->disabled("(action != 'browse')");
95         
96         $args = $this->getArgs($argstr, $request);
97         $this->_args = $args;
98         if (!empty($args['exclude']))
99             $exclude = explodePageList($args['exclude']);
100         else
101             $exclude = false;
102
103         $p = $request->getArg('p');
104         $post_args = $request->getArg('admin_rename');
105         $next_action = 'select';
106         $pages = array();
107         if ($p && !$request->isPost())
108             $pages = $p;
109         if ($p && $request->isPost() && $request->_user->isAdmin()
110             && !empty($post_args['rename']) && empty($post_args['cancel'])) {
111             // FIXME: error message if not admin.
112             if ($post_args['action'] == 'verify') {
113                 // Real action
114                 return $this->renamePages($dbi, $request, array_keys($p), 
115                                           $post_args['from'], $post_args['to'], !empty($post_args['updatelinks']));
116             }
117             if ($post_args['action'] == 'select') {
118                 if (!empty($post_args['from']))
119                     $next_action = 'verify';
120                 foreach ($p as $name => $c) {
121                     $pages[$name] = 1;
122                 }
123             }
124         }
125         if ($next_action == 'select' and empty($pages)) {
126             // List all pages to select from.
127             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit']);
128         }
129         if ($next_action == 'verify') {
130             $args['info'] = "checkbox,pagename,renamed_pagename";
131         }
132         $pagelist = new PageList_Selectable($args['info'], $exclude);
133         $pagelist->addPageList($pages);
134
135         $header = HTML::p();
136         if ($next_action == 'verify') {
137             $button_label = _("Yes");
138             $header->pushContent(
139               HTML::p(HTML::strong(
140                                    _("Are you sure you want to permanently rename the selected files?"))));
141             $header = $this->renameForm($header, $post_args);
142         }
143         else {
144             $button_label = _("Rename selected pages");
145             $header->pushContent(HTML::p(_("Select the pages to rename:")));
146             $header = $this->renameForm($header, $post_args);
147         }
148
149
150         $buttons = HTML::p(Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
151                            Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
152
153         return HTML::form(array('action' => $request->getPostURL(),
154                                 'method' => 'post'),
155                           $header,
156                           $pagelist->getContent(),
157                           HiddenInputs($request->getArgs(),
158                                         false,
159                                         array('admin_rename')),
160                           HiddenInputs(array('admin_rename[action]' => $next_action,
161                                              'require_authority_for_post' => WIKIAUTH_ADMIN)),
162                           $buttons);
163     }
164
165     function renameForm(&$header, $post_args) {
166         $header->pushContent(_("Rename")." "._("from").': ');
167         $header->pushContent(HTML::input(array('name' => 'admin_rename[from]',
168                                                'value' => $post_args['from'])));
169         $header->pushContent(' '._("to").': ');
170         $header->pushContent(HTML::input(array('name' => 'admin_rename[to]',
171                                                'value' => $post_args['to'])));
172         $header->pushContent(' '._("(no regex, case-sensitive)"));
173         if (1) { // not yet tested
174             $header->pushContent(HTML::br());
175             $header->pushContent(_("Change pagename in all linked pages also?"));
176             $checkbox = HTML::input(array('type' => 'checkbox',
177                                           'name' => 'admin_rename[updatelinks]',
178                                           'value' => 1));
179             if (!empty($post_args['updatelinks']))
180                 $checkbox->setAttr('checked','checked');
181             $header->pushContent($checkbox);
182         }
183         $header->pushContent(HTML::p());
184         return $header;
185     }
186
187 }
188
189 // $Log: not supported by cvs2svn $
190 // Revision 1.7  2004/02/22 23:20:33  rurban
191 // fixed DumpHtmlToDir,
192 // enhanced sortby handling in PageList
193 //   new button_heading th style (enabled),
194 // added sortby and limit support to the db backends and plugins
195 //   for paging support (<<prev, next>> links on long lists)
196 //
197 // Revision 1.6  2004/02/17 12:11:36  rurban
198 // 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, ...)
199 //
200 // Revision 1.5  2004/02/15 21:34:37  rurban
201 // PageList enhanced and improved.
202 // fixed new WikiAdmin... plugins
203 // editpage, Theme with exp. htmlarea framework
204 //   (htmlarea yet committed, this is really questionable)
205 // WikiUser... code with better session handling for prefs
206 // enhanced UserPreferences (again)
207 // RecentChanges for show_deleted: how should pages be deleted then?
208 //
209 // Revision 1.4  2004/02/12 17:05:39  rurban
210 // WikiAdminRename:
211 //   added "Change pagename in all linked pages also"
212 // PageList:
213 //   added javascript toggle for Select
214 // WikiAdminSearchReplace:
215 //   fixed another typo
216 //
217 // Revision 1.3  2004/02/12 13:05:50  rurban
218 // Rename functional for PearDB backend
219 // some other minor changes
220 // SiteMap comes with a not yet functional feature request: includepages (tbd)
221 //
222 // Revision 1.2  2004/02/12 11:45:11  rurban
223 // only WikiDB method missing
224 //
225 // Revision 1.1  2004/02/11 20:00:16  rurban
226 // WikiAdmin... series overhaul. Rename misses the db backend methods yet. Chmod + Chwon still missing.
227 //
228
229 // Local Variables:
230 // mode: php
231 // tab-width: 8
232 // c-basic-offset: 4
233 // c-hanging-comment-ender-p: nil
234 // indent-tabs-mode: nil
235 // End:
236 ?>