]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PluginManager.php
New features: Also show plugin pages for localized variants.
[SourceForge/phpwiki.git] / lib / plugin / PluginManager.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PluginManager.php,v 1.11 2003-12-10 01:01:24 carstenklapp Exp $');
3 /**
4  Copyright 1999, 2000, 2001, 2002 $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 // Set this to true if you don't want regular users to view this page.
24 // So far there are no known security issues.
25 define('REQUIRE_ADMIN', false);
26
27 class WikiPlugin_PluginManager
28 extends WikiPlugin
29 {
30     function getName () {
31         return _("PluginManager");
32     }
33
34     function getDescription () {
35         return _("Provides a list of plugins on this wiki.");
36     }
37
38     function getVersion() {
39         return preg_replace("/[Revision: $]/", '',
40                             "\$Revision: 1.11 $");
41     }
42
43     function getDefaultArguments() {
44         return array('info' => 'args');
45     }
46
47     function run($dbi, $argstr, $request) {
48         extract($this->getArgs($argstr, $request));
49
50         $h = HTML();
51         $this->_generatePageheader($info, $h);
52
53         if (! REQUIRE_ADMIN || $request->_user->isadmin()) {
54             $h->pushContent(HTML::h2(_("Plugins")));
55
56             $table = HTML::table(array('class' => "pagelist"));
57             $this->_generateColgroups($info, $table);
58             $this->_generateColheadings($info, $table);
59             $this->_generateTableBody($info, $dbi, $request, $table);
60             $h->pushContent($table);
61
62             //$h->pushContent(HTML::h2(_("Disabled Plugins")));
63         }
64         else {
65             $h->pushContent(fmt("You must be an administrator to %s.",
66                                 _("use this plugin")));
67         }
68         return $h;
69     }
70
71     function _generatePageheader(&$info, &$html) {
72         $html->pushContent(HTML::p($this->getDescription()));
73     }
74
75     function _generateColgroups(&$info, &$table) {
76         // specify last two column widths
77         $colgroup = HTML::colgroup();
78         $colgroup->pushContent(HTML::col(array('width' => '0*')));
79         $colgroup->pushContent(HTML::col(array('width' => '0*',
80                                                'align' => 'right')));
81         $colgroup->pushContent(HTML::col(array('width' => '9*')));
82         if ($info == 'args')
83             $colgroup->pushContent(HTML::col(array('width' => '2*')));
84         $table->pushcontent($colgroup);
85     }
86
87     function _generateColheadings(&$info, &$table) {
88         // table headings
89         $tr = HTML::tr();
90         $headings = array(_("Plugin"), _("Version"), _("Description"));
91         if ($info == 'args')
92             $headings []= _("Arguments");
93         foreach ($headings as $title) {
94             $tr->pushContent(HTML::td($title));
95         }
96         $table->pushContent(HTML::thead($tr));
97     }
98
99     function _generateTableBody(&$info, &$dbi, &$request, &$table) {
100         $plugin_dir = 'lib/plugin';
101         if (defined('PHPWIKI_DIR'))
102             $plugin_dir = PHPWIKI_DIR . "/$plugin_dir";
103         $pd = new fileSet($plugin_dir, '*.php');
104         $plugins = $pd->getFiles();
105         unset($pd);
106         sort($plugins);
107
108         // table body
109         $tbody = HTML::tbody();
110         $row_no = 0;
111
112         $w = new WikiPluginLoader;
113         foreach($plugins as $pluginName) {
114             // instantiate a plugin
115             $pluginName = str_replace(".php", "", $pluginName);
116             $temppluginclass = "<? plugin $pluginName ?>"; // hackish
117             $p = $w->getPlugin($pluginName, false); // second arg?
118             // trap php files which aren't WikiPlugin~s
119             if (!substr(get_parent_class($p), 0, 10) == 'wikiplugin') {
120                 // Security: Hide names of extraneous files within
121                 // plugin dir from non-admins.
122                 if ($request->_user->isadmin())
123                     trigger_error(sprintf(_("%s does not appear to be a WikiPlugin."),
124                                           $pluginName . ".php"));
125                 continue; // skip this non WikiPlugin file
126             }
127             $desc = $p->getDescription();
128             $ver = $p->getVersion();
129             $args = $p->getDefaultArguments();
130             unset($p); //done querying plugin object, release from memory
131
132             $arguments = HTML();
133             foreach ($args as $arg => $default) {
134                 // Word around UserPreferences plugin to avoid error
135                 if ((is_array($default))) {
136                     $default = '(array)';
137                     // This is a bit flawed with UserPreferences object
138                     //$default = sprintf("array('%s')",
139                     //                   implode("', '", array_keys($default)));
140                 }
141                 else
142                     if (stristr($default, ' '))
143                         $default = "'$default'";
144                 $arguments->pushcontent("$arg=$default", HTML::br());
145             }
146             // make a link if an actionpage exists
147             $pluginNamelink = $pluginName;
148             $pluginDocPageNamelink = false;
149             // Also look for pages in the current locale
150             // Maybe FIXME? warn about case language != en and _(p) == "p"?
151             if (_($pluginName) != $pluginName) {
152                 $localizedPluginName = _($pluginName);
153             }
154             else
155                 $localizedPluginName = '';
156             $pluginNamelink = WikiLink($pluginName, 'if_known');
157             // make another link for the localized plugin description
158             // page if it exists
159             $pluginDocPageName = $pluginName . "Plugin";
160             // Also look for pages in the current locale
161             if (_($pluginDocPageName) != $pluginDocPageName) {
162                 $localizedPluginDocPageName = _($pluginDocPageName);
163             }
164             else
165                 $localizedPluginDocPageName = '';
166
167             global $WikiNameRegexp;
168             if (preg_match("/^$WikiNameRegexp\$/", $pluginDocPageName)
169                 && $dbi->isWikiPage($pluginDocPageName))
170                 {
171                 $pluginDocPageNamelink = HTML(WikiLink($pluginDocPageName));
172             }
173             else {
174                 // don't link to actionpages and plugins starting with
175                 // an _ from page list
176                 if (!preg_match("/^_/", $pluginName)
177                     //&& !(@$request->isActionPage($pluginName)) //FIXME?
178                     ) {
179                     $pluginDocPageNamelink = WikiLink($pluginDocPageName,
180                                                       'unknown');
181                 }
182                 else
183                     $pluginDocPageNamelink = false;
184             }
185             // insert any found locale-specific pages at the bottom of
186             // the td
187             if ($localizedPluginName || $localizedPluginDocPageName) {
188                 $par = HTML::p();
189                 if ($localizedPluginName) {
190                     // Don't offer to create a link to a non-wikiword
191                     // localized plugin page but show those that
192                     // already exist (Calendar, Comment, etc.)  (Non
193                     // non-wikiword plugins are okay, they just can't
194                     // become actionPages.)
195                     if (preg_match("/^$WikiNameRegexp\$/",
196                                    $localizedPluginName)
197                         || $dbi->isWikiPage($localizedPluginName))
198                         {
199                         $par->pushContent(WikiLink($localizedPluginName,
200                                                    'auto'));
201                     }
202                     else {
203                         // probably incorrectly translated, so no page
204                         // link
205                         $par->pushContent($localizedPluginName, ' '
206                                           . _("(Not a WikiWord)"));
207                     }
208                 }
209                 if ($localizedPluginName && $localizedPluginDocPageName)
210                     $par->pushContent(HTML::br());
211                 if ($localizedPluginDocPageName) {
212                     if (preg_match("/^$WikiNameRegexp\$/",
213                                    $localizedPluginDocPageName)
214                         || $dbi->isWikiPage($localizedPluginDocPageName))
215                         {
216                         $par->pushContent(WikiLink($localizedPluginDocPageName,
217                                                    'auto'));
218                     }
219                     else {
220                         // probably incorrectly translated, so no page
221                         // link
222                         $par->pushContent($localizedPluginDocPageName, ' '
223                                           . _("(Not a WikiWord)"));
224                     }
225                 }
226                 $pluginDocPageNamelink->pushContent($par);
227             }
228
229             // highlight alternate rows
230             $row_no++;
231             $group = (int)($row_no / 1); //_group_rows
232             $class = ($group % 2) ? 'evenrow' : 'oddrow';
233             // generate table row
234             $tr = HTML::tr(array('class' => $class));
235             if ($pluginDocPageNamelink) {
236                 // plugin has a description page 'PluginName' . 'Plugin'
237                 $tr->pushContent(HTML::td($pluginNamelink, HTML::br(),
238                                           $pluginDocPageNamelink));
239                 $pluginDocPageNamelink = false;
240                 //$row_no++;
241             }
242             else {
243                 // plugin just has an actionpage
244                 $tr->pushContent(HTML::td($pluginNamelink));
245             }
246             $tr->pushContent(HTML::td($ver), HTML::td($desc));
247             if ($info == 'args') {
248                 // add Arguments column
249                 $style = array('style'
250                                => 'font-family:monospace;font-size:smaller');
251                 $tr->pushContent(HTML::td($style, $arguments));
252             }
253             $tbody->pushContent($tr);
254         }
255         $table->pushContent($tbody);
256     }
257 };
258
259 // $Log: not supported by cvs2svn $
260 // Revision 1.10  2003/11/30 18:23:48  carstenklapp
261 // Code housekeeping: PEAR coding standards reformatting only.
262 //
263 // Revision 1.9  2003/11/19 00:02:42  carstenklapp
264 // Include found locale-specific pages for the current (non-English)
265 // locale.
266 //
267 // Revision 1.8  2003/11/15 21:53:53  wainstead
268 // Minor change: list plugins in asciibetical order. It'd be better if
269 // they were alphabetical.
270 //
271 // Revision 1.7  2003/02/24 01:36:25  dairiki
272 // Don't use PHPWIKI_DIR unless it's defined.
273 // (Also typo/bugfix in SystemInfo plugin.)
274 //
275 // Revision 1.6  2003/02/24 00:56:53  carstenklapp
276 // Updated to work with recent changes to WikiLink function (fix
277 // "==Object(wikipagename)==" for unknown wiki links).
278 //
279 // Revision 1.5  2003/02/22 20:49:56  dairiki
280 // Fixes for "Call-time pass by reference has been deprecated" errors.
281 //
282 // Revision 1.4  2003/02/20 18:13:38  carstenklapp
283 // Workaround for recent changes to WikiPlugin->getPlugin.
284 // Made admin restriction for viewing this page optional.
285 // Now defaults to any user may view this page (mainly for PhpWiki Demo site).
286 // Minor code changes & reformatting.
287 //
288 // Revision 1.3  2003/01/04 02:30:12  carstenklapp
289 // Added 'info' argument to show / hide plugin "Arguments"
290 // column. Improved row highlighting and error message when viewed by
291 // non-admin user. Code refactored. Added copyleft.
292
293 // Local Variables:
294 // mode: php
295 // tab-width: 8
296 // c-basic-offset: 4
297 // c-hanging-comment-ender-p: nil
298 // indent-tabs-mode: nil
299 // End:
300 ?>