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