]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRemove.php
Corrected spelling error -- reported by bishop <bishop@platypus.bc.ca>
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRemove.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminRemove.php,v 1.10 2004-02-11 15:29:43 zorloc Exp $');
3 /*
4  Copyright 2002 $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
35 class WikiPlugin_WikiAdminRemove
36 extends WikiPlugin
37 {
38     function getName() {
39         return _("WikiAdminRemove");
40     }
41
42     function getDescription() {
43         return _("Permanently remove all selected pages.");
44     }
45
46     function getVersion() {
47         return preg_replace("/[Revision: $]/", '',
48                             "\$Revision: 1.10 $");
49     }
50
51     function getDefaultArguments() {
52         return 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
70                      /* Pages to exclude */
71                      'exclude'  => '',
72
73                      /* Columns to include in listing */
74                      'info'     => '',
75
76                      /* How to sort */
77                      'sortby'   => ''
78                      );
79     }
80
81     function collectPages(&$list, &$dbi) {
82         extract($this->_args);
83
84         $now = time();
85         
86         $allPages = $dbi->getAllPages('include_deleted');
87         while ($pagehandle = $allPages->next()) {
88             $pagename = $pagehandle->getName();
89             $current = $pagehandle->getCurrentRevision();
90             if ($current->getVersion() < 1)
91                 continue;       // No versions in database
92
93             $empty = $current->hasDefaultContents();
94             if ($empty) {
95                 $age = ($now - $current->get('mtime')) / (24 * 3600.0);
96                 $checked = $age >= $max_age;
97             }
98             else {
99                 $age = 0;
100                 $checked = false;
101             }
102
103             if ($age > $min_age) {
104                 if (empty($list[$pagename]))
105                     $list[$pagename] = $checked;
106             }
107         }
108     }
109
110     function removePages(&$request, $pages) {
111         $ul = HTML::ul();
112         $dbi = $request->getDbh();
113         foreach ($pages as $name) {
114             $dbi->deletePage($name);
115             $ul->pushContent(HTML::li(fmt("Removed page '%s' successfully.", $name)));
116         }
117         $dbi->touch();
118         return HTML($ul,
119                     HTML::p(_('All selected pages have been permanently removed.')));
120     }
121     
122     function run($dbi, $argstr, $request) {
123         if ($request->getArg('action') != 'browse')
124             return $this->disabled("(action != 'browse')");
125         
126         $args = $this->getArgs($argstr, $request);
127         if (!is_numeric($args['min_age']))
128             $args['min_age'] = -1;
129         $this->_args = $args;
130         
131         if (!empty($args['exclude']))
132             $exclude = explodePageList($args['exclude']);
133         else
134             $exclude = false;
135
136
137         $p = $request->getArg('p');
138         $post_args = $request->getArg('admin_remove');
139
140         $next_action = 'select';
141         $pages = array();
142         
143         if ($p && $request->isPost() && $request->_user->isAdmin()
144             && !empty($post_args['remove']) && empty($post_args['cancel'])) {
145             // FIXME: error message if not admin.
146             if ($post_args['action'] == 'verify') {
147                 // Real delete.
148                 return $this->removePages($request, $p);
149             }
150
151             if ($post_args['action'] == 'select') {
152                 $next_action = 'verify';
153                 foreach ($p as $name) {
154                     $pages[$name] = 1;
155                 }
156             }
157         }
158         if ($next_action == 'select') {
159             // List all pages to select from.
160             $list = $this->collectPages($pages, $dbi);
161         }
162
163
164         $info = 'checkbox';
165         if ($args['info'])
166             $info .= "," . $args['info'];
167         $pagelist = new PageList_Selectable($info, $exclude);
168         $pagelist->addPageList($pages);
169
170         $header = HTML::p();
171         if ($next_action == 'verify') {
172             $button_label = _("Permanently remove selected pages");
173             $header->pushContent(HTML::strong(
174                 _("Are you sure you want to permanently remove the selected files?")));
175         }
176         else {
177             $button_label = _("Remove selected pages");
178             if ($args['min_age'] >= 0) {
179                 $header->pushContent(
180                     fmt("Listing pages which have been deleted at least %s days.",
181                         $args['min_age']));
182             }
183             else {
184                 $header->pushContent(_("Listing all pages."));
185             }
186             
187             if ($args['max_age'] >= 0) {
188                 $header->pushContent(
189                     " ",
190                     fmt("(Automatically checking pages which have been deleted at least %s days.)",
191                         $args['max_age']));
192             }
193         }
194
195
196         $buttons = HTML::p(Button('submit:admin_remove[remove]', $button_label, 'wikiadmin'),
197                            Button('submit:admin_remove[cancel]', _("Cancel"), 'button'));
198
199         return HTML::form(array('action' => $request->getPostURL(),
200                                 'method' => 'post'),
201
202                           $header,
203                           
204                           $pagelist->getContent(),
205
206                           HiddenInputs($request->getArgs(),
207                                         false,
208                                         array('admin_remove')),
209
210                           HiddenInputs(array('admin_remove[action]' => $next_action,
211                                              'require_authority_for_post' => WIKIAUTH_ADMIN)),
212                           $buttons);
213     }
214 }
215
216 // $Log: not supported by cvs2svn $
217 // Revision 1.9  2003/02/26 22:27:22  dairiki
218 // Fix and refactor FrameInclude plugin (more or less).
219 //
220 // (This should now generate valid HTML.  Woohoo!)
221 //
222 // The output when using the Sidebar theme is ugly enough that it should
223 // be considered broken.  (But the Sidebar theme appears pretty broken in
224 // general right now.)
225 //
226 // (Personal comment (not to be taken personally): I must say that I
227 // remain unconvinced of the usefulness of this plugin.)
228 //
229 // Revision 1.8  2003/02/17 17:23:59  dairiki
230 // Disable plugin unless action='browse'.
231 //
232 // Add a header to the output, and adjust the HTML formatting a bit.
233 //
234 // Revision 1.7  2003/02/17 06:06:33  dairiki
235 // Refactor & code cleanup.
236 //
237 // Added two new plugin arguments:
238 //
239 //   min_age - only display pages which have been "deleted" for at
240 //             least this many days.  (Use min_age=none to get all
241 //             pages, even non-deleted ones listed.)
242 //
243 //   max_age - automatically check the checkboxes of pages which
244 //             have been "deleted" this many days or more.
245 //
246 // ("Deleted" means the current version of the page is empty.
247 // For the most part, PhpWiki treats these "deleted" pages as
248 // if they didn't exist --- but, of course, the PageHistory is
249 // still available, allowing old versions of the page to be restored.)
250 //
251 // Revision 1.6  2003/02/16 19:47:17  dairiki
252 // Update WikiDB timestamp when editing or deleting pages.
253 //
254 // Revision 1.5  2003/01/18 22:14:28  carstenklapp
255 // Code cleanup:
256 // Reformatting & tabs to spaces;
257 // Added copyleft, getVersion, getDescription, rcs_id.
258 //
259
260 // Local Variables:
261 // mode: php
262 // tab-width: 8
263 // c-basic-offset: 4
264 // c-hanging-comment-ender-p: nil
265 // indent-tabs-mode: nil
266 // End:
267 ?>