]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminUtils.php
Normalize header
[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 getVersion() {
47         return preg_replace("/[Revision: $]/", '',
48                             "\$Revision$");
49     }
50
51     function getDefaultArguments() {
52         return array('action'           => '',
53                      'label'            => '',
54                      );
55     }
56
57     function run($dbi, $argstr, &$request, $basepage) {
58         $args = $this->getArgs($argstr, $request);
59         $args['action'] = strtolower($args['action']);
60         extract($args);
61         
62         if (!$action)
63             $this->error("No action specified");
64         if (!($default_label = $this->_getLabel($action))) {
65             return HTML::div(array('class' => "error"), fmt("Bad action requested: %s", $action));
66         }
67         if ($request->getArg('action') != 'browse')
68             return $this->disabled("(action != 'browse')");
69         
70         $posted = $request->getArg('wikiadminutils');
71
72         if ($request->isPost() and $posted['action'] == $action) { // a different form. we might have multiple
73             $user = $request->getUser();
74             if (!$user->isAdmin()) {
75                 $request->_notAuthorized(WIKIAUTH_ADMIN);
76                 return $this->error(_("You must be an administrator to use this plugin."));
77             }
78             return $this->do_action($request, $posted);
79         }
80         if (empty($label))
81             $label = $default_label;
82
83         return $this->_makeButton($request, $args, $label);
84     }
85
86     function _makeButton(&$request, $args, $label) {
87         $args['return_url'] = $request->getURLtoSelf();
88         return HTML::form(array('action' => $request->getPostURL(),
89                                 'method' => 'post'),
90                           HTML::p(Button('submit:', $label, 'wikiadmin')),
91                           HiddenInputs($args, 'wikiadminutils'),
92                           HiddenInputs(array('require_authority_for_post' =>
93                                              WIKIAUTH_ADMIN)),
94                           HiddenInputs($request->getArgs(),false,array('action')));
95     }
96     
97     function do_action(&$request, $args) {
98         $method = strtolower('_do_' . str_replace('-', '_', $args['action']));
99         if (!method_exists($this, $method))
100             return $this->error("Bad action $method");
101
102         $message = call_user_func(array(&$this, $method), $request, $args);
103
104         // display as seperate page or as alert?
105         $alert = new Alert(fmt("WikiAdminUtils %s returned:", $args['action']),
106                            $message,
107                            array(_("Back") => $args['return_url']));
108         $alert->show();         // noreturn
109     }
110
111     function _getLabel($action) {
112         $labels = array('purge-cache'           => _("Purge Markup Cache"),
113                         'purge-bad-pagenames'   => _("Purge all Pages With Invalid Names"),
114                         'purge-empty-pages'     => _("Purge all empty, unreferenced Pages"),
115                         'access-restrictions'   => _("Access Restrictions"),
116                         'email-verification'    => _("Email Verification"),
117                         'convert-cached-html'   => _("Convert cached_html"),
118                         'db-check'              => _("DB Check"),
119                         'db-rebuild'            => _("Db Rebuild")
120                         );
121         return @$labels[$action];
122     }
123
124     function _do_purge_cache(&$request, $args) {
125         $dbi = $request->getDbh();
126         $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
127         while (($page = $pages->next())) {
128             $page->set('_cached_html', false);
129         }
130         return _("Markup cache purged!");
131     }
132
133     function _do_purge_bad_pagenames(&$request, $args) {
134         // FIXME: this should be moved into WikiDB::normalize() or something...
135         $dbi = $request->getDbh();
136         $count = 0;
137         $list = HTML::ol(array('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 %s pages with invalid names:", $count),
153                         HTML::div(array('align'=>'left'), $list));
154         }
155     }
156
157     /** 
158      * Purge all non-referenced empty pages. Mainly those created by bad link extraction.
159      */
160     function _do_purge_empty_pages(&$request, $args) {
161         $dbi = $request->getDbh();
162         $count = 0; $notpurgable = 0;
163         $list = HTML::ol(array('align'=>'left'));
164         $pages = $dbi->getAllPages('include_empty');
165         while (($page = $pages->next())) {
166             if (!$page->exists() 
167                 and ($links = $page->getBackLinks('include_empty')) 
168                      and !$links->next())
169             {
170                 $pagename = $page->getName();
171                 if ($pagename == 'global_data' or $pagename == '.') continue;
172                 if ($dbi->purgePage($pagename))
173                     $list->pushContent(HTML::li($pagename.' '._("[purged]")));
174                 else {
175                     $list->pushContent(HTML::li($pagename.' '._("[not purgable]")));
176                     $notpurgable++;
177                 }
178                 $count++;
179             }
180         }
181         $pages->free();
182         if (!$count)
183             return _("No empty, unreferenced pages were found.");
184         else
185             return HTML(fmt("Deleted %s unreferenced pages:", $count),
186                         HTML::div(array('align'=>'left'), $list),
187                         ($notpurgable ? 
188         fmt("The %d not-purgable pages/links are links in some page(s). You might want to edit them.", 
189             $notpurgable)
190                                       : ''));
191     }
192
193
194     function _do_convert_cached_html(&$request, $args) {
195
196         require_once("lib/upgrade.php");
197         $dbh = $request->_dbi;
198         _upgrade_db_init($dbh);
199
200         $count = _upgrade_cached_html($dbh, false);
201
202         if (!$count)
203             return _("No old _cached_html pagedata found.");
204         else {
205             return HTML(fmt("Converted successfully %d pages", $count),
206                         HTML::div(array('align'=>'left'), $list));
207         }
208     }
209
210     function _do_db_check(&$request, $args) {
211         longer_timeout(180);
212         $dbh = $request->getDbh();
213         //FIXME: display result.
214         return $dbh->_backend->check($args);
215     }
216
217     function _do_db_rebuild(&$request, $args) {
218         longer_timeout(240);
219         $dbh = $request->getDbh();
220         //FIXME: display result.
221         return $dbh->_backend->rebuild($args);
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 ?>