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