]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminUtils.php
Minor improvements
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminUtils.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminUtils.php,v 1.22 2006-12-22 17:57:41 rurban Exp $');
3 /**
4  Copyright 2003,2004,2006 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24   valid actions: 
25         purge-cache
26         purge-bad-pagenames
27         purge-empty-pages
28         access-restrictions
29         email-verification
30         convert-cached-html
31         db-check
32         db-rebuild
33  */
34 class WikiPlugin_WikiAdminUtils
35 extends WikiPlugin
36 {
37     function getName () {
38         return _("WikiAdminUtils");
39     }
40
41     function getDescription () {
42         return _("Miscellaneous utility functions for the Administrator.");
43     }
44
45     function getVersion() {
46         return preg_replace("/[Revision: $]/", '',
47                             "\$Revision: 1.22 $");
48     }
49
50     function getDefaultArguments() {
51         return array('action'           => '',
52                      'label'            => '',
53                      );
54     }
55
56     function run($dbi, $argstr, &$request, $basepage) {
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         if (!($default_label = $this->_getLabel($action)))
64             $this->error("Bad action");
65         if ($request->getArg('action') != 'browse')
66             return $this->disabled("(action != 'browse')");
67         
68         $posted = $request->getArg('wikiadminutils');
69
70         if ($request->isPost() and $posted['action'] == $action) { // a different form. we might have multiple
71             $user = $request->getUser();
72             if (!$user->isAdmin()) {
73                 $request->_notAuthorized(WIKIAUTH_ADMIN);
74                 return $this->error(_("You must be an administrator to use this plugin."));
75             }
76             return $this->do_action($request, $posted);
77         }
78         if (empty($label))
79             $label = $default_label;
80
81         return $this->_makeButton($request, $args, $label);
82     }
83
84     function _makeButton(&$request, $args, $label) {
85         $args['return_url'] = $request->getURLtoSelf();
86         return HTML::form(array('action' => $request->getPostURL(),
87                                 'method' => 'post'),
88                           HTML::p(Button('submit:', $label, 'wikiadmin')),
89                           HiddenInputs($args, 'wikiadminutils'),
90                           HiddenInputs(array('require_authority_for_post' =>
91                                              WIKIAUTH_ADMIN)),
92                           HiddenInputs($request->getArgs()));
93     }
94     
95     function do_action(&$request, $args) {
96         $method = strtolower('_do_' . str_replace('-', '_', $args['action']));
97         if (!method_exists($this, $method))
98             return $this->error("Bad action");
99
100         $message = call_user_func(array(&$this, $method), $request, $args);
101
102         // display as seperate page or as alert?
103         $alert = new Alert(fmt("WikiAdminUtils %s returned:", $args['action']),
104                            $message,
105                            array(_("Okay") => $args['return_url']));
106         $alert->show();         // noreturn
107     }
108
109     function _getLabel($action) {
110         $labels = array('purge-cache'           => _("Purge Markup Cache"),
111                         'purge-bad-pagenames'   => _("Purge all Pages With Invalid Names"),
112                         'purge-empty-pages'     => _("Purge all empty, unreferenced Pages"),
113                         'access-restrictions'   => _("Access Restrictions"),
114                         'email-verification'    => _("Email Verification"),
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     function _do_purge_cache(&$request, $args) {
123         $dbi = $request->getDbh();
124         $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
125         while (($page = $pages->next())) {
126             $page->set('_cached_html', false);
127         }
128         return _("Markup cache purged!");
129     }
130
131     function _do_purge_bad_pagenames(&$request, $args) {
132         // FIXME: this should be moved into WikiDB::normalize() or something...
133         $dbi = $request->getDbh();
134         $count = 0;
135         $list = HTML::ol(array('align'=>'left'));
136         $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
137         while (($page = $pages->next())) {
138             $pagename = $page->getName();
139             $wpn = new WikiPageName($pagename);
140             if (! $wpn->isValid() ) {
141                 $dbi->purgePage($pagename);
142                 $list->pushContent(HTML::li($pagename));
143                 $count++;
144             }
145         }
146         $pages->free();
147         if (!$count)
148             return _("No pages with bad names had to be deleted.");
149         else {
150             return HTML(fmt("Deleted %s pages with invalid names:", $count),
151                         HTML::div(array('align'=>'left'), $list));
152         }
153     }
154
155     /** 
156      * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
157      */
158     function _do_purge_empty_pages(&$request, $args) {
159         $dbi = $request->getDbh();
160         $count = 0; $notpurgable = 0;
161         $list = HTML::ol(array('align'=>'left'));
162         $pages = $dbi->getAllPages('include_empty');
163         while (($page = $pages->next())) {
164             if (!$page->exists() 
165                 and ($links = $page->getBackLinks('include_empty')) 
166                      and !$links->next())
167             {
168                 $pagename = $page->getName();
169                 if ($pagename == 'global_data' or $pagename == '.') continue;
170                 if ($dbi->purgePage($pagename))
171                     $list->pushContent(HTML::li($pagename.' '._("[purged]")));
172                 else {
173                     $list->pushContent(HTML::li($pagename.' '._("[not purgable]")));
174                     $notpurgable++;
175                 }
176                 $count++;
177             }
178         }
179         $pages->free();
180         if (!$count)
181             return _("No empty, unreferenced pages were found.");
182         else
183             return HTML(fmt("Deleted %s unreferenced pages:", $count),
184                         HTML::div(array('align'=>'left'), $list),
185                         ($notpurgable ? 
186         fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.", 
187             $notpurgable)
188                                       : ''));
189     }
190
191
192     function _do_convert_cached_html(&$request, $args) {
193
194         require_once("lib/upgrade.php");
195         $dbh = $request->_dbi;
196         _upgrade_db_init($dbh);
197
198         $count = _upgrade_cached_html($dbh, false);
199
200         if (!$count)
201             return _("No old _cached_html pagedata found.");
202         else {
203             return HTML(fmt("Converted successfully %d pages", $count),
204                         HTML::div(array('align'=>'left'), $list));
205         }
206     }
207
208     function _do_db_check(&$request, $args) {
209         $dbh = $request->_dbi;
210         //FIXME: display result.
211         $result = $dbh->_backend->check();
212         return $result;
213     }
214
215     function _do_db_rebuild(&$request, $args) {
216         $dbh = $request->_dbi;
217         //FIXME: display result.
218         $result = $dbh->_backend->rebuild();
219         return $result;
220     }
221
222     //TODO: We need a seperate plugin for this.
223     //      Too many options.
224     function _do_access_restrictions(&$request, &$args) {
225         return _("Sorry. Access Restrictions not yet implemented");
226     }
227     
228     // pagelist with enable/disable button
229     function _do_email_verification(&$request, &$args) {
230         $dbi = $request->getDbh();
231         $pagelist = new PageList('pagename',0,$args);
232         //$args['return_url'] = 'action=email-verification-verified';
233         $email = new _PageList_Column_email('email',_("E-Mail"),'left');
234         $emailVerified = new _PageList_Column_emailVerified('emailVerified',
235                                                             _("Verification Status"),'center');
236         $pagelist->_columns[] = $email;
237         $pagelist->_columns[] = $emailVerified;
238         //This is the best method to find all users (Db and PersonalPage)
239         $current_user = $request->_user;
240         if (empty($args['verify'])) {
241             $group = $request->getGroup();
242             $allusers = $group->_allUsers();
243         } else {
244             if (!empty($args['user']))
245                 $allusers = array_keys($args['user']);
246             else 
247                 $allusers = array();
248         }
249         foreach ($allusers as $username) {
250             if (ENABLE_USER_NEW)
251                 $user = WikiUser($username);
252             else 
253                 $user = new WikiUser($request, $username);
254             $prefs = $user->getPreferences();
255             if ($prefs->get('email')) {
256                 if (!$prefs->get('userid'))
257                     $prefs->set('userid',$username);
258                 if (!empty($pagelist->_rows))
259                     $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
260                 else 
261                     $group = 0;
262                 $class = ($group % 2) ? 'oddrow' : 'evenrow';
263                 $row = HTML::tr(array('class' => $class));
264                 $page_handle = $dbi->getPage($username);
265                 $row->pushContent($pagelist->_columns[0]->format($pagelist, 
266                                                                  $page_handle, $page_handle));
267                 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
268                 if (!empty($args['verify'])) {
269                     $prefs->_prefs['email']->set('emailVerified', 
270                                                  empty($args['verified'][$username]) ? 0 : 2);
271                     $user->setPreferences($prefs);
272                 }
273                 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
274                 $pagelist->_rows[] = $row;
275             }
276         }
277         $request->_user = $current_user;
278         if (!empty($args['verify']) or empty($pagelist->_rows)) {
279             return HTML($pagelist->_generateTable(false));
280         } elseif (!empty($pagelist->_rows)) {
281             $args['verify'] = 1;
282             $args['return_url'] = $request->getURLtoSelf();
283             return HTML::form(array('action' => $request->getPostURL(),
284                                     'method' => 'post'),
285                           HiddenInputs($args, 'wikiadminutils'),
286                           HiddenInputs(array('require_authority_for_post' =>
287                                              WIKIAUTH_ADMIN)),
288                           HiddenInputs($request->getArgs()),
289                           $pagelist->_generateTable(false),                   
290                           HTML::p(Button('submit:', _("Change Verification Status"), 
291                                          'wikiadmin'),
292                                   HTML::Raw('&nbsp;'),
293                                   Button('cancel', _("Cancel")))
294                                  );
295         }
296     }
297 };
298
299 require_once("lib/PageList.php");
300
301 class _PageList_Column_email 
302 extends _PageList_Column {
303     function _getValue (&$prefs, $dummy) {
304         return $prefs->get('email');
305     }
306 }
307
308 class _PageList_Column_emailVerified
309 extends _PageList_Column {
310     function _getValue (&$prefs, $status) {
311         $name = $prefs->get('userid');
312         $input = HTML::input(array('type' => 'checkbox',
313                                    'name' => 'wikiadminutils[verified]['.$name.']',
314                                    'value' => 1));
315         if ($prefs->get('emailVerified'))
316             $input->setAttr('checked','1');
317         if ($status)
318             $input->setAttr('disabled','1');
319         return HTML($input, HTML::input
320                     (array('type' => 'hidden',
321                            'name' => 'wikiadminutils[user]['.$name.']',
322                            'value' => $name)));
323     }
324 }
325
326
327 // For emacs users
328 // Local Variables:
329 // mode: php
330 // tab-width: 8
331 // c-basic-offset: 4
332 // c-hanging-comment-ender-p: nil
333 // indent-tabs-mode: nil
334 // End:
335 ?>