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
113 private function _getLabel($action)
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")
123 return @$labels[$action];
126 private function _do_purge_cache(&$request, $args)
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);
133 return _("Markup cache purged!");
136 private function _do_purge_bad_pagenames(&$request, $args)
138 // FIXME: this should be moved into WikiDB::normalize() or something...
139 $dbi = $request->getDbh();
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));
154 return _("No pages with bad names had to be deleted.");
156 return HTML(fmt("Deleted %d pages with invalid names:", $count),
157 HTML::div(array('align' => 'left'), $list));
162 * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
164 private function _do_purge_empty_pages(&$request, $args)
166 $dbi = $request->getDbh();
169 $list = HTML::ol(array('align' => 'left'));
170 $pages = $dbi->getAllPages('include_empty');
171 while (($page = $pages->next())) {
173 and ($links = $page->getBackLinks('include_empty'))
176 $pagename = $page->getName();
177 if ($pagename == 'global_data' or $pagename == '.') continue;
178 if ($dbi->purgePage($pagename))
179 $list->pushContent(HTML::li($pagename . ' ' . _("[purged]")));
181 $list->pushContent(HTML::li($pagename . ' ' . _("[not purgable]")));
189 return _("No empty, unreferenced pages were found.");
191 return HTML(fmt("Deleted %d unreferenced pages:", $count),
192 HTML::div(array('align' => 'left'), $list),
194 fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.",
199 private function _do_convert_cached_html(&$request, $args)
202 require_once 'lib/upgrade.php';
203 $dbh = $request->_dbi;
204 _upgrade_db_init($dbh);
206 $count = _upgrade_cached_html($dbh, false);
209 return _("No old _cached_html pagedata found.");
211 return HTML(fmt("Converted successfully %d pages", $count),
212 HTML::div(array('align' => 'left'), $list));
216 private function _do_db_check(&$request, $args)
219 $dbh = $request->getDbh();
220 //FIXME: display result.
221 return $dbh->_backend->check($args);
224 private function _do_db_rebuild(&$request, $args)
227 $dbh = $request->getDbh();
228 //FIXME: display result.
229 return $dbh->_backend->rebuild($args);
232 // pagelist with enable/disable button
233 private function _do_email_verification(&$request, &$args)
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();
250 if (!empty($args['user']))
251 $allusers = array_keys($args['user']);
255 foreach ($allusers as $username) {
257 $user = WikiUser($username);
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);
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);
279 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
280 $pagelist->_rows[] = $row;
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)) {
288 $args['return_url'] = $request->getURLtoSelf();
289 return HTML::form(array('action' => $request->getPostURL(),
291 HiddenInputs($args, 'wikiadminutils'),
292 HiddenInputs(array('require_authority_for_post' =>
294 HiddenInputs($request->getArgs()),
295 $pagelist->_generateTable(false),
296 HTML::p(Button('submit:', _("Change Verification Status"),
299 Button('cancel', _("Cancel")))
305 require_once 'lib/PageList.php';
307 class _PageList_Column_email
308 extends _PageList_Column
310 function _getValue(&$prefs, $dummy)
312 return $prefs->get('email');
316 class _PageList_Column_emailVerified
317 extends _PageList_Column
319 function _getValue(&$prefs, $status)
321 $name = $prefs->get('userid');
322 $input = HTML::input(array('type' => 'checkbox',
323 'name' => 'wikiadminutils[verified][' . $name . ']',
325 if ($prefs->get('emailVerified'))
326 $input->setAttr('checked', '1');
328 $input->setAttr('disabled', '1');
329 return HTML($input, HTML::input
330 (array('type' => 'hidden',
331 'name' => 'wikiadminutils[user][' . $name . ']',
340 // c-hanging-comment-ender-p: nil
341 // indent-tabs-mode: nil