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