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 along
20 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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");
60 if (!($default_label = $this->_getLabel($action))) {
61 return HTML::div(array('class' => "error"), fmt("Bad action requested: %s", $action));
63 if ($request->getArg('action') != 'browse') {
64 return $this->disabled(_("Plugin not run: not in browse mode"));
67 $posted = $request->getArg('wikiadminutils');
69 if ($request->isPost() and $posted['action'] == $action) { // a different form. we might have multiple
70 $user = $request->getUser();
71 if (!$user->isAdmin()) {
72 $request->_notAuthorized(WIKIAUTH_ADMIN);
73 return $this->error(_("You must be an administrator to use this plugin."));
75 return $this->do_action($request, $posted);
78 $label = $default_label;
80 return $this->_makeButton($request, $args, $label);
83 function _makeButton(&$request, $args, $label) {
84 $args['return_url'] = $request->getURLtoSelf();
85 return HTML::form(array('action' => $request->getPostURL(),
87 HTML::p(Button('submit:', $label, 'wikiadmin')),
88 HiddenInputs($args, 'wikiadminutils'),
89 HiddenInputs(array('require_authority_for_post' =>
91 HiddenInputs($request->getArgs(),false,array('action')));
94 function do_action(&$request, $args) {
95 $method = strtolower('_do_' . str_replace('-', '_', $args['action']));
96 if (!method_exists($this, $method))
97 return $this->error("Bad action $method");
99 $message = call_user_func(array(&$this, $method), $request, $args);
101 // display as seperate page or as alert?
102 $alert = new Alert(fmt("WikiAdminUtils %s returned:", $args['action']),
104 array(_("Back") => $args['return_url']));
105 $alert->show(); // noreturn
108 function _getLabel($action) {
109 $labels = array('purge-cache' => _("Purge Markup Cache"),
110 'purge-bad-pagenames' => _("Purge all Pages With Invalid Names"),
111 'purge-empty-pages' => _("Purge all empty, unreferenced Pages"),
112 'access-restrictions' => _("Access Restrictions"),
113 'email-verification' => _("E-mail address confirmation"),
114 'convert-cached-html' => _("Convert cached_html"),
115 'db-check' => _("DB Check"),
116 'db-rebuild' => _("Db Rebuild")
118 return @$labels[$action];
121 function _do_purge_cache(&$request, $args) {
122 $dbi = $request->getDbh();
123 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
124 while (($page = $pages->next())) {
125 $page->set('_cached_html', false);
127 return _("Markup cache purged!");
130 function _do_purge_bad_pagenames(&$request, $args) {
131 // FIXME: this should be moved into WikiDB::normalize() or something...
132 $dbi = $request->getDbh();
134 $list = HTML::ol(array('align'=>'left'));
135 $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
136 while (($page = $pages->next())) {
137 $pagename = $page->getName();
138 $wpn = new WikiPageName($pagename);
139 if (! $wpn->isValid() ) {
140 $dbi->purgePage($pagename);
141 $list->pushContent(HTML::li($pagename));
147 return _("No pages with bad names had to be deleted.");
149 return HTML(fmt("Deleted %d pages with invalid names:", $count),
150 HTML::div(array('align'=>'left'), $list));
155 * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
157 function _do_purge_empty_pages(&$request, $args) {
158 $dbi = $request->getDbh();
159 $count = 0; $notpurgable = 0;
160 $list = HTML::ol(array('align'=>'left'));
161 $pages = $dbi->getAllPages('include_empty');
162 while (($page = $pages->next())) {
164 and ($links = $page->getBackLinks('include_empty'))
167 $pagename = $page->getName();
168 if ($pagename == 'global_data' or $pagename == '.') continue;
169 if ($dbi->purgePage($pagename))
170 $list->pushContent(HTML::li($pagename.' '._("[purged]")));
172 $list->pushContent(HTML::li($pagename.' '._("[not purgable]")));
180 return _("No empty, unreferenced pages were found.");
182 return HTML(fmt("Deleted %d unreferenced pages:", $count),
183 HTML::div(array('align'=>'left'), $list),
185 fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.",
190 function _do_convert_cached_html(&$request, $args) {
192 require_once 'lib/upgrade.php';
193 $dbh = $request->_dbi;
194 _upgrade_db_init($dbh);
196 $count = _upgrade_cached_html($dbh, false);
199 return _("No old _cached_html pagedata found.");
201 return HTML(fmt("Converted successfully %d pages", $count),
202 HTML::div(array('align'=>'left'), $list));
206 function _do_db_check(&$request, $args) {
208 $dbh = $request->getDbh();
209 //FIXME: display result.
210 return $dbh->_backend->check($args);
213 function _do_db_rebuild(&$request, $args) {
215 $dbh = $request->getDbh();
216 //FIXME: display result.
217 return $dbh->_backend->rebuild($args);
220 //TODO: We need a seperate plugin for this.
222 function _do_access_restrictions(&$request, &$args) {
223 return _("Sorry. Access Restrictions not yet implemented");
226 // pagelist with enable/disable button
227 function _do_email_verification(&$request, &$args) {
228 $dbi = $request->getDbh();
229 $pagelist = new PageList('pagename',0,$args);
230 //$args['return_url'] = 'action=email-verification-verified';
231 $email = new _PageList_Column_email('email',_("E-mail"),'left');
232 $emailVerified = new _PageList_Column_emailVerified('emailVerified',
233 _("Verification Status"),'center');
234 $pagelist->_columns[0]->_heading = _("Username");
235 $pagelist->_columns[] = $email;
236 $pagelist->_columns[] = $emailVerified;
237 //This is the best method to find all users (Db and PersonalPage)
238 $current_user = $request->_user;
239 if (empty($args['verify'])) {
240 $group = $request->getGroup();
241 $allusers = $group->_allUsers();
243 if (!empty($args['user']))
244 $allusers = array_keys($args['user']);
248 foreach ($allusers as $username) {
250 $user = WikiUser($username);
252 $user = new WikiUser($request, $username);
253 $prefs = $user->getPreferences();
254 if ($prefs->get('email')) {
255 if (!$prefs->get('userid'))
256 $prefs->set('userid',$username);
257 if (!empty($pagelist->_rows))
258 $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
261 $class = ($group % 2) ? 'oddrow' : 'evenrow';
262 $row = HTML::tr(array('class' => $class));
263 $page_handle = $dbi->getPage($username);
264 $row->pushContent($pagelist->_columns[0]->format($pagelist,
265 $page_handle, $page_handle));
266 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
267 if (!empty($args['verify'])) {
268 $prefs->_prefs['email']->set('emailVerified',
269 empty($args['verified'][$username]) ? 0 : true);
270 $user->setPreferences($prefs);
272 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
273 $pagelist->_rows[] = $row;
276 $request->_user = $current_user;
277 if (!empty($args['verify']) or empty($pagelist->_rows)) {
278 return HTML($pagelist->_generateTable(false));
279 } elseif (!empty($pagelist->_rows)) {
281 $args['return_url'] = $request->getURLtoSelf();
282 return HTML::form(array('action' => $request->getPostURL(),
284 HiddenInputs($args, 'wikiadminutils'),
285 HiddenInputs(array('require_authority_for_post' =>
287 HiddenInputs($request->getArgs()),
288 $pagelist->_generateTable(false),
289 HTML::p(Button('submit:', _("Change Verification Status"),
292 Button('cancel', _("Cancel")))
298 require_once 'lib/PageList.php';
300 class _PageList_Column_email
301 extends _PageList_Column {
302 function _getValue (&$prefs, $dummy) {
303 return $prefs->get('email');
307 class _PageList_Column_emailVerified
308 extends _PageList_Column {
309 function _getValue (&$prefs, $status) {
310 $name = $prefs->get('userid');
311 $input = HTML::input(array('type' => 'checkbox',
312 'name' => 'wikiadminutils[verified]['.$name.']',
314 if ($prefs->get('emailVerified'))
315 $input->setAttr('checked','1');
317 $input->setAttr('disabled','1');
318 return HTML($input, HTML::input
319 (array('type' => 'hidden',
320 'name' => 'wikiadminutils[user]['.$name.']',
329 // c-hanging-comment-ender-p: nil
330 // indent-tabs-mode: nil