]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminUtils.php
access-restrictions are not implemented
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminUtils.php
1 <?php
2
3 /**
4  * Copyright 2003,2004,2006 $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 valid actions:
26 purge-cache
27 purge-bad-pagenames
28 purge-empty-pages
29 email-verification
30 convert-cached-html
31 db-check
32 db-rebuild
33  */
34 class WikiPlugin_WikiAdminUtils
35     extends WikiPlugin
36 {
37     function getName()
38     {
39         return _("WikiAdminUtils");
40     }
41
42     function getDescription()
43     {
44         return _("Miscellaneous utility functions for the Administrator.");
45     }
46
47     function getDefaultArguments()
48     {
49         return array('action' => '',
50             'label' => '',
51         );
52     }
53
54     function run($dbi, $argstr, &$request, $basepage)
55     {
56         $args = $this->getArgs($argstr, $request);
57         $args['action'] = strtolower($args['action']);
58         extract($args);
59
60         if (!$action) {
61             $this->error("No action specified");
62         }
63         if (!($default_label = $this->_getLabel($action))) {
64             return HTML::div(array('class' => "error"), fmt("Bad action requested: %s", $action));
65         }
66         if ($request->getArg('action') != 'browse') {
67             return $this->disabled(_("Plugin not run: not in browse mode"));
68         }
69
70         $posted = $request->getArg('wikiadminutils');
71
72         if ($request->isPost() and $posted['action'] == $action) { // a different form. we might have multiple
73             $user = $request->getUser();
74             if (!$user->isAdmin()) {
75                 $request->_notAuthorized(WIKIAUTH_ADMIN);
76                 return $this->error(_("You must be an administrator to use this plugin."));
77             }
78             return $this->do_action($request, $posted);
79         }
80         if (empty($label))
81             $label = $default_label;
82
83         return $this->_makeButton($request, $args, $label);
84     }
85
86     private function _makeButton(&$request, $args, $label)
87     {
88         $args['return_url'] = $request->getURLtoSelf();
89         return HTML::form(array('action' => $request->getPostURL(),
90                 'method' => 'post'),
91             HTML::p(Button('submit:', $label, 'wikiadmin')),
92             HiddenInputs($args, 'wikiadminutils'),
93             HiddenInputs(array('require_authority_for_post' =>
94             WIKIAUTH_ADMIN)),
95             HiddenInputs($request->getArgs(), false, array('action')));
96     }
97
98     function do_action(&$request, $args)
99     {
100         $method = strtolower('_do_' . str_replace('-', '_', $args['action']));
101         if (!method_exists($this, $method))
102             return $this->error("Bad action $method");
103
104         $message = call_user_func(array(&$this, $method), $request, $args);
105
106         // display as separate page or as alert?
107         $alert = new Alert(fmt("WikiAdminUtils %s returned:", $args['action']),
108             $message,
109             array(_("Back") => $args['return_url']));
110         $alert->show(); // noreturn
111     }
112
113     private function _getLabel($action)
114     {
115         $labels = array('purge-cache' => _("Purge Markup Cache"),
116             'purge-bad-pagenames' => _("Purge all Pages With Invalid Names"),
117             'purge-empty-pages' => _("Purge all empty, unreferenced Pages"),
118             'email-verification' => _("E-mail address confirmation"),
119             'convert-cached-html' => _("Convert cached_html"),
120             'db-check' => _("DB Check"),
121             'db-rebuild' => _("Db Rebuild")
122         );
123         return @$labels[$action];
124     }
125
126     private function _do_purge_cache(&$request, $args)
127     {
128         $dbi = $request->getDbh();
129         $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
130         while (($page = $pages->next())) {
131             $page->set('_cached_html', false);
132         }
133         return _("Markup cache purged!");
134     }
135
136     private function _do_purge_bad_pagenames(&$request, $args)
137     {
138         // FIXME: this should be moved into WikiDB::normalize() or something...
139         $dbi = $request->getDbh();
140         $count = 0;
141         $list = HTML::ol(array('align' => 'left'));
142         $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
143         while (($page = $pages->next())) {
144             $pagename = $page->getName();
145             $wpn = new WikiPageName($pagename);
146             if (!$wpn->isValid()) {
147                 $dbi->purgePage($pagename);
148                 $list->pushContent(HTML::li($pagename));
149                 $count++;
150             }
151         }
152         $pages->free();
153         if (!$count)
154             return _("No pages with bad names had to be deleted.");
155         else {
156             return HTML(fmt("Deleted %d pages with invalid names:", $count),
157                 HTML::div(array('align' => 'left'), $list));
158         }
159     }
160
161     /**
162      * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
163      */
164     private function _do_purge_empty_pages(&$request, $args)
165     {
166         $dbi = $request->getDbh();
167         $count = 0;
168         $notpurgable = 0;
169         $list = HTML::ol(array('align' => 'left'));
170         $pages = $dbi->getAllPages('include_empty');
171         while (($page = $pages->next())) {
172             if (!$page->exists()
173                 and ($links = $page->getBackLinks('include_empty'))
174                     and !$links->next()
175             ) {
176                 $pagename = $page->getName();
177                 if ($pagename == 'global_data' or $pagename == '.') continue;
178                 if ($dbi->purgePage($pagename))
179                     $list->pushContent(HTML::li($pagename . ' ' . _("[purged]")));
180                 else {
181                     $list->pushContent(HTML::li($pagename . ' ' . _("[not purgable]")));
182                     $notpurgable++;
183                 }
184                 $count++;
185             }
186         }
187         $pages->free();
188         if (!$count)
189             return _("No empty, unreferenced pages were found.");
190         else
191             return HTML(fmt("Deleted %d unreferenced pages:", $count),
192                 HTML::div(array('align' => 'left'), $list),
193                 ($notpurgable ?
194                     fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.",
195                         $notpurgable)
196                     : ''));
197     }
198
199     private function _do_convert_cached_html(&$request, $args)
200     {
201
202         require_once 'lib/upgrade.php';
203         $dbh = $request->_dbi;
204         _upgrade_db_init($dbh);
205
206         $count = _upgrade_cached_html($dbh, false);
207
208         if (!$count)
209             return _("No old _cached_html pagedata found.");
210         else {
211             return HTML(fmt("Converted successfully %d pages", $count),
212                 HTML::div(array('align' => 'left'), $list));
213         }
214     }
215
216     private function _do_db_check(&$request, $args)
217     {
218         longer_timeout(180);
219         $dbh = $request->getDbh();
220         //FIXME: display result.
221         return $dbh->_backend->check($args);
222     }
223
224     private function _do_db_rebuild(&$request, $args)
225     {
226         longer_timeout(240);
227         $dbh = $request->getDbh();
228         //FIXME: display result.
229         return $dbh->_backend->rebuild($args);
230     }
231
232     // pagelist with enable/disable button
233     private function _do_email_verification(&$request, &$args)
234     {
235         $dbi = $request->getDbh();
236         $pagelist = new PageList('pagename', 0, $args);
237         //$args['return_url'] = 'action=email-verification-verified';
238         $email = new _PageList_Column_email('email', _("E-mail"), 'left');
239         $emailVerified = new _PageList_Column_emailVerified('emailVerified',
240             _("Verification Status"), 'center');
241         $pagelist->_columns[0]->_heading = _("Username");
242         $pagelist->_columns[] = $email;
243         $pagelist->_columns[] = $emailVerified;
244         //This is the best method to find all users (Db and PersonalPage)
245         $current_user = $request->_user;
246         if (empty($args['verify'])) {
247             $group = $request->getGroup();
248             $allusers = $group->_allUsers();
249         } else {
250             if (!empty($args['user']))
251                 $allusers = array_keys($args['user']);
252             else
253                 $allusers = array();
254         }
255         foreach ($allusers as $username) {
256             if (ENABLE_USER_NEW)
257                 $user = WikiUser($username);
258             else
259                 $user = new WikiUser($request, $username);
260             $prefs = $user->getPreferences();
261             if ($prefs->get('email')) {
262                 if (!$prefs->get('userid'))
263                     $prefs->set('userid', $username);
264                 if (!empty($pagelist->_rows))
265                     $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
266                 else
267                     $group = 0;
268                 $class = ($group % 2) ? 'oddrow' : 'evenrow';
269                 $row = HTML::tr(array('class' => $class));
270                 $page_handle = $dbi->getPage($username);
271                 $row->pushContent($pagelist->_columns[0]->format($pagelist,
272                     $page_handle, $page_handle));
273                 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
274                 if (!empty($args['verify'])) {
275                     $prefs->_prefs['email']->set('emailVerified',
276                         empty($args['verified'][$username]) ? 0 : true);
277                     $user->setPreferences($prefs);
278                 }
279                 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
280                 $pagelist->_rows[] = $row;
281             }
282         }
283         $request->_user = $current_user;
284         if (!empty($args['verify']) or empty($pagelist->_rows)) {
285             return HTML($pagelist->_generateTable(false));
286         } elseif (!empty($pagelist->_rows)) {
287             $args['verify'] = 1;
288             $args['return_url'] = $request->getURLtoSelf();
289             return HTML::form(array('action' => $request->getPostURL(),
290                     'method' => 'post'),
291                 HiddenInputs($args, 'wikiadminutils'),
292                 HiddenInputs(array('require_authority_for_post' =>
293                 WIKIAUTH_ADMIN)),
294                 HiddenInputs($request->getArgs()),
295                 $pagelist->_generateTable(false),
296                 HTML::p(Button('submit:', _("Change Verification Status"),
297                         'wikiadmin'),
298                     HTML::raw('&nbsp;'),
299                     Button('cancel', _("Cancel")))
300             );
301         }
302     }
303 }
304
305 require_once 'lib/PageList.php';
306
307 class _PageList_Column_email
308     extends _PageList_Column
309 {
310     function _getValue(&$prefs, $dummy)
311     {
312         return $prefs->get('email');
313     }
314 }
315
316 class _PageList_Column_emailVerified
317     extends _PageList_Column
318 {
319     function _getValue(&$prefs, $status)
320     {
321         $name = $prefs->get('userid');
322         $input = HTML::input(array('type' => 'checkbox',
323             'name' => 'wikiadminutils[verified][' . $name . ']',
324             'value' => 1));
325         if ($prefs->get('emailVerified'))
326             $input->setAttr('checked', '1');
327         if ($status)
328             $input->setAttr('disabled', '1');
329         return HTML($input, HTML::input
330         (array('type' => 'hidden',
331             'name' => 'wikiadminutils[user][' . $name . ']',
332             'value' => $name)));
333     }
334 }
335
336 // Local Variables:
337 // mode: php
338 // tab-width: 8
339 // c-basic-offset: 4
340 // c-hanging-comment-ender-p: nil
341 // indent-tabs-mode: nil
342 // End: