4 * Copyright 2003,2004,2006 $ThePhpWikiProgrammingTeam
5 * Copyright 2009 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
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.
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.
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.
34 class WikiPlugin_WikiAdminUtils
39 return _("WikiAdminUtils");
42 function getDescription()
44 return _("Miscellaneous utility functions for the Administrator.");
47 function getDefaultArguments()
49 return array('action' => '',
54 function run($dbi, $argstr, &$request, $basepage)
56 $args = $this->getArgs($argstr, $request);
57 $args['action'] = strtolower($args['action']);
61 $this->error("No action specified");
63 if (!($default_label = $this->_getLabel($action))) {
64 return HTML::div(array('class' => "error"), fmt("Bad action requested: %s", $action));
66 if ($request->getArg('action') != 'browse') {
67 return $this->disabled(_("Plugin not run: not in browse mode"));
70 $posted = $request->getArg('wikiadminutils');
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."));
78 return $this->do_action($request, $posted);
81 $label = $default_label;
83 return $this->_makeButton($request, $args, $label);
86 private function _makeButton(&$request, $args, $label)
88 $args['return_url'] = $request->getURLtoSelf();
89 return HTML::form(array('action' => $request->getPostURL(),
91 HTML::p(Button('submit:', $label, 'wikiadmin')),
92 HiddenInputs($args, 'wikiadminutils'),
93 HiddenInputs(array('require_authority_for_post' =>
95 HiddenInputs($request->getArgs(), false, array('action')));
98 function do_action(&$request, $args)
100 $method = strtolower('_do_' . str_replace('-', '_', $args['action']));
101 if (!method_exists($this, $method))
102 return $this->error("Bad action $method");
104 $message = call_user_func(array(&$this, $method), $request, $args);
106 // display as separate page or as alert?
107 $alert = new Alert(fmt("WikiAdminUtils %s returned:", $args['action']),
109 array(_("Back") => $args['return_url']));
110 $alert->show(); // noreturn
114 private function _getLabel($action)
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")
124 return @$labels[$action];
127 private function _do_purge_cache(&$request, $args)
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);
134 return _("Markup cache purged!");
137 private function _do_purge_bad_pagenames(&$request, $args)
139 // FIXME: this should be moved into WikiDB::normalize() or something...
140 $dbi = $request->getDbh();
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));
155 return _("No pages with bad names had to be deleted.");
157 return HTML(fmt("Deleted %d pages with invalid names:", $count),
158 HTML::div(array('align' => 'left'), $list));
163 * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
165 private function _do_purge_empty_pages(&$request, $args)
167 $dbi = $request->getDbh();
170 $list = HTML::ol(array('align' => 'left'));
171 $pages = $dbi->getAllPages('include_empty');
172 while (($page = $pages->next())) {
174 and ($links = $page->getBackLinks('include_empty'))
177 $pagename = $page->getName();
178 if ($pagename == 'global_data' or $pagename == '.') continue;
179 if ($dbi->purgePage($pagename))
180 $list->pushContent(HTML::li($pagename . ' ' . _("[purged]")));
182 $list->pushContent(HTML::li($pagename . ' ' . _("[not purgable]")));
190 return _("No empty, unreferenced pages were found.");
192 return HTML(fmt("Deleted %d unreferenced pages:", $count),
193 HTML::div(array('align' => 'left'), $list),
195 fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.",
200 private function _do_convert_cached_html(&$request, $args)
203 require_once 'lib/upgrade.php';
204 $dbh = $request->_dbi;
205 _upgrade_db_init($dbh);
207 $count = _upgrade_cached_html($dbh, false);
210 return _("No old _cached_html pagedata found.");
212 return HTML(fmt("Converted successfully %d pages", $count),
213 HTML::div(array('align' => 'left'), $list));
217 private function _do_db_check(&$request, $args)
220 $dbh = $request->getDbh();
221 //FIXME: display result.
222 return $dbh->_backend->check($args);
225 private function _do_db_rebuild(&$request, $args)
228 $dbh = $request->getDbh();
229 //FIXME: display result.
230 return $dbh->_backend->rebuild($args);
233 // pagelist with enable/disable button
234 private function _do_email_verification(&$request, &$args)
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();
251 if (!empty($args['user']))
252 $allusers = array_keys($args['user']);
256 foreach ($allusers as $username) {
258 $user = WikiUser($username);
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);
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);
280 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
281 $pagelist->_rows[] = $row;
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)) {
289 $args['return_url'] = $request->getURLtoSelf();
290 return HTML::form(array('action' => $request->getPostURL(),
292 HiddenInputs($args, 'wikiadminutils'),
293 HiddenInputs(array('require_authority_for_post' =>
295 HiddenInputs($request->getArgs()),
296 $pagelist->_generateTable(false),
297 HTML::p(Button('submit:', _("Change Verification Status"),
300 Button('cancel', _("Cancel")))
306 require_once 'lib/PageList.php';
308 class _PageList_Column_email
309 extends _PageList_Column
311 function _getValue(&$prefs, $dummy)
313 return $prefs->get('email');
317 class _PageList_Column_emailVerified
318 extends _PageList_Column
320 function _getValue(&$prefs, $status)
322 $name = $prefs->get('userid');
323 $input = HTML::input(array('type' => 'checkbox',
324 'name' => 'wikiadminutils[verified][' . $name . ']',
326 if ($prefs->get('emailVerified'))
327 $input->setAttr('checked', '1');
329 $input->setAttr('disabled', '1');
330 return HTML($input, HTML::input
331 (array('type' => 'hidden',
332 'name' => 'wikiadminutils[user][' . $name . ']',
341 // c-hanging-comment-ender-p: nil
342 // indent-tabs-mode: nil