]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminUtils.php
Use real list instead of middot
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminUtils.php
1 <?php
2
3 /**
4  * Copyright 2003,2004,2006 $ThePhpWikiProgrammingTeam
5  * Copyright 2009 Marc-Etienne Vargenau, Alcatel-Lucent
6  *
7  * This file is part of PhpWiki.
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 /**
25 valid actions:
26 purge-cache
27 purge-bad-pagenames
28 purge-empty-pages
29 email-verification
30 convert-cached-html
31 db-check
32 db-rebuild
33  */
34 class WikiPlugin_WikiAdminUtils
35     extends WikiPlugin
36 {
37     function getDescription()
38     {
39         return _("Miscellaneous utility functions for the Administrator.");
40     }
41
42     function getDefaultArguments()
43     {
44         return array('action' => '',
45             'label' => '',
46         );
47     }
48
49     /**
50      * @param WikiDB $dbi
51      * @param string $argstr
52      * @param WikiRequest $request
53      * @param string $basepage
54      * @return mixed
55      */
56     function run($dbi, $argstr, &$request, $basepage)
57     {
58         $args = $this->getArgs($argstr, $request);
59         $args['action'] = strtolower($args['action']);
60         extract($args);
61
62         if (!$action) {
63             $this->error("No action specified");
64         }
65         if (!($default_label = $this->_getLabel($action))) {
66             return HTML::div(array('class' => "error"), fmt("Bad action requested: %s", $action));
67         }
68         if ($request->getArg('action') != 'browse') {
69             return $this->disabled(_("Plugin not run: not in browse mode"));
70         }
71
72         $posted = $request->getArg('wikiadminutils');
73
74         if ($request->isPost() and $posted['action'] == $action) { // a different form. we might have multiple
75             $user = $request->getUser();
76             if (!$user->isAdmin()) {
77                 $request->_notAuthorized(WIKIAUTH_ADMIN);
78                 return $this->error(_("You must be an administrator to use this plugin."));
79             }
80             return $this->do_action($request, $posted);
81         }
82         if (empty($label))
83             $label = $default_label;
84
85         return $this->_makeButton($request, $args, $label);
86     }
87
88     protected function _makeButton(&$request, $args, $label)
89     {
90         $args['return_url'] = $request->getURLtoSelf();
91         return HTML::form(array('action' => $request->getPostURL(),
92                 'method' => 'post'),
93             HTML::p(Button('submit:', $label, 'wikiadmin')),
94             HiddenInputs($args, 'wikiadminutils'),
95             HiddenInputs(array('require_authority_for_post' =>
96             WIKIAUTH_ADMIN)),
97             HiddenInputs($request->getArgs(), false, array('action')));
98     }
99
100     function do_action(&$request, $args)
101     {
102         $method = strtolower('_do_' . str_replace('-', '_', $args['action']));
103         if (!method_exists($this, $method))
104             return $this->error("Bad action $method");
105
106         $message = call_user_func(array(&$this, $method), $request, $args);
107
108         // display as separate page or as alert?
109         $alert = new Alert(fmt("WikiAdminUtils %s returned:", $args['action']),
110             $message,
111             array(_("Back") => $args['return_url']));
112         $alert->show(); // noreturn
113         return '';
114     }
115
116     private function _getLabel($action)
117     {
118         $labels = array('purge-cache' => _("Purge Markup Cache"),
119             'purge-bad-pagenames' => _("Purge all Pages With Invalid Names"),
120             'purge-empty-pages' => _("Purge all empty, unreferenced Pages"),
121             'email-verification' => _("E-mail address confirmation"),
122             'convert-cached-html' => _("Convert cached_html"),
123             'db-check' => _("DB Check"),
124             'db-rebuild' => _("Db Rebuild")
125         );
126         return @$labels[$action];
127     }
128
129     private function _do_purge_cache(&$request, $args)
130     {
131         $dbi = $request->getDbh();
132         $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
133         while (($page = $pages->next())) {
134             $page->set('_cached_html', false);
135         }
136         return _("Markup cache purged!");
137     }
138
139     private function _do_purge_bad_pagenames(&$request, $args)
140     {
141         // FIXME: this should be moved into WikiDB::normalize() or something...
142         $dbi = $request->getDbh();
143         $count = 0;
144         $list = HTML::ol(array('class' => 'align-left'));
145         $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
146         while (($page = $pages->next())) {
147             $pagename = $page->getName();
148             $wpn = new WikiPageName($pagename);
149             if (!$wpn->isValid()) {
150                 $dbi->purgePage($pagename);
151                 $list->pushContent(HTML::li($pagename));
152                 $count++;
153             }
154         }
155         $pages->free();
156         if (!$count)
157             return _("No pages with bad names had to be deleted.");
158         else {
159             return HTML(fmt("Deleted %d pages with invalid names:", $count),
160                 HTML::div(array('class' => 'align-left'), $list));
161         }
162     }
163
164     /**
165      * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
166      *
167      * @param WikiRequest $request
168      * @param array $args
169      * @return string|XmlContent
170      */
171     private function _do_purge_empty_pages(&$request, $args)
172     {
173         $dbi = $request->getDbh();
174         $count = 0;
175         $notpurgable = 0;
176         $list = HTML::ol(array('class' => 'align-left'));
177         $pages = $dbi->getAllPages('include_empty');
178         while (($page = $pages->next())) {
179             if (!$page->exists()
180                 and ($links = $page->getBackLinks('include_empty'))
181                     and !$links->next()
182             ) {
183                 $pagename = $page->getName();
184                 if ($pagename == 'global_data' or $pagename == '.') continue;
185                 if ($dbi->purgePage($pagename))
186                     $list->pushContent(HTML::li($pagename . ' ' . _("[purged]")));
187                 else {
188                     $list->pushContent(HTML::li($pagename . ' ' . _("[not purgable]")));
189                     $notpurgable++;
190                 }
191                 $count++;
192             }
193         }
194         $pages->free();
195         if (!$count)
196             return _("No empty, unreferenced pages were found.");
197         else
198             return HTML(fmt("Deleted %d unreferenced pages:", $count),
199                 HTML::div(array('class' => 'align-left'), $list),
200                 ($notpurgable ?
201                     fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.",
202                         $notpurgable)
203                     : ''));
204     }
205
206     private function _do_convert_cached_html(&$request, $args)
207     {
208
209         require_once 'lib/upgrade.php';
210         $dbh = $request->_dbi;
211         _upgrade_db_init($dbh);
212
213         $count = _upgrade_cached_html($dbh, false);
214
215         if (!$count)
216             return _("No old _cached_html pagedata found.");
217         else {
218             return HTML(fmt("Converted successfully %d pages", $count),
219                 HTML::div(array('class' => 'align-left'), $list));
220         }
221     }
222
223     private function _do_db_check(&$request, $args)
224     {
225         longer_timeout(180);
226         $dbh = $request->getDbh();
227         //FIXME: display result.
228         return $dbh->_backend->check($args);
229     }
230
231     private function _do_db_rebuild(&$request, $args)
232     {
233         longer_timeout(240);
234         $dbh = $request->getDbh();
235         //FIXME: display result.
236         return $dbh->_backend->rebuild($args);
237     }
238
239     // pagelist with enable/disable button
240     private function _do_email_verification(&$request, &$args)
241     {
242         $dbi = $request->getDbh();
243         $pagelist = new PageList('pagename', array(), $args);
244         //$args['return_url'] = 'action=email-verification-verified';
245         $email = new _PageList_Column_email('email', _("E-mail"), 'left');
246         $emailVerified = new _PageList_Column_emailVerified('emailVerified',
247             _("Verification Status"), 'center');
248         $pagelist->_columns[0]->_heading = _("Username");
249         $pagelist->_columns[] = $email;
250         $pagelist->_columns[] = $emailVerified;
251         //This is the best method to find all users (Db and PersonalPage)
252         $current_user = $request->_user;
253         if (empty($args['verify'])) {
254             $group = $request->getGroup();
255             $allusers = $group->_allUsers();
256         } else {
257             if (!empty($args['user']))
258                 $allusers = array_keys($args['user']);
259             else
260                 $allusers = array();
261         }
262         foreach ($allusers as $username) {
263             $user = WikiUser($username);
264             $prefs = $user->getPreferences();
265             if ($prefs->get('email')) {
266                 if (!$prefs->get('userid'))
267                     $prefs->set('userid', $username);
268                 if (!empty($pagelist->_rows))
269                     $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
270                 else
271                     $group = 0;
272                 $class = ($group % 2) ? 'oddrow' : 'evenrow';
273                 $row = HTML::tr(array('class' => $class));
274                 $page_handle = $dbi->getPage($username);
275                 $row->pushContent($pagelist->_columns[0]->format($pagelist,
276                     $page_handle, $page_handle));
277                 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
278                 if (!empty($args['verify'])) {
279                     $prefs->_prefs['email']->set('emailVerified',
280                         empty($args['verified'][$username]) ? 0 : true);
281                     $user->setPreferences($prefs);
282                 }
283                 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
284                 $pagelist->_rows[] = $row;
285             }
286         }
287         $request->_user = $current_user;
288         if (!empty($args['verify']) or empty($pagelist->_rows)) {
289             return HTML($pagelist->_generateTable());
290         } elseif (!empty($pagelist->_rows)) {
291             $args['verify'] = 1;
292             $args['return_url'] = $request->getURLtoSelf();
293             return HTML::form(array('action' => $request->getPostURL(),
294                     'method' => 'post'),
295                 HiddenInputs($args, 'wikiadminutils'),
296                 HiddenInputs(array('require_authority_for_post' =>
297                 WIKIAUTH_ADMIN)),
298                 HiddenInputs($request->getArgs()),
299                 $pagelist->_generateTable(),
300                 HTML::p(Button('submit:', _("Change Verification Status"),
301                         'wikiadmin'),
302                     HTML::raw('&nbsp;'),
303                     Button('cancel', _("Cancel")))
304             );
305         }
306     return HTML::raw('');
307     }
308 }
309
310 require_once 'lib/PageList.php';
311
312 class _PageList_Column_email
313     extends _PageList_Column
314 {
315     function _getValue(&$prefs, $dummy)
316     {
317         return $prefs->get('email');
318     }
319 }
320
321 class _PageList_Column_emailVerified
322     extends _PageList_Column
323 {
324     function _getValue(&$prefs, $status)
325     {
326         $name = $prefs->get('userid');
327         $input = HTML::input(array('type' => 'checkbox',
328             'name' => 'wikiadminutils[verified][' . $name . ']',
329             'value' => 1));
330         if ($prefs->get('emailVerified'))
331             $input->setAttr('checked', '1');
332         if ($status)
333             $input->setAttr('disabled', '1');
334         return HTML($input, HTML::input
335         (array('type' => 'hidden',
336             'name' => 'wikiadminutils[user][' . $name . ']',
337             'value' => $name)));
338     }
339 }
340
341 // Local Variables:
342 // mode: php
343 // tab-width: 8
344 // c-basic-offset: 4
345 // c-hanging-comment-ender-p: nil
346 // indent-tabs-mode: nil
347 // End: