]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRemove.php
Disable plugin unless action='browse'.
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRemove.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminRemove.php,v 1.8 2003-02-17 17:23:59 dairiki 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  * Displays a url in a seperate frame inside our body.
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
36 class WikiPlugin_WikiAdminRemove
37 extends WikiPlugin
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: 1.8 $");
50     }
51
52     function getDefaultArguments() {
53         return array(
54                      /*
55                       * Show only pages which have been 'deleted' this
56                       * long (in days).  (negative or non-numeric
57                       * means show all pages, even non-deleted ones.)
58                       *
59                       * FIXME: could use a better name.
60                       */
61                      'min_age' => 0,
62
63                      /*
64                       * Automatically check the checkboxes for files
65                       * which have been 'deleted' this long (in days).
66                       *
67                       * FIXME: could use a better name.
68                       */
69                      'max_age' => 31,
70
71                      /* Pages to exclude */
72                      'exclude'  => '',
73
74                      /* Columns to include in listing */
75                      'info'     => '',
76
77                      /* How to sort */
78                      'sortby'   => ''
79                      );
80     }
81
82     function collectPages(&$list, &$dbi) {
83         extract($this->_args);
84
85         $now = time();
86         
87         $allPages = $dbi->getAllPages('include_deleted');
88         while ($pagehandle = $allPages->next()) {
89             $pagename = $pagehandle->getName();
90             $current = $pagehandle->getCurrentRevision();
91             if ($current->getVersion() < 1)
92                 continue;       // No versions in database
93
94             $empty = $current->hasDefaultContents();
95             if ($empty) {
96                 $age = ($now - $current->get('mtime')) / (24 * 3600.0);
97                 $checked = $age >= $max_age;
98             }
99             else {
100                 $age = 0;
101                 $checked = false;
102             }
103
104             if ($age > $min_age) {
105                 if (empty($list[$pagename]))
106                     $list[$pagename] = $checked;
107             }
108         }
109     }
110
111     function removePages(&$request, $pages) {
112         $ul = HTML::ul();
113         $dbi = $request->getDbh();
114         foreach ($pages as $name) {
115             $dbi->deletePage($name);
116             $ul->pushContent(HTML::li(fmt("Removed page '%s' succesfully.", $name)));
117         }
118         $dbi->touch();
119         return HTML($ul,
120                     HTML::p(_('All selected pages have been permanently removed.')));
121     }
122     
123     function run($dbi, $argstr, $request) {
124         if ($request->getArg('action') != 'browse')
125             return $this->disabled("(action != 'browse')");
126         
127         $args = $this->getArgs($argstr, $request);
128         if (!is_numeric($args['min_age']))
129             $args['min_age'] = -1;
130         $this->_args = $args;
131         
132         if (!empty($args['exclude']))
133             $exclude = explodePageList($args['exclude']);
134         else
135             $exclude = false;
136
137
138         $p = $request->getArg('p');
139         $post_args = $request->getArg('admin_remove');
140
141         $next_action = 'select';
142         $pages = array();
143         
144         if ($p && $request->isPost() && $request->_user->isAdmin()
145             && !empty($post_args['remove']) && empty($post_args['cancel'])) {
146             // FIXME: error message if not admin.
147             if ($post_args['action'] == 'verify') {
148                 // Real delete.
149                 return $this->removePages($request, $p);
150             }
151
152             if ($post_args['action'] == 'select') {
153                 $next_action = 'verify';
154                 foreach ($p as $name) {
155                     $pages[$name] = 1;
156                 }
157             }
158         }
159         if ($next_action == 'select') {
160             // List all pages to select from.
161             $list = $this->collectPages($pages, $dbi);
162         }
163
164
165         $info = 'checkbox';
166         if ($args['info'])
167             $info .= "," . $args['info'];
168         $pagelist = new PageList_Selectable($info, $exclude);
169         $pagelist->addPageList($pages);
170
171         $header = HTML::p();
172         if ($next_action == 'verify') {
173             $button_label = _("Permanently remove selected pages");
174             $header->pushContent(HTML::strong(
175                 _("Are you sure you want to permanently remove the selected files?")));
176         }
177         else {
178             $button_label = _("Remove selected pages");
179             if ($args['min_age'] >= 0) {
180                 $header->pushContent(
181                     fmt("Listing pages which have been deleted at least %s days.",
182                         $args['min_age']));
183             }
184             else {
185                 $header->pushContent(_("Listing all pages."));
186             }
187             
188             if ($args['max_age'] >= 0) {
189                 $header->pushContent(
190                     " ",
191                     fmt("(Automatically checking pages which have been deleted at least %s days.)",
192                         $args['max_age']));
193             }
194         }
195
196
197         $buttons = HTML::p(Button('submit:admin_remove[remove]', $button_label, 'wikiadmin'),
198                            Button('submit:admin_remove[cancel]', _("Cancel"), 'button'));
199
200         return HTML::form(array('action' => $request->getPostURL(),
201                                 'method' => 'post'),
202
203                           $header,
204                           
205                           $pagelist->getContent(),
206
207                           HiddenInputs($request->getArgs(),
208                                         false,
209                                         array('admin_remove')),
210
211                           HiddenInputs(array('admin_remove[action]' => $next_action,
212                                              'require_authority_for_post' => WIKIAUTH_ADMIN)),
213                           $buttons);
214     }
215 }
216
217 // $Log: not supported by cvs2svn $
218 // Revision 1.7  2003/02/17 06:06:33  dairiki
219 // Refactor & code cleanup.
220 //
221 // Added two new plugin arguments:
222 //
223 //   min_age - only display pages which have been "deleted" for at
224 //             least this many days.  (Use min_age=none to get all
225 //             pages, even non-deleted ones listed.)
226 //
227 //   max_age - automatically check the checkboxes of pages which
228 //             have been "deleted" this many days or more.
229 //
230 // ("Deleted" means the current version of the page is empty.
231 // For the most part, PhpWiki treats these "deleted" pages as
232 // if they didn't exist --- but, of course, the PageHistory is
233 // still available, allowing old versions of the page to be restored.)
234 //
235 // Revision 1.6  2003/02/16 19:47:17  dairiki
236 // Update WikiDB timestamp when editing or deleting pages.
237 //
238 // Revision 1.5  2003/01/18 22:14:28  carstenklapp
239 // Code cleanup:
240 // Reformatting & tabs to spaces;
241 // Added copyleft, getVersion, getDescription, rcs_id.
242 //
243
244 // Local Variables:
245 // mode: php
246 // tab-width: 8
247 // c-basic-offset: 4
248 // c-hanging-comment-ender-p: nil
249 // indent-tabs-mode: nil
250 // End:
251 ?>