]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminChown.php
Replace tabs by spaces; remove EOL spaces
[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              WikiPlugin_WikiAdminSelect::getDefaultArguments(),
54              array(
55                    'user'         => false,
56                    /* Columns to include in listing */
57                    'info'     => 'pagename,owner,mtime',
58                    ));
59     }
60
61     function chownPages(&$dbi, &$request, $pages, $newowner) {
62         $result = HTML::div();
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'] = "Change page owner from '".$owner."' to '".$newowner."'";
78                     $meta['is_minor_edit'] = 1;
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             $result->setAttr('class', 'feedback');
97             if ($count == 1) {
98                 $result->pushContent(HTML::p("One page has been permanently changed:"));
99             } else {
100                 $result->pushContent(HTML::p(fmt("%s pages have been permanently changed:", $count)));
101             }
102             $result->pushContent($ul);
103             return $result;
104         } else {
105             $result->setAttr('class', 'error');
106             $result->pushContent(HTML::p("No pages changed."));
107             return $result;
108         }
109     }
110
111     function run($dbi, $argstr, &$request, $basepage) {
112         if ($request->getArg('action') != 'browse')
113             if (!$request->getArg('action') == _("PhpWikiAdministration/Chown"))
114                 return $this->disabled("(action != 'browse')");
115
116         $args = $this->getArgs($argstr, $request);
117         $this->_args = $args;
118         if (empty($args['user']))
119             $args['user'] = $request->_user->UserName();
120         /*if (!empty($args['exclude']))
121             $exclude = explodePageList($args['exclude']);
122         else
123         $exclude = false;*/
124         $this->preSelectS($args, $request);
125
126         $p = $request->getArg('p');
127         if (!$p) $p = $this->_list;
128         $post_args = $request->getArg('admin_chown');
129         if (!$request->isPost() and empty($post_args['user']))
130             $post_args['user'] = $args['user'];
131         $next_action = 'select';
132         $pages = array();
133         if ($p && !$request->isPost())
134             $pages = $p;
135         if ($p && $request->isPost() &&
136             !empty($post_args['chown']) && empty($post_args['cancel'])) {
137             // without individual PagePermissions:
138             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
139                 $request->_notAuthorized(WIKIAUTH_ADMIN);
140                 $this->disabled("! user->isAdmin");
141             }
142             // DONE: error message if not allowed.
143             if ($post_args['action'] == 'verify') {
144                 // Real action
145                 return $this->chownPages($dbi, $request, array_keys($p),
146                                           trim($post_args['user']));
147             }
148             if ($post_args['action'] == 'select') {
149                 if (!empty($post_args['user']))
150                     $next_action = 'verify';
151                 foreach ($p as $name => $c) {
152                     $pages[$name] = 1;
153                 }
154             }
155         }
156         if ($next_action == 'select' and empty($pages)) {
157             // List all pages to select from.
158             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'],
159                                          $args['exclude']);
160         }
161         /* // let the user decide which info
162          if ($next_action == 'verify') {
163             $args['info'] = "checkbox,pagename,owner,mtime";
164         }
165         */
166         if ($next_action == 'select') {
167             $pagelist = new PageList_Selectable($args['info'], $args['exclude'], $args);
168         } else {
169             $pagelist = new PageList_Unselectable($args['info'], $args['exclude'], $args);
170         }
171         $pagelist->addPageList($pages);
172
173         $header = HTML::fieldset();
174         if ($next_action == 'verify') {
175             $button_label = _("Yes");
176             $header->pushContent(
177               HTML::p(HTML::strong(
178                 _("Are you sure you want to permanently change the owner of the selected pages?"))));
179             $header = $this->chownForm($header, $post_args);
180         }
181         else {
182             $button_label = _("Change owner of selected pages");
183             $header->pushContent(HTML::legend(_("Select the pages to change the owner")));
184             $header = $this->chownForm($header, $post_args);
185         }
186
187         $buttons = HTML::p(Button('submit:admin_chown[chown]', $button_label, 'wikiadmin'),
188                            Button('submit:admin_chown[cancel]', _("Cancel"), 'button'));
189         $header->pushContent($buttons);
190
191         return HTML::form(array('action' => $request->getPostURL(),
192                                 'method' => 'post'),
193                           $header,
194                           $pagelist->getContent(),
195                           HiddenInputs($request->getArgs(),
196                                         false,
197                                         array('admin_chown')),
198                           HiddenInputs(array('admin_chown[action]' => $next_action)),
199                           ENABLE_PAGEPERM
200                           ? ''
201                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
202     }
203
204     function chownForm(&$header, $post_args) {
205         $header->pushContent(_("Change owner")." ");
206         $header->pushContent(' '._("to").': ');
207         $header->pushContent(HTML::input(array('name' => 'admin_chown[user]',
208                                                'value' => $post_args['user'],
209                                                'size' => 40)));
210         return $header;
211     }
212 }
213
214 // Local Variables:
215 // mode: php
216 // tab-width: 8
217 // c-basic-offset: 4
218 // c-hanging-comment-ender-p: nil
219 // indent-tabs-mode: nil
220 // End:
221 ?>