]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminChown.php
include [all] Include and file path should be devided with single space. File path...
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminChown.php
1 <?php // -*-php-*-
2
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 along
20  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * Usage:   <<WikiAdminChown s||=* >> or called via WikiAdminSelect
26  * @author:  Reini Urban <rurban@x-ray.at>
27  *
28  */
29 require_once 'lib/PageList.php';
30 require_once 'lib/plugin/WikiAdminSelect.php';
31
32 class WikiPlugin_WikiAdminChown
33 extends WikiPlugin_WikiAdminSelect
34 {
35     function getName() {
36         return _("WikiAdminChown");
37     }
38
39     function getDescription() {
40         return _("Change owner of selected pages.");
41     }
42
43     function getDefaultArguments() {
44         return array_merge
45             (
46              WikiPlugin_WikiAdminSelect::getDefaultArguments(),
47              array(
48                    'user'         => false,
49                    /* Columns to include in listing */
50                    'info'     => 'pagename,owner,mtime',
51                    ));
52     }
53
54     function chownPages(&$dbi, &$request, $pages, $newowner) {
55         $result = HTML::div();
56         $ul = HTML::ul();
57         $count = 0;
58         foreach ($pages as $name) {
59             $page = $dbi->getPage($name);
60             $current = $page->getCurrentRevision();
61             if ( ($owner = $page->getOwner()) and
62                  $newowner != $owner ) {
63                 if (!mayAccessPage('change', $name)) {
64                     $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",
65                                                   WikiLink($name))));
66                 } else {
67                     $version = $current->getVersion();
68                     $meta = $current->_data;
69                     $text = $current->getPackedContent();
70                     $meta['summary'] = "Change page owner from '".$owner."' to '".$newowner."'";
71                     $meta['is_minor_edit'] = 1;
72                     $meta['author'] =  $request->_user->UserName();
73                     unset($meta['mtime']); // force new date
74                     $page->set('owner', $newowner);
75                     $page->save($text, $version + 1, $meta);
76                     if ($page->get('owner') === $newowner) {
77                         $ul->pushContent(HTML::li(fmt("Change owner of page '%s' to '%s'.",
78                                                       WikiLink($name), WikiLink($newowner))));
79                         $count++;
80                     } else {
81                         $ul->pushContent(HTML::li(fmt("Could not change owner of page '%s' to '%s'.",
82                                                       WikiLink($name), $newowner)));
83                     }
84                 }
85             }
86         }
87         if ($count) {
88             $dbi->touch();
89             $result->setAttr('class', 'feedback');
90             if ($count == 1) {
91                 $result->pushContent(HTML::p(_("One page has been changed:")));
92             } else {
93                 $result->pushContent(HTML::p(fmt("%d pages have been changed:", $count)));
94             }
95             $result->pushContent($ul);
96             return $result;
97         } else {
98             $result->setAttr('class', 'error');
99             $result->pushContent(HTML::p(_("No pages changed.")));
100             return $result;
101         }
102     }
103
104     function run($dbi, $argstr, &$request, $basepage) {
105         if ($request->getArg('action') != 'browse') {
106             if (!$request->getArg('action') == _("PhpWikiAdministration/Chown")) {
107                 return $this->disabled(_("Plugin not run: not in browse mode"));
108             }
109         }
110
111         $args = $this->getArgs($argstr, $request);
112         $this->_args = $args;
113         if (empty($args['user']))
114             $args['user'] = $request->_user->UserName();
115         /*if (!empty($args['exclude']))
116             $exclude = explodePageList($args['exclude']);
117         else
118         $exclude = false;*/
119         $this->preSelectS($args, $request);
120
121         $p = $request->getArg('p');
122         if (!$p) $p = $this->_list;
123         $post_args = $request->getArg('admin_chown');
124         if (!$request->isPost() and empty($post_args['user']))
125             $post_args['user'] = $args['user'];
126         $next_action = 'select';
127         $pages = array();
128         if ($p && !$request->isPost())
129             $pages = $p;
130         if ($p && $request->isPost() &&
131             !empty($post_args['chown']) && empty($post_args['cancel'])) {
132             // without individual PagePermissions:
133             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
134                 $request->_notAuthorized(WIKIAUTH_ADMIN);
135                 $this->disabled("! user->isAdmin");
136             }
137             // DONE: error message if not allowed.
138             if ($post_args['action'] == 'verify') {
139                 // Real action
140                 return $this->chownPages($dbi, $request, array_keys($p),
141                                           trim($post_args['user']));
142             }
143             if ($post_args['action'] == 'select') {
144                 if (!empty($post_args['user']))
145                     $next_action = 'verify';
146                 foreach ($p as $name => $c) {
147                     $pages[$name] = 1;
148                 }
149             }
150         }
151         if ($next_action == 'select' and empty($pages)) {
152             // List all pages to select from.
153             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'],
154                                          $args['exclude']);
155         }
156         /* // let the user decide which info
157          if ($next_action == 'verify') {
158             $args['info'] = "checkbox,pagename,owner,mtime";
159         }
160         */
161         if ($next_action == 'select') {
162             $pagelist = new PageList_Selectable($args['info'], $args['exclude'], $args);
163         } else {
164             $pagelist = new PageList_Unselectable($args['info'], $args['exclude'], $args);
165         }
166         $pagelist->addPageList($pages);
167
168         $header = HTML::fieldset();
169         if ($next_action == 'verify') {
170             $button_label = _("Yes");
171             $header->pushContent(HTML::legend(_("Confirm ownership change")));
172             $header->pushContent(
173               HTML::p(HTML::strong(
174                 _("Are you sure you want to change the owner of the selected pages?"))));
175             $header = $this->chownForm($header, $post_args);
176         }
177         else {
178             $button_label = _("Change owner of selected pages");
179             $header->pushContent(HTML::legend(_("Select the pages to change the owner")));
180             $header = $this->chownForm($header, $post_args);
181         }
182
183         $buttons = HTML::p(Button('submit:admin_chown[chown]', $button_label, 'wikiadmin'),
184                            Button('submit:admin_chown[cancel]', _("Cancel"), 'button'));
185         $header->pushContent($buttons);
186
187         return HTML::form(array('action' => $request->getPostURL(),
188                                 'method' => 'post'),
189                           $header,
190                           $pagelist->getContent(),
191                           HiddenInputs($request->getArgs(),
192                                         false,
193                                         array('admin_chown')),
194                           HiddenInputs(array('admin_chown[action]' => $next_action)),
195                           ENABLE_PAGEPERM
196                           ? ''
197                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
198     }
199
200     function chownForm(&$header, $post_args) {
201         $header->pushContent(_("Change owner to: "));
202         $header->pushContent(HTML::input(array('name' => 'admin_chown[user]',
203                                                'value' => $post_args['user'],
204                                                'size' => 40)));
205         return $header;
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: