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