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