]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminUtils.php
Update PHP Doc; type compatibility
[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     private function _do_purge_empty_pages(&$request, $args)
168     {
169         $dbi = $request->getDbh();
170         $count = 0;
171         $notpurgable = 0;
172         $list = HTML::ol(array('class' => 'align-left'));
173         $pages = $dbi->getAllPages('include_empty');
174         while (($page = $pages->next())) {
175             if (!$page->exists()
176                 and ($links = $page->getBackLinks('include_empty'))
177                     and !$links->next()
178             ) {
179                 $pagename = $page->getName();
180                 if ($pagename == 'global_data' or $pagename == '.') continue;
181                 if ($dbi->purgePage($pagename))
182                     $list->pushContent(HTML::li($pagename . ' ' . _("[purged]")));
183                 else {
184                     $list->pushContent(HTML::li($pagename . ' ' . _("[not purgable]")));
185                     $notpurgable++;
186                 }
187                 $count++;
188             }
189         }
190         $pages->free();
191         if (!$count)
192             return _("No empty, unreferenced pages were found.");
193         else
194             return HTML(fmt("Deleted %d unreferenced pages:", $count),
195                 HTML::div(array('class' => 'align-left'), $list),
196                 ($notpurgable ?
197                     fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.",
198                         $notpurgable)
199                     : ''));
200     }
201
202     private function _do_convert_cached_html(&$request, $args)
203     {
204
205         require_once 'lib/upgrade.php';
206         $dbh = $request->_dbi;
207         _upgrade_db_init($dbh);
208
209         $count = _upgrade_cached_html($dbh, false);
210
211         if (!$count)
212             return _("No old _cached_html pagedata found.");
213         else {
214             return HTML(fmt("Converted successfully %d pages", $count),
215                 HTML::div(array('class' => 'align-left'), $list));
216         }
217     }
218
219     private function _do_db_check(&$request, $args)
220     {
221         longer_timeout(180);
222         $dbh = $request->getDbh();
223         //FIXME: display result.
224         return $dbh->_backend->check($args);
225     }
226
227     private function _do_db_rebuild(&$request, $args)
228     {
229         longer_timeout(240);
230         $dbh = $request->getDbh();
231         //FIXME: display result.
232         return $dbh->_backend->rebuild($args);
233     }
234
235     // pagelist with enable/disable button
236     private function _do_email_verification(&$request, &$args)
237     {
238         $dbi = $request->getDbh();
239         $pagelist = new PageList('pagename', array(), $args);
240         //$args['return_url'] = 'action=email-verification-verified';
241         $email = new _PageList_Column_email('email', _("E-mail"), 'left');
242         $emailVerified = new _PageList_Column_emailVerified('emailVerified',
243             _("Verification Status"), 'center');
244         $pagelist->_columns[0]->_heading = _("Username");
245         $pagelist->_columns[] = $email;
246         $pagelist->_columns[] = $emailVerified;
247         //This is the best method to find all users (Db and PersonalPage)
248         $current_user = $request->_user;
249         if (empty($args['verify'])) {
250             $group = $request->getGroup();
251             $allusers = $group->_allUsers();
252         } else {
253             if (!empty($args['user']))
254                 $allusers = array_keys($args['user']);
255             else
256                 $allusers = array();
257         }
258         foreach ($allusers as $username) {
259             $user = WikiUser($username);
260             $prefs = $user->getPreferences();
261             if ($prefs->get('email')) {
262                 if (!$prefs->get('userid'))
263                     $prefs->set('userid', $username);
264                 if (!empty($pagelist->_rows))
265                     $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
266                 else
267                     $group = 0;
268                 $class = ($group % 2) ? 'oddrow' : 'evenrow';
269                 $row = HTML::tr(array('class' => $class));
270                 $page_handle = $dbi->getPage($username);
271                 $row->pushContent($pagelist->_columns[0]->format($pagelist,
272                     $page_handle, $page_handle));
273                 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
274                 if (!empty($args['verify'])) {
275                     $prefs->_prefs['email']->set('emailVerified',
276                         empty($args['verified'][$username]) ? 0 : true);
277                     $user->setPreferences($prefs);
278                 }
279                 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
280                 $pagelist->_rows[] = $row;
281             }
282         }
283         $request->_user = $current_user;
284         if (!empty($args['verify']) or empty($pagelist->_rows)) {
285             return HTML($pagelist->_generateTable(false));
286         } elseif (!empty($pagelist->_rows)) {
287             $args['verify'] = 1;
288             $args['return_url'] = $request->getURLtoSelf();
289             return HTML::form(array('action' => $request->getPostURL(),
290                     'method' => 'post'),
291                 HiddenInputs($args, 'wikiadminutils'),
292                 HiddenInputs(array('require_authority_for_post' =>
293                 WIKIAUTH_ADMIN)),
294                 HiddenInputs($request->getArgs()),
295                 $pagelist->_generateTable(false),
296                 HTML::p(Button('submit:', _("Change Verification Status"),
297                         'wikiadmin'),
298                     HTML::raw('&nbsp;'),
299                     Button('cancel', _("Cancel")))
300             );
301         }
302     return HTML::raw('');
303     }
304 }
305
306 require_once 'lib/PageList.php';
307
308 class _PageList_Column_email
309     extends _PageList_Column
310 {
311     function _getValue(&$prefs, $dummy)
312     {
313         return $prefs->get('email');
314     }
315 }
316
317 class _PageList_Column_emailVerified
318     extends _PageList_Column
319 {
320     function _getValue(&$prefs, $status)
321     {
322         $name = $prefs->get('userid');
323         $input = HTML::input(array('type' => 'checkbox',
324             'name' => 'wikiadminutils[verified][' . $name . ']',
325             'value' => 1));
326         if ($prefs->get('emailVerified'))
327             $input->setAttr('checked', '1');
328         if ($status)
329             $input->setAttr('disabled', '1');
330         return HTML($input, HTML::input
331         (array('type' => 'hidden',
332             'name' => 'wikiadminutils[user][' . $name . ']',
333             'value' => $name)));
334     }
335 }
336
337 // Local Variables:
338 // mode: php
339 // tab-width: 8
340 // c-basic-offset: 4
341 // c-hanging-comment-ender-p: nil
342 // indent-tabs-mode: nil
343 // End: