]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminUtils.php
support new check and rebuild args
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminUtils.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminUtils.php,v 1.25 2007-08-25 18:55:49 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.25 $");
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(),false,array('action')));
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 $method");
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(_("Back") => $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         longer_timeout(180);
210         $dbh = $request->getDbh();
211         //FIXME: display result.
212         $result = $dbh->_backend->check($args);
213         return $result;
214     }
215
216     function _do_db_rebuild(&$request, $args) {
217         longer_timeout(240);
218         $dbh = $request->getDbh();
219         //FIXME: display result.
220         $result = $dbh->_backend->rebuild($args);
221         return $result;
222     }
223
224     //TODO: We need a seperate plugin for this.
225     //      Too many options.
226     function _do_access_restrictions(&$request, &$args) {
227         return _("Sorry. Access Restrictions not yet implemented");
228     }
229     
230     // pagelist with enable/disable button
231     function _do_email_verification(&$request, &$args) {
232         $dbi = $request->getDbh();
233         $pagelist = new PageList('pagename',0,$args);
234         //$args['return_url'] = 'action=email-verification-verified';
235         $email = new _PageList_Column_email('email',_("E-Mail"),'left');
236         $emailVerified = new _PageList_Column_emailVerified('emailVerified',
237                                                             _("Verification Status"),'center');
238         $pagelist->_columns[0]->_heading = _("Username");                                                    
239         $pagelist->_columns[] = $email;
240         $pagelist->_columns[] = $emailVerified;
241         //This is the best method to find all users (Db and PersonalPage)
242         $current_user = $request->_user;
243         if (empty($args['verify'])) {
244             $group = $request->getGroup();
245             $allusers = $group->_allUsers();
246         } else {
247             if (!empty($args['user']))
248                 $allusers = array_keys($args['user']);
249             else 
250                 $allusers = array();
251         }
252         foreach ($allusers as $username) {
253             if (ENABLE_USER_NEW)
254                 $user = WikiUser($username);
255             else 
256                 $user = new WikiUser($request, $username);
257             $prefs = $user->getPreferences();
258             if ($prefs->get('email')) {
259                 if (!$prefs->get('userid'))
260                     $prefs->set('userid',$username);
261                 if (!empty($pagelist->_rows))
262                     $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
263                 else 
264                     $group = 0;
265                 $class = ($group % 2) ? 'oddrow' : 'evenrow';
266                 $row = HTML::tr(array('class' => $class));
267                 $page_handle = $dbi->getPage($username);
268                 $row->pushContent($pagelist->_columns[0]->format($pagelist, 
269                                                                  $page_handle, $page_handle));
270                 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
271                 if (!empty($args['verify'])) {
272                     $prefs->_prefs['email']->set('emailVerified', 
273                                                  empty($args['verified'][$username]) ? 0 : true);
274                     $user->setPreferences($prefs);
275                 }
276                 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
277                 $pagelist->_rows[] = $row;
278             }
279         }
280         $request->_user = $current_user;
281         if (!empty($args['verify']) or empty($pagelist->_rows)) {
282             return HTML($pagelist->_generateTable(false));
283         } elseif (!empty($pagelist->_rows)) {
284             $args['verify'] = 1;
285             $args['return_url'] = $request->getURLtoSelf();
286             return HTML::form(array('action' => $request->getPostURL(),
287                                     'method' => 'post'),
288                           HiddenInputs($args, 'wikiadminutils'),
289                           HiddenInputs(array('require_authority_for_post' =>
290                                              WIKIAUTH_ADMIN)),
291                           HiddenInputs($request->getArgs()),
292                           $pagelist->_generateTable(false),                   
293                           HTML::p(Button('submit:', _("Change Verification Status"), 
294                                          'wikiadmin'),
295                                   HTML::Raw('&nbsp;'),
296                                   Button('cancel', _("Cancel")))
297                                  );
298         }
299     }
300 };
301
302 require_once("lib/PageList.php");
303
304 class _PageList_Column_email 
305 extends _PageList_Column {
306     function _getValue (&$prefs, $dummy) {
307         return $prefs->get('email');
308     }
309 }
310
311 class _PageList_Column_emailVerified
312 extends _PageList_Column {
313     function _getValue (&$prefs, $status) {
314         $name = $prefs->get('userid');
315         $input = HTML::input(array('type' => 'checkbox',
316                                    'name' => 'wikiadminutils[verified]['.$name.']',
317                                    'value' => 1));
318         if ($prefs->get('emailVerified'))
319             $input->setAttr('checked','1');
320         if ($status)
321             $input->setAttr('disabled','1');
322         return HTML($input, HTML::input
323                     (array('type' => 'hidden',
324                            'name' => 'wikiadminutils[user]['.$name.']',
325                            'value' => $name)));
326     }
327 }
328
329
330 // For emacs users
331 // Local Variables:
332 // mode: php
333 // tab-width: 8
334 // c-basic-offset: 4
335 // c-hanging-comment-ender-p: nil
336 // indent-tabs-mode: nil
337 // End:
338 ?>