]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminUtils.php
Remove ENABLE_USER_NEW (always true), remove lib/WikiUser.php
[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     protected 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('class' => '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('class' => '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('class' => '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('class' => '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('class' => '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             $user = WikiUser($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);
259                 else
260                     $group = 0;
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);
271                 }
272                 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
273                 $pagelist->_rows[] = $row;
274             }
275         }
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)) {
280             $args['verify'] = 1;
281             $args['return_url'] = $request->getURLtoSelf();
282             return HTML::form(array('action' => $request->getPostURL(),
283                     'method' => 'post'),
284                 HiddenInputs($args, 'wikiadminutils'),
285                 HiddenInputs(array('require_authority_for_post' =>
286                 WIKIAUTH_ADMIN)),
287                 HiddenInputs($request->getArgs()),
288                 $pagelist->_generateTable(false),
289                 HTML::p(Button('submit:', _("Change Verification Status"),
290                         'wikiadmin'),
291                     HTML::raw('&nbsp;'),
292                     Button('cancel', _("Cancel")))
293             );
294         }
295     return HTML::raw('');
296     }
297 }
298
299 require_once 'lib/PageList.php';
300
301 class _PageList_Column_email
302     extends _PageList_Column
303 {
304     function _getValue(&$prefs, $dummy)
305     {
306         return $prefs->get('email');
307     }
308 }
309
310 class _PageList_Column_emailVerified
311     extends _PageList_Column
312 {
313     function _getValue(&$prefs, $status)
314     {
315         $name = $prefs->get('userid');
316         $input = HTML::input(array('type' => 'checkbox',
317             'name' => 'wikiadminutils[verified][' . $name . ']',
318             'value' => 1));
319         if ($prefs->get('emailVerified'))
320             $input->setAttr('checked', '1');
321         if ($status)
322             $input->setAttr('disabled', '1');
323         return HTML($input, HTML::input
324         (array('type' => 'hidden',
325             'name' => 'wikiadminutils[user][' . $name . ']',
326             'value' => $name)));
327     }
328 }
329
330 // Local Variables:
331 // mode: php
332 // tab-width: 8
333 // c-basic-offset: 4
334 // c-hanging-comment-ender-p: nil
335 // indent-tabs-mode: nil
336 // End: