]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminUtils.php
add WikiAdminUtils method for convert-cached-html
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminUtils.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminUtils.php,v 1.15 2004-12-10 22:33: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 WIKIADMIN.");
41     }
42
43     function getVersion() {
44         return preg_replace("/[Revision: $]/", '',
45                             "\$Revision: 1.15 $");
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 were found.");
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         _upgrade_db_init($dbh);
185
186         $database = $dbh->_backend->database();
187         extract($dbh->_backend->_table_names);
188         $fields = $dbh->_backend->listOfFields($database, $page_tbl);
189         if (!strstr(strtolower(join(':', $fields)), "cached_html")) {
190             $backend_type = $dbh->_backend->backendType();
191             if (substr($backend_type,0,5) == 'mysql')
192                 $dbh->genericSqlQuery("ALTER TABLE $page_tbl ADD cached_html MEDIUMBLOB");
193             else
194                 $dbh->genericSqlQuery("ALTER TABLE $page_tbl ADD cached_html BLOB");
195         }
196
197         $count = _convert_cached_html($dbh);
198
199         if (!$count)
200             return _("No old _cached_html pagedata found.");
201         else {
202             return HTML(fmt("Converted successfully %d pages", $count),
203                         HTML::div(array('align'=>'left'), $list));
204         }
205     }
206
207
208     //TODO: We need a seperate plugin for this.
209     //      Too many options.
210     function _do_access_restrictions(&$request, &$args) {
211         return _("Sorry. Access Restrictions not yet implemented");
212     }
213     
214     // pagelist with enable/disable button
215     function _do_email_verification(&$request, &$args) {
216         $dbi = $request->getDbh();
217         $pagelist = new PageList('pagename',0,$args);
218         //$args['return_url'] = 'action=email-verification-verified';
219         $email = new _PageList_Column_email('email',_("E-Mail"),'left');
220         $emailVerified = new _PageList_Column_emailVerified('emailVerified',
221                                                             _("Verification Status"),'center');
222         $pagelist->_columns[] = $email;
223         $pagelist->_columns[] = $emailVerified;
224         //This is the best method to find all users (Db and PersonalPage)
225         $current_user = $request->_user;
226         if (empty($args['verify'])) {
227             $group = $request->getGroup();
228             $allusers = $group->_allUsers();
229         } else {
230             $allusers = array_keys($args['user']);
231         }
232         foreach ($allusers as $username) {
233             if (ENABLE_USER_NEW)
234                 $user = WikiUser($username);
235             else 
236                 $user = new WikiUser($request, $username);
237             $prefs = $user->getPreferences();
238             if ($prefs->get('email')) {
239                 if (!$prefs->get('userid'))
240                     $prefs->set('userid',$username);
241                 $group = (int)(count($pagelist->_rows) / $pagelist->_group_rows);
242                 $class = ($group % 2) ? 'oddrow' : 'evenrow';
243                 $row = HTML::tr(array('class' => $class));
244                 $page_handle = $dbi->getPage($username);
245                 $row->pushContent($pagelist->_columns[0]->format($pagelist, 
246                                                                  $page_handle, $page_handle));
247                 $row->pushContent($email->format($pagelist, $prefs, $page_handle));
248                 if (!empty($args['verify'])) {
249                     $prefs->_prefs['email']->set('emailVerified', 
250                                                  empty($args['verified'][$username]) ? 0 : 2);
251                     $user->setPreferences($prefs);
252                 }
253                 $row->pushContent($emailVerified->format($pagelist, $prefs, $args['verify']));
254                 $pagelist->_rows[] = $row;
255             }
256         }
257         $request->_user = $current_user;
258         if (!empty($args['verify'])) {
259             return HTML($pagelist->_generateTable(false));
260         } else {
261             $args['verify'] = 1;
262             $args['return_url'] = $request->getURLtoSelf();
263             return HTML::form(array('action' => $request->getPostURL(),
264                                     'method' => 'post'),
265                           HiddenInputs($args, 'wikiadminutils'),
266                           HiddenInputs(array('require_authority_for_post' =>
267                                              WIKIAUTH_ADMIN)),
268                           HiddenInputs($request->getArgs()),
269                           $pagelist->_generateTable(false),                   
270                           HTML::p(Button('submit:', _("Change Verification Status"), 
271                                          'wikiadmin'),
272                                   HTML::Raw('&nbsp;'),
273                                   Button('cancel', _("Cancel")))
274                                  );
275         }
276     }
277 };
278
279 require_once("lib/PageList.php");
280
281 class _PageList_Column_email 
282 extends _PageList_Column {
283     function _getValue (&$prefs, $dummy) {
284         return $prefs->get('email');
285     }
286 }
287
288 class _PageList_Column_emailVerified
289 extends _PageList_Column {
290     function _getValue (&$prefs, $status) {
291         $name = $prefs->get('userid');
292         $input = HTML::input(array('type' => 'checkbox',
293                                    'name' => 'wikiadminutils[verified]['.$name.']',
294                                    'value' => 1));
295         if ($prefs->get('emailVerified'))
296             $input->setAttr('checked','1');
297         if ($status)
298             $input->setAttr('disabled','1');
299         return HTML($input, HTML::input
300                     (array('type' => 'hidden',
301                            'name' => 'wikiadminutils[user]['.$name.']',
302                            'value' => $name)));
303     }
304 }
305
306
307 // For emacs users
308 // Local Variables:
309 // mode: php
310 // tab-width: 8
311 // c-basic-offset: 4
312 // c-hanging-comment-ender-p: nil
313 // indent-tabs-mode: nil
314 // End:
315 ?>