]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRemove.php
Removed history
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRemove.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  Copyright 2002,2004 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * Usage:   <?plugin WikiAdminRemove?>
25  * Author:  Reini Urban <rurban@x-ray.at>
26  *
27  * KNOWN ISSUES:
28  * Currently we must be Admin.
29  * Future versions will support PagePermissions.
30  * requires PHP 4.2 so far.
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 getVersion() {
48         return preg_replace("/[Revision: $]/", '',
49                             "\$Revision$");
50     }
51
52     function getDefaultArguments() {
53         return array_merge
54             (
55              PageList::supportedArgs(),
56              array(
57                    's'  => false,
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         $ul = HTML::ul();
111         $dbi = $request->getDbh(); $count = 0;
112         foreach ($pages as $name) {
113             $name = str_replace(array('%5B','%5D'),array('[',']'),$name);
114             if (mayAccessPage('remove',$name)) {
115                 $dbi->deletePage($name);
116                 $ul->pushContent(HTML::li(fmt("Removed page '%s' successfully.", $name)));
117                 $count++;
118             } else {
119                 $ul->pushContent(HTML::li(fmt("Didn't removed page '%s'. Access denied.", $name)));
120             }
121         }
122         if ($count) $dbi->touch();
123         return HTML($ul,
124                     HTML::p(fmt("%d pages have been permanently removed.",$count)));
125     }
126     
127     function run($dbi, $argstr, &$request, $basepage) {
128         if ($request->getArg('action') != 'browse')
129             if ($request->getArg('action') != _("PhpWikiAdministration/Remove"))
130                 return $this->disabled("(action != 'browse')");
131         
132         $args = $this->getArgs($argstr, $request);
133         if (!is_numeric($args['min_age']))
134             $args['min_age'] = -1;
135         $this->_args =& $args;
136         /*if (!empty($args['exclude']))
137             $exclude = explodePageList($args['exclude']);
138         else
139         $exclude = false;*/
140         $this->preSelectS($args, $request);
141
142         $p = $request->getArg('p');
143         if (!$p) $p = $this->_list;
144         $post_args = $request->getArg('admin_remove');
145
146         $next_action = 'select';
147         $pages = array();
148         if ($p && $request->isPost() &&
149             !empty($post_args['remove']) && empty($post_args['cancel'])) {
150
151             // check individual PagePermissions
152             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
153                 $request->_notAuthorized(WIKIAUTH_ADMIN);
154                 $this->disabled("! user->isAdmin");
155             }
156             if ($post_args['action'] == 'verify') {
157                 // Real delete.
158                 return $this->removePages($request, array_keys($p));
159             }
160
161             if ($post_args['action'] == 'select') {
162                 $next_action = 'verify';
163                 foreach ($p as $name => $c) {
164                     $name = str_replace(array('%5B','%5D'),array('[',']'),$name);
165                     $pages[$name] = $c;
166                 }
167             }
168         } elseif ($p && is_array($p) && !$request->isPost()) { // from WikiAdminSelect
169             $next_action = 'verify';
170             foreach ($p as $name => $c) {
171                 $name = str_replace(array('%5B','%5D'),array('[',']'),$name);
172                 $pages[$name] = $c;
173             }
174             $request->setArg('p',false);
175         }
176         if ($next_action == 'select') {
177             // List all pages to select from.
178             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
179         }
180         $pagelist = new PageList_Selectable($args['info'], $args['exclude'], 
181                                             array('types' => 
182                                                   array('remove'
183                                                         => new _PageList_Column_remove('remove', _("Remove")))));
184         $pagelist->addPageList($pages);
185
186         $header = HTML::p();
187         if ($next_action == 'verify') {
188             $button_label = _("Yes");
189             $header->pushContent(HTML::strong(
190                 _("Are you sure you want to permanently remove the selected files?")));
191         }
192         else {
193             $button_label = _("Remove selected pages");
194             $header->pushContent(_("Permanently remove the selected files:"),HTML::br());
195             if ($args['min_age'] > 0) {
196                 $header->pushContent(
197                     fmt("Also pages which have been deleted at least %s days.",
198                         $args['min_age']));
199             }
200             else {
201                 $header->pushContent(_("List all pages."));
202             }
203             
204             if ($args['max_age'] > 0) {
205                 $header->pushContent(
206                     " ",
207                     fmt("(Pages which have been deleted at least %s days are already checked.)",
208                         $args['max_age']));
209             }
210         }
211
212
213         $buttons = HTML::p(Button('submit:admin_remove[remove]', $button_label, 'wikiadmin'),
214                            Button('submit:admin_remove[cancel]', _("Cancel"), 'button'));
215
216         // TODO: quick select by regex javascript?
217         return HTML::form(array('action' => $request->getPostURL(),
218                                 'method' => 'post'),
219                           $header,
220                           $pagelist->getContent(),
221                           HiddenInputs($request->getArgs(),
222                                         false,
223                                         array('admin_remove')),
224                           HiddenInputs(array('admin_remove[action]' => $next_action,
225                                              'require_authority_for_post' => WIKIAUTH_ADMIN)),
226                           $buttons);
227     }
228 }
229
230 class _PageList_Column_remove extends _PageList_Column {
231     function _getValue ($page_handle, &$revision_handle) {
232         return Button(array('action' => 'remove'), _("Remove"),
233                       $page_handle->getName());
234     }
235 };
236
237 // Local Variables:
238 // mode: php
239 // tab-width: 8
240 // c-basic-offset: 4
241 // c-hanging-comment-ender-p: nil
242 // indent-tabs-mode: nil
243 // End:
244 ?>