2 rcs_id('$Id: WikiAdminUtils.php,v 1.18 2005-09-10 11:30:40 rurban Exp $');
4 Copyright 2003, 2004 $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
32 class WikiPlugin_WikiAdminUtils
36 return _("WikiAdminUtils");
39 function getDescription () {
40 return _("Miscellaneous utility functions for the Administrator.");
43 function getVersion() {
44 return preg_replace("/[Revision: $]/", '',
45 "\$Revision: 1.18 $");
48 function getDefaultArguments() {
49 return array('action' => '',
54 function run($dbi, $argstr, &$request, $basepage) {
55 $args = $this->getArgs($argstr, $request);
56 $args['action'] = strtolower($args['action']);
60 $this->error("No action specified");
61 if (!($default_label = $this->_getLabel($action)))
62 $this->error("Bad action");
63 if ($request->getArg('action') != 'browse')
64 return $this->disabled("(action != 'browse')");
66 $posted = $request->getArg('wikiadminutils');
68 if ($request->isPost() and $posted['action'] == $action) { // a different form. we might have multiple
69 $user = $request->getUser();
70 if (!$user->isAdmin()) {
71 $request->_notAuthorized(WIKIAUTH_ADMIN);
72 return $this->error(_("You must be an administrator to use this plugin."));
74 return $this->do_action($request, $posted);
77 $label = $default_label;
79 return $this->_makeButton($request, $args, $label);
82 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()));
93 function do_action(&$request, $args) {
94 $method = strtolower('_do_' . str_replace('-', '_', $args['action']));
95 if (!method_exists($this, $method))
96 return $this->error("Bad action");
98 $message = call_user_func(array(&$this, $method), $request, $args);
100 // display as seperate page or as alert?
101 $alert = new Alert(_("WikiAdminUtils says:"),
103 array(_("Okay") => $args['return_url']));
104 $alert->show(); // noreturn
107 function _getLabel($action) {
108 $labels = array('purge-cache' => _("Purge Markup Cache"),
109 'purge-bad-pagenames' => _("Purge all Pages With Invalid Names"),
110 'purge-empty-pages' => _("Purge all empty, unreferenced Pages"));
111 return @$labels[$action];
114 function _do_purge_cache(&$request, $args) {
115 $dbi = $request->getDbh();
116 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
117 while (($page = $pages->next())) {
118 $page->set('_cached_html', false);
120 return _("Markup cache purged!");
123 function _do_purge_bad_pagenames(&$request, $args) {
124 // FIXME: this should be moved into WikiDB::normalize() or something...
125 $dbi = $request->getDbh();
127 $list = HTML::ol(array('align'=>'left'));
128 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
129 while (($page = $pages->next())) {
130 $pagename = $page->getName();
131 $wpn = new WikiPageName($pagename);
132 if (! $wpn->isValid() ) {
133 $dbi->purgePage($pagename);
134 $list->pushContent(HTML::li($pagename));
140 return _("No pages with bad names had to be deleted.");
142 return HTML(fmt("Deleted %s pages with invalid names:", $count),
143 HTML::div(array('align'=>'left'), $list));
148 * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
150 function _do_purge_empty_pages(&$request, $args) {
151 $dbi = $request->getDbh();
152 $count = 0; $notpurgable = 0;
153 $list = HTML::ol(array('align'=>'left'));
154 $pages = $dbi->getAllPages('include_empty');
155 while (($page = $pages->next())) {
156 if (!$page->exists() and ($links = $page->getBackLinks('include_empty')) and !$links->next()) {
157 $pagename = $page->getName();
158 if ($pagename == 'global_data' or $pagename == '.') continue;
159 if ($dbi->purgePage($pagename))
160 $list->pushContent(HTML::li($pagename.' '._("[purged]")));
162 $list->pushContent(HTML::li($pagename.' '._("[not purgable]")));
170 return _("No empty, unreferenced pages were found.");
172 return HTML(fmt("Deleted %s unreferenced pages:", $count),
173 HTML::div(array('align'=>'left'), $list),
175 fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.",
181 function _do_convert_cached_html(&$request, $args) {
183 require_once("lib/upgrade.php");
184 $dbh = $request->_dbi;
185 _upgrade_db_init($dbh);
187 $count = _upgrade_cached_html($dbh, false);
190 return _("No old _cached_html pagedata found.");
192 return HTML(fmt("Converted successfully %d pages", $count),
193 HTML::div(array('align'=>'left'), $list));
198 //TODO: We need a seperate plugin for this.
200 function _do_access_restrictions(&$request, &$args) {
201 return _("Sorry. Access Restrictions not yet implemented");
204 // pagelist with enable/disable button
205 function _do_email_verification(&$request, &$args) {
206 $dbi = $request->getDbh();
207 $pagelist = new PageList('pagename',0,$args);
208 //$args['return_url'] = 'action=email-verification-verified';
209 $email = new _PageList_Column_email('email',_("E-Mail"),'left');
210 $emailVerified = new _PageList_Column_emailVerified('emailVerified',
211 _("Verification Status"),'center');
212 $pagelist->_columns[] = $email;
213 $pagelist->_columns[] = $emailVerified;
214 //This is the best method to find all users (Db and PersonalPage)
215 $current_user = $request->_user;
216 if (empty($args['verify'])) {
217 $group = $request->getGroup();
218 $allusers = $group->_allUsers();
220 $allusers = array_keys($args['user']);
222 foreach ($allusers as $username) {
224 $user = WikiUser($username);
226 $user = new WikiUser($request, $username);
227 $prefs = $user->getPreferences();
228 if ($prefs->get('email')) {
229 if (!$prefs->get('userid'))
230 $prefs->set('userid',$username);
231 $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
232 $class = ($group % 2) ? 'oddrow' : 'evenrow';
233 $row = HTML::tr(array('class' => $class));
234 $page_handle = $dbi->getPage($username);
235 $row->pushContent($pagelist->_columns[0]->format($pagelist,
236 $page_handle, $page_handle));
237 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
238 if (!empty($args['verify'])) {
239 $prefs->_prefs['email']->set('emailVerified',
240 empty($args['verified'][$username]) ? 0 : 2);
241 $user->setPreferences($prefs);
243 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
244 $pagelist->_rows[] = $row;
247 $request->_user = $current_user;
248 if (!empty($args['verify'])) {
249 return HTML($pagelist->_generateTable(false));
252 $args['return_url'] = $request->getURLtoSelf();
253 return HTML::form(array('action' => $request->getPostURL(),
255 HiddenInputs($args, 'wikiadminutils'),
256 HiddenInputs(array('require_authority_for_post' =>
258 HiddenInputs($request->getArgs()),
259 $pagelist->_generateTable(false),
260 HTML::p(Button('submit:', _("Change Verification Status"),
263 Button('cancel', _("Cancel")))
269 require_once("lib/PageList.php");
271 class _PageList_Column_email
272 extends _PageList_Column {
273 function _getValue (&$prefs, $dummy) {
274 return $prefs->get('email');
278 class _PageList_Column_emailVerified
279 extends _PageList_Column {
280 function _getValue (&$prefs, $status) {
281 $name = $prefs->get('userid');
282 $input = HTML::input(array('type' => 'checkbox',
283 'name' => 'wikiadminutils[verified]['.$name.']',
285 if ($prefs->get('emailVerified'))
286 $input->setAttr('checked','1');
288 $input->setAttr('disabled','1');
289 return HTML($input, HTML::input
290 (array('type' => 'hidden',
291 'name' => 'wikiadminutils[user]['.$name.']',
302 // c-hanging-comment-ender-p: nil
303 // indent-tabs-mode: nil