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