]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminChown.php
No need for empty <p>
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminChown.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  Copyright 2004 $ThePhpWikiProgrammingTeam
5  Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
6
7  This file is part of PhpWiki.
8
9  PhpWiki is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  PhpWiki is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with PhpWiki; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /**
25  * Usage:   <?plugin WikiAdminChown s||=* ?> or called via WikiAdminSelect
26  * @author:  Reini Urban <rurban@x-ray.at>
27  *
28  * KNOWN ISSUES:
29  * Requires PHP 4.2 so far.
30  */
31 require_once('lib/PageList.php');
32 require_once('lib/plugin/WikiAdminSelect.php');
33
34 class WikiPlugin_WikiAdminChown
35 extends WikiPlugin_WikiAdminSelect
36 {
37     function getName() {
38         return _("WikiAdminChown");
39     }
40
41     function getDescription() {
42         return _("Change owner of selected pages.");
43     }
44
45     function getVersion() {
46         return preg_replace("/[Revision: $]/", '',
47                             "\$Revision$");
48     }
49
50     function getDefaultArguments() {
51         return array_merge 
52             (
53              PageList::supportedArgs(),
54              array(
55                    's'          => false,
56                    'user'       => false,
57                    /* Columns to include in listing */
58                    'info'     => 'pagename,owner,mtime',
59                    ));
60     }
61
62     function chownPages(&$dbi, &$request, $pages, $newowner) {
63         $ul = HTML::ul();
64         $count = 0;
65         foreach ($pages as $name) {
66             $page = $dbi->getPage($name);
67             $current = $page->getCurrentRevision();
68             if ( ($owner = $page->getOwner()) and 
69                  $newowner != $owner ) {
70                 if (!mayAccessPage('change', $name)) {
71                     $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",
72                                                   WikiLink($name))));
73                 } else {
74                     $version = $current->getVersion();
75                     $meta = $current->_data;
76                     $text = $current->getPackedContent();
77                     $meta['summary'] = sprintf(_("Change page owner from %s to %s"), $owner, $newowner);
78                     $meta['is_minor_edit'] = 0;
79                     $meta['author'] =  $request->_user->UserName();
80                     unset($meta['mtime']); // force new date
81                     $page->set('owner', $newowner);
82                     $page->save($text, $version + 1, $meta);
83                     if ($page->get('owner') === $newowner) {
84                         $ul->pushContent(HTML::li(fmt("Change owner of page '%s' to '%s'.",
85                                                       WikiLink($name), WikiLink($newowner))));
86                         $count++;
87                     } else {
88                         $ul->pushContent(HTML::li(fmt("Could not change owner of page '%s' to '%s'.", 
89                                                       WikiLink($name), $newowner)));
90                     }
91                 }
92             }
93         }
94         if ($count) {
95             $dbi->touch();
96             return HTML($ul, HTML::p(fmt("%s pages have been permanently changed.",
97                                          $count)));
98         } else {
99             return HTML($ul, HTML::p(fmt("No pages changed.")));
100         }
101     }
102     
103     function run($dbi, $argstr, &$request, $basepage) {
104         if ($request->getArg('action') != 'browse')
105             if (!$request->getArg('action') == _("PhpWikiAdministration/Chown"))
106                 return $this->disabled("(action != 'browse')");
107         
108         $args = $this->getArgs($argstr, $request);
109         $this->_args = $args;
110         if (empty($args['user']))
111             $args['user'] = $request->_user->UserName();
112         /*if (!empty($args['exclude']))
113             $exclude = explodePageList($args['exclude']);
114         else
115         $exclude = false;*/
116         $this->preSelectS($args, $request);
117
118         $p = $request->getArg('p');
119         if (!$p) $p = $this->_list;
120         $post_args = $request->getArg('admin_chown');
121         if (!$request->isPost() and empty($post_args['user']))
122             $post_args['user'] = $args['user'];
123         $next_action = 'select';
124         $pages = array();
125         if ($p && !$request->isPost())
126             $pages = $p;
127         if ($p && $request->isPost() &&
128             !empty($post_args['chown']) && empty($post_args['cancel'])) {
129             // without individual PagePermissions:
130             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
131                 $request->_notAuthorized(WIKIAUTH_ADMIN);
132                 $this->disabled("! user->isAdmin");
133             }
134             // DONE: error message if not allowed.
135             if ($post_args['action'] == 'verify') {
136                 // Real action
137                 return $this->chownPages($dbi, $request, array_keys($p), 
138                                           trim($post_args['user']));
139             }
140             if ($post_args['action'] == 'select') {
141                 if (!empty($post_args['user']))
142                     $next_action = 'verify';
143                 foreach ($p as $name => $c) {
144                     $pages[$name] = 1;
145                 }
146             }
147         }
148         if ($next_action == 'select' and empty($pages)) {
149             // List all pages to select from.
150             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], 
151                                          $args['exclude']);
152         }
153         /* // let the user decide which info
154          if ($next_action == 'verify') {
155             $args['info'] = "checkbox,pagename,owner,mtime";
156         }
157         */
158         $pagelist = new PageList_Selectable($args['info'], $args['exclude'], $args);
159         $pagelist->addPageList($pages);
160
161         $header = HTML::div();
162         if ($next_action == 'verify') {
163             $button_label = _("Yes");
164             $header->pushContent(
165               HTML::p(HTML::strong(
166                 _("Are you sure you want to permanently change the owner of the selected pages?"))));
167             $header = $this->chownForm($header, $post_args);
168         }
169         else {
170             $button_label = _("Change owner of selected pages");
171             $header->pushContent(HTML::p(_("Select the pages to change the owner:")));
172             $header = $this->chownForm($header, $post_args);
173         }
174
175         $buttons = HTML::p(Button('submit:admin_chown[chown]', $button_label, 'wikiadmin'),
176                            Button('submit:admin_chown[cancel]', _("Cancel"), 'button'));
177
178         return HTML::form(array('action' => $request->getPostURL(),
179                                 'method' => 'post'),
180                           $header,
181                           $buttons,
182                           $pagelist->getContent(),
183                           HiddenInputs($request->getArgs(),
184                                         false,
185                                         array('admin_chown')),
186                           HiddenInputs(array('admin_chown[action]' => $next_action)),
187                           ENABLE_PAGEPERM
188                           ? ''
189                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
190     }
191
192     function chownForm(&$header, $post_args) {
193         $header->pushContent(_("Change owner")." ");
194         $header->pushContent(' '._("to").': ');
195         $header->pushContent(HTML::input(array('name' => 'admin_chown[user]',
196                                                'value' => $post_args['user'])));
197         return $header;
198     }
199 }
200
201 // Local Variables:
202 // mode: php
203 // tab-width: 8
204 // c-basic-offset: 4
205 // c-hanging-comment-ender-p: nil
206 // indent-tabs-mode: nil
207 // End:
208 ?>