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
20 * along with PhpWiki; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 class WikiPlugin_WikiAdminUtils
39 return _("WikiAdminUtils");
42 function getDescription () {
43 return _("Miscellaneous utility functions for the Administrator.");
46 function getDefaultArguments() {
47 return array('action' => '',
52 function run($dbi, $argstr, &$request, $basepage) {
53 $args = $this->getArgs($argstr, $request);
54 $args['action'] = strtolower($args['action']);
58 $this->error("No action specified");
59 if (!($default_label = $this->_getLabel($action))) {
60 return HTML::div(array('class' => "error"), fmt("Bad action requested: %s", $action));
62 if ($request->getArg('action') != 'browse')
63 return $this->disabled("(action != 'browse')");
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 function _makeButton(&$request, $args, $label) {
82 $args['return_url'] = $request->getURLtoSelf();
83 return HTML::form(array('action' => $request->getPostURL(),
85 HTML::p(Button('submit:', $label, 'wikiadmin')),
86 HiddenInputs($args, 'wikiadminutils'),
87 HiddenInputs(array('require_authority_for_post' =>
89 HiddenInputs($request->getArgs(),false,array('action')));
92 function do_action(&$request, $args) {
93 $method = strtolower('_do_' . str_replace('-', '_', $args['action']));
94 if (!method_exists($this, $method))
95 return $this->error("Bad action $method");
97 $message = call_user_func(array(&$this, $method), $request, $args);
99 // display as seperate page or as alert?
100 $alert = new Alert(fmt("WikiAdminUtils %s returned:", $args['action']),
102 array(_("Back") => $args['return_url']));
103 $alert->show(); // noreturn
106 function _getLabel($action) {
107 $labels = array('purge-cache' => _("Purge Markup Cache"),
108 'purge-bad-pagenames' => _("Purge all Pages With Invalid Names"),
109 'purge-empty-pages' => _("Purge all empty, unreferenced Pages"),
110 'access-restrictions' => _("Access Restrictions"),
111 'email-verification' => _("Email Verification"),
112 'convert-cached-html' => _("Convert cached_html"),
113 'db-check' => _("DB Check"),
114 'db-rebuild' => _("Db Rebuild")
116 return @$labels[$action];
119 function _do_purge_cache(&$request, $args) {
120 $dbi = $request->getDbh();
121 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
122 while (($page = $pages->next())) {
123 $page->set('_cached_html', false);
125 return _("Markup cache purged!");
128 function _do_purge_bad_pagenames(&$request, $args) {
129 // FIXME: this should be moved into WikiDB::normalize() or something...
130 $dbi = $request->getDbh();
132 $list = HTML::ol(array('align'=>'left'));
133 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
134 while (($page = $pages->next())) {
135 $pagename = $page->getName();
136 $wpn = new WikiPageName($pagename);
137 if (! $wpn->isValid() ) {
138 $dbi->purgePage($pagename);
139 $list->pushContent(HTML::li($pagename));
145 return _("No pages with bad names had to be deleted.");
147 return HTML(fmt("Deleted %s pages with invalid names:", $count),
148 HTML::div(array('align'=>'left'), $list));
153 * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
155 function _do_purge_empty_pages(&$request, $args) {
156 $dbi = $request->getDbh();
157 $count = 0; $notpurgable = 0;
158 $list = HTML::ol(array('align'=>'left'));
159 $pages = $dbi->getAllPages('include_empty');
160 while (($page = $pages->next())) {
162 and ($links = $page->getBackLinks('include_empty'))
165 $pagename = $page->getName();
166 if ($pagename == 'global_data' or $pagename == '.') continue;
167 if ($dbi->purgePage($pagename))
168 $list->pushContent(HTML::li($pagename.' '._("[purged]")));
170 $list->pushContent(HTML::li($pagename.' '._("[not purgable]")));
178 return _("No empty, unreferenced pages were found.");
180 return HTML(fmt("Deleted %s unreferenced pages:", $count),
181 HTML::div(array('align'=>'left'), $list),
183 fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.",
189 function _do_convert_cached_html(&$request, $args) {
191 require_once("lib/upgrade.php");
192 $dbh = $request->_dbi;
193 _upgrade_db_init($dbh);
195 $count = _upgrade_cached_html($dbh, false);
198 return _("No old _cached_html pagedata found.");
200 return HTML(fmt("Converted successfully %d pages", $count),
201 HTML::div(array('align'=>'left'), $list));
205 function _do_db_check(&$request, $args) {
207 $dbh = $request->getDbh();
208 //FIXME: display result.
209 return $dbh->_backend->check($args);
212 function _do_db_rebuild(&$request, $args) {
214 $dbh = $request->getDbh();
215 //FIXME: display result.
216 return $dbh->_backend->rebuild($args);
219 //TODO: We need a seperate plugin for this.
221 function _do_access_restrictions(&$request, &$args) {
222 return _("Sorry. Access Restrictions not yet implemented");
225 // pagelist with enable/disable button
226 function _do_email_verification(&$request, &$args) {
227 $dbi = $request->getDbh();
228 $pagelist = new PageList('pagename',0,$args);
229 //$args['return_url'] = 'action=email-verification-verified';
230 $email = new _PageList_Column_email('email',_("E-Mail"),'left');
231 $emailVerified = new _PageList_Column_emailVerified('emailVerified',
232 _("Verification Status"),'center');
233 $pagelist->_columns[0]->_heading = _("Username");
234 $pagelist->_columns[] = $email;
235 $pagelist->_columns[] = $emailVerified;
236 //This is the best method to find all users (Db and PersonalPage)
237 $current_user = $request->_user;
238 if (empty($args['verify'])) {
239 $group = $request->getGroup();
240 $allusers = $group->_allUsers();
242 if (!empty($args['user']))
243 $allusers = array_keys($args['user']);
247 foreach ($allusers as $username) {
249 $user = WikiUser($username);
251 $user = new WikiUser($request, $username);
252 $prefs = $user->getPreferences();
253 if ($prefs->get('email')) {
254 if (!$prefs->get('userid'))
255 $prefs->set('userid',$username);
256 if (!empty($pagelist->_rows))
257 $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
260 $class = ($group % 2) ? 'oddrow' : 'evenrow';
261 $row = HTML::tr(array('class' => $class));
262 $page_handle = $dbi->getPage($username);
263 $row->pushContent($pagelist->_columns[0]->format($pagelist,
264 $page_handle, $page_handle));
265 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
266 if (!empty($args['verify'])) {
267 $prefs->_prefs['email']->set('emailVerified',
268 empty($args['verified'][$username]) ? 0 : true);
269 $user->setPreferences($prefs);
271 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
272 $pagelist->_rows[] = $row;
275 $request->_user = $current_user;
276 if (!empty($args['verify']) or empty($pagelist->_rows)) {
277 return HTML($pagelist->_generateTable(false));
278 } elseif (!empty($pagelist->_rows)) {
280 $args['return_url'] = $request->getURLtoSelf();
281 return HTML::form(array('action' => $request->getPostURL(),
283 HiddenInputs($args, 'wikiadminutils'),
284 HiddenInputs(array('require_authority_for_post' =>
286 HiddenInputs($request->getArgs()),
287 $pagelist->_generateTable(false),
288 HTML::p(Button('submit:', _("Change Verification Status"),
291 Button('cancel', _("Cancel")))
297 require_once("lib/PageList.php");
299 class _PageList_Column_email
300 extends _PageList_Column {
301 function _getValue (&$prefs, $dummy) {
302 return $prefs->get('email');
306 class _PageList_Column_emailVerified
307 extends _PageList_Column {
308 function _getValue (&$prefs, $status) {
309 $name = $prefs->get('userid');
310 $input = HTML::input(array('type' => 'checkbox',
311 'name' => 'wikiadminutils[verified]['.$name.']',
313 if ($prefs->get('emailVerified'))
314 $input->setAttr('checked','1');
316 $input->setAttr('disabled','1');
317 return HTML($input, HTML::input
318 (array('type' => 'hidden',
319 'name' => 'wikiadminutils[user]['.$name.']',
328 // c-hanging-comment-ender-p: nil
329 // indent-tabs-mode: nil