]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRemove.php
seperate PassUser methods into seperate dir (memory usage)
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRemove.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminRemove.php,v 1.28 2004-11-01 10:43:59 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 require_once('lib/plugin/WikiAdminSelect.php');
35
36 class WikiPlugin_WikiAdminRemove
37 extends WikiPlugin_WikiAdminSelect
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.28 $");
50     }
51
52     function getDefaultArguments() {
53         return array_merge
54             (
55              PageList::supportedArgs(),
56              array(
57                    's'  => false,
58                      /*
59                       * Show only pages which have been 'deleted' this
60                       * long (in days).  (negative or non-numeric
61                       * means show all pages, even non-deleted ones.)
62                       *
63                       * FIXME: could use a better name.
64                       */
65                      'min_age' => 0,
66
67                      /*
68                       * Automatically check the checkboxes for files
69                       * which have been 'deleted' this long (in days).
70                       *
71                       * FIXME: could use a better name.
72                       */
73                      'max_age' => 31,
74                      /* Columns to include in listing */
75                      'info'     => 'most',
76                    ));
77     }
78
79     function collectPages(&$list, &$dbi, $sortby, $limit=0) {
80         extract($this->_args);
81
82         $now = time();
83         
84         $allPages = $dbi->getAllPages('include_deleted',$sortby,$limit);
85         while ($pagehandle = $allPages->next()) {
86             $pagename = $pagehandle->getName();
87             $current = $pagehandle->getCurrentRevision();
88             if ($current->getVersion() < 1)
89                 continue;       // No versions in database
90
91             $empty = $current->hasDefaultContents();
92             if ($empty) {
93                 $age = ($now - $current->get('mtime')) / (24 * 3600.0);
94                 $checked = $age >= $max_age;
95             }
96             else {
97                 $age = 0;
98                 $checked = false;
99             }
100
101             if ($age >= $min_age) {
102                 if (empty($list[$pagename]))
103                     $list[$pagename] = $checked;
104             }
105         }
106         return $list;
107     }
108
109     function removePages(&$request, $pages) {
110         $ul = HTML::ul();
111         $dbi = $request->getDbh(); $count = 0;
112         foreach ($pages as $name) {
113             $name = str_replace(array('%5B','%5D'),array('[',']'),$name);
114             if (mayAccessPage('remove',$name)) {
115                 $dbi->deletePage($name);
116                 $ul->pushContent(HTML::li(fmt("Removed page '%s' successfully.", $name)));
117                 $count++;
118             } else {
119                 $ul->pushContent(HTML::li(fmt("Didn't removed page '%s'. Access denied.", $name)));
120             }
121         }
122         if ($count) $dbi->touch();
123         return HTML($ul,
124                     HTML::p(fmt("%d pages have been permanently removed.",$count)));
125     }
126     
127     function run($dbi, $argstr, &$request, $basepage) {
128         if ($request->getArg('action') != 'browse')
129             if ($request->getArg('action') != _("PhpWikiAdministration/Remove"))
130                 return $this->disabled("(action != 'browse')");
131         
132         $args = $this->getArgs($argstr, $request);
133         if (!is_numeric($args['min_age']))
134             $args['min_age'] = -1;
135         $this->_args =& $args;
136         
137         if (!empty($args['exclude']))
138             $exclude = explodePageList($args['exclude']);
139         else
140             $exclude = false;
141         $this->preSelectS($args, $request);
142
143         $p = $request->getArg('p');
144         if (!$p) $p = $this->_list;
145         $post_args = $request->getArg('admin_remove');
146
147         $next_action = 'select';
148         $pages = array();
149         if ($p && $request->isPost() &&
150             !empty($post_args['remove']) && empty($post_args['cancel'])) {
151
152             // check individual PagePermissions
153             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
154                 $request->_notAuthorized(WIKIAUTH_ADMIN);
155                 $this->disabled("! user->isAdmin");
156             }
157             if ($post_args['action'] == 'verify') {
158                 // Real delete.
159                 return $this->removePages($request, array_keys($p));
160             }
161
162             if ($post_args['action'] == 'select') {
163                 $next_action = 'verify';
164                 foreach ($p as $name => $c) {
165                     $name = str_replace(array('%5B','%5D'),array('[',']'),$name);
166                     $pages[$name] = $c;
167                 }
168             }
169         } elseif ($p && is_array($p) && !$request->isPost()) { // from WikiAdminSelect
170             $next_action = 'verify';
171             foreach ($p as $name => $c) {
172                 $name = str_replace(array('%5B','%5D'),array('[',']'),$name);
173                 $pages[$name] = $c;
174             }
175             $request->setArg('p',false);
176         }
177         if ($next_action == 'select') {
178             // List all pages to select from.
179             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit']);
180         }
181         $pagelist = new PageList_Selectable($args['info'], $exclude, 
182                                             array('types' => 
183                                                   array('remove'
184                                                         => new _PageList_Column_remove('remove', _("Remove")))));
185         $pagelist->addPageList($pages);
186
187         $header = HTML::p();
188         if ($next_action == 'verify') {
189             $button_label = _("Yes");
190             $header->pushContent(HTML::strong(
191                 _("Are you sure you want to permanently remove the selected files?")));
192         }
193         else {
194             $button_label = _("Remove selected pages");
195             $header->pushContent(_("Permanently remove the selected files:"),HTML::br());
196             if ($args['min_age'] > 0) {
197                 $header->pushContent(
198                     fmt("Also pages which have been deleted at least %s days.",
199                         $args['min_age']));
200             }
201             else {
202                 $header->pushContent(_("List all pages."));
203             }
204             
205             if ($args['max_age'] > 0) {
206                 $header->pushContent(
207                     " ",
208                     fmt("(Pages which have been deleted at least %s days are already checked.)",
209                         $args['max_age']));
210             }
211         }
212
213
214         $buttons = HTML::p(Button('submit:admin_remove[remove]', $button_label, 'wikiadmin'),
215                            Button('submit:admin_remove[cancel]', _("Cancel"), 'button'));
216
217         // TODO: quick select by regex javascript?
218         return HTML::form(array('action' => $request->getPostURL(),
219                                 'method' => 'post'),
220                           $header,
221                           $pagelist->getContent(),
222                           HiddenInputs($request->getArgs(),
223                                         false,
224                                         array('admin_remove')),
225                           HiddenInputs(array('admin_remove[action]' => $next_action,
226                                              'require_authority_for_post' => WIKIAUTH_ADMIN)),
227                           $buttons);
228     }
229 }
230
231 class _PageList_Column_remove extends _PageList_Column {
232     function _getValue ($page_handle, &$revision_handle) {
233         return Button(array('action' => 'remove'), _("Remove"),
234                       $page_handle->getName());
235     }
236 };
237
238
239 // $Log: not supported by cvs2svn $
240 // Revision 1.27  2004/06/16 10:38:59  rurban
241 // Disallow refernces in calls if the declaration is a reference
242 // ("allow_call_time_pass_reference clean").
243 //   PhpWiki is now allow_call_time_pass_reference = Off clean,
244 //   but several external libraries may not.
245 //   In detail these libs look to be affected (not tested):
246 //   * Pear_DB odbc
247 //   * adodb oracle
248 //
249 // Revision 1.26  2004/06/14 11:31:39  rurban
250 // renamed global $Theme to $WikiTheme (gforge nameclash)
251 // inherit PageList default options from PageList
252 //   default sortby=pagename
253 // use options in PageList_Selectable (limit, sortby, ...)
254 // added action revert, with button at action=diff
255 // added option regex to WikiAdminSearchReplace
256 //
257 // Revision 1.25  2004/06/13 15:33:20  rurban
258 // new support for arguments owner, author, creator in most relevant
259 // PageList plugins. in WikiAdmin* via preSelectS()
260 //
261 // Revision 1.24  2004/06/08 10:05:11  rurban
262 // simplified admin action shortcuts
263 //
264 // Revision 1.23  2004/06/03 22:24:48  rurban
265 // reenable admin check on !ENABLE_PAGEPERM, honor s=Wildcard arg, fix warning after Remove
266 //
267 // Revision 1.22  2004/05/16 22:07:35  rurban
268 // check more config-default and predefined constants
269 // various PagePerm fixes:
270 //   fix default PagePerms, esp. edit and view for Bogo and Password users
271 //   implemented Creator and Owner
272 //   BOGOUSERS renamed to BOGOUSER
273 // fixed syntax errors in signin.tmpl
274 //
275 // Revision 1.21  2004/05/04 16:34:22  rurban
276 // prvent hidden p overwrite checked p
277 //
278 // Revision 1.20  2004/05/03 11:02:30  rurban
279 // fix passing args from WikiAdminSelect to WikiAdminRemove
280 //
281 // Revision 1.19  2004/04/12 09:12:23  rurban
282 // fix syntax errors
283 //
284 // Revision 1.18  2004/04/07 23:13:19  rurban
285 // fixed pear/File_Passwd for Windows
286 // fixed FilePassUser sessions (filehandle revive) and password update
287 //
288 // Revision 1.17  2004/03/17 20:23:44  rurban
289 // fixed p[] pagehash passing from WikiAdminSelect, fixed problem removing pages with [] in the pagename
290 //
291 // Revision 1.16  2004/03/12 13:31:43  rurban
292 // enforce PagePermissions, errormsg if not Admin
293 //
294 // Revision 1.15  2004/03/01 13:48:46  rurban
295 // rename fix
296 // p[] consistency fix
297 //
298 // Revision 1.14  2004/02/22 23:20:33  rurban
299 // fixed DumpHtmlToDir,
300 // enhanced sortby handling in PageList
301 //   new button_heading th style (enabled),
302 // added sortby and limit support to the db backends and plugins
303 //   for paging support (<<prev, next>> links on long lists)
304 //
305 // Revision 1.13  2004/02/17 12:11:36  rurban
306 // 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, ...)
307 //
308 // Revision 1.12  2004/02/15 21:34:37  rurban
309 // PageList enhanced and improved.
310 // fixed new WikiAdmin... plugins
311 // editpage, Theme with exp. htmlarea framework
312 //   (htmlarea yet committed, this is really questionable)
313 // WikiUser... code with better session handling for prefs
314 // enhanced UserPreferences (again)
315 // RecentChanges for show_deleted: how should pages be deleted then?
316 //
317 // Revision 1.11  2004/02/11 20:00:16  rurban
318 // WikiAdmin... series overhaul. Rename misses the db backend methods yet. Chmod + Chwon still missing.
319 //
320 // Revision 1.9  2003/02/26 22:27:22  dairiki
321 // Fix and refactor FrameInclude plugin (more or less).
322 //
323 // (This should now generate valid HTML.  Woohoo!)
324 //
325 // The output when using the Sidebar theme is ugly enough that it should
326 // be considered broken.  (But the Sidebar theme appears pretty broken in
327 // general right now.)
328 //
329 // (Personal comment (not to be taken personally): I must say that I
330 // remain unconvinced of the usefulness of this plugin.)
331 //
332 // Revision 1.8  2003/02/17 17:23:59  dairiki
333 // Disable plugin unless action='browse'.
334 //
335 // Add a header to the output, and adjust the HTML formatting a bit.
336 //
337 // Revision 1.7  2003/02/17 06:06:33  dairiki
338 // Refactor & code cleanup.
339 //
340 // Added two new plugin arguments:
341 //
342 //   min_age - only display pages which have been "deleted" for at
343 //             least this many days.  (Use min_age=none to get all
344 //             pages, even non-deleted ones listed.)
345 //
346 //   max_age - automatically check the checkboxes of pages which
347 //             have been "deleted" this many days or more.
348 //
349 // ("Deleted" means the current version of the page is empty.
350 // For the most part, PhpWiki treats these "deleted" pages as
351 // if they didn't exist --- but, of course, the PageHistory is
352 // still available, allowing old versions of the page to be restored.)
353 //
354 // Revision 1.6  2003/02/16 19:47:17  dairiki
355 // Update WikiDB timestamp when editing or deleting pages.
356 //
357 // Revision 1.5  2003/01/18 22:14:28  carstenklapp
358 // Code cleanup:
359 // Reformatting & tabs to spaces;
360 // Added copyleft, getVersion, getDescription, rcs_id.
361 //
362
363 // Local Variables:
364 // mode: php
365 // tab-width: 8
366 // c-basic-offset: 4
367 // c-hanging-comment-ender-p: nil
368 // indent-tabs-mode: nil
369 // End:
370 ?>