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.
35 class WikiPlugin_WikiAdminUtils
40 return _("WikiAdminUtils");
43 function getDescription()
45 return _("Miscellaneous utility functions for the Administrator.");
48 function getDefaultArguments()
50 return array('action' => '',
55 function run($dbi, $argstr, &$request, $basepage)
57 $args = $this->getArgs($argstr, $request);
58 $args['action'] = strtolower($args['action']);
62 $this->error("No action specified");
64 if (!($default_label = $this->_getLabel($action))) {
65 return HTML::div(array('class' => "error"), fmt("Bad action requested: %s", $action));
67 if ($request->getArg('action') != 'browse') {
68 return $this->disabled(_("Plugin not run: not in browse mode"));
71 $posted = $request->getArg('wikiadminutils');
73 if ($request->isPost() and $posted['action'] == $action) { // a different form. we might have multiple
74 $user = $request->getUser();
75 if (!$user->isAdmin()) {
76 $request->_notAuthorized(WIKIAUTH_ADMIN);
77 return $this->error(_("You must be an administrator to use this plugin."));
79 return $this->do_action($request, $posted);
82 $label = $default_label;
84 return $this->_makeButton($request, $args, $label);
87 private function _makeButton(&$request, $args, $label)
89 $args['return_url'] = $request->getURLtoSelf();
90 return HTML::form(array('action' => $request->getPostURL(),
92 HTML::p(Button('submit:', $label, 'wikiadmin')),
93 HiddenInputs($args, 'wikiadminutils'),
94 HiddenInputs(array('require_authority_for_post' =>
96 HiddenInputs($request->getArgs(), false, array('action')));
99 function do_action(&$request, $args)
101 $method = strtolower('_do_' . str_replace('-', '_', $args['action']));
102 if (!method_exists($this, $method))
103 return $this->error("Bad action $method");
105 $message = call_user_func(array(&$this, $method), $request, $args);
107 // display as separate page or as alert?
108 $alert = new Alert(fmt("WikiAdminUtils %s returned:", $args['action']),
110 array(_("Back") => $args['return_url']));
111 $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 'access-restrictions' => _("Access Restrictions"),
120 'email-verification' => _("E-mail address confirmation"),
121 'convert-cached-html' => _("Convert cached_html"),
122 'db-check' => _("DB Check"),
123 'db-rebuild' => _("Db Rebuild")
125 return @$labels[$action];
128 private function _do_purge_cache(&$request, $args)
130 $dbi = $request->getDbh();
131 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
132 while (($page = $pages->next())) {
133 $page->set('_cached_html', false);
135 return _("Markup cache purged!");
138 private function _do_purge_bad_pagenames(&$request, $args)
140 // FIXME: this should be moved into WikiDB::normalize() or something...
141 $dbi = $request->getDbh();
143 $list = HTML::ol(array('align' => 'left'));
144 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
145 while (($page = $pages->next())) {
146 $pagename = $page->getName();
147 $wpn = new WikiPageName($pagename);
148 if (!$wpn->isValid()) {
149 $dbi->purgePage($pagename);
150 $list->pushContent(HTML::li($pagename));
156 return _("No pages with bad names had to be deleted.");
158 return HTML(fmt("Deleted %d pages with invalid names:", $count),
159 HTML::div(array('align' => 'left'), $list));
164 * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
166 private function _do_purge_empty_pages(&$request, $args)
168 $dbi = $request->getDbh();
171 $list = HTML::ol(array('align' => 'left'));
172 $pages = $dbi->getAllPages('include_empty');
173 while (($page = $pages->next())) {
175 and ($links = $page->getBackLinks('include_empty'))
178 $pagename = $page->getName();
179 if ($pagename == 'global_data' or $pagename == '.') continue;
180 if ($dbi->purgePage($pagename))
181 $list->pushContent(HTML::li($pagename . ' ' . _("[purged]")));
183 $list->pushContent(HTML::li($pagename . ' ' . _("[not purgable]")));
191 return _("No empty, unreferenced pages were found.");
193 return HTML(fmt("Deleted %d unreferenced pages:", $count),
194 HTML::div(array('align' => 'left'), $list),
196 fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.",
201 private function _do_convert_cached_html(&$request, $args)
204 require_once 'lib/upgrade.php';
205 $dbh = $request->_dbi;
206 _upgrade_db_init($dbh);
208 $count = _upgrade_cached_html($dbh, false);
211 return _("No old _cached_html pagedata found.");
213 return HTML(fmt("Converted successfully %d pages", $count),
214 HTML::div(array('align' => 'left'), $list));
218 private function _do_db_check(&$request, $args)
221 $dbh = $request->getDbh();
222 //FIXME: display result.
223 return $dbh->_backend->check($args);
226 private function _do_db_rebuild(&$request, $args)
229 $dbh = $request->getDbh();
230 //FIXME: display result.
231 return $dbh->_backend->rebuild($args);
234 //TODO: We need a separate plugin for this.
236 private function _do_access_restrictions(&$request, &$args)
238 return _("Sorry. Access Restrictions not yet implemented");
241 // pagelist with enable/disable button
242 private function _do_email_verification(&$request, &$args)
244 $dbi = $request->getDbh();
245 $pagelist = new PageList('pagename', 0, $args);
246 //$args['return_url'] = 'action=email-verification-verified';
247 $email = new _PageList_Column_email('email', _("E-mail"), 'left');
248 $emailVerified = new _PageList_Column_emailVerified('emailVerified',
249 _("Verification Status"), 'center');
250 $pagelist->_columns[0]->_heading = _("Username");
251 $pagelist->_columns[] = $email;
252 $pagelist->_columns[] = $emailVerified;
253 //This is the best method to find all users (Db and PersonalPage)
254 $current_user = $request->_user;
255 if (empty($args['verify'])) {
256 $group = $request->getGroup();
257 $allusers = $group->_allUsers();
259 if (!empty($args['user']))
260 $allusers = array_keys($args['user']);
264 foreach ($allusers as $username) {
266 $user = WikiUser($username);
268 $user = new WikiUser($request, $username);
269 $prefs = $user->getPreferences();
270 if ($prefs->get('email')) {
271 if (!$prefs->get('userid'))
272 $prefs->set('userid', $username);
273 if (!empty($pagelist->_rows))
274 $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
277 $class = ($group % 2) ? 'oddrow' : 'evenrow';
278 $row = HTML::tr(array('class' => $class));
279 $page_handle = $dbi->getPage($username);
280 $row->pushContent($pagelist->_columns[0]->format($pagelist,
281 $page_handle, $page_handle));
282 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
283 if (!empty($args['verify'])) {
284 $prefs->_prefs['email']->set('emailVerified',
285 empty($args['verified'][$username]) ? 0 : true);
286 $user->setPreferences($prefs);
288 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
289 $pagelist->_rows[] = $row;
292 $request->_user = $current_user;
293 if (!empty($args['verify']) or empty($pagelist->_rows)) {
294 return HTML($pagelist->_generateTable(false));
295 } elseif (!empty($pagelist->_rows)) {
297 $args['return_url'] = $request->getURLtoSelf();
298 return HTML::form(array('action' => $request->getPostURL(),
300 HiddenInputs($args, 'wikiadminutils'),
301 HiddenInputs(array('require_authority_for_post' =>
303 HiddenInputs($request->getArgs()),
304 $pagelist->_generateTable(false),
305 HTML::p(Button('submit:', _("Change Verification Status"),
308 Button('cancel', _("Cancel")))
314 require_once 'lib/PageList.php';
316 class _PageList_Column_email
317 extends _PageList_Column
319 private function _getValue(&$prefs, $dummy)
321 return $prefs->get('email');
325 class _PageList_Column_emailVerified
326 extends _PageList_Column
328 private function _getValue(&$prefs, $status)
330 $name = $prefs->get('userid');
331 $input = HTML::input(array('type' => 'checkbox',
332 'name' => 'wikiadminutils[verified][' . $name . ']',
334 if ($prefs->get('emailVerified'))
335 $input->setAttr('checked', '1');
337 $input->setAttr('disabled', '1');
338 return HTML($input, HTML::input
339 (array('type' => 'hidden',
340 'name' => 'wikiadminutils[user][' . $name . ']',
349 // c-hanging-comment-ender-p: nil
350 // indent-tabs-mode: nil