]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiPlugin.php
getName should not translate
[SourceForge/phpwiki.git] / lib / WikiPlugin.php
1 <?php
2
3 class WikiPlugin
4 {
5     public $_pi;
6
7     function getDefaultArguments()
8     {
9         return array('description' => $this->getDescription());
10     }
11
12     /** Does the plugin manage its own HTTP validators?
13      *
14      * This should be overwritten by (some) individual plugins.
15      *
16      * If the output of the plugin is static, depending only
17      * on the plugin arguments, query arguments and contents
18      * of the current page, this can (and should) return true.
19      *
20      * If the plugin can deduce a modification time, or equivalent
21      * sort of tag for it's content, then the plugin should
22      * call $request->appendValidators() with appropriate arguments,
23      * and should override this method to return true.
24      *
25      * When in doubt, the safe answer here is false.
26      * Unfortunately, returning false here will most likely make
27      * any page which invokes the plugin uncacheable (by HTTP proxies
28      * or browsers).
29      */
30     function managesValidators()
31     {
32         return false;
33     }
34
35     // FIXME: args?
36     function run($dbi, $argstr, &$request, $basepage)
37     {
38         trigger_error("WikiPlugin::run: pure virtual function", E_USER_ERROR);
39         return false;
40     }
41
42     /** Get wiki-pages linked to by plugin invocation.
43      *
44      * A plugin may override this method to add pages to the
45      * link database for the invoking page.
46      *
47      * For example, the IncludePage plugin should override this so
48      * that the including page shows up in the backlinks list for the
49      * included page.
50      *
51      * Not all plugins which generate links to wiki-pages need list
52      * those pages here.
53      *
54      * Note also that currently the links are calculated at page save
55      * time, so only static page links (e.g. those dependent on the PI
56      * args, not the rest of the wikidb state or any request query args)
57      * will work correctly here.
58      *
59      * @param  string $argstr   The plugin argument string.
60      * @param  string $basepage The pagename the plugin is invoked from.
61      * @return array  List of pagenames linked to (or false).
62      */
63     function getWikiPageLinks($argstr, $basepage)
64     {
65         return false;
66     }
67
68     /**
69      * Get name of plugin.
70      *
71      * This is used (by default) by getDefaultLinkArguments and
72      * getDefaultFormArguments to compute the default link/form
73      * targets.
74      *
75      * If you override this method in your plugin class,
76      * you MUST NOT translate the name.
77      * <pre>
78      *   function getName() { return "MyPlugin"; }
79      * </pre>
80      *
81      * @return string plugin name/target.
82      */
83     function getName()
84     {
85         return preg_replace('/^WikiPlugin_/', '', get_class($this));
86     }
87
88     /**
89      * Get description of plugin.
90      *
91      * This method should be overriden in your plugin class, like:
92      * <pre>
93      *   function getDescription() { return _("MyPlugin does this..."); }
94      * </pre>
95      *
96      * @return string plugin description
97      */
98
99     function getDescription()
100     {
101         return _('This plugin has no description.');
102     }
103
104     function getArgs($argstr, $request = false, $defaults = array())
105     {
106         if (empty($defaults)) {
107             $defaults = $this->getDefaultArguments();
108         }
109         //Fixme: on POST argstr is empty
110         list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
111         $args = array();
112         if (!empty($defaults))
113             foreach ($defaults as $arg => $default_val) {
114                 if (isset($argstr_args[$arg])) {
115                     $args[$arg] = $argstr_args[$arg];
116                 } elseif ($request and ($argval = $request->getArg($arg)) !== false) {
117                     $args[$arg] = $argval;
118                 } elseif (isset($argstr_defaults[$arg])) {
119                     $args[$arg] = (string)$argstr_defaults[$arg];
120                 } else {
121                     $args[$arg] = $default_val;
122                 }
123                 // expand [arg]
124                 if ($request and is_string($args[$arg]) and strstr($args[$arg], "[")) {
125                     $args[$arg] = $this->expandArg($args[$arg], $request);
126                 }
127
128                 unset($argstr_args[$arg]);
129                 unset($argstr_defaults[$arg]);
130             }
131
132         foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
133             // TODO: where the heck comes this from? Put the new method over there and peace.
134             /*if ($request and $request->getArg('pagename') == _("PhpWikiAdministration")
135                 and $arg == 'overwrite') // silence this warning
136                 ;*/
137             if ($this->allow_undeclared_arg($arg, $val))
138                 $args[$arg] = $val;
139         }
140
141         // Add special handling of pages and exclude args to accept <! plugin-list !>
142         // and split explodePageList($args['exclude']) => array()
143         // TODO : handle p[] pagehash
144         foreach (array('pages', 'exclude') as $key) {
145             if (!empty($args[$key]) and array_key_exists($key, $defaults))
146                 $args[$key] = is_string($args[$key])
147                     ? explodePageList($args[$key])
148                     : $args[$key]; // <! plugin-list !>
149         }
150
151         return $args;
152     }
153
154     // Patch by Dan F:
155     // Expand [arg] to $request->getArg("arg") unless preceded by ~
156     function expandArg($argval, &$request)
157     {
158         // return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
159         // Replace the arg unless it is preceded by a ~
160         $ret = preg_replace('/([^~]|^)\[(\w[\w\d]*)\]/e',
161             '"$1" . $request->getArg("$2")',
162             $argval);
163         // Ditch the ~ so later versions can be expanded if desired
164         return preg_replace('/~(\[\w[\w\d]*\])/', '$1', $ret);
165     }
166
167     function parseArgStr($argstr)
168     {
169         $args = array();
170         $defaults = array();
171         if (empty($argstr))
172             return array($args, $defaults);
173
174         $arg_p = '\w+';
175         $op_p = '(?:\|\|)?=';
176         $word_p = '\S+';
177         $opt_ws = '\s*';
178         $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
179         //"<--kludge for brain-dead syntax coloring
180         $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
181         $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
182         $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
183
184         // handle plugin-list arguments separately
185         $plugin_p = '<!plugin-list\s+\w+.*?!>';
186         while (preg_match("/^($arg_p) $opt_ws ($op_p) $opt_ws ($plugin_p) $opt_ws/x", $argstr, $m)) {
187             @ list(, $arg, $op, $plugin_val) = $m;
188             $argstr = substr($argstr, strlen($m[0]));
189             $loader = new WikiPluginLoader();
190             $markup = null;
191             $basepage = null;
192             $plugin_val = preg_replace(array("/^<!/", "/!>$/"), array("<?", "?>"), $plugin_val);
193             $val = $loader->expandPI($plugin_val, $GLOBALS['request'], $markup, $basepage);
194             if ($op == '=') {
195                 $args[$arg] = $val; // comma delimited pagenames or array()?
196             } else {
197                 assert($op == '||=');
198                 $defaults[$arg] = $val;
199             }
200         }
201         while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
202             $qq_val = '';
203             $q_val = '';
204             $gt_val = '';
205             $word_val = '';
206             $op = '';
207             $arg = '';
208             $count = count($m);
209             if ($count >= 7) {
210                 list(, $arg, $op, $qq_val, $q_val, $gt_val, $word_val) = $m;
211             } elseif ($count == 6) {
212                 list(, $arg, $op, $qq_val, $q_val, $gt_val) = $m;
213             } elseif ($count == 5) {
214                 list(, $arg, $op, $qq_val, $q_val) = $m;
215             } elseif ($count == 4) {
216                 list(, $arg, $op, $qq_val) = $m;
217             }
218             $argstr = substr($argstr, strlen($m[0]));
219             // Remove quotes from string values.
220             if ($qq_val)
221                 $val = stripslashes($qq_val);
222             elseif ($count > 4 and $q_val)
223                 $val = stripslashes($q_val); elseif ($count >= 6 and $gt_val)
224                 $val = _(stripslashes($gt_val)); elseif ($count >= 7)
225                 $val = $word_val; else
226                 $val = '';
227
228             if ($op == '=') {
229                 $args[$arg] = $val;
230             } else {
231                 // NOTE: This does work for multiple args. Use the
232                 // separator character defined in your webserver
233                 // configuration, usually & or &amp; (See
234                 // http://www.htmlhelp.com/faq/cgifaq.4.html)
235                 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
236                 // url: RecentChanges?days=1&show_all=1&show_minor=0
237                 assert($op == '||=');
238                 $defaults[$arg] = $val;
239             }
240         }
241
242         if ($argstr) {
243             $this->handle_plugin_args_cruft($argstr, $args);
244         }
245
246         return array($args, $defaults);
247     }
248
249     /* A plugin can override this function to define how any remaining text is handled */
250     function handle_plugin_args_cruft($argstr, $args)
251     {
252         trigger_error(sprintf(_("trailing cruft in plugin args: ā€œ%sā€"),
253             $argstr), E_USER_NOTICE);
254     }
255
256     /* A plugin can override this to allow undeclared arguments.
257        Or to silence the warning.
258      */
259     function allow_undeclared_arg($name, $value)
260     {
261         trigger_error(sprintf(_("Argument ā€œ%sā€ not declared by plugin."),
262             $name), E_USER_NOTICE);
263         return false;
264     }
265
266     /* handle plugin-list argument: use run(). */
267     function makeList($plugin_args, $request, $basepage)
268     {
269         $dbi = $request->getDbh();
270         $pagelist = $this->run($dbi, $plugin_args, $request, $basepage);
271         $list = array();
272         if (is_object($pagelist) and isa($pagelist, 'PageList'))
273             return $pagelist->pageNames();
274         elseif (is_array($pagelist))
275             return $pagelist; else
276             return $list;
277     }
278
279     function getDefaultLinkArguments()
280     {
281         return array('targetpage' => $this->getName(),
282             'linktext' => $this->getName(),
283             'description' => $this->getDescription(),
284             'class' => 'wikiaction');
285     }
286
287     function getDefaultFormArguments()
288     {
289         return array('targetpage' => $this->getName(),
290             'buttontext' => _($this->getName()),
291             'class' => 'wikiaction',
292             'method' => 'get',
293             'textinput' => 's',
294             'description' => $this->getDescription(),
295             'formsize' => 30);
296     }
297
298     function makeForm($argstr, $request)
299     {
300         $form_defaults = $this->getDefaultFormArguments();
301         $defaults = array_merge($form_defaults,
302             array('start_debug' => $request->getArg('start_debug')),
303             $this->getDefaultArguments());
304
305         $args = $this->getArgs($argstr, $request, $defaults);
306         $textinput = $args['textinput'];
307         assert(!empty($textinput) && isset($args['textinput']));
308
309         $form = HTML::form(array('action' => WikiURL($args['targetpage']),
310             'method' => $args['method'],
311             'class' => $args['class'],
312             'accept-charset' => 'UTF-8'));
313         if (!USE_PATH_INFO) {
314             $pagename = $request->get('pagename');
315             $form->pushContent(HTML::input(array('type' => 'hidden',
316                 'name' => 'pagename',
317                 'value' => $args['targetpage'])));
318         }
319         if ($args['targetpage'] != $this->getName()) {
320             $form->pushContent(HTML::input(array('type' => 'hidden',
321                 'name' => 'action',
322                 'value' => $this->getName())));
323         }
324         $contents = HTML::div();
325         $contents->setAttr('class', $args['class']);
326
327         foreach ($args as $arg => $val) {
328             if (isset($form_defaults[$arg]))
329                 continue;
330             if ($arg != $textinput && $val == $defaults[$arg])
331                 continue;
332
333             $i = HTML::input(array('name' => $arg, 'value' => $val));
334
335             if ($arg == $textinput) {
336                 //if ($inputs[$arg] == 'file')
337                 //    $attr['type'] = 'file';
338                 //else
339                 $i->setAttr('type', 'text');
340                 $i->setAttr('size', $args['formsize']);
341                 if ($args['description'])
342                     $i->addTooltip($args['description']);
343             } else {
344                 $i->setAttr('type', 'hidden');
345             }
346             $contents->pushContent($i);
347
348             // FIXME: hackage
349             if ($i->getAttr('type') == 'file') {
350                 $form->setAttr('enctype', 'multipart/form-data');
351                 $form->setAttr('method', 'post');
352                 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
353                     'value' => MAX_UPLOAD_SIZE,
354                     'type' => 'hidden')));
355             }
356         }
357
358         if (!empty($args['buttontext']))
359             $contents->pushContent(HTML::input(array('type' => 'submit',
360                 'class' => 'button',
361                 'value' => $args['buttontext'])));
362         $form->pushContent($contents);
363         return $form;
364     }
365
366     // box is used to display a fixed-width, narrow version with common header
367     function box($args = false, $request = false, $basepage = false)
368     {
369         if (!$request) $request =& $GLOBALS['request'];
370         $dbi = $request->getDbh();
371         return $this->makeBox('', $this->run($dbi, $args, $request, $basepage));
372     }
373
374     function makeBox($title, $body)
375     {
376         if (!$title) $title = $this->getName();
377         return HTML::div(array('class' => 'box'),
378             HTML::div(array('class' => 'box-title'), $title),
379             HTML::div(array('class' => 'box-data'), $body));
380     }
381
382     function error($message)
383     {
384         return HTML::span(array('class' => 'error'),
385             HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
386             $message);
387     }
388
389     function disabled($message = '')
390     {
391         $html[] = HTML::div(array('class' => 'title'),
392             fmt("Plugin %s disabled.", $this->getName()),
393             ' ', $message);
394         $html[] = HTML::pre($this->_pi);
395         return HTML::div(array('class' => 'disabled-plugin'), $html);
396     }
397
398     // TODO: Not really needed, since our plugins generally initialize their own
399     // PageList object, which accepts options['types'].
400     // Register custom PageList types for special plugins, like
401     // 'hi_content' for WikiAdminSearcheplace, 'renamed_pagename' for WikiAdminRename, ...
402     function addPageListColumn($array)
403     {
404         global $customPageListColumns;
405         if (empty($customPageListColumns)) $customPageListColumns = array();
406         foreach ($array as $column => $obj) {
407             $customPageListColumns[$column] = $obj;
408         }
409     }
410
411     // provide a sample usage text for automatic edit-toolbar insertion
412     function getUsage()
413     {
414         $args = $this->getDefaultArguments();
415         $string = '<<' . $this->getName() . ' ';
416         if ($args) {
417             foreach ($args as $key => $value) {
418                 $string .= ($key . "||=" . (string)$value . " ");
419             }
420         }
421         return $string . '>>';
422     }
423
424     function getArgumentsDescription()
425     {
426         $arguments = HTML();
427         foreach ($this->getDefaultArguments() as $arg => $default) {
428             // Work around UserPreferences plugin to avoid error
429             if ((is_array($default))) {
430                 $default = '(array)';
431                 // This is a bit flawed with UserPreferences object
432                 //$default = sprintf("array('%s')",
433                 //                   implode("', '", array_keys($default)));
434             } else
435                 if (stristr($default, ' '))
436                     $default = "'$default'";
437             $arguments->pushcontent("$arg=$default", HTML::br());
438         }
439         return $arguments;
440     }
441
442 }
443
444 class WikiPluginLoader
445 {
446     public $_errors;
447
448     function expandPI($pi, &$request, &$markup, $basepage = false)
449     {
450         if (!($ppi = $this->parsePi($pi)))
451             return false;
452         list($pi_name, $plugin, $plugin_args) = $ppi;
453
454         if (!is_object($plugin)) {
455             return new HtmlElement('div',
456                 array('class' => 'error'),
457                 $this->getErrorDetail());
458         }
459         switch ($pi_name) {
460             case 'plugin':
461                 // FIXME: change API for run() (no $dbi needed).
462                 $dbi = $request->getDbh();
463                 // pass the parsed CachedMarkup context in dbi to the plugin
464                 // to be able to know about itself, or even to change the markup XmlTree (CreateToc)
465                 $dbi->_markup = &$markup;
466                 // FIXME: could do better here...
467                 if (!$plugin->managesValidators()) {
468                     // Output of plugin (potentially) depends on
469                     // the state of the WikiDB (other than the current
470                     // page.)
471
472                     // Lacking other information, we'll assume things
473                     // changed last time the wikidb was touched.
474
475                     // As an additional hack, mark the ETag weak, since,
476                     // for all we know, the page might depend
477                     // on things other than the WikiDB (e.g. PhpWeather,
478                     // Calendar...)
479
480                     $timestamp = $dbi->getTimestamp();
481                     $request->appendValidators(array('dbi_timestamp' => $timestamp,
482                         '%mtime' => (int)$timestamp,
483                         '%weak' => true));
484                 }
485                 return $plugin->run($dbi, $plugin_args, $request, $basepage);
486             case 'plugin-list':
487                 return $plugin->makeList($plugin_args, $request, $basepage);
488             case 'plugin-form':
489                 return $plugin->makeForm($plugin_args, $request);
490         }
491         return false;
492     }
493
494     function getWikiPageLinks($pi, $basepage)
495     {
496         if (!($ppi = $this->parsePi($pi)))
497             return false;
498         list($pi_name, $plugin, $plugin_args) = $ppi;
499         if (!is_object($plugin))
500             return false;
501         if ($pi_name != 'plugin')
502             return false;
503         return $plugin->getWikiPageLinks($plugin_args, $basepage);
504     }
505
506     function parsePI($pi)
507     {
508         if (!preg_match('/^\s*<\?(plugin(?:-form|-link|-list)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
509             return $this->_error(sprintf("Bad %s", 'PI'));
510
511         list(, $pi_name, $plugin_name, $plugin_args) = $m;
512         $plugin = $this->getPlugin($plugin_name, $pi);
513
514         return array($pi_name, $plugin, $plugin_args);
515     }
516
517     function getPlugin($plugin_name, $pi = false)
518     {
519         global $ErrorManager;
520         global $AllAllowedPlugins;
521
522         if (in_array($plugin_name, $AllAllowedPlugins) === false) {
523             return $this->_error(sprintf(_("Plugin ā€œ%sā€ does not exist."),
524                 $plugin_name));
525         }
526
527         // Note that there seems to be no way to trap parse errors
528         // from this include.  (At least not via set_error_handler().)
529         $plugin_source = "lib/plugin/$plugin_name.php";
530
531         $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
532         $plugin_class = "WikiPlugin_$plugin_name";
533         if (!class_exists($plugin_class)) {
534             // $include_failed = !@include_once("lib/plugin/$plugin_name.php");
535             $include_failed = !include_once($plugin_source);
536             $ErrorManager->popErrorHandler();
537
538             if (!class_exists($plugin_class)) {
539                 if ($include_failed) {
540                     return $this->_error(sprintf(_("Plugin ā€œ%sā€ does not exist."),
541                         $plugin_name));
542                 }
543                 return $this->_error(sprintf(_("%s: no such class"), $plugin_class));
544             }
545         }
546         $ErrorManager->popErrorHandler();
547         $plugin = new $plugin_class;
548         if (!is_subclass_of($plugin, "WikiPlugin"))
549             return $this->_error(sprintf(_("%s: not a subclass of WikiPlugin."),
550                 $plugin_class));
551
552         $plugin->_pi = $pi;
553         return $plugin;
554     }
555
556     function _plugin_error_filter($err)
557     {
558         if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
559             return true; // Ignore this error --- it's expected.
560         return false;
561     }
562
563     function getErrorDetail()
564     {
565         return $this->_errors;
566     }
567
568     function _error($message)
569     {
570         $this->_errors = $message;
571         return false;
572     }
573 }
574
575 // Local Variables:
576 // mode: php
577 // tab-width: 8
578 // c-basic-offset: 4
579 // c-hanging-comment-ender-p: nil
580 // indent-tabs-mode: nil
581 // End: