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