]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRemove.php
getName should not translate
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRemove.php
1 <?php
2
3 /*
4  * Copyright 2002,2004 $ThePhpWikiProgrammingTeam
5  * Copyright 2008-2009 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 along
20  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * Usage:   <<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  */
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 getDescription()
40     {
41         return _("Permanently remove all selected pages.");
42     }
43
44     function getDefaultArguments()
45     {
46         return array_merge
47         (
48             WikiPlugin_WikiAdminSelect::getDefaultArguments(),
49             array(
50                 /*
51                  * Show only pages which have been 'deleted' this
52                  * long (in days).  (negative or non-numeric
53                  * means show all pages, even non-deleted ones.)
54                  *
55                  * FIXME: could use a better name.
56                  */
57                 'min_age' => 0,
58
59                 /*
60                  * Automatically check the checkboxes for files
61                  * which have been 'deleted' this long (in days).
62                  *
63                  * FIXME: could use a better name.
64                  */
65                 'max_age' => 31,
66                 /* Columns to include in listing */
67                 'info' => 'most',
68             ));
69     }
70
71     function collectPages(&$list, &$dbi, $sortby, $limit = 0)
72     {
73         extract($this->_args);
74
75         $now = time();
76
77         $allPages = $dbi->getAllPages('include_empty', $sortby, $limit);
78         while ($pagehandle = $allPages->next()) {
79             $pagename = $pagehandle->getName();
80             $current = $pagehandle->getCurrentRevision();
81             if ($current->getVersion() < 1)
82                 continue; // No versions in database
83
84             $empty = $current->hasDefaultContents();
85             if ($empty) {
86                 $age = ($now - $current->get('mtime')) / (24 * 3600.0);
87                 $checked = $age >= $max_age;
88             } else {
89                 $age = 0;
90                 $checked = false;
91             }
92
93             if ($age >= $min_age) {
94                 if (empty($list[$pagename]))
95                     $list[$pagename] = $checked;
96             }
97         }
98         return $list;
99     }
100
101     function removePages(&$request, $pages)
102     {
103         $result = HTML::div();
104         $ul = HTML::ul();
105         $dbi = $request->getDbh();
106         $count = 0;
107         foreach ($pages as $name) {
108             $name = str_replace(array('%5B', '%5D'), array('[', ']'), $name);
109             if (mayAccessPage('remove', $name)) {
110                 $dbi->deletePage($name);
111                 $ul->pushContent(HTML::li(fmt("Removed page ā€œ%sā€ successfully.", $name)));
112                 $count++;
113             } else {
114                 $ul->pushContent(HTML::li(fmt("Didn't remove page ā€œ%sā€. Access denied.", $name)));
115             }
116         }
117         if ($count) {
118             $dbi->touch();
119             $result->setAttr('class', 'feedback');
120             if ($count == 1) {
121                 $result->pushContent(HTML::p(_("One page has been removed:")));
122             } else {
123                 $result->pushContent(HTML::p(fmt("%d pages have been removed:", $count)));
124             }
125             $result->pushContent($ul);
126             return $result;
127         } else {
128             $result->setAttr('class', 'error');
129             $result->pushContent(HTML::p(_("No pages removed.")));
130             return $result;
131         }
132     }
133
134     function run($dbi, $argstr, &$request, $basepage)
135     {
136         if ($request->getArg('action') != 'browse') {
137             if ($request->getArg('action') != _("PhpWikiAdministration/Remove")) {
138                 return $this->disabled(_("Plugin not run: not in browse mode"));
139             }
140         }
141
142         $args = $this->getArgs($argstr, $request);
143         if (!is_numeric($args['min_age']))
144             $args['min_age'] = -1;
145         $this->_args =& $args;
146         /*if (!empty($args['exclude']))
147             $exclude = explodePageList($args['exclude']);
148         else
149         $exclude = false;*/
150         $this->preSelectS($args, $request);
151
152         $p = $request->getArg('p');
153         if (!$p) $p = $this->_list;
154         $post_args = $request->getArg('admin_remove');
155
156         $next_action = 'select';
157         $pages = array();
158         if ($p && $request->isPost() &&
159             !empty($post_args['remove']) && empty($post_args['cancel'])
160         ) {
161
162             // check individual PagePermissions
163             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
164                 $request->_notAuthorized(WIKIAUTH_ADMIN);
165                 $this->disabled("! user->isAdmin");
166             }
167             if ($post_args['action'] == 'verify') {
168                 // Real delete.
169                 return $this->removePages($request, array_keys($p));
170             }
171
172             if ($post_args['action'] == 'select') {
173                 $next_action = 'verify';
174                 foreach ($p as $name => $c) {
175                     $name = str_replace(array('%5B', '%5D'), array('[', ']'), $name);
176                     $pages[$name] = $c;
177                 }
178             }
179         } elseif ($p && is_array($p) && !$request->isPost()) { // from WikiAdminSelect
180             $next_action = 'verify';
181             foreach ($p as $name => $c) {
182                 $name = str_replace(array('%5B', '%5D'), array('[', ']'), $name);
183                 $pages[$name] = $c;
184             }
185             $request->setArg('p', false);
186         }
187         if ($next_action == 'select') {
188             // List all pages to select from.
189             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
190         }
191         $pagelist = new PageList_Selectable($args['info'], $args['exclude'],
192             array('types' =>
193             array('remove'
194             => new _PageList_Column_remove('remove', _("Remove")))));
195         $pagelist->addPageList($pages);
196
197         $header = HTML::fieldset();
198         if ($next_action == 'verify') {
199             $button_label = _("Yes");
200             $header->pushContent(HTML::legend(_("Confirm removal")));
201             $header->pushContent(HTML::p(HTML::strong(
202                 _("Are you sure you want to remove the selected files?"))));
203         } else {
204             $button_label = _("Remove selected pages");
205             $header->pushContent(HTML::legend(_("Select the files to remove")));
206             if ($args['min_age'] > 0) {
207                 $header->pushContent(
208                     fmt("Also pages which have been deleted at least %s days.",
209                         $args['min_age']));
210             }
211
212             if ($args['max_age'] > 0) {
213                 $header->pushContent(
214                     " ",
215                     fmt("Pages which have been deleted at least %s days are already checked.",
216                         $args['max_age']));
217             }
218         }
219
220         $buttons = HTML::p(Button('submit:admin_remove[remove]', $button_label, 'wikiadmin'),
221             Button('submit:admin_remove[cancel]', _("Cancel"), 'button'));
222         $header->pushContent($buttons);
223
224         // TODO: quick select by regex javascript?
225         return HTML::form(array('action' => $request->getPostURL(),
226                 'method' => 'post'),
227             $header,
228             $pagelist->getContent(),
229             HiddenInputs($request->getArgs(),
230                 false,
231                 array('admin_remove')),
232             HiddenInputs(array('admin_remove[action]' => $next_action,
233                 'require_authority_for_post' => WIKIAUTH_ADMIN)));
234     }
235 }
236
237 class _PageList_Column_remove extends _PageList_Column
238 {
239     function _getValue($page_handle, &$revision_handle)
240     {
241         return Button(array('action' => 'remove'), _("Remove"),
242             $page_handle->getName());
243     }
244 }
245
246 // Local Variables:
247 // mode: php
248 // tab-width: 8
249 // c-basic-offset: 4
250 // c-hanging-comment-ender-p: nil
251 // indent-tabs-mode: nil
252 // End: