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