4 * Copyright 2002,2004 $ThePhpWikiProgrammingTeam
5 * Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
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.
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.
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.
25 * Usage: <<WikiAdminRemove>>
26 * Author: Reini Urban <rurban@x-ray.at>
29 * Currently we must be Admin.
30 * Future versions will support PagePermissions.
32 // maybe display more attributes with this class...
33 require_once('lib/PageList.php');
34 require_once('lib/plugin/WikiAdminSelect.php');
36 class WikiPlugin_WikiAdminRemove
37 extends WikiPlugin_WikiAdminSelect
40 return _("WikiAdminRemove");
43 function getDescription() {
44 return _("Permanently remove all selected pages.");
47 function getDefaultArguments() {
50 WikiPlugin_WikiAdminSelect::getDefaultArguments(),
53 * Show only pages which have been 'deleted' this
54 * long (in days). (negative or non-numeric
55 * means show all pages, even non-deleted ones.)
57 * FIXME: could use a better name.
62 * Automatically check the checkboxes for files
63 * which have been 'deleted' this long (in days).
65 * FIXME: could use a better name.
68 /* Columns to include in listing */
73 function collectPages(&$list, &$dbi, $sortby, $limit=0) {
74 extract($this->_args);
78 $allPages = $dbi->getAllPages('include_empty',$sortby,$limit);
79 while ($pagehandle = $allPages->next()) {
80 $pagename = $pagehandle->getName();
81 $current = $pagehandle->getCurrentRevision();
82 if ($current->getVersion() < 1)
83 continue; // No versions in database
85 $empty = $current->hasDefaultContents();
87 $age = ($now - $current->get('mtime')) / (24 * 3600.0);
88 $checked = $age >= $max_age;
95 if ($age >= $min_age) {
96 if (empty($list[$pagename]))
97 $list[$pagename] = $checked;
103 function removePages(&$request, $pages) {
104 $result = HTML::div();
106 $dbi = $request->getDbh(); $count = 0;
107 foreach ($pages as $name) {
108 $name = str_replace(array('%5B','%5D'),array('[',']'),$name);
109 if (mayAccessPage('remove',$name)) {
110 $dbi->deletePage($name);
111 $ul->pushContent(HTML::li(fmt("Removed page '%s' successfully.", $name)));
114 $ul->pushContent(HTML::li(fmt("Didn't remove page '%s'. Access denied.", $name)));
119 $result->setAttr('class', 'feedback');
121 $result->pushContent(HTML::p(_("One page has been removed:")));
123 $result->pushContent(HTML::p(fmt("%d pages have been removed:", $count)));
125 $result->pushContent($ul);
128 $result->setAttr('class', 'error');
129 $result->pushContent(HTML::p(_("No pages removed.")));
134 function run($dbi, $argstr, &$request, $basepage) {
135 if ($request->getArg('action') != 'browse') {
136 if ($request->getArg('action') != _("PhpWikiAdministration/Remove")) {
137 return $this->disabled(_("Plugin not run: not in browse mode"));
141 $args = $this->getArgs($argstr, $request);
142 if (!is_numeric($args['min_age']))
143 $args['min_age'] = -1;
144 $this->_args =& $args;
145 /*if (!empty($args['exclude']))
146 $exclude = explodePageList($args['exclude']);
149 $this->preSelectS($args, $request);
151 $p = $request->getArg('p');
152 if (!$p) $p = $this->_list;
153 $post_args = $request->getArg('admin_remove');
155 $next_action = 'select';
157 if ($p && $request->isPost() &&
158 !empty($post_args['remove']) && empty($post_args['cancel'])) {
160 // check individual PagePermissions
161 if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
162 $request->_notAuthorized(WIKIAUTH_ADMIN);
163 $this->disabled("! user->isAdmin");
165 if ($post_args['action'] == 'verify') {
167 return $this->removePages($request, array_keys($p));
170 if ($post_args['action'] == 'select') {
171 $next_action = 'verify';
172 foreach ($p as $name => $c) {
173 $name = str_replace(array('%5B','%5D'),array('[',']'),$name);
177 } elseif ($p && is_array($p) && !$request->isPost()) { // from WikiAdminSelect
178 $next_action = 'verify';
179 foreach ($p as $name => $c) {
180 $name = str_replace(array('%5B','%5D'),array('[',']'),$name);
183 $request->setArg('p',false);
185 if ($next_action == 'select') {
186 // List all pages to select from.
187 $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
189 $pagelist = new PageList_Selectable($args['info'], $args['exclude'],
192 => new _PageList_Column_remove('remove', _("Remove")))));
193 $pagelist->addPageList($pages);
195 $header = HTML::fieldset();
196 if ($next_action == 'verify') {
197 $button_label = _("Yes");
198 $header->pushContent(HTML::legend(_("Confirm removal")));
199 $header->pushContent(HTML::p(HTML::strong(
200 _("Are you sure you want to remove the selected files?"))));
203 $button_label = _("Remove selected pages");
204 $header->pushContent(HTML::legend(_("Select the files to remove")));
205 if ($args['min_age'] > 0) {
206 $header->pushContent(
207 fmt("Also pages which have been deleted at least %s days.",
211 if ($args['max_age'] > 0) {
212 $header->pushContent(
214 fmt("Pages which have been deleted at least %s days are already checked.",
219 $buttons = HTML::p(Button('submit:admin_remove[remove]', $button_label, 'wikiadmin'),
220 Button('submit:admin_remove[cancel]', _("Cancel"), 'button'));
221 $header->pushContent($buttons);
223 // TODO: quick select by regex javascript?
224 return HTML::form(array('action' => $request->getPostURL(),
227 $pagelist->getContent(),
228 HiddenInputs($request->getArgs(),
230 array('admin_remove')),
231 HiddenInputs(array('admin_remove[action]' => $next_action,
232 'require_authority_for_post' => WIKIAUTH_ADMIN)));
236 class _PageList_Column_remove extends _PageList_Column {
237 function _getValue ($page_handle, &$revision_handle) {
238 return Button(array('action' => 'remove'), _("Remove"),
239 $page_handle->getName());
247 // c-hanging-comment-ender-p: nil
248 // indent-tabs-mode: nil