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