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