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