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