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
37 function getDescription()
39 return _("Miscellaneous utility functions for the Administrator.");
42 function getDefaultArguments()
44 return array('action' => '',
49 function run($dbi, $argstr, &$request, $basepage)
51 $args = $this->getArgs($argstr, $request);
52 $args['action'] = strtolower($args['action']);
56 $this->error("No action specified");
58 if (!($default_label = $this->_getLabel($action))) {
59 return HTML::div(array('class' => "error"), fmt("Bad action requested: %s", $action));
61 if ($request->getArg('action') != 'browse') {
62 return $this->disabled(_("Plugin not run: not in browse mode"));
65 $posted = $request->getArg('wikiadminutils');
67 if ($request->isPost() and $posted['action'] == $action) { // a different form. we might have multiple
68 $user = $request->getUser();
69 if (!$user->isAdmin()) {
70 $request->_notAuthorized(WIKIAUTH_ADMIN);
71 return $this->error(_("You must be an administrator to use this plugin."));
73 return $this->do_action($request, $posted);
76 $label = $default_label;
78 return $this->_makeButton($request, $args, $label);
81 private function _makeButton(&$request, $args, $label)
83 $args['return_url'] = $request->getURLtoSelf();
84 return HTML::form(array('action' => $request->getPostURL(),
86 HTML::p(Button('submit:', $label, 'wikiadmin')),
87 HiddenInputs($args, 'wikiadminutils'),
88 HiddenInputs(array('require_authority_for_post' =>
90 HiddenInputs($request->getArgs(), false, array('action')));
93 function do_action(&$request, $args)
95 $method = strtolower('_do_' . str_replace('-', '_', $args['action']));
96 if (!method_exists($this, $method))
97 return $this->error("Bad action $method");
99 $message = call_user_func(array(&$this, $method), $request, $args);
101 // display as separate page or as alert?
102 $alert = new Alert(fmt("WikiAdminUtils %s returned:", $args['action']),
104 array(_("Back") => $args['return_url']));
105 $alert->show(); // noreturn
109 private function _getLabel($action)
111 $labels = array('purge-cache' => _("Purge Markup Cache"),
112 'purge-bad-pagenames' => _("Purge all Pages With Invalid Names"),
113 'purge-empty-pages' => _("Purge all empty, unreferenced Pages"),
114 'email-verification' => _("E-mail address confirmation"),
115 'convert-cached-html' => _("Convert cached_html"),
116 'db-check' => _("DB Check"),
117 'db-rebuild' => _("Db Rebuild")
119 return @$labels[$action];
122 private function _do_purge_cache(&$request, $args)
124 $dbi = $request->getDbh();
125 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
126 while (($page = $pages->next())) {
127 $page->set('_cached_html', false);
129 return _("Markup cache purged!");
132 private function _do_purge_bad_pagenames(&$request, $args)
134 // FIXME: this should be moved into WikiDB::normalize() or something...
135 $dbi = $request->getDbh();
137 $list = HTML::ol(array('align' => 'left'));
138 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
139 while (($page = $pages->next())) {
140 $pagename = $page->getName();
141 $wpn = new WikiPageName($pagename);
142 if (!$wpn->isValid()) {
143 $dbi->purgePage($pagename);
144 $list->pushContent(HTML::li($pagename));
150 return _("No pages with bad names had to be deleted.");
152 return HTML(fmt("Deleted %d pages with invalid names:", $count),
153 HTML::div(array('align' => 'left'), $list));
158 * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
160 private function _do_purge_empty_pages(&$request, $args)
162 $dbi = $request->getDbh();
165 $list = HTML::ol(array('align' => 'left'));
166 $pages = $dbi->getAllPages('include_empty');
167 while (($page = $pages->next())) {
169 and ($links = $page->getBackLinks('include_empty'))
172 $pagename = $page->getName();
173 if ($pagename == 'global_data' or $pagename == '.') continue;
174 if ($dbi->purgePage($pagename))
175 $list->pushContent(HTML::li($pagename . ' ' . _("[purged]")));
177 $list->pushContent(HTML::li($pagename . ' ' . _("[not purgable]")));
185 return _("No empty, unreferenced pages were found.");
187 return HTML(fmt("Deleted %d unreferenced pages:", $count),
188 HTML::div(array('align' => 'left'), $list),
190 fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.",
195 private function _do_convert_cached_html(&$request, $args)
198 require_once 'lib/upgrade.php';
199 $dbh = $request->_dbi;
200 _upgrade_db_init($dbh);
202 $count = _upgrade_cached_html($dbh, false);
205 return _("No old _cached_html pagedata found.");
207 return HTML(fmt("Converted successfully %d pages", $count),
208 HTML::div(array('align' => 'left'), $list));
212 private function _do_db_check(&$request, $args)
215 $dbh = $request->getDbh();
216 //FIXME: display result.
217 return $dbh->_backend->check($args);
220 private function _do_db_rebuild(&$request, $args)
223 $dbh = $request->getDbh();
224 //FIXME: display result.
225 return $dbh->_backend->rebuild($args);
228 // pagelist with enable/disable button
229 private function _do_email_verification(&$request, &$args)
231 $dbi = $request->getDbh();
232 $pagelist = new PageList('pagename', 0, $args);
233 //$args['return_url'] = 'action=email-verification-verified';
234 $email = new _PageList_Column_email('email', _("E-mail"), 'left');
235 $emailVerified = new _PageList_Column_emailVerified('emailVerified',
236 _("Verification Status"), 'center');
237 $pagelist->_columns[0]->_heading = _("Username");
238 $pagelist->_columns[] = $email;
239 $pagelist->_columns[] = $emailVerified;
240 //This is the best method to find all users (Db and PersonalPage)
241 $current_user = $request->_user;
242 if (empty($args['verify'])) {
243 $group = $request->getGroup();
244 $allusers = $group->_allUsers();
246 if (!empty($args['user']))
247 $allusers = array_keys($args['user']);
251 foreach ($allusers as $username) {
253 $user = WikiUser($username);
255 $user = new WikiUser($request, $username);
256 $prefs = $user->getPreferences();
257 if ($prefs->get('email')) {
258 if (!$prefs->get('userid'))
259 $prefs->set('userid', $username);
260 if (!empty($pagelist->_rows))
261 $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
264 $class = ($group % 2) ? 'oddrow' : 'evenrow';
265 $row = HTML::tr(array('class' => $class));
266 $page_handle = $dbi->getPage($username);
267 $row->pushContent($pagelist->_columns[0]->format($pagelist,
268 $page_handle, $page_handle));
269 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
270 if (!empty($args['verify'])) {
271 $prefs->_prefs['email']->set('emailVerified',
272 empty($args['verified'][$username]) ? 0 : true);
273 $user->setPreferences($prefs);
275 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
276 $pagelist->_rows[] = $row;
279 $request->_user = $current_user;
280 if (!empty($args['verify']) or empty($pagelist->_rows)) {
281 return HTML($pagelist->_generateTable(false));
282 } elseif (!empty($pagelist->_rows)) {
284 $args['return_url'] = $request->getURLtoSelf();
285 return HTML::form(array('action' => $request->getPostURL(),
287 HiddenInputs($args, 'wikiadminutils'),
288 HiddenInputs(array('require_authority_for_post' =>
290 HiddenInputs($request->getArgs()),
291 $pagelist->_generateTable(false),
292 HTML::p(Button('submit:', _("Change Verification Status"),
295 Button('cancel', _("Cancel")))
302 require_once 'lib/PageList.php';
304 class _PageList_Column_email
305 extends _PageList_Column
307 function _getValue(&$prefs, $dummy)
309 return $prefs->get('email');
313 class _PageList_Column_emailVerified
314 extends _PageList_Column
316 function _getValue(&$prefs, $status)
318 $name = $prefs->get('userid');
319 $input = HTML::input(array('type' => 'checkbox',
320 'name' => 'wikiadminutils[verified][' . $name . ']',
322 if ($prefs->get('emailVerified'))
323 $input->setAttr('checked', '1');
325 $input->setAttr('disabled', '1');
326 return HTML($input, HTML::input
327 (array('type' => 'hidden',
328 'name' => 'wikiadminutils[user][' . $name . ']',
337 // c-hanging-comment-ender-p: nil
338 // indent-tabs-mode: nil