]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRemove.php
PageList enhanced and improved.
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRemove.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminRemove.php,v 1.12 2004-02-15 21:34:37 rurban Exp $');
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
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.12 $");
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 or regex to exclude */
71                      'exclude'  => '',
72
73                      /* Columns to include in listing */
74                      'info'     => 'most',
75
76                      /* How to sort */
77                      'sortby'   => 'pagename'
78                      );
79     }
80
81     function collectPages(&$list, &$dbi, $sortby) {
82         extract($this->_args);
83
84         $now = time();
85         
86         $allPages = $dbi->getAllPages('include_deleted',$sortby);
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         return $list;
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' successfully.", $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             $pages = $this->collectPages($pages, $dbi, $args['sortby']);
162         }
163
164         $pagelist = new PageList_Selectable($args['info'], $exclude);
165         $pagelist->addPageList($pages);
166
167         $header = HTML::p();
168         if ($next_action == 'verify') {
169             $button_label = _("Yes");
170             $header->pushContent(HTML::strong(
171                 _("Are you sure you want to permanently remove the selected files?")));
172         }
173         else {
174             $button_label = _("Remove selected pages");
175             $header->pushContent(_("Permanently remove the selected files:"),HTML::br());
176             if ($args['min_age'] > 0) {
177                 $header->pushContent(
178                     fmt("Also pages which have been deleted at least %s days.",
179                         $args['min_age']));
180             }
181             else {
182                 $header->pushContent(_("List all pages."));
183             }
184             
185             if ($args['max_age'] > 0) {
186                 $header->pushContent(
187                     " ",
188                     fmt("(Pages which have been deleted at least %s days are already checked.)",
189                         $args['max_age']));
190             }
191         }
192
193
194         $buttons = HTML::p(Button('submit:admin_remove[remove]', $button_label, 'wikiadmin'),
195                            Button('submit:admin_remove[cancel]', _("Cancel"), 'button'));
196
197         return HTML::form(array('action' => $request->getPostURL(),
198                                 'method' => 'post'),
199
200                           $header,
201                           
202                           $pagelist->getContent(),
203
204                           HiddenInputs($request->getArgs(),
205                                         false,
206                                         array('admin_remove')),
207
208                           HiddenInputs(array('admin_remove[action]' => $next_action,
209                                              'require_authority_for_post' => WIKIAUTH_ADMIN)),
210                           $buttons);
211     }
212 }
213
214 // $Log: not supported by cvs2svn $
215 // Revision 1.11  2004/02/11 20:00:16  rurban
216 // WikiAdmin... series overhaul. Rename misses the db backend methods yet. Chmod + Chwon still missing.
217 //
218 // Revision 1.9  2003/02/26 22:27:22  dairiki
219 // Fix and refactor FrameInclude plugin (more or less).
220 //
221 // (This should now generate valid HTML.  Woohoo!)
222 //
223 // The output when using the Sidebar theme is ugly enough that it should
224 // be considered broken.  (But the Sidebar theme appears pretty broken in
225 // general right now.)
226 //
227 // (Personal comment (not to be taken personally): I must say that I
228 // remain unconvinced of the usefulness of this plugin.)
229 //
230 // Revision 1.8  2003/02/17 17:23:59  dairiki
231 // Disable plugin unless action='browse'.
232 //
233 // Add a header to the output, and adjust the HTML formatting a bit.
234 //
235 // Revision 1.7  2003/02/17 06:06:33  dairiki
236 // Refactor & code cleanup.
237 //
238 // Added two new plugin arguments:
239 //
240 //   min_age - only display pages which have been "deleted" for at
241 //             least this many days.  (Use min_age=none to get all
242 //             pages, even non-deleted ones listed.)
243 //
244 //   max_age - automatically check the checkboxes of pages which
245 //             have been "deleted" this many days or more.
246 //
247 // ("Deleted" means the current version of the page is empty.
248 // For the most part, PhpWiki treats these "deleted" pages as
249 // if they didn't exist --- but, of course, the PageHistory is
250 // still available, allowing old versions of the page to be restored.)
251 //
252 // Revision 1.6  2003/02/16 19:47:17  dairiki
253 // Update WikiDB timestamp when editing or deleting pages.
254 //
255 // Revision 1.5  2003/01/18 22:14:28  carstenklapp
256 // Code cleanup:
257 // Reformatting & tabs to spaces;
258 // Added copyleft, getVersion, getDescription, rcs_id.
259 //
260
261 // Local Variables:
262 // mode: php
263 // tab-width: 8
264 // c-basic-offset: 4
265 // c-hanging-comment-ender-p: nil
266 // indent-tabs-mode: nil
267 // End:
268 ?>