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