]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminUtils.php
Add missing return
[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         return '';
112     }
113
114     private function _getLabel($action)
115     {
116         $labels = array('purge-cache' => _("Purge Markup Cache"),
117             'purge-bad-pagenames' => _("Purge all Pages With Invalid Names"),
118             'purge-empty-pages' => _("Purge all empty, unreferenced Pages"),
119             'email-verification' => _("E-mail address confirmation"),
120             'convert-cached-html' => _("Convert cached_html"),
121             'db-check' => _("DB Check"),
122             'db-rebuild' => _("Db Rebuild")
123         );
124         return @$labels[$action];
125     }
126
127     private function _do_purge_cache(&$request, $args)
128     {
129         $dbi = $request->getDbh();
130         $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
131         while (($page = $pages->next())) {
132             $page->set('_cached_html', false);
133         }
134         return _("Markup cache purged!");
135     }
136
137     private function _do_purge_bad_pagenames(&$request, $args)
138     {
139         // FIXME: this should be moved into WikiDB::normalize() or something...
140         $dbi = $request->getDbh();
141         $count = 0;
142         $list = HTML::ol(array('align' => 'left'));
143         $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
144         while (($page = $pages->next())) {
145             $pagename = $page->getName();
146             $wpn = new WikiPageName($pagename);
147             if (!$wpn->isValid()) {
148                 $dbi->purgePage($pagename);
149                 $list->pushContent(HTML::li($pagename));
150                 $count++;
151             }
152         }
153         $pages->free();
154         if (!$count)
155             return _("No pages with bad names had to be deleted.");
156         else {
157             return HTML(fmt("Deleted %d pages with invalid names:", $count),
158                 HTML::div(array('align' => 'left'), $list));
159         }
160     }
161
162     /**
163      * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
164      */
165     private function _do_purge_empty_pages(&$request, $args)
166     {
167         $dbi = $request->getDbh();
168         $count = 0;
169         $notpurgable = 0;
170         $list = HTML::ol(array('align' => 'left'));
171         $pages = $dbi->getAllPages('include_empty');
172         while (($page = $pages->next())) {
173             if (!$page->exists()
174                 and ($links = $page->getBackLinks('include_empty'))
175                     and !$links->next()
176             ) {
177                 $pagename = $page->getName();
178                 if ($pagename == 'global_data' or $pagename == '.') continue;
179                 if ($dbi->purgePage($pagename))
180                     $list->pushContent(HTML::li($pagename . ' ' . _("[purged]")));
181                 else {
182                     $list->pushContent(HTML::li($pagename . ' ' . _("[not purgable]")));
183                     $notpurgable++;
184                 }
185                 $count++;
186             }
187         }
188         $pages->free();
189         if (!$count)
190             return _("No empty, unreferenced pages were found.");
191         else
192             return HTML(fmt("Deleted %d unreferenced pages:", $count),
193                 HTML::div(array('align' => 'left'), $list),
194                 ($notpurgable ?
195                     fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.",
196                         $notpurgable)
197                     : ''));
198     }
199
200     private function _do_convert_cached_html(&$request, $args)
201     {
202
203         require_once 'lib/upgrade.php';
204         $dbh = $request->_dbi;
205         _upgrade_db_init($dbh);
206
207         $count = _upgrade_cached_html($dbh, false);
208
209         if (!$count)
210             return _("No old _cached_html pagedata found.");
211         else {
212             return HTML(fmt("Converted successfully %d pages", $count),
213                 HTML::div(array('align' => 'left'), $list));
214         }
215     }
216
217     private function _do_db_check(&$request, $args)
218     {
219         longer_timeout(180);
220         $dbh = $request->getDbh();
221         //FIXME: display result.
222         return $dbh->_backend->check($args);
223     }
224
225     private function _do_db_rebuild(&$request, $args)
226     {
227         longer_timeout(240);
228         $dbh = $request->getDbh();
229         //FIXME: display result.
230         return $dbh->_backend->rebuild($args);
231     }
232
233     // pagelist with enable/disable button
234     private function _do_email_verification(&$request, &$args)
235     {
236         $dbi = $request->getDbh();
237         $pagelist = new PageList('pagename', 0, $args);
238         //$args['return_url'] = 'action=email-verification-verified';
239         $email = new _PageList_Column_email('email', _("E-mail"), 'left');
240         $emailVerified = new _PageList_Column_emailVerified('emailVerified',
241             _("Verification Status"), 'center');
242         $pagelist->_columns[0]->_heading = _("Username");
243         $pagelist->_columns[] = $email;
244         $pagelist->_columns[] = $emailVerified;
245         //This is the best method to find all users (Db and PersonalPage)
246         $current_user = $request->_user;
247         if (empty($args['verify'])) {
248             $group = $request->getGroup();
249             $allusers = $group->_allUsers();
250         } else {
251             if (!empty($args['user']))
252                 $allusers = array_keys($args['user']);
253             else
254                 $allusers = array();
255         }
256         foreach ($allusers as $username) {
257             if (ENABLE_USER_NEW)
258                 $user = WikiUser($username);
259             else
260                 $user = new WikiUser($request, $username);
261             $prefs = $user->getPreferences();
262             if ($prefs->get('email')) {
263                 if (!$prefs->get('userid'))
264                     $prefs->set('userid', $username);
265                 if (!empty($pagelist->_rows))
266                     $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
267                 else
268                     $group = 0;
269                 $class = ($group % 2) ? 'oddrow' : 'evenrow';
270                 $row = HTML::tr(array('class' => $class));
271                 $page_handle = $dbi->getPage($username);
272                 $row->pushContent($pagelist->_columns[0]->format($pagelist,
273                     $page_handle, $page_handle));
274                 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
275                 if (!empty($args['verify'])) {
276                     $prefs->_prefs['email']->set('emailVerified',
277                         empty($args['verified'][$username]) ? 0 : true);
278                     $user->setPreferences($prefs);
279                 }
280                 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
281                 $pagelist->_rows[] = $row;
282             }
283         }
284         $request->_user = $current_user;
285         if (!empty($args['verify']) or empty($pagelist->_rows)) {
286             return HTML($pagelist->_generateTable(false));
287         } elseif (!empty($pagelist->_rows)) {
288             $args['verify'] = 1;
289             $args['return_url'] = $request->getURLtoSelf();
290             return HTML::form(array('action' => $request->getPostURL(),
291                     'method' => 'post'),
292                 HiddenInputs($args, 'wikiadminutils'),
293                 HiddenInputs(array('require_authority_for_post' =>
294                 WIKIAUTH_ADMIN)),
295                 HiddenInputs($request->getArgs()),
296                 $pagelist->_generateTable(false),
297                 HTML::p(Button('submit:', _("Change Verification Status"),
298                         'wikiadmin'),
299                     HTML::raw('&nbsp;'),
300                     Button('cancel', _("Cancel")))
301             );
302         }
303     }
304 }
305
306 require_once 'lib/PageList.php';
307
308 class _PageList_Column_email
309     extends _PageList_Column
310 {
311     function _getValue(&$prefs, $dummy)
312     {
313         return $prefs->get('email');
314     }
315 }
316
317 class _PageList_Column_emailVerified
318     extends _PageList_Column
319 {
320     function _getValue(&$prefs, $status)
321     {
322         $name = $prefs->get('userid');
323         $input = HTML::input(array('type' => 'checkbox',
324             'name' => 'wikiadminutils[verified][' . $name . ']',
325             'value' => 1));
326         if ($prefs->get('emailVerified'))
327             $input->setAttr('checked', '1');
328         if ($status)
329             $input->setAttr('disabled', '1');
330         return HTML($input, HTML::input
331         (array('type' => 'hidden',
332             'name' => 'wikiadminutils[user][' . $name . ']',
333             'value' => $name)));
334     }
335 }
336
337 // Local Variables:
338 // mode: php
339 // tab-width: 8
340 // c-basic-offset: 4
341 // c-hanging-comment-ender-p: nil
342 // indent-tabs-mode: nil
343 // End: