]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRemove.php
Update poll
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRemove.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  Copyright 2002,2004 $ThePhpWikiProgrammingTeam
5  Copyright 2008 Marc-Etienne Vargenau, Alcatel-Lucent
6
7  This file is part of PhpWiki.
8
9  PhpWiki is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  PhpWiki is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with PhpWiki; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /**
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 require_once('lib/plugin/WikiAdminSelect.php');
36
37 class WikiPlugin_WikiAdminRemove
38 extends WikiPlugin_WikiAdminSelect
39 {
40     function getName() {
41         return _("WikiAdminRemove");
42     }
43
44     function getDescription() {
45         return _("Permanently remove all selected pages.");
46     }
47
48     function getVersion() {
49         return preg_replace("/[Revision: $]/", '',
50                             "\$Revision$");
51     }
52
53     function getDefaultArguments() {
54         return array_merge
55             (
56              PageList::supportedArgs(),
57              array(
58                    's'  => false,
59                      /*
60                       * Show only pages which have been 'deleted' this
61                       * long (in days).  (negative or non-numeric
62                       * means show all pages, even non-deleted ones.)
63                       *
64                       * FIXME: could use a better name.
65                       */
66                      'min_age' => 0,
67
68                      /*
69                       * Automatically check the checkboxes for files
70                       * which have been 'deleted' this long (in days).
71                       *
72                       * FIXME: could use a better name.
73                       */
74                      'max_age' => 31,
75                      /* Columns to include in listing */
76                      'info'     => 'most',
77                    ));
78     }
79
80     function collectPages(&$list, &$dbi, $sortby, $limit=0) {
81         extract($this->_args);
82
83         $now = time();
84         
85         $allPages = $dbi->getAllPages('include_empty',$sortby,$limit);
86         while ($pagehandle = $allPages->next()) {
87             $pagename = $pagehandle->getName();
88             $current = $pagehandle->getCurrentRevision();
89             if ($current->getVersion() < 1)
90                 continue;       // No versions in database
91
92             $empty = $current->hasDefaultContents();
93             if ($empty) {
94                 $age = ($now - $current->get('mtime')) / (24 * 3600.0);
95                 $checked = $age >= $max_age;
96             }
97             else {
98                 $age = 0;
99                 $checked = false;
100             }
101
102             if ($age >= $min_age) {
103                 if (empty($list[$pagename]))
104                     $list[$pagename] = $checked;
105             }
106         }
107         return $list;
108     }
109
110     function removePages(&$request, $pages) {
111         $ul = HTML::ul();
112         $dbi = $request->getDbh(); $count = 0;
113         foreach ($pages as $name) {
114             $name = str_replace(array('%5B','%5D'),array('[',']'),$name);
115             if (mayAccessPage('remove',$name)) {
116                 $dbi->deletePage($name);
117                 $ul->pushContent(HTML::li(fmt("Removed page '%s' successfully.", $name)));
118                 $count++;
119             } else {
120                 $ul->pushContent(HTML::li(fmt("Didn't removed page '%s'. Access denied.", $name)));
121             }
122         }
123         if ($count) $dbi->touch();
124         return HTML($ul,
125                     HTML::p(fmt("%d pages have been permanently removed.",$count)));
126     }
127     
128     function run($dbi, $argstr, &$request, $basepage) {
129         if ($request->getArg('action') != 'browse')
130             if ($request->getArg('action') != _("PhpWikiAdministration/Remove"))
131                 return $this->disabled("(action != 'browse')");
132         
133         $args = $this->getArgs($argstr, $request);
134         if (!is_numeric($args['min_age']))
135             $args['min_age'] = -1;
136         $this->_args =& $args;
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'], $args['exclude']);
180         }
181         $pagelist = new PageList_Selectable($args['info'], $args['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                           $buttons,
222                           $pagelist->getContent(),
223                           HiddenInputs($request->getArgs(),
224                                         false,
225                                         array('admin_remove')),
226                           HiddenInputs(array('admin_remove[action]' => $next_action,
227                                              'require_authority_for_post' => WIKIAUTH_ADMIN)));
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 // Local Variables:
239 // mode: php
240 // tab-width: 8
241 // c-basic-offset: 4
242 // c-hanging-comment-ender-p: nil
243 // indent-tabs-mode: nil
244 // End:
245 ?>