]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRemove.php
function _getValue is not private
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRemove.php
1 <?php
2
3 /*
4  * Copyright 2002,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:   <<WikiAdminRemove>>
26  * Author:  Reini Urban <rurban@x-ray.at>
27  *
28  * KNOWN ISSUES:
29  * Currently we must be Admin.
30  * Future versions will support PagePermissions.
31  */
32 // maybe display more attributes with this class...
33 require_once 'lib/PageList.php';
34 require_once 'lib/plugin/WikiAdminSelect.php';
35
36 class WikiPlugin_WikiAdminRemove
37     extends WikiPlugin_WikiAdminSelect
38 {
39     function getName()
40     {
41         return _("WikiAdminRemove");
42     }
43
44     function getDescription()
45     {
46         return _("Permanently remove all selected pages.");
47     }
48
49     function getDefaultArguments()
50     {
51         return array_merge
52         (
53             WikiPlugin_WikiAdminSelect::getDefaultArguments(),
54             array(
55                 /*
56                  * Show only pages which have been 'deleted' this
57                  * long (in days).  (negative or non-numeric
58                  * means show all pages, even non-deleted ones.)
59                  *
60                  * FIXME: could use a better name.
61                  */
62                 'min_age' => 0,
63
64                 /*
65                  * Automatically check the checkboxes for files
66                  * which have been 'deleted' this long (in days).
67                  *
68                  * FIXME: could use a better name.
69                  */
70                 'max_age' => 31,
71                 /* Columns to include in listing */
72                 'info' => 'most',
73             ));
74     }
75
76     function collectPages(&$list, &$dbi, $sortby, $limit = 0)
77     {
78         extract($this->_args);
79
80         $now = time();
81
82         $allPages = $dbi->getAllPages('include_empty', $sortby, $limit);
83         while ($pagehandle = $allPages->next()) {
84             $pagename = $pagehandle->getName();
85             $current = $pagehandle->getCurrentRevision();
86             if ($current->getVersion() < 1)
87                 continue; // No versions in database
88
89             $empty = $current->hasDefaultContents();
90             if ($empty) {
91                 $age = ($now - $current->get('mtime')) / (24 * 3600.0);
92                 $checked = $age >= $max_age;
93             } else {
94                 $age = 0;
95                 $checked = false;
96             }
97
98             if ($age >= $min_age) {
99                 if (empty($list[$pagename]))
100                     $list[$pagename] = $checked;
101             }
102         }
103         return $list;
104     }
105
106     function removePages(&$request, $pages)
107     {
108         $result = HTML::div();
109         $ul = HTML::ul();
110         $dbi = $request->getDbh();
111         $count = 0;
112         foreach ($pages as $name) {
113             $name = str_replace(array('%5B', '%5D'), array('[', ']'), $name);
114             if (mayAccessPage('remove', $name)) {
115                 $dbi->deletePage($name);
116                 $ul->pushContent(HTML::li(fmt("Removed page ā€œ%sā€ successfully.", $name)));
117                 $count++;
118             } else {
119                 $ul->pushContent(HTML::li(fmt("Didn't remove page ā€œ%sā€. Access denied.", $name)));
120             }
121         }
122         if ($count) {
123             $dbi->touch();
124             $result->setAttr('class', 'feedback');
125             if ($count == 1) {
126                 $result->pushContent(HTML::p(_("One page has been removed:")));
127             } else {
128                 $result->pushContent(HTML::p(fmt("%d pages have been removed:", $count)));
129             }
130             $result->pushContent($ul);
131             return $result;
132         } else {
133             $result->setAttr('class', 'error');
134             $result->pushContent(HTML::p(_("No pages removed.")));
135             return $result;
136         }
137     }
138
139     function run($dbi, $argstr, &$request, $basepage)
140     {
141         if ($request->getArg('action') != 'browse') {
142             if ($request->getArg('action') != _("PhpWikiAdministration/Remove")) {
143                 return $this->disabled(_("Plugin not run: not in browse mode"));
144             }
145         }
146
147         $args = $this->getArgs($argstr, $request);
148         if (!is_numeric($args['min_age']))
149             $args['min_age'] = -1;
150         $this->_args =& $args;
151         /*if (!empty($args['exclude']))
152             $exclude = explodePageList($args['exclude']);
153         else
154         $exclude = false;*/
155         $this->preSelectS($args, $request);
156
157         $p = $request->getArg('p');
158         if (!$p) $p = $this->_list;
159         $post_args = $request->getArg('admin_remove');
160
161         $next_action = 'select';
162         $pages = array();
163         if ($p && $request->isPost() &&
164             !empty($post_args['remove']) && empty($post_args['cancel'])
165         ) {
166
167             // check individual PagePermissions
168             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
169                 $request->_notAuthorized(WIKIAUTH_ADMIN);
170                 $this->disabled("! user->isAdmin");
171             }
172             if ($post_args['action'] == 'verify') {
173                 // Real delete.
174                 return $this->removePages($request, array_keys($p));
175             }
176
177             if ($post_args['action'] == 'select') {
178                 $next_action = 'verify';
179                 foreach ($p as $name => $c) {
180                     $name = str_replace(array('%5B', '%5D'), array('[', ']'), $name);
181                     $pages[$name] = $c;
182                 }
183             }
184         } elseif ($p && is_array($p) && !$request->isPost()) { // from WikiAdminSelect
185             $next_action = 'verify';
186             foreach ($p as $name => $c) {
187                 $name = str_replace(array('%5B', '%5D'), array('[', ']'), $name);
188                 $pages[$name] = $c;
189             }
190             $request->setArg('p', false);
191         }
192         if ($next_action == 'select') {
193             // List all pages to select from.
194             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
195         }
196         $pagelist = new PageList_Selectable($args['info'], $args['exclude'],
197             array('types' =>
198             array('remove'
199             => new _PageList_Column_remove('remove', _("Remove")))));
200         $pagelist->addPageList($pages);
201
202         $header = HTML::fieldset();
203         if ($next_action == 'verify') {
204             $button_label = _("Yes");
205             $header->pushContent(HTML::legend(_("Confirm removal")));
206             $header->pushContent(HTML::p(HTML::strong(
207                 _("Are you sure you want to remove the selected files?"))));
208         } else {
209             $button_label = _("Remove selected pages");
210             $header->pushContent(HTML::legend(_("Select the files to remove")));
211             if ($args['min_age'] > 0) {
212                 $header->pushContent(
213                     fmt("Also pages which have been deleted at least %s days.",
214                         $args['min_age']));
215             }
216
217             if ($args['max_age'] > 0) {
218                 $header->pushContent(
219                     " ",
220                     fmt("Pages which have been deleted at least %s days are already checked.",
221                         $args['max_age']));
222             }
223         }
224
225         $buttons = HTML::p(Button('submit:admin_remove[remove]', $button_label, 'wikiadmin'),
226             Button('submit:admin_remove[cancel]', _("Cancel"), 'button'));
227         $header->pushContent($buttons);
228
229         // TODO: quick select by regex javascript?
230         return HTML::form(array('action' => $request->getPostURL(),
231                 'method' => 'post'),
232             $header,
233             $pagelist->getContent(),
234             HiddenInputs($request->getArgs(),
235                 false,
236                 array('admin_remove')),
237             HiddenInputs(array('admin_remove[action]' => $next_action,
238                 'require_authority_for_post' => WIKIAUTH_ADMIN)));
239     }
240 }
241
242 class _PageList_Column_remove extends _PageList_Column
243 {
244     function _getValue($page_handle, &$revision_handle)
245     {
246         return Button(array('action' => 'remove'), _("Remove"),
247             $page_handle->getName());
248     }
249 }
250
251 // Local Variables:
252 // mode: php
253 // tab-width: 8
254 // c-basic-offset: 4
255 // c-hanging-comment-ender-p: nil
256 // indent-tabs-mode: nil
257 // End: