]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminUtils.php
added missing 4th basepage arg at plugin->run() to almost all plugins. This caused...
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminUtils.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminUtils.php,v 1.5 2004-02-17 12:11:36 rurban Exp $');
3 /**
4  Copyright 2003 $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  */
25 class WikiPlugin_WikiAdminUtils
26 extends WikiPlugin
27 {
28     function getName () {
29         return _("WikiAdminUtils");
30     }
31
32     function getDescription () {
33         return _("Miscellaneous utility functions of use to the administrator.");
34     }
35
36     function getVersion() {
37         return preg_replace("/[Revision: $]/", '',
38                             "\$Revision: 1.5 $");
39     }
40
41     function getDefaultArguments() {
42         return array('action'           => '',
43                      'label'            => '',
44                      );
45     }
46
47     function run($dbi, $argstr, &$request, $basepage) {
48         $args = $this->getArgs($argstr, $request);
49         $args['action'] = strtolower($args['action']);
50         extract($args);
51         
52         if (!$action)
53             $this->error("No action specified");
54         if (!($default_label = $this->_getLabel($action)))
55             $this->error("Bad action");
56         if ($request->getArg('action') != 'browse')
57             return $this->disabled("(action != 'browse')");
58         
59         $posted = $request->getArg('wikiadminutils');
60         $request->setArg('wikiadminutils', false);
61
62         if ($request->isPost()) {
63             $user = $request->getUser();
64             if (!$user->isAdmin())
65                 return $this->error(_("You must be an administrator to use this plugin."));
66             return $this->do_action($request, $posted);
67         }
68
69         if (empty($label))
70             $label = $default_label;
71         
72         return $this->_makeButton($request, $args, $label);
73     }
74
75     function _makeButton(&$request, $args, $label) {
76         $args['return_url'] = $request->getURLtoSelf();
77         return HTML::form(array('action' => $request->getPostURL(),
78                                 'method' => 'post'),
79                           HTML::p(Button('submit:', $label, 'wikiadmin')),
80                           HiddenInputs($args, 'wikiadminutils'),
81                           HiddenInputs(array('require_authority_for_post' =>
82                                              WIKIAUTH_ADMIN)),
83                           HiddenInputs($request->getArgs()));
84     }
85     
86     function do_action(&$request, $args) {
87         $method = '_do_' . str_replace('-', '_', $args['action']);
88         if (!method_exists($this, $method))
89             return $this->error("Bad action");
90
91         $message = call_user_func(array(&$this, $method), $request, $args);
92
93         $alert = new Alert(_("WikiAdminUtils says:"),
94                            $message,
95                            array(_("Okay") => $args['return_url']));
96
97         $alert->show();         // noreturn
98     }
99
100     function _getLabel($action) {
101         $labels = array('purge-cache' => _("Purge Markup Cache"),
102                         'purge-bad-pagenames' => _("Delete Pages With Invalid Names"));
103         return @$labels[$action];
104     }
105
106     function _do_purge_cache(&$request, $args) {
107         $dbi = $request->getDbh();
108         $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
109         while (($page = $pages->next())) {
110             $page->set('_cached_html', false);
111         }
112         return _("Markup cache purged!");
113     }
114
115     function _do_purge_bad_pagenames(&$request, $args) {
116         // FIXME: this should be moved into WikiDB::normalize() or something...
117         $dbi = $request->getDbh();
118         $pages = $dbi->getAllPages('include_empty'); // Do we really want the empty ones too?
119         $badpages = array();
120         while (($page = $pages->next())) {
121             $pagename = $page->getName();
122             $wpn = new WikiPageName($pagename);
123             if (! $wpn->isValid())
124                 $badpages[] = $pagename;
125         }
126
127         if (!$badpages)
128             return _("No pages with bad names were found.");
129         
130         $list = HTML::ul();
131         foreach ($badpages as $pagename) {
132             $dbi->deletePage($pagename);
133             $list->pushContent(HTML::li($pagename));
134         }
135         
136         return HTML(fmt("Deleted %s pages with invalid names:",
137                         count($badpages)),
138                     $list);
139     }
140 };
141
142 // For emacs users
143 // Local Variables:
144 // mode: php
145 // tab-width: 8
146 // c-basic-offset: 4
147 // c-hanging-comment-ender-p: nil
148 // indent-tabs-mode: nil
149 // End:
150 ?>