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