]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminUtils.php
add db-check db-rebuild for dba links
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminUtils.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminUtils.php,v 1.20 2006-09-30 10:08:38 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.20 $");
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         return @$labels[$action];
114     }
115
116     function _do_purge_cache(&$request, $args) {
117         $dbi = $request->getDbh();
118         $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
119         while (($page = $pages->next())) {
120             $page->set('_cached_html', false);
121         }
122         return _("Markup cache purged!");
123     }
124
125     function _do_purge_bad_pagenames(&$request, $args) {
126         // FIXME: this should be moved into WikiDB::normalize() or something...
127         $dbi = $request->getDbh();
128         $count = 0;
129         $list = HTML::ol(array('align'=>'left'));
130         $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
131         while (($page = $pages->next())) {
132             $pagename = $page->getName();
133             $wpn = new WikiPageName($pagename);
134             if (! $wpn->isValid() ) {
135                 $dbi->purgePage($pagename);
136                 $list->pushContent(HTML::li($pagename));
137                 $count++;
138             }
139         }
140         $pages->free();
141         if (!$count)
142             return _("No pages with bad names had to be deleted.");
143         else {
144             return HTML(fmt("Deleted %s pages with invalid names:", $count),
145                         HTML::div(array('align'=>'left'), $list));
146         }
147     }
148
149     /** 
150      * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
151      */
152     function _do_purge_empty_pages(&$request, $args) {
153         $dbi = $request->getDbh();
154         $count = 0; $notpurgable = 0;
155         $list = HTML::ol(array('align'=>'left'));
156         $pages = $dbi->getAllPages('include_empty');
157         while (($page = $pages->next())) {
158             if (!$page->exists() 
159                 and ($links = $page->getBackLinks('include_empty')) 
160                      and !$links->next())
161             {
162                 $pagename = $page->getName();
163                 if ($pagename == 'global_data' or $pagename == '.') continue;
164                 if ($dbi->purgePage($pagename))
165                     $list->pushContent(HTML::li($pagename.' '._("[purged]")));
166                 else {
167                     $list->pushContent(HTML::li($pagename.' '._("[not purgable]")));
168                     $notpurgable++;
169                 }
170                 $count++;
171             }
172         }
173         $pages->free();
174         if (!$count)
175             return _("No empty, unreferenced pages were found.");
176         else
177             return HTML(fmt("Deleted %s unreferenced pages:", $count),
178                         HTML::div(array('align'=>'left'), $list),
179                         ($notpurgable ? 
180         fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.", 
181             $notpurgable)
182                                       : ''));
183     }
184
185
186     function _do_convert_cached_html(&$request, $args) {
187
188         require_once("lib/upgrade.php");
189         $dbh = $request->_dbi;
190         _upgrade_db_init($dbh);
191
192         $count = _upgrade_cached_html($dbh, false);
193
194         if (!$count)
195             return _("No old _cached_html pagedata found.");
196         else {
197             return HTML(fmt("Converted successfully %d pages", $count),
198                         HTML::div(array('align'=>'left'), $list));
199         }
200     }
201
202     function _do_db_check(&$request, $args) {
203         $dbh = $request->_dbi;
204         //FIXME: display result.
205         $result = $dbh->_backend->check();
206         return $result;
207     }
208
209     function _do_db_rebuild(&$request, $args) {
210         $dbh = $request->_dbi;
211         //FIXME: display result.
212         $result = $dbh->_backend->rebuild();
213         return $result;
214     }
215
216     //TODO: We need a seperate plugin for this.
217     //      Too many options.
218     function _do_access_restrictions(&$request, &$args) {
219         return _("Sorry. Access Restrictions not yet implemented");
220     }
221     
222     // pagelist with enable/disable button
223     function _do_email_verification(&$request, &$args) {
224         $dbi = $request->getDbh();
225         $pagelist = new PageList('pagename',0,$args);
226         //$args['return_url'] = 'action=email-verification-verified';
227         $email = new _PageList_Column_email('email',_("E-Mail"),'left');
228         $emailVerified = new _PageList_Column_emailVerified('emailVerified',
229                                                             _("Verification Status"),'center');
230         $pagelist->_columns[] = $email;
231         $pagelist->_columns[] = $emailVerified;
232         //This is the best method to find all users (Db and PersonalPage)
233         $current_user = $request->_user;
234         if (empty($args['verify'])) {
235             $group = $request->getGroup();
236             $allusers = $group->_allUsers();
237         } else {
238             $allusers = array_keys($args['user']);
239         }
240         foreach ($allusers as $username) {
241             if (ENABLE_USER_NEW)
242                 $user = WikiUser($username);
243             else 
244                 $user = new WikiUser($request, $username);
245             $prefs = $user->getPreferences();
246             if ($prefs->get('email')) {
247                 if (!$prefs->get('userid'))
248                     $prefs->set('userid',$username);
249                 if (!empty($pagelist->_rows))
250                     $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
251                 else 
252                     $group = 0;
253                 $class = ($group % 2) ? 'oddrow' : 'evenrow';
254                 $row = HTML::tr(array('class' => $class));
255                 $page_handle = $dbi->getPage($username);
256                 $row->pushContent($pagelist->_columns[0]->format($pagelist, 
257                                                                  $page_handle, $page_handle));
258                 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
259                 if (!empty($args['verify'])) {
260                     $prefs->_prefs['email']->set('emailVerified', 
261                                                  empty($args['verified'][$username]) ? 0 : 2);
262                     $user->setPreferences($prefs);
263                 }
264                 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
265                 $pagelist->_rows[] = $row;
266             }
267         }
268         $request->_user = $current_user;
269         if (!empty($args['verify'])) {
270             return HTML($pagelist->_generateTable(false));
271         } else {
272             $args['verify'] = 1;
273             $args['return_url'] = $request->getURLtoSelf();
274             return HTML::form(array('action' => $request->getPostURL(),
275                                     'method' => 'post'),
276                           HiddenInputs($args, 'wikiadminutils'),
277                           HiddenInputs(array('require_authority_for_post' =>
278                                              WIKIAUTH_ADMIN)),
279                           HiddenInputs($request->getArgs()),
280                           $pagelist->_generateTable(false),                   
281                           HTML::p(Button('submit:', _("Change Verification Status"), 
282                                          'wikiadmin'),
283                                   HTML::Raw('&nbsp;'),
284                                   Button('cancel', _("Cancel")))
285                                  );
286         }
287     }
288 };
289
290 require_once("lib/PageList.php");
291
292 class _PageList_Column_email 
293 extends _PageList_Column {
294     function _getValue (&$prefs, $dummy) {
295         return $prefs->get('email');
296     }
297 }
298
299 class _PageList_Column_emailVerified
300 extends _PageList_Column {
301     function _getValue (&$prefs, $status) {
302         $name = $prefs->get('userid');
303         $input = HTML::input(array('type' => 'checkbox',
304                                    'name' => 'wikiadminutils[verified]['.$name.']',
305                                    'value' => 1));
306         if ($prefs->get('emailVerified'))
307             $input->setAttr('checked','1');
308         if ($status)
309             $input->setAttr('disabled','1');
310         return HTML($input, HTML::input
311                     (array('type' => 'hidden',
312                            'name' => 'wikiadminutils[user]['.$name.']',
313                            'value' => $name)));
314     }
315 }
316
317
318 // For emacs users
319 // Local Variables:
320 // mode: php
321 // tab-width: 8
322 // c-basic-offset: 4
323 // c-hanging-comment-ender-p: nil
324 // indent-tabs-mode: nil
325 // End:
326 ?>