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