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