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