]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminPurge.php
New FSF address
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminPurge.php
1 <?php // -*-php-*-
2 // $Id$
3 /*
4  * Copyright 2002,2004 $ThePhpWikiProgrammingTeam
5  * Copyright 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:   <<WikiAdminPurge>>
26  */
27 require_once('lib/PageList.php');
28 require_once('lib/plugin/WikiAdminSelect.php');
29
30 class WikiPlugin_WikiAdminPurge
31 extends WikiPlugin_WikiAdminSelect
32 {
33     function getName() {
34         return _("WikiAdminPurge");
35     }
36
37     function getDescription() {
38         return _("Permanently purge all selected pages.");
39     }
40
41     /* getDefaultArguments() is inherited from WikiAdminSelect class */
42
43     function collectPages(&$list, &$dbi, $sortby, $limit=0) {
44
45         $allPages = $dbi->getAllPages('include_empty',$sortby,$limit);
46         while ($pagehandle = $allPages->next()) {
47             $pagename = $pagehandle->getName();
48             $current = $pagehandle->getCurrentRevision();
49             if ($current->getVersion() < 1) {
50                 continue;       // No versions in database
51             }
52             if (empty($list[$pagename])) {
53                 $list[$pagename] = false;
54             }
55         }
56         return $list;
57     }
58
59     function purgePages(&$request, $pages) {
60         $result = HTML::div();
61         $ul = HTML::ul();
62         $dbi = $request->getDbh(); $count = 0;
63         foreach ($pages as $name) {
64             $name = str_replace(array('%5B','%5D'),array('[',']'),$name);
65             if (mayAccessPage('purge',$name)) {
66                 $dbi->purgePage($name);
67                 $ul->pushContent(HTML::li(fmt("Purged page '%s' successfully.", $name)));
68                 $count++;
69             } else {
70                     $ul->pushContent(HTML::li(fmt("Didn't purge page '%s'. Access denied.", $name)));
71             }
72         }
73         if ($count) {
74             $dbi->touch();
75             $result->setAttr('class', 'feedback');
76             if ($count == 1) {
77                 $result->pushContent(HTML::p(_("One page has been permanently purged:")));
78             } else {
79                 $result->pushContent(HTML::p(fmt("%d pages have been permanently purged:", $count)));
80             }
81             $result->pushContent($ul);
82             return $result;
83         } else {
84             $result->setAttr('class', 'error');
85             $result->pushContent(HTML::p(_("No pages purged.")));
86             return $result;
87         }
88     }
89
90     function run($dbi, $argstr, &$request, $basepage) {
91         if ($request->getArg('action') != 'browse') {
92             if ($request->getArg('action') != _("PhpWikiAdministration/Purge")) {
93                 return $this->disabled(_("Plugin not run: not in browse mode"));
94             }
95         }
96
97         $args = $this->getArgs($argstr, $request);
98         $this->_args =& $args;
99         $this->preSelectS($args, $request);
100
101         $p = $request->getArg('p');
102         if (!$p) $p = $this->_list;
103         $post_args = $request->getArg('admin_purge');
104
105         $next_action = 'select';
106         $pages = array();
107         if ($p && $request->isPost() &&
108             !empty($post_args['purge']) && empty($post_args['cancel'])) {
109
110             // check individual PagePermissions
111             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
112                 $request->_notAuthorized(WIKIAUTH_ADMIN);
113                 $this->disabled("! user->isAdmin");
114             }
115             if ($post_args['action'] == 'verify') {
116                 // Real purge.
117                 return $this->purgePages($request, array_keys($p));
118             }
119
120             if ($post_args['action'] == 'select') {
121                 $next_action = 'verify';
122                 foreach ($p as $name => $c) {
123                     $name = str_replace(array('%5B','%5D'),array('[',']'),$name);
124                     $pages[$name] = $c;
125                 }
126             }
127         } elseif ($p && is_array($p) && !$request->isPost()) { // from WikiAdminSelect
128             $next_action = 'verify';
129             foreach ($p as $name => $c) {
130                 $name = str_replace(array('%5B','%5D'),array('[',']'),$name);
131                 $pages[$name] = $c;
132             }
133             $request->setArg('p',false);
134         }
135         if ($next_action == 'select') {
136             // List all pages to select from.
137             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
138         }
139         $pagelist = new PageList_Selectable($args['info'], $args['exclude'], array());
140         $pagelist->addPageList($pages);
141
142         $header = HTML::fieldset();
143         if ($next_action == 'verify') {
144             $button_label = _("Yes");
145             $header->pushContent(HTML::p(HTML::strong(
146                 _("Are you sure you want to permanently purge the following files?"))));
147         }
148         else {
149             $button_label = _("Permanently purge selected pages");
150             $header->pushContent(HTML::legend(_("Select the files to purge")));
151         }
152
153         $buttons = HTML::p(Button('submit:admin_purge[purge]', $button_label, 'wikiadmin'),
154                            Button('submit:admin_purge[cancel]', _("Cancel"), 'button'));
155         $header->pushContent($buttons);
156
157         // TODO: quick select by regex javascript?
158         return HTML::form(array('action' => $request->getPostURL(),
159                                 'method' => 'post'),
160                           $header,
161                           $pagelist->getContent(),
162                           HiddenInputs($request->getArgs(),
163                                         false,
164                                         array('admin_purge')),
165                           HiddenInputs(array('admin_purge[action]' => $next_action,
166                                              'require_authority_for_post' => WIKIAUTH_ADMIN)));
167     }
168 }
169
170 // Local Variables:
171 // mode: php
172 // tab-width: 8
173 // c-basic-offset: 4
174 // c-hanging-comment-ender-p: nil
175 // indent-tabs-mode: nil
176 // End:
177 ?>