]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRemove.php
New FSF address
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRemove.php
1 <?php // -*-php-*-
2 // $Id$
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         return _("WikiAdminRemove");
41     }
42
43     function getDescription() {
44         return _("Permanently remove all selected pages.");
45     }
46
47     function getDefaultArguments() {
48         return array_merge
49             (
50              WikiPlugin_WikiAdminSelect::getDefaultArguments(),
51              array(
52                      /*
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.)
56                       *
57                       * FIXME: could use a better name.
58                       */
59                      'min_age' => 0,
60
61                      /*
62                       * Automatically check the checkboxes for files
63                       * which have been 'deleted' this long (in days).
64                       *
65                       * FIXME: could use a better name.
66                       */
67                      'max_age' => 31,
68                      /* Columns to include in listing */
69                      'info'     => 'most',
70                    ));
71     }
72
73     function collectPages(&$list, &$dbi, $sortby, $limit=0) {
74         extract($this->_args);
75
76         $now = time();
77
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
84
85             $empty = $current->hasDefaultContents();
86             if ($empty) {
87                 $age = ($now - $current->get('mtime')) / (24 * 3600.0);
88                 $checked = $age >= $max_age;
89             }
90             else {
91                 $age = 0;
92                 $checked = false;
93             }
94
95             if ($age >= $min_age) {
96                 if (empty($list[$pagename]))
97                     $list[$pagename] = $checked;
98             }
99         }
100         return $list;
101     }
102
103     function removePages(&$request, $pages) {
104         $result = HTML::div();
105         $ul = HTML::ul();
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)));
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             $result->pushContent($ul);
126             return $result;
127         } else {
128             $result->setAttr('class', 'error');
129             $result->pushContent(HTML::p(_("No pages removed.")));
130             return $result;
131         }
132     }
133
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"));
138             }
139         }
140
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']);
147         else
148         $exclude = false;*/
149         $this->preSelectS($args, $request);
150
151         $p = $request->getArg('p');
152         if (!$p) $p = $this->_list;
153         $post_args = $request->getArg('admin_remove');
154
155         $next_action = 'select';
156         $pages = array();
157         if ($p && $request->isPost() &&
158             !empty($post_args['remove']) && empty($post_args['cancel'])) {
159
160             // check individual PagePermissions
161             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
162                 $request->_notAuthorized(WIKIAUTH_ADMIN);
163                 $this->disabled("! user->isAdmin");
164             }
165             if ($post_args['action'] == 'verify') {
166                 // Real delete.
167                 return $this->removePages($request, array_keys($p));
168             }
169
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);
174                     $pages[$name] = $c;
175                 }
176             }
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);
181                 $pages[$name] = $c;
182             }
183             $request->setArg('p',false);
184         }
185         if ($next_action == 'select') {
186             // List all pages to select from.
187             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
188         }
189         $pagelist = new PageList_Selectable($args['info'], $args['exclude'],
190                                             array('types' =>
191                                                   array('remove'
192                                                         => new _PageList_Column_remove('remove', _("Remove")))));
193         $pagelist->addPageList($pages);
194
195         $header = HTML::fieldset();
196         if ($next_action == 'verify') {
197             $button_label = _("Yes");
198             $header->pushContent(HTML::p(HTML::strong(
199                 _("Are you sure you want to remove the selected files?"))));
200         }
201         else {
202             $button_label = _("Remove selected pages");
203             $header->pushContent(HTML::legend(_("Select the files to remove")));
204             if ($args['min_age'] > 0) {
205                 $header->pushContent(
206                     fmt("Also pages which have been deleted at least %s days.",
207                         $args['min_age']));
208             }
209
210             if ($args['max_age'] > 0) {
211                 $header->pushContent(
212                     " ",
213                     fmt("Pages which have been deleted at least %s days are already checked.",
214                         $args['max_age']));
215             }
216         }
217
218         $buttons = HTML::p(Button('submit:admin_remove[remove]', $button_label, 'wikiadmin'),
219                            Button('submit:admin_remove[cancel]', _("Cancel"), 'button'));
220         $header->pushContent($buttons);
221
222         // TODO: quick select by regex javascript?
223         return HTML::form(array('action' => $request->getPostURL(),
224                                 'method' => 'post'),
225                           $header,
226                           $pagelist->getContent(),
227                           HiddenInputs($request->getArgs(),
228                                         false,
229                                         array('admin_remove')),
230                           HiddenInputs(array('admin_remove[action]' => $next_action,
231                                              'require_authority_for_post' => WIKIAUTH_ADMIN)));
232     }
233 }
234
235 class _PageList_Column_remove extends _PageList_Column {
236     function _getValue ($page_handle, &$revision_handle) {
237         return Button(array('action' => 'remove'), _("Remove"),
238                       $page_handle->getName());
239     }
240 };
241
242 // Local Variables:
243 // mode: php
244 // tab-width: 8
245 // c-basic-offset: 4
246 // c-hanging-comment-ender-p: nil
247 // indent-tabs-mode: nil
248 // End:
249 ?>