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