]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRemove.php
fixed pear/File_Passwd for Windows
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRemove.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminRemove.php,v 1.18 2004-04-07 23:13:19 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.18 $");
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                      'limit'    => 0
79                      );
80     }
81
82     function collectPages(&$list, &$dbi, $sortby, $limit=0) {
83         extract($this->_args);
84
85         $now = time();
86         
87         $allPages = $dbi->getAllPages('include_deleted',$sortby,$limit);
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         return $list;
110     }
111
112     function removePages(&$request, $pages) {
113         $ul = HTML::ul();
114         $dbi = $request->getDbh();
115         foreach ($pages as $name) {
116             $name = str_replace(array('%5B','%5D'),array('[',']'),$name);
117             $dbi->deletePage($name);
118             $ul->pushContent(HTML::li(fmt("Removed page '%s' successfully.", $name)));
119         }
120         $dbi->touch();
121         return HTML($ul,
122                     HTML::p(_('All selected pages have been permanently removed.')));
123     }
124     
125     function run($dbi, $argstr, &$request, $basepage) {
126         if ($request->getArg('action') != 'browse')
127             return $this->disabled("(action != 'browse')");
128         
129         $args = $this->getArgs($argstr, $request);
130         if (!is_numeric($args['min_age']))
131             $args['min_age'] = -1;
132         $this->_args = $args;
133         
134         if (!empty($args['exclude']))
135             $exclude = explodePageList($args['exclude']);
136         else
137             $exclude = false;
138
139
140         $p = $request->getArg('p');
141         $post_args = $request->getArg('admin_remove');
142
143         $next_action = 'select';
144         $pages = array();
145         
146         if ($p && $request->isPost() &&
147             !empty($post_args['remove']) && empty($post_args['cancel'])) {
148
149             // FIXME: check individual PagePermissions
150             if (!$request->_user->isAdmin()) {
151                 $request->_notAuthorized(WIKIAUTH_ADMIN);
152                 $this->disabled("! user->isAdmin");
153             }
154             if ($post_args['action'] == 'verify') {
155                 // Real delete.
156                 return $this->removePages($request, array_keys($p));
157             }
158
159             if ($post_args['action'] == 'select') {
160                 $next_action = 'verify';
161                 foreach ($p as $name => $c) {
162                     $name = str_replace(array('%5B','%5D'),array('[',']'),$name);
163                     $pages[$name] = $c;
164                 }
165             }
166         }
167         if ($next_action == 'select') {
168             // List all pages to select from.
169             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit']);
170         }
171         $pagelist = new PageList_Selectable($args['info'], $exclude, 
172                                             array('types' => 
173                                                   array('remove'
174                                                         => new _PageList_Column_remove('remove', _("Remove"))));
175         $pagelist->addPageList($pages);
176
177         $header = HTML::p();
178         if ($next_action == 'verify') {
179             $button_label = _("Yes");
180             $header->pushContent(HTML::strong(
181                 _("Are you sure you want to permanently remove the selected files?")));
182         }
183         else {
184             $button_label = _("Remove selected pages");
185             $header->pushContent(_("Permanently remove the selected files:"),HTML::br());
186             if ($args['min_age'] > 0) {
187                 $header->pushContent(
188                     fmt("Also pages which have been deleted at least %s days.",
189                         $args['min_age']));
190             }
191             else {
192                 $header->pushContent(_("List all pages."));
193             }
194             
195             if ($args['max_age'] > 0) {
196                 $header->pushContent(
197                     " ",
198                     fmt("(Pages which have been deleted at least %s days are already checked.)",
199                         $args['max_age']));
200             }
201         }
202
203
204         $buttons = HTML::p(Button('submit:admin_remove[remove]', $button_label, 'wikiadmin'),
205                            Button('submit:admin_remove[cancel]', _("Cancel"), 'button'));
206
207         return HTML::form(array('action' => $request->getPostURL(),
208                                 'method' => 'post'),
209
210                           $header,
211                           
212                           $pagelist->getContent(),
213
214                           HiddenInputs($request->getArgs(),
215                                         false,
216                                         array('admin_remove')),
217
218                           HiddenInputs(array('admin_remove[action]' => $next_action,
219                                              'require_authority_for_post' => WIKIAUTH_ADMIN)),
220                           $buttons);
221     }
222 }
223
224 class _PageList_Column_remove extends _PageList_Column {
225     function _getValue ($page_handle, &$revision_handle) {
226         return Button(array('action' => 'remove'), _("Remove"),
227                       $page_handle->getName());
228     }
229 };
230
231
232 // $Log: not supported by cvs2svn $
233 // Revision 1.17  2004/03/17 20:23:44  rurban
234 // fixed p[] pagehash passing from WikiAdminSelect, fixed problem removing pages with [] in the pagename
235 //
236 // Revision 1.16  2004/03/12 13:31:43  rurban
237 // enforce PagePermissions, errormsg if not Admin
238 //
239 // Revision 1.15  2004/03/01 13:48:46  rurban
240 // rename fix
241 // p[] consistency fix
242 //
243 // Revision 1.14  2004/02/22 23:20:33  rurban
244 // fixed DumpHtmlToDir,
245 // enhanced sortby handling in PageList
246 //   new button_heading th style (enabled),
247 // added sortby and limit support to the db backends and plugins
248 //   for paging support (<<prev, next>> links on long lists)
249 //
250 // Revision 1.13  2004/02/17 12:11:36  rurban
251 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
252 //
253 // Revision 1.12  2004/02/15 21:34:37  rurban
254 // PageList enhanced and improved.
255 // fixed new WikiAdmin... plugins
256 // editpage, Theme with exp. htmlarea framework
257 //   (htmlarea yet committed, this is really questionable)
258 // WikiUser... code with better session handling for prefs
259 // enhanced UserPreferences (again)
260 // RecentChanges for show_deleted: how should pages be deleted then?
261 //
262 // Revision 1.11  2004/02/11 20:00:16  rurban
263 // WikiAdmin... series overhaul. Rename misses the db backend methods yet. Chmod + Chwon still missing.
264 //
265 // Revision 1.9  2003/02/26 22:27:22  dairiki
266 // Fix and refactor FrameInclude plugin (more or less).
267 //
268 // (This should now generate valid HTML.  Woohoo!)
269 //
270 // The output when using the Sidebar theme is ugly enough that it should
271 // be considered broken.  (But the Sidebar theme appears pretty broken in
272 // general right now.)
273 //
274 // (Personal comment (not to be taken personally): I must say that I
275 // remain unconvinced of the usefulness of this plugin.)
276 //
277 // Revision 1.8  2003/02/17 17:23:59  dairiki
278 // Disable plugin unless action='browse'.
279 //
280 // Add a header to the output, and adjust the HTML formatting a bit.
281 //
282 // Revision 1.7  2003/02/17 06:06:33  dairiki
283 // Refactor & code cleanup.
284 //
285 // Added two new plugin arguments:
286 //
287 //   min_age - only display pages which have been "deleted" for at
288 //             least this many days.  (Use min_age=none to get all
289 //             pages, even non-deleted ones listed.)
290 //
291 //   max_age - automatically check the checkboxes of pages which
292 //             have been "deleted" this many days or more.
293 //
294 // ("Deleted" means the current version of the page is empty.
295 // For the most part, PhpWiki treats these "deleted" pages as
296 // if they didn't exist --- but, of course, the PageHistory is
297 // still available, allowing old versions of the page to be restored.)
298 //
299 // Revision 1.6  2003/02/16 19:47:17  dairiki
300 // Update WikiDB timestamp when editing or deleting pages.
301 //
302 // Revision 1.5  2003/01/18 22:14:28  carstenklapp
303 // Code cleanup:
304 // Reformatting & tabs to spaces;
305 // Added copyleft, getVersion, getDescription, rcs_id.
306 //
307
308 // Local Variables:
309 // mode: php
310 // tab-width: 8
311 // c-basic-offset: 4
312 // c-hanging-comment-ender-p: nil
313 // indent-tabs-mode: nil
314 // End:
315 ?>