]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRename.php
only WikiDB method missing
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRename.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminRename.php,v 1.2 2004-02-12 11:45:11 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.2 $");
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 $newname != $name) {
71                 //Fixme!
72                 break;
73                 $dbi->renamePage($name,$newname); /* not yet implemented */
74                 $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.", $name, $newname)));
75                 $count++;
76             } elseif (! $newname) {
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                         HTML::p(HTML::em(fmt("WikiDB::renamePage(\"%s\",\"%s\") not yet implemented for this backend.",$name,$newname))));
85         } else {
86             return HTML($ul,
87                         HTML::p(fmt("No pages renamed.")),
88                         HTML::p(HTML::em(fmt("WikiDB::renamePage(\"%s\",\"%s\") not yet implemented for this backend.",$name,$newname))));
89         }
90     }
91     
92     function run($dbi, $argstr, $request) {
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
104         $p = $request->getArg('p');
105         $post_args = $request->getArg('admin_rename');
106         $next_action = 'select';
107         $pages = array();
108         
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, $p, $post_args['from'], $post_args['to']);
115             }
116
117             if ($post_args['action'] == 'select') {
118                 if (!empty($post_args['from']))
119                     $next_action = 'verify';
120                 foreach ($p as $name) {
121                     $pages[$name] = 1;
122                 }
123             }
124         }
125         if ($next_action == 'select') {
126             // List all pages to select from.
127             $list = $this->collectPages($pages, $dbi, $args['sortby']);
128         }
129
130
131         $info = 'checkbox';
132         if ($args['info'])
133             $info .= "," . $args['info'];
134         if ($next_action == 'verify') {
135             $info = "checkbox,pagename,renamed_pagename";
136         }
137         $pagelist = new PageList_Selectable($info, $exclude);
138         $pagelist->addPageList($pages);
139
140         $header = HTML::p();
141         if ($next_action == 'verify') {
142             $button_label = _("Yes");
143             $header->pushContent(
144               HTML::p(HTML::strong(
145                                    _("Are you sure you want to permanently rename the selected files?"))));
146             $header = $this->renameForm($header, $post_args);
147         }
148         else {
149             $button_label = _("Rename selected pages");
150             $header->pushContent(HTML::p(_("Select the pages to rename:")));
151             $header = $this->renameForm($header, $post_args);
152         }
153
154
155         $buttons = HTML::p(Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
156                            Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
157
158         return HTML::form(array('action' => $request->getPostURL(),
159                                 'method' => 'post'),
160                           $header,
161                           $pagelist->getContent(),
162                           HiddenInputs($request->getArgs(),
163                                         false,
164                                         array('admin_rename')),
165                           HiddenInputs(array('admin_rename[action]' => $next_action,
166                                              'require_authority_for_post' => WIKIAUTH_ADMIN)),
167                           $buttons);
168     }
169
170     function renameForm(&$header, $post_args) {
171         $header->pushContent(_("Rename")." "._("from").': ');
172         $header->pushContent(HTML::input(array('name' => 'admin_rename[from]',
173                                                'value' => $post_args['from'])));
174         $header->pushContent(' '._("to").': ');
175         $header->pushContent(HTML::input(array('name' => 'admin_rename[to]',
176                                                'value' => $post_args['to'])));
177         $header->pushContent(' '._("(no regex, case-sensitive)"));
178         $header->pushContent(HTML::p());
179         return $header;
180     }
181
182 }
183
184 // $Log: not supported by cvs2svn $
185 // Revision 1.1  2004/02/11 20:00:16  rurban
186 // WikiAdmin... series overhaul. Rename misses the db backend methods yet. Chmod + Chwon still missing.
187 //
188
189 // Local Variables:
190 // mode: php
191 // tab-width: 8
192 // c-basic-offset: 4
193 // c-hanging-comment-ender-p: nil
194 // indent-tabs-mode: nil
195 // End:
196 ?>