]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRename.php
Rename functional for PearDB backend
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRename.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminRename.php,v 1.3 2004-02-12 13:05:50 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.3 $");
49     }
50
51     function getDefaultArguments() {
52         return array(
53                      /* Pages to exclude */
54                      'exclude'  => '',
55                      /* Columns to include in listing */
56                      'info'     => 'pagename,mtime',
57                      /* How to sort */
58                      'sortby'   => 'pagename',
59                      );
60     }
61
62     function renameHelper($name, $from, $to) {
63         return str_replace($from,$to,$name);
64     }
65
66     function renamePages(&$dbi, &$request, $pages, $from, $to) {
67         $ul = HTML::ul();
68         $count = 0;
69         foreach ($pages as $name) {
70             if ( ($newname = $this->renameHelper($name,$from,$to)) and 
71                   $newname != $name and
72                   $dbi->renamePage($name,$newname) ) {
73                 /* not yet implemented for all backends */
74                 $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",$name,WikiLink($newname))));
75                 $count++;
76             } else {
77                 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", $name, $newname)));
78             }
79         }
80         if ($count) {
81             $dbi->touch();
82             return HTML($ul,
83                         HTML::p(fmt("%s pages have been permanently renamed.",$count)));
84         } else {
85             return HTML($ul,
86                         HTML::p(fmt("No pages renamed.")));
87         }
88     }
89     
90     function run($dbi, $argstr, $request) {
91         if ($request->getArg('action') != 'browse')
92             return $this->disabled("(action != 'browse')");
93         
94         $args = $this->getArgs($argstr, $request);
95         $this->_args = $args;
96         if (!empty($args['exclude']))
97             $exclude = explodePageList($args['exclude']);
98         else
99             $exclude = false;
100
101
102         $p = $request->getArg('p');
103         $post_args = $request->getArg('admin_rename');
104         $next_action = 'select';
105         $pages = array();
106         
107         if ($p && $request->isPost() && $request->_user->isAdmin()
108             && !empty($post_args['rename']) && empty($post_args['cancel'])) {
109             // FIXME: error message if not admin.
110             if ($post_args['action'] == 'verify') {
111                 // Real action
112                 return $this->renamePages($dbi, $request, $p, $post_args['from'], $post_args['to']);
113             }
114
115             if ($post_args['action'] == 'select') {
116                 if (!empty($post_args['from']))
117                     $next_action = 'verify';
118                 foreach ($p as $name) {
119                     $pages[$name] = 1;
120                 }
121             }
122         }
123         if ($next_action == 'select') {
124             // List all pages to select from.
125             $list = $this->collectPages($pages, $dbi, $args['sortby']);
126         }
127
128
129         $info = 'checkbox';
130         if ($args['info'])
131             $info .= "," . $args['info'];
132         if ($next_action == 'verify') {
133             $info = "checkbox,pagename,renamed_pagename";
134         }
135         $pagelist = new PageList_Selectable($info, $exclude);
136         $pagelist->addPageList($pages);
137
138         $header = HTML::p();
139         if ($next_action == 'verify') {
140             $button_label = _("Yes");
141             $header->pushContent(
142               HTML::p(HTML::strong(
143                                    _("Are you sure you want to permanently rename the selected files?"))));
144             $header = $this->renameForm($header, $post_args);
145         }
146         else {
147             $button_label = _("Rename selected pages");
148             $header->pushContent(HTML::p(_("Select the pages to rename:")));
149             $header = $this->renameForm($header, $post_args);
150         }
151
152
153         $buttons = HTML::p(Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
154                            Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
155
156         return HTML::form(array('action' => $request->getPostURL(),
157                                 'method' => 'post'),
158                           $header,
159                           $pagelist->getContent(),
160                           HiddenInputs($request->getArgs(),
161                                         false,
162                                         array('admin_rename')),
163                           HiddenInputs(array('admin_rename[action]' => $next_action,
164                                              'require_authority_for_post' => WIKIAUTH_ADMIN)),
165                           $buttons);
166     }
167
168     function renameForm(&$header, $post_args) {
169         $header->pushContent(_("Rename")." "._("from").': ');
170         $header->pushContent(HTML::input(array('name' => 'admin_rename[from]',
171                                                'value' => $post_args['from'])));
172         $header->pushContent(' '._("to").': ');
173         $header->pushContent(HTML::input(array('name' => 'admin_rename[to]',
174                                                'value' => $post_args['to'])));
175         $header->pushContent(' '._("(no regex, case-sensitive)"));
176         $header->pushContent(HTML::p());
177         return $header;
178     }
179
180 }
181
182 // $Log: not supported by cvs2svn $
183 // Revision 1.2  2004/02/12 11:45:11  rurban
184 // only WikiDB method missing
185 //
186 // Revision 1.1  2004/02/11 20:00:16  rurban
187 // WikiAdmin... series overhaul. Rename misses the db backend methods yet. Chmod + Chwon still missing.
188 //
189
190 // Local Variables:
191 // mode: php
192 // tab-width: 8
193 // c-basic-offset: 4
194 // c-hanging-comment-ender-p: nil
195 // indent-tabs-mode: nil
196 // End:
197 ?>