]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminChown.php
rcs_id no longer makes sense with Subversion global version number
[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 getDefaultArguments() {
46         return array_merge
47             (
48              WikiPlugin_WikiAdminSelect::getDefaultArguments(),
49              array(
50                    'user'         => false,
51                    /* Columns to include in listing */
52                    'info'     => 'pagename,owner,mtime',
53                    ));
54     }
55
56     function chownPages(&$dbi, &$request, $pages, $newowner) {
57         $result = HTML::div();
58         $ul = HTML::ul();
59         $count = 0;
60         foreach ($pages as $name) {
61             $page = $dbi->getPage($name);
62             $current = $page->getCurrentRevision();
63             if ( ($owner = $page->getOwner()) and
64                  $newowner != $owner ) {
65                 if (!mayAccessPage('change', $name)) {
66                     $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",
67                                                   WikiLink($name))));
68                 } else {
69                     $version = $current->getVersion();
70                     $meta = $current->_data;
71                     $text = $current->getPackedContent();
72                     $meta['summary'] = "Change page owner from '".$owner."' to '".$newowner."'";
73                     $meta['is_minor_edit'] = 1;
74                     $meta['author'] =  $request->_user->UserName();
75                     unset($meta['mtime']); // force new date
76                     $page->set('owner', $newowner);
77                     $page->save($text, $version + 1, $meta);
78                     if ($page->get('owner') === $newowner) {
79                         $ul->pushContent(HTML::li(fmt("Change owner of page '%s' to '%s'.",
80                                                       WikiLink($name), WikiLink($newowner))));
81                         $count++;
82                     } else {
83                         $ul->pushContent(HTML::li(fmt("Could not change owner of page '%s' to '%s'.",
84                                                       WikiLink($name), $newowner)));
85                     }
86                 }
87             }
88         }
89         if ($count) {
90             $dbi->touch();
91             $result->setAttr('class', 'feedback');
92             if ($count == 1) {
93                 $result->pushContent(HTML::p("One page has been permanently changed:"));
94             } else {
95                 $result->pushContent(HTML::p(fmt("%s pages have been permanently changed:", $count)));
96             }
97             $result->pushContent($ul);
98             return $result;
99         } else {
100             $result->setAttr('class', 'error');
101             $result->pushContent(HTML::p("No pages changed."));
102             return $result;
103         }
104     }
105
106     function run($dbi, $argstr, &$request, $basepage) {
107         if ($request->getArg('action') != 'browse')
108             if (!$request->getArg('action') == _("PhpWikiAdministration/Chown"))
109                 return $this->disabled("(action != 'browse')");
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(
172               HTML::p(HTML::strong(
173                 _("Are you sure you want to permanently change the owner of the selected pages?"))));
174             $header = $this->chownForm($header, $post_args);
175         }
176         else {
177             $button_label = _("Change owner of selected pages");
178             $header->pushContent(HTML::legend(_("Select the pages to change the owner")));
179             $header = $this->chownForm($header, $post_args);
180         }
181
182         $buttons = HTML::p(Button('submit:admin_chown[chown]', $button_label, 'wikiadmin'),
183                            Button('submit:admin_chown[cancel]', _("Cancel"), 'button'));
184         $header->pushContent($buttons);
185
186         return HTML::form(array('action' => $request->getPostURL(),
187                                 'method' => 'post'),
188                           $header,
189                           $pagelist->getContent(),
190                           HiddenInputs($request->getArgs(),
191                                         false,
192                                         array('admin_chown')),
193                           HiddenInputs(array('admin_chown[action]' => $next_action)),
194                           ENABLE_PAGEPERM
195                           ? ''
196                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
197     }
198
199     function chownForm(&$header, $post_args) {
200         $header->pushContent(_("Change owner")." ");
201         $header->pushContent(' '._("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:
216 ?>