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