]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminChown.php
new support for arguments owner, author, creator in most relevant
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminChown.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminChown.php,v 1.4 2004-06-13 15:33:20 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 WikiAdminChown s||=* ?> or called via WikiAdminSelect
25  * @author:  Reini Urban <rurban@x-ray.at>
26  *
27  * KNOWN ISSUES:
28  * Requires PHP 4.2 so far.
29  */
30 require_once('lib/PageList.php');
31 require_once('lib/plugin/WikiAdminSelect.php');
32
33 class WikiPlugin_WikiAdminChown
34 extends WikiPlugin_WikiAdminSelect
35 {
36     function getName() {
37         return _("WikiAdminChown");
38     }
39
40     function getDescription() {
41         return _("Chown selected pages.");
42     }
43
44     function getVersion() {
45         return preg_replace("/[Revision: $]/", '',
46                             "\$Revision: 1.4 $");
47     }
48
49     function getDefaultArguments() {
50         return array(
51                      's'        => false,
52                      'user'     => false,
53                      /* select pages by meta-data: */
54                      'author'   => false,
55                      'owner'    => false,
56                      'creator'  => false,
57                      /* Pages to exclude in listing */
58                      'exclude'  => '',
59                      /* Columns to include in listing */
60                      'info'     => 'pagename,owner,mtime',
61                      /* How to sort */
62                      'sortby'   => 'pagename',
63                      'limit'    => 0,
64                      );
65     }
66
67     function chownPages(&$dbi, &$request, $pages, $newowner) {
68         $ul = HTML::ul();
69         $count = 0;
70         foreach ($pages as $name) {
71             $page = $dbi->getPage($name);
72             if ( ($owner = $page->getOwner()) and 
73                  $newowner != $owner ) {
74                 if (!mayAccessPage('change', $name)) {
75                     $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",
76                                                   WikiLink($name))));
77                 } else {
78                     $page->set('owner', $newowner);
79                     if ($page->get('owner') === $newowner) {
80                         $ul->pushContent(HTML::li(fmt("Chown page '%s' to '%s'.",
81                                                       WikiLink($name), WikiLink($newowner))));
82                         $count++;
83                     } else {
84                         $ul->pushContent(HTML::li(fmt("Couldn't chown page '%s' to '%s'.", 
85                                                       WikiLink($name), $newowner)));
86                     }
87                 }
88             }
89         }
90         if ($count) {
91             $dbi->touch();
92             return HTML($ul, HTML::p(fmt("%s pages have been permanently changed.",
93                                          $count)));
94         } else {
95             return HTML($ul, HTML::p(fmt("No pages changed.")));
96         }
97     }
98     
99     function run($dbi, $argstr, &$request, $basepage) {
100         if ($request->getArg('action') != 'browse')
101             if (!$request->getArg('action') == _("PhpWikiAdministration/Chown"))
102                 return $this->disabled("(action != 'browse')");
103         
104         $args = $this->getArgs($argstr, $request);
105         $this->_args = $args;
106         if (empty($args['user']))
107             $args['user'] = $request->_user->UserName();
108         if (!empty($args['exclude']))
109             $exclude = explodePageList($args['exclude']);
110         else
111             $exclude = false;
112         $this->preSelectS(&$args, &$request);
113
114         $p = $request->getArg('p');
115         if (!$p) $p = $this->_list;
116         $post_args = $request->getArg('admin_chown');
117         if (!$request->isPost() and empty($post_args['user']))
118             $post_args['user'] = $args['user'];
119         $next_action = 'select';
120         $pages = array();
121         if ($p && !$request->isPost())
122             $pages = $p;
123         if ($p && $request->isPost() &&
124             !empty($post_args['chown']) && empty($post_args['cancel'])) {
125             // without individual PagePermissions:
126             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
127                 $request->_notAuthorized(WIKIAUTH_ADMIN);
128                 $this->disabled("! user->isAdmin");
129             }
130             // DONE: error message if not allowed.
131             if ($post_args['action'] == 'verify') {
132                 // Real action
133                 return $this->chownPages($dbi, $request, array_keys($p), 
134                                           $post_args['user']);
135             }
136             if ($post_args['action'] == 'select') {
137                 if (!empty($post_args['user']))
138                     $next_action = 'verify';
139                 foreach ($p as $name => $c) {
140                     $pages[$name] = 1;
141                 }
142             }
143         }
144         if ($next_action == 'select' and empty($pages)) {
145             // List all pages to select from.
146             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit']);
147         }
148         /* // let the user decide which info
149          if ($next_action == 'verify') {
150             $args['info'] = "checkbox,pagename,owner,mtime";
151         }
152         */
153         $pagelist = new PageList_Selectable($args['info'], $exclude);
154         $pagelist->addPageList($pages);
155
156         $header = HTML::p();
157         if ($next_action == 'verify') {
158             $button_label = _("Yes");
159             $header->pushContent(
160               HTML::p(HTML::strong(
161                 _("Are you sure you want to permanently chown the selected files?"))));
162             $header = $this->chownForm($header, $post_args);
163         }
164         else {
165             $button_label = _("Chown selected pages");
166             $header->pushContent(HTML::p(_("Select the pages to change the owner:")));
167             $header = $this->chownForm($header, $post_args);
168         }
169
170         $buttons = HTML::p(Button('submit:admin_chown[chown]', $button_label, 'wikiadmin'),
171                            Button('submit:admin_chown[cancel]', _("Cancel"), 'button'));
172
173         return HTML::form(array('action' => $request->getPostURL(),
174                                 'method' => 'post'),
175                           $header,
176                           $pagelist->getContent(),
177                           HiddenInputs($request->getArgs(),
178                                         false,
179                                         array('admin_chown')),
180                           HiddenInputs(array('admin_chown[action]' => $next_action)),
181                           ENABLE_PAGEPERM
182                           ? ''
183                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
184                           $buttons);
185     }
186
187     function chownForm(&$header, $post_args) {
188         $header->pushContent(_("Chown")." ");
189         $header->pushContent(' '._("to").': ');
190         $header->pushContent(HTML::input(array('name' => 'admin_chown[user]',
191                                                'value' => $post_args['user'])));
192         $header->pushContent(HTML::p());
193         return $header;
194     }
195 }
196
197 // $Log: not supported by cvs2svn $
198 // Revision 1.3  2004/06/08 10:05:11  rurban
199 // simplified admin action shortcuts
200 //
201 // Revision 1.2  2004/06/07 18:59:42  rurban
202 // added Chown link to Owner in statusbar
203 //
204 // Revision 1.1  2004/06/07 17:58:58  rurban
205 // new chown plugin
206 //
207 //
208
209 // Local Variables:
210 // mode: php
211 // tab-width: 8
212 // c-basic-offset: 4
213 // c-hanging-comment-ender-p: nil
214 // indent-tabs-mode: nil
215 // End:
216 ?>