2 rcs_id('$Id: WikiAdminUtils.php,v 1.14 2004-12-06 19:50:05 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
31 class WikiPlugin_WikiAdminUtils
35 return _("WikiAdminUtils");
38 function getDescription () {
39 return _("Miscellaneous utility functions for the WIKIADMIN.");
42 function getVersion() {
43 return preg_replace("/[Revision: $]/", '',
44 "\$Revision: 1.14 $");
47 function getDefaultArguments() {
48 return array('action' => '',
53 function run($dbi, $argstr, &$request, $basepage) {
54 $args = $this->getArgs($argstr, $request);
55 $args['action'] = strtolower($args['action']);
59 $this->error("No action specified");
60 if (!($default_label = $this->_getLabel($action)))
61 $this->error("Bad 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()));
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");
97 $message = call_user_func(array(&$this, $method), $request, $args);
99 // display as seperate page or as alert?
100 $alert = new Alert(_("WikiAdminUtils says:"),
102 array(_("Okay") => $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 return @$labels[$action];
113 function _do_purge_cache(&$request, $args) {
114 $dbi = $request->getDbh();
115 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
116 while (($page = $pages->next())) {
117 $page->set('_cached_html', false);
119 return _("Markup cache purged!");
122 function _do_purge_bad_pagenames(&$request, $args) {
123 // FIXME: this should be moved into WikiDB::normalize() or something...
124 $dbi = $request->getDbh();
126 $list = HTML::ol(array('align'=>'left'));
127 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
128 while (($page = $pages->next())) {
129 $pagename = $page->getName();
130 $wpn = new WikiPageName($pagename);
131 if (! $wpn->isValid()) {
132 $dbi->purgePage($pagename);
133 $list->pushContent(HTML::li($pagename));
139 return _("No pages with bad names were found.");
141 return HTML(fmt("Deleted %s pages with invalid names:", $count),
142 HTML::div(array('align'=>'left'), $list));
147 * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
149 function _do_purge_empty_pages(&$request, $args) {
150 $dbi = $request->getDbh();
151 $count = 0; $notpurgable = 0;
152 $list = HTML::ol(array('align'=>'left'));
153 $pages = $dbi->getAllPages('include_empty');
154 while (($page = $pages->next())) {
155 if (!$page->exists() and ($links = $page->getBackLinks('include_empty')) and !$links->next()) {
156 $pagename = $page->getName();
157 if ($pagename == 'global_data' or $pagename == '.') continue;
158 if ($dbi->purgePage($pagename))
159 $list->pushContent(HTML::li($pagename.' '._("[purged]")));
161 $list->pushContent(HTML::li($pagename.' '._("[not purgable]")));
169 return _("No empty, unreferenced pages were found.");
171 return HTML(fmt("Deleted %s unreferenced pages:", $count),
172 HTML::div(array('align'=>'left'), $list),
174 fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.",
179 //TODO: We need a seperate plugin for this.
181 function _do_access_restrictions(&$request, &$args) {
182 return _("Sorry. Access Restrictions not yet implemented");
185 // pagelist with enable/disable button
186 function _do_email_verification(&$request, &$args) {
187 $dbi = $request->getDbh();
188 $pagelist = new PageList('pagename',0,$args);
189 //$args['return_url'] = 'action=email-verification-verified';
190 $email = new _PageList_Column_email('email',_("E-Mail"),'left');
191 $emailVerified = new _PageList_Column_emailVerified('emailVerified',
192 _("Verification Status"),'center');
193 $pagelist->_columns[] = $email;
194 $pagelist->_columns[] = $emailVerified;
195 //This is the best method to find all users (Db and PersonalPage)
196 $current_user = $request->_user;
197 if (empty($args['verify'])) {
198 $group = $request->getGroup();
199 $allusers = $group->_allUsers();
201 $allusers = array_keys($args['user']);
203 foreach ($allusers as $username) {
205 $user = WikiUser($username);
207 $user = new WikiUser($request, $username);
208 $prefs = $user->getPreferences();
209 if ($prefs->get('email')) {
210 if (!$prefs->get('userid'))
211 $prefs->set('userid',$username);
212 $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
213 $class = ($group % 2) ? 'oddrow' : 'evenrow';
214 $row = HTML::tr(array('class' => $class));
215 $page_handle = $dbi->getPage($username);
216 $row->pushContent($pagelist->_columns[0]->format($pagelist,
217 $page_handle, $page_handle));
218 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
219 if (!empty($args['verify'])) {
220 $prefs->_prefs['email']->set('emailVerified',
221 empty($args['verified'][$username]) ? 0 : 2);
222 $user->setPreferences($prefs);
224 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
225 $pagelist->_rows[] = $row;
228 $request->_user = $current_user;
229 if (!empty($args['verify'])) {
230 return HTML($pagelist->_generateTable(false));
233 $args['return_url'] = $request->getURLtoSelf();
234 return HTML::form(array('action' => $request->getPostURL(),
236 HiddenInputs($args, 'wikiadminutils'),
237 HiddenInputs(array('require_authority_for_post' =>
239 HiddenInputs($request->getArgs()),
240 $pagelist->_generateTable(false),
241 HTML::p(Button('submit:', _("Change Verification Status"),
244 Button('cancel', _("Cancel")))
250 require_once("lib/PageList.php");
252 class _PageList_Column_email
253 extends _PageList_Column {
254 function _getValue (&$prefs, $dummy) {
255 return $prefs->get('email');
259 class _PageList_Column_emailVerified
260 extends _PageList_Column {
261 function _getValue (&$prefs, $status) {
262 $name = $prefs->get('userid');
263 $input = HTML::input(array('type' => 'checkbox',
264 'name' => 'wikiadminutils[verified]['.$name.']',
266 if ($prefs->get('emailVerified'))
267 $input->setAttr('checked','1');
269 $input->setAttr('disabled','1');
270 return HTML($input, HTML::input
271 (array('type' => 'hidden',
272 'name' => 'wikiadminutils[user]['.$name.']',
283 // c-hanging-comment-ender-p: nil
284 // indent-tabs-mode: nil