2 rcs_id('$Id: WikiAdminUtils.php,v 1.25 2007-08-25 18:55:49 rurban Exp $');
4 Copyright 2003,2004,2006 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 class WikiPlugin_WikiAdminUtils
38 return _("WikiAdminUtils");
41 function getDescription () {
42 return _("Miscellaneous utility functions for the Administrator.");
45 function getVersion() {
46 return preg_replace("/[Revision: $]/", '',
47 "\$Revision: 1.25 $");
50 function getDefaultArguments() {
51 return array('action' => '',
56 function run($dbi, $argstr, &$request, $basepage) {
57 $args = $this->getArgs($argstr, $request);
58 $args['action'] = strtolower($args['action']);
62 $this->error("No action specified");
63 if (!($default_label = $this->_getLabel($action)))
64 $this->error("Bad action");
65 if ($request->getArg('action') != 'browse')
66 return $this->disabled("(action != 'browse')");
68 $posted = $request->getArg('wikiadminutils');
70 if ($request->isPost() and $posted['action'] == $action) { // a different form. we might have multiple
71 $user = $request->getUser();
72 if (!$user->isAdmin()) {
73 $request->_notAuthorized(WIKIAUTH_ADMIN);
74 return $this->error(_("You must be an administrator to use this plugin."));
76 return $this->do_action($request, $posted);
79 $label = $default_label;
81 return $this->_makeButton($request, $args, $label);
84 function _makeButton(&$request, $args, $label) {
85 $args['return_url'] = $request->getURLtoSelf();
86 return HTML::form(array('action' => $request->getPostURL(),
88 HTML::p(Button('submit:', $label, 'wikiadmin')),
89 HiddenInputs($args, 'wikiadminutils'),
90 HiddenInputs(array('require_authority_for_post' =>
92 HiddenInputs($request->getArgs(),false,array('action')));
95 function do_action(&$request, $args) {
96 $method = strtolower('_do_' . str_replace('-', '_', $args['action']));
97 if (!method_exists($this, $method))
98 return $this->error("Bad action $method");
100 $message = call_user_func(array(&$this, $method), $request, $args);
102 // display as seperate page or as alert?
103 $alert = new Alert(fmt("WikiAdminUtils %s returned:", $args['action']),
105 array(_("Back") => $args['return_url']));
106 $alert->show(); // noreturn
109 function _getLabel($action) {
110 $labels = array('purge-cache' => _("Purge Markup Cache"),
111 'purge-bad-pagenames' => _("Purge all Pages With Invalid Names"),
112 'purge-empty-pages' => _("Purge all empty, unreferenced Pages"),
113 'access-restrictions' => _("Access Restrictions"),
114 'email-verification' => _("Email Verification"),
115 'convert-cached-html' => _("Convert cached_html"),
116 'db-check' => _("DB Check"),
117 'db-rebuild' => _("Db Rebuild")
119 return @$labels[$action];
122 function _do_purge_cache(&$request, $args) {
123 $dbi = $request->getDbh();
124 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
125 while (($page = $pages->next())) {
126 $page->set('_cached_html', false);
128 return _("Markup cache purged!");
131 function _do_purge_bad_pagenames(&$request, $args) {
132 // FIXME: this should be moved into WikiDB::normalize() or something...
133 $dbi = $request->getDbh();
135 $list = HTML::ol(array('align'=>'left'));
136 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
137 while (($page = $pages->next())) {
138 $pagename = $page->getName();
139 $wpn = new WikiPageName($pagename);
140 if (! $wpn->isValid() ) {
141 $dbi->purgePage($pagename);
142 $list->pushContent(HTML::li($pagename));
148 return _("No pages with bad names had to be deleted.");
150 return HTML(fmt("Deleted %s pages with invalid names:", $count),
151 HTML::div(array('align'=>'left'), $list));
156 * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
158 function _do_purge_empty_pages(&$request, $args) {
159 $dbi = $request->getDbh();
160 $count = 0; $notpurgable = 0;
161 $list = HTML::ol(array('align'=>'left'));
162 $pages = $dbi->getAllPages('include_empty');
163 while (($page = $pages->next())) {
165 and ($links = $page->getBackLinks('include_empty'))
168 $pagename = $page->getName();
169 if ($pagename == 'global_data' or $pagename == '.') continue;
170 if ($dbi->purgePage($pagename))
171 $list->pushContent(HTML::li($pagename.' '._("[purged]")));
173 $list->pushContent(HTML::li($pagename.' '._("[not purgable]")));
181 return _("No empty, unreferenced pages were found.");
183 return HTML(fmt("Deleted %s unreferenced pages:", $count),
184 HTML::div(array('align'=>'left'), $list),
186 fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.",
192 function _do_convert_cached_html(&$request, $args) {
194 require_once("lib/upgrade.php");
195 $dbh = $request->_dbi;
196 _upgrade_db_init($dbh);
198 $count = _upgrade_cached_html($dbh, false);
201 return _("No old _cached_html pagedata found.");
203 return HTML(fmt("Converted successfully %d pages", $count),
204 HTML::div(array('align'=>'left'), $list));
208 function _do_db_check(&$request, $args) {
210 $dbh = $request->getDbh();
211 //FIXME: display result.
212 $result = $dbh->_backend->check($args);
216 function _do_db_rebuild(&$request, $args) {
218 $dbh = $request->getDbh();
219 //FIXME: display result.
220 $result = $dbh->_backend->rebuild($args);
224 //TODO: We need a seperate plugin for this.
226 function _do_access_restrictions(&$request, &$args) {
227 return _("Sorry. Access Restrictions not yet implemented");
230 // pagelist with enable/disable button
231 function _do_email_verification(&$request, &$args) {
232 $dbi = $request->getDbh();
233 $pagelist = new PageList('pagename',0,$args);
234 //$args['return_url'] = 'action=email-verification-verified';
235 $email = new _PageList_Column_email('email',_("E-Mail"),'left');
236 $emailVerified = new _PageList_Column_emailVerified('emailVerified',
237 _("Verification Status"),'center');
238 $pagelist->_columns[0]->_heading = _("Username");
239 $pagelist->_columns[] = $email;
240 $pagelist->_columns[] = $emailVerified;
241 //This is the best method to find all users (Db and PersonalPage)
242 $current_user = $request->_user;
243 if (empty($args['verify'])) {
244 $group = $request->getGroup();
245 $allusers = $group->_allUsers();
247 if (!empty($args['user']))
248 $allusers = array_keys($args['user']);
252 foreach ($allusers as $username) {
254 $user = WikiUser($username);
256 $user = new WikiUser($request, $username);
257 $prefs = $user->getPreferences();
258 if ($prefs->get('email')) {
259 if (!$prefs->get('userid'))
260 $prefs->set('userid',$username);
261 if (!empty($pagelist->_rows))
262 $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
265 $class = ($group % 2) ? 'oddrow' : 'evenrow';
266 $row = HTML::tr(array('class' => $class));
267 $page_handle = $dbi->getPage($username);
268 $row->pushContent($pagelist->_columns[0]->format($pagelist,
269 $page_handle, $page_handle));
270 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
271 if (!empty($args['verify'])) {
272 $prefs->_prefs['email']->set('emailVerified',
273 empty($args['verified'][$username]) ? 0 : true);
274 $user->setPreferences($prefs);
276 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
277 $pagelist->_rows[] = $row;
280 $request->_user = $current_user;
281 if (!empty($args['verify']) or empty($pagelist->_rows)) {
282 return HTML($pagelist->_generateTable(false));
283 } elseif (!empty($pagelist->_rows)) {
285 $args['return_url'] = $request->getURLtoSelf();
286 return HTML::form(array('action' => $request->getPostURL(),
288 HiddenInputs($args, 'wikiadminutils'),
289 HiddenInputs(array('require_authority_for_post' =>
291 HiddenInputs($request->getArgs()),
292 $pagelist->_generateTable(false),
293 HTML::p(Button('submit:', _("Change Verification Status"),
296 Button('cancel', _("Cancel")))
302 require_once("lib/PageList.php");
304 class _PageList_Column_email
305 extends _PageList_Column {
306 function _getValue (&$prefs, $dummy) {
307 return $prefs->get('email');
311 class _PageList_Column_emailVerified
312 extends _PageList_Column {
313 function _getValue (&$prefs, $status) {
314 $name = $prefs->get('userid');
315 $input = HTML::input(array('type' => 'checkbox',
316 'name' => 'wikiadminutils[verified]['.$name.']',
318 if ($prefs->get('emailVerified'))
319 $input->setAttr('checked','1');
321 $input->setAttr('disabled','1');
322 return HTML($input, HTML::input
323 (array('type' => 'hidden',
324 'name' => 'wikiadminutils[user]['.$name.']',
335 // c-hanging-comment-ender-p: nil
336 // indent-tabs-mode: nil