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