]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiPlugin.php
* changed stored pref representation as before.
[SourceForge/phpwiki.git] / lib / WikiPlugin.php
1 <?php //-*-php-*-
2 rcs_id('$Id: WikiPlugin.php,v 1.34 2004-01-26 09:17:48 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.34 $");
91     }
92
93     function getArgs($argstr, $request=false, $defaults = false) {
94         if ($defaults === false)
95             $defaults = $this->getDefaultArguments();
96
97         list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
98         $args = array();
99         if (!empty($defaults))
100           foreach ($defaults as $arg => $default_val) {
101             if (isset($argstr_args[$arg]))
102                 $args[$arg] = $argstr_args[$arg];
103             elseif ( $request and ($argval = $request->getArg($arg)) !== false )
104                 $args[$arg] = $argval;
105             elseif (isset($argstr_defaults[$arg]))
106                 $args[$arg] = (string) $argstr_defaults[$arg];
107             else
108                 $args[$arg] = $default_val;
109
110             if ($request)
111                 $args[$arg] = $this->expandArg($args[$arg], $request);
112
113             unset($argstr_args[$arg]);
114             unset($argstr_defaults[$arg]);
115         }
116
117         foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
118             trigger_error(sprintf(_("argument '%s' not declared by plugin"),
119                                   $arg), E_USER_NOTICE);
120         }
121
122         return $args;
123     }
124
125     function expandArg($argval, $request) {
126         return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
127                             $argval);
128     }
129
130     function parseArgStr($argstr) {
131         $arg_p = '\w+';
132         $op_p = '(?:\|\|)?=';
133         $word_p = '\S+';
134         $opt_ws = '\s*';
135         $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
136         //"<--kludge for brain-dead syntax coloring
137         $q_p  = "' ( (?:[^'\\\\]|\\\\.)* ) '";
138         $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
139         $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
140
141         $args = array();
142         $defaults = array();
143
144         while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
145             @ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
146             $argstr = substr($argstr, strlen($m[0]));
147
148             // Remove quotes from string values.
149             if ($qq_val)
150                 $val = stripslashes($qq_val);
151             elseif ($q_val)
152                 $val = stripslashes($q_val);
153             elseif ($gt_val)
154                 $val = _(stripslashes($gt_val));
155             else
156                 $val = $word_val;
157
158             if ($op == '=') {
159                 $args[$arg] = $val;
160             }
161             else {
162                 // NOTE: This does work for multiple args. Use the
163                 // separator character defined in your webserver
164                 // configuration, usually & or &amp; (See
165                 // http://www.htmlhelp.com/faq/cgifaq.4.html)
166                 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
167                 // url: RecentChanges?days=1&show_all=1&show_minor=0
168                 assert($op == '||=');
169                 $defaults[$arg] = $val;
170             }
171         }
172
173         if ($argstr) {
174            $this->handle_plugin_args_cruft($argstr, $args);
175         }
176
177         return array($args, $defaults);
178     }
179
180     /* A plugin can override this function to define how any remaining text is handled */
181     function handle_plugin_args_cruft($argstr, $args) {
182         trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),
183                               $argstr), E_USER_NOTICE);
184     }
185
186     function getDefaultLinkArguments() {
187         return array('targetpage' => $this->getName(),
188                      'linktext' => $this->getName(),
189                      'description' => $this->getDescription(),
190                      'class' => 'wikiaction');
191     }
192
193     function makeLink($argstr, $request) {
194         $defaults = $this->getDefaultArguments();
195         $link_defaults = $this->getDefaultLinkArguments();
196         $defaults = array_merge($defaults, $link_defaults);
197     
198         $args = $this->getArgs($argstr, $request, $defaults);
199         $plugin = $this->getName();
200     
201         $query_args = array();
202         foreach ($args as $arg => $val) {
203             if (isset($link_defaults[$arg]))
204                 continue;
205             if ($val != $defaults[$arg])
206                 $query_args[$arg] = $val;
207         }
208     
209         $link = Button($query_args, $args['linktext'], $args['targetpage']);
210         if (!empty($args['description']))
211             $link->addTooltip($args['description']);
212     
213         return $link;
214     }
215     
216     function getDefaultFormArguments() {
217         return array('targetpage' => $this->getName(),
218                      'buttontext' => $this->getName(),
219                      'class' => 'wikiaction',
220                      'method' => 'get',
221                      'textinput' => 's',
222                      'description' => $this->getDescription(),
223                      'formsize' => 30);
224     }
225     
226     function makeForm($argstr, $request) {
227         $form_defaults = $this->getDefaultFormArguments();
228         $defaults = array_merge($this->getDefaultArguments(),
229                                 $form_defaults);
230     
231         $args = $this->getArgs($argstr, $request, $defaults);
232         $plugin = $this->getName();
233         $textinput = $args['textinput'];
234         assert(!empty($textinput) && isset($args['textinput']));
235     
236         $form = HTML::form(array('action' => WikiURL($args['targetpage']),
237                                  'method' => $args['method'],
238                                  'class' => $args['class'],
239                                  'accept-charset' => CHARSET));
240         if (! USE_PATH_INFO ) {
241             $pagename = $request->get('pagename');
242             $form->pushContent(HTML::input(array('type' => 'hidden', 'name' => 'pagename', 
243                                                  'value' => $args['targetpage'])));
244         }
245         $contents = HTML::div();
246         $contents->setAttr('class', $args['class']);
247     
248         foreach ($args as $arg => $val) {
249             if (isset($form_defaults[$arg]))
250                 continue;
251             if ($arg != $textinput && $val == $defaults[$arg])
252                 continue;
253     
254             $i = HTML::input(array('name' => $arg, 'value' => $val));
255     
256             if ($arg == $textinput) {
257                 //if ($inputs[$arg] == 'file')
258                 //    $attr['type'] = 'file';
259                 //else
260                 $i->setAttr('type', 'text');
261                 $i->setAttr('size', $args['formsize']);
262                 if ($args['description'])
263                     $i->addTooltip($args['description']);
264             }
265             else {
266                 $i->setAttr('type', 'hidden');
267             }
268             $contents->pushContent($i);
269     
270             // FIXME: hackage
271             if ($i->getAttr('type') == 'file') {
272                 $form->setAttr('enctype', 'multipart/form-data');
273                 $form->setAttr('method', 'post');
274                 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
275                                                          'value' => MAX_UPLOAD_SIZE,
276                                                          'type' => 'hidden')));
277             }
278         }
279     
280         if (!empty($args['buttontext']))
281             $contents->pushContent(HTML::input(array('type' => 'submit',
282                                                      'class' => 'button',
283                                                      'value' => $args['buttontext'])));
284     
285         $form->pushContent($contents);
286         return $form;
287     }
288     
289     function error ($message) {
290         return HTML::div(array('class' => 'errors'),
291                         HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
292                         $message);
293     }
294
295     function disabled ($message='') {
296         $html[] = HTML::div(array('class' => 'title'),
297                             fmt("Plugin %s disabled.", $this->getName()),
298                             ' ', $message);
299         $html[] = HTML::pre($this->_pi);
300         return HTML::div(array('class' => 'disabled-plugin'), $html);
301     }
302 }
303
304 class WikiPluginLoader {
305     var $_errors;
306
307     function expandPI($pi, &$request, $basepage=false) {
308         if (!($ppi = $this->parsePi($pi)))
309             return false;
310         list($pi_name, $plugin, $plugin_args) = $ppi;
311
312         if (!is_object($plugin)) {
313             return new HtmlElement($pi_name == 'plugin-link' ? 'span' : 'p',
314                                    array('class' => 'plugin-error'),
315                                    $this->getErrorDetail());
316         }
317         switch ($pi_name) {
318             case 'plugin':
319                 // FIXME: change API for run() (no $dbi needed).
320                 $dbi = $request->getDbh();
321                 // FIXME: could do better here...
322                 if (! $plugin->managesValidators()) {
323                     // Output of plugin (potentially) depends on
324                     // the state of the WikiDB (other than the current
325                     // page.)
326                     
327                     // Lacking other information, we'll assume things
328                     // changed last time the wikidb was touched.
329                     
330                     // As an additional hack, mark the ETag weak, since,
331                     // for all we know, the page might depend
332                     // on things other than the WikiDB (e.g. PhpWeather,
333                     // Calendar...)
334                     
335                     $timestamp = $dbi->getTimestamp();
336                     $request->appendValidators(array('dbi_timestamp' => $timestamp,
337                                                      '%mtime' => (int)$timestamp,
338                                                      '%weak' => true));
339                 }
340                 return $plugin->run($dbi, $plugin_args, $request, $basepage);
341             case 'plugin-link':
342                 return $plugin->makeLink($plugin_args, $request);
343             case 'plugin-form':
344                 return $plugin->makeForm($plugin_args, $request);
345         }
346     }
347
348     function getWikiPageLinks($pi, $basepage) {
349         if (!($ppi = $this->parsePi($pi)))
350             return false;
351         list($pi_name, $plugin, $plugin_args) = $ppi;
352         if (!is_object($plugin))
353             return false;
354         if ($pi_name != 'plugin')
355             return false;
356         return $plugin->getWikiPageLinks($plugin_args, $basepage);
357     }
358     
359     function parsePI($pi) {
360         if (!preg_match('/^\s*<\?(plugin(?:-form|-link)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
361             return $this->_error(sprintf("Bad %s", 'PI'));
362
363         list(, $pi_name, $plugin_name, $plugin_args) = $m;
364         $plugin = $this->getPlugin($plugin_name, $pi);
365
366         return array($pi_name, $plugin, $plugin_args);
367     }
368     
369     function getPlugin($plugin_name, $pi) {
370         global $ErrorManager;
371
372         // Note that there seems to be no way to trap parse errors
373         // from this include.  (At least not via set_error_handler().)
374         $plugin_source = "lib/plugin/$plugin_name.php";
375
376         $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
377         // $include_failed = !@include_once("lib/plugin/$plugin_name.php");
378         $include_failed = !include_once("lib/plugin/$plugin_name.php");
379         $ErrorManager->popErrorHandler();
380
381         $plugin_class = "WikiPlugin_$plugin_name";
382         if (!class_exists($plugin_class)) {
383             if ($include_failed)
384                 return $this->_error(sprintf(_("Include of '%s' failed"),
385                                              $plugin_source));
386             return $this->_error(sprintf("%s: no such class", $plugin_class));
387         }
388
389         $plugin = new $plugin_class;
390         if (!is_subclass_of($plugin, "WikiPlugin"))
391             return $this->_error(sprintf("%s: not a subclass of WikiPlugin",
392                                          $plugin_class));
393
394         $plugin->_pi = $pi;
395         return $plugin;
396     }
397
398     function _plugin_error_filter ($err) {
399         if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
400             return true;        // Ignore this error --- it's expected.
401         return false;
402     }
403
404     function getErrorDetail() {
405         return $this->_errors;
406     }
407
408     function _error($message) {
409         $this->_errors = $message;
410         return false;
411     }
412 };
413
414 // (c-file-style: "gnu")
415 // Local Variables:
416 // mode: php
417 // tab-width: 8
418 // c-basic-offset: 4
419 // c-hanging-comment-ender-p: nil
420 // indent-tabs-mode: nil
421 // End:
422 ?>