2 rcs_id('$Id: WikiAdminUtils.php,v 1.20 2006-09-30 10:08:38 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.20 $");
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()));
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");
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(_("Okay") => $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 return @$labels[$action];
116 function _do_purge_cache(&$request, $args) {
117 $dbi = $request->getDbh();
118 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
119 while (($page = $pages->next())) {
120 $page->set('_cached_html', false);
122 return _("Markup cache purged!");
125 function _do_purge_bad_pagenames(&$request, $args) {
126 // FIXME: this should be moved into WikiDB::normalize() or something...
127 $dbi = $request->getDbh();
129 $list = HTML::ol(array('align'=>'left'));
130 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
131 while (($page = $pages->next())) {
132 $pagename = $page->getName();
133 $wpn = new WikiPageName($pagename);
134 if (! $wpn->isValid() ) {
135 $dbi->purgePage($pagename);
136 $list->pushContent(HTML::li($pagename));
142 return _("No pages with bad names had to be deleted.");
144 return HTML(fmt("Deleted %s pages with invalid names:", $count),
145 HTML::div(array('align'=>'left'), $list));
150 * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
152 function _do_purge_empty_pages(&$request, $args) {
153 $dbi = $request->getDbh();
154 $count = 0; $notpurgable = 0;
155 $list = HTML::ol(array('align'=>'left'));
156 $pages = $dbi->getAllPages('include_empty');
157 while (($page = $pages->next())) {
159 and ($links = $page->getBackLinks('include_empty'))
162 $pagename = $page->getName();
163 if ($pagename == 'global_data' or $pagename == '.') continue;
164 if ($dbi->purgePage($pagename))
165 $list->pushContent(HTML::li($pagename.' '._("[purged]")));
167 $list->pushContent(HTML::li($pagename.' '._("[not purgable]")));
175 return _("No empty, unreferenced pages were found.");
177 return HTML(fmt("Deleted %s unreferenced pages:", $count),
178 HTML::div(array('align'=>'left'), $list),
180 fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.",
186 function _do_convert_cached_html(&$request, $args) {
188 require_once("lib/upgrade.php");
189 $dbh = $request->_dbi;
190 _upgrade_db_init($dbh);
192 $count = _upgrade_cached_html($dbh, false);
195 return _("No old _cached_html pagedata found.");
197 return HTML(fmt("Converted successfully %d pages", $count),
198 HTML::div(array('align'=>'left'), $list));
202 function _do_db_check(&$request, $args) {
203 $dbh = $request->_dbi;
204 //FIXME: display result.
205 $result = $dbh->_backend->check();
209 function _do_db_rebuild(&$request, $args) {
210 $dbh = $request->_dbi;
211 //FIXME: display result.
212 $result = $dbh->_backend->rebuild();
216 //TODO: We need a seperate plugin for this.
218 function _do_access_restrictions(&$request, &$args) {
219 return _("Sorry. Access Restrictions not yet implemented");
222 // pagelist with enable/disable button
223 function _do_email_verification(&$request, &$args) {
224 $dbi = $request->getDbh();
225 $pagelist = new PageList('pagename',0,$args);
226 //$args['return_url'] = 'action=email-verification-verified';
227 $email = new _PageList_Column_email('email',_("E-Mail"),'left');
228 $emailVerified = new _PageList_Column_emailVerified('emailVerified',
229 _("Verification Status"),'center');
230 $pagelist->_columns[] = $email;
231 $pagelist->_columns[] = $emailVerified;
232 //This is the best method to find all users (Db and PersonalPage)
233 $current_user = $request->_user;
234 if (empty($args['verify'])) {
235 $group = $request->getGroup();
236 $allusers = $group->_allUsers();
238 $allusers = array_keys($args['user']);
240 foreach ($allusers as $username) {
242 $user = WikiUser($username);
244 $user = new WikiUser($request, $username);
245 $prefs = $user->getPreferences();
246 if ($prefs->get('email')) {
247 if (!$prefs->get('userid'))
248 $prefs->set('userid',$username);
249 if (!empty($pagelist->_rows))
250 $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
253 $class = ($group % 2) ? 'oddrow' : 'evenrow';
254 $row = HTML::tr(array('class' => $class));
255 $page_handle = $dbi->getPage($username);
256 $row->pushContent($pagelist->_columns[0]->format($pagelist,
257 $page_handle, $page_handle));
258 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
259 if (!empty($args['verify'])) {
260 $prefs->_prefs['email']->set('emailVerified',
261 empty($args['verified'][$username]) ? 0 : 2);
262 $user->setPreferences($prefs);
264 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
265 $pagelist->_rows[] = $row;
268 $request->_user = $current_user;
269 if (!empty($args['verify'])) {
270 return HTML($pagelist->_generateTable(false));
273 $args['return_url'] = $request->getURLtoSelf();
274 return HTML::form(array('action' => $request->getPostURL(),
276 HiddenInputs($args, 'wikiadminutils'),
277 HiddenInputs(array('require_authority_for_post' =>
279 HiddenInputs($request->getArgs()),
280 $pagelist->_generateTable(false),
281 HTML::p(Button('submit:', _("Change Verification Status"),
284 Button('cancel', _("Cancel")))
290 require_once("lib/PageList.php");
292 class _PageList_Column_email
293 extends _PageList_Column {
294 function _getValue (&$prefs, $dummy) {
295 return $prefs->get('email');
299 class _PageList_Column_emailVerified
300 extends _PageList_Column {
301 function _getValue (&$prefs, $status) {
302 $name = $prefs->get('userid');
303 $input = HTML::input(array('type' => 'checkbox',
304 'name' => 'wikiadminutils[verified]['.$name.']',
306 if ($prefs->get('emailVerified'))
307 $input->setAttr('checked','1');
309 $input->setAttr('disabled','1');
310 return HTML($input, HTML::input
311 (array('type' => 'hidden',
312 'name' => 'wikiadminutils[user]['.$name.']',
323 // c-hanging-comment-ender-p: nil
324 // indent-tabs-mode: nil