]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiPlugin.php
Starting to fix handling of the HTTP validator headers, Last-Modified,
[SourceForge/phpwiki.git] / lib / WikiPlugin.php
1 <?php //-*-php-*-
2 rcs_id('$Id: WikiPlugin.php,v 1.29 2003-02-16 05:09:43 dairiki Exp $');
3
4 class WikiPlugin
5 {
6     function getDefaultArguments() {
7         return array('description' => $this->getDescription());
8     }
9
10     /** Does plugin have static output?
11      *
12      * Should return true iff the output of the plugin only depends
13      * on its arguments and/or the contents of the current page.
14      *
15      * The safe answer is false, but that will probably make the page
16      * output uncacheable.  Plugins should override this method to return
17      * true if possible.
18      */
19     function hasStaticOutput() {
20         return false;
21     }
22     
23
24     // FIXME: args?
25     function run ($dbi, $argstr, &$request) {
26         trigger_error("WikiPlugin::run: pure virtual function",
27                       E_USER_ERROR);
28     }
29
30     /**
31      * Get name of plugin.
32      *
33      * This is used (by default) by getDefaultLinkArguments and
34      * getDefaultFormArguments to compute the default link/form
35      * targets.
36      *
37      * If you want to gettextify the name (probably a good idea),
38      * override this method in your plugin class, like:
39      * <pre>
40      *   function getName() { return _("MyPlugin"); }
41      * </pre>
42      *
43      * @return string plugin name/target.
44      */
45     function getName() {
46         return preg_replace('/^.*_/', '',  get_class($this));
47     }
48
49     function getDescription() {
50         return $this->getName();
51     }
52
53
54     function getArgs($argstr, $request, $defaults = false) {
55         if ($defaults === false)
56             $defaults = $this->getDefaultArguments();
57
58         list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
59         $args = array();
60         foreach ($defaults as $arg => $default_val) {
61             if (isset($argstr_args[$arg]))
62                 $args[$arg] = $argstr_args[$arg];
63             elseif ( ($argval = $request->getArg($arg)) !== false )
64                 $args[$arg] = $argval;
65             elseif (isset($argstr_defaults[$arg]))
66                 $args[$arg] = (string) $argstr_defaults[$arg];
67             else
68                 $args[$arg] = $default_val;
69
70             $args[$arg] = $this->expandArg($args[$arg], $request);
71
72             unset($argstr_args[$arg]);
73             unset($argstr_defaults[$arg]);
74         }
75
76         foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
77             trigger_error(sprintf(_("argument '%s' not declared by plugin"),
78                                   $arg), E_USER_NOTICE);
79         }
80
81         return $args;
82     }
83
84     function expandArg($argval, $request) {
85         return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
86                             $argval);
87     }
88
89
90     function parseArgStr($argstr) {
91         $arg_p = '\w+';
92         $op_p = '(?:\|\|)?=';
93         $word_p = '\S+';
94         $opt_ws = '\s*';
95         $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
96         //"<--kludge for brain-dead syntax coloring
97         $q_p  = "' ( (?:[^'\\\\]|\\\\.)* ) '";
98         $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
99         $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
100
101         $args = array();
102         $defaults = array();
103
104         while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
105             @ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
106             $argstr = substr($argstr, strlen($m[0]));
107
108             // Remove quotes from string values.
109             if ($qq_val)
110                 $val = stripslashes($qq_val);
111             elseif ($q_val)
112                 $val = stripslashes($q_val);
113             elseif ($gt_val)
114                 $val = _(stripslashes($gt_val));
115             else
116                 $val = $word_val;
117
118             if ($op == '=') {
119                 $args[$arg] = $val;
120             }
121             else {
122                 // NOTE: This does work for multiple args. Use the
123                 // separator character defined in your webserver
124                 // configuration, usually & or &amp; (See
125                 // http://www.htmlhelp.com/faq/cgifaq.4.html)
126                 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
127                 // url: RecentChanges?days=1&show_all=1&show_minor=0
128                 assert($op == '||=');
129                 $defaults[$arg] = $val;
130             }
131         }
132
133         if ($argstr) {
134            $this->handle_plugin_args_cruft($argstr, $args);
135         }
136
137         return array($args, $defaults);
138     }
139
140     /* A plugin can override this function to define how any remaining text is handled */
141     function handle_plugin_args_cruft($argstr, $args) {
142         trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),
143                               $argstr), E_USER_NOTICE);
144     }
145
146     function getDefaultLinkArguments() {
147         return array('targetpage' => $this->getName(),
148                      'linktext' => $this->getName(),
149                      'description' => $this->getDescription(),
150                      'class' => 'wikiaction');
151     }
152
153     function makeLink($argstr, $request) {
154         $defaults = $this->getDefaultArguments();
155         $link_defaults = $this->getDefaultLinkArguments();
156         $defaults = array_merge($defaults, $link_defaults);
157     
158         $args = $this->getArgs($argstr, $request, $defaults);
159         $plugin = $this->getName();
160     
161         $query_args = array();
162         foreach ($args as $arg => $val) {
163             if (isset($link_defaults[$arg]))
164                 continue;
165             if ($val != $defaults[$arg])
166                 $query_args[$arg] = $val;
167         }
168     
169         $link = Button($query_args, $args['linktext'], $args['targetpage']);
170         if (!empty($args['description']))
171             $link->addTooltip($args['description']);
172     
173         return $link;
174     }
175     
176     function getDefaultFormArguments() {
177         return array('targetpage' => $this->getName(),
178                      'buttontext' => $this->getName(),
179                      'class' => 'wikiaction',
180                      'method' => 'get',
181                      'textinput' => 's',
182                      'description' => $this->getDescription(),
183                      'formsize' => 30);
184     }
185     
186     function makeForm($argstr, $request) {
187         $form_defaults = $this->getDefaultFormArguments();
188         $defaults = array_merge($this->getDefaultArguments(),
189                                 $form_defaults);
190     
191         $args = $this->getArgs($argstr, $request, $defaults);
192         $plugin = $this->getName();
193         $textinput = $args['textinput'];
194         assert(!empty($textinput) && isset($args['textinput']));
195     
196         $form = HTML::form(array('action' => WikiURL($args['targetpage']),
197                                  'method' => $args['method'],
198                                  'class' => $args['class'],
199                                  'accept-charset' => CHARSET));
200         if (! USE_PATH_INFO ) {
201             $pagename = $request->get('pagename');
202             $form->pushContent(HTML::input(array('type' => 'hidden', 'name' => 'pagename', 
203                                                  'value' => $args['targetpage'])));
204         }
205         $contents = HTML::div();
206         $contents->setAttr('class', $args['class']);
207     
208         foreach ($args as $arg => $val) {
209             if (isset($form_defaults[$arg]))
210                 continue;
211             if ($arg != $textinput && $val == $defaults[$arg])
212                 continue;
213     
214             $i = HTML::input(array('name' => $arg, 'value' => $val));
215     
216             if ($arg == $textinput) {
217                 //if ($inputs[$arg] == 'file')
218                 //    $attr['type'] = 'file';
219                 //else
220                 $i->setAttr('type', 'text');
221                 $i->setAttr('size', $args['formsize']);
222                 if ($args['description'])
223                     $i->addTooltip($args['description']);
224             }
225             else {
226                 $i->setAttr('type', 'hidden');
227             }
228             $contents->pushContent($i);
229     
230             // FIXME: hackage
231             if ($i->getAttr('type') == 'file') {
232                 $form->setAttr('enctype', 'multipart/form-data');
233                 $form->setAttr('method', 'post');
234                 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
235                                                          'value' => MAX_UPLOAD_SIZE,
236                                                          'type' => 'hidden')));
237             }
238         }
239     
240         if (!empty($args['buttontext']))
241             $contents->pushContent(HTML::input(array('type' => 'submit',
242                                                      'class' => 'button',
243                                                      'value' => $args['buttontext'])));
244     
245         $form->pushContent($contents);
246         return $form;
247     }
248     
249     function error ($message) {
250         return HTML::div(array('class' => 'errors'),
251                         HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
252                         $message);
253     }
254
255     function disabled ($message='') {
256         $html[] = HTML::div(array('class' => 'title'),
257                             fmt("Plugin %s disabled.", $this->getName()),
258                             ' ', $message);
259         $html[] = HTML::pre($this->_pi);
260         return HTML::div(array('class' => 'disabled-plugin'), $html);
261     }
262 }
263
264 class WikiPluginLoader {
265     var $_errors;
266
267     function expandPI($pi, &$request) {
268         if (!preg_match('/^\s*<\?(plugin(?:-form|-link)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
269             return $this->_error(sprintf("Bad %s", 'PI'));
270
271         list(, $pi_name, $plugin_name, $plugin_args) = $m;
272         $plugin = $this->getPlugin($plugin_name, $pi);
273         if (!is_object($plugin)) {
274             return new HtmlElement($pi_name == 'plugin-link' ? 'span' : 'p',
275                                    array('class' => 'plugin-error'),
276                                    $this->getErrorDetail());
277         }
278         switch ($pi_name) {
279             case 'plugin':
280                 // FIXME: change API for run() (no $dbi needed).
281                 $dbi = $request->getDbh();
282                 // FIXME: could do better here...
283                 if (! $plugin->hasStaticOutput()) {
284                     // Output of plugin (potentially) depends on
285                     // the state of the WikiDB (other than the current
286                     // page.)  Individual plugins should try to add
287                     // there own bits to ETag --- but for now, we'll
288                     // at least mark ETag as a weak validator.
289                     $request->setETagIsWeak();
290                 }
291                 
292                 return $plugin->run($dbi, $plugin_args, $request);
293             case 'plugin-link':
294                 return $plugin->makeLink($plugin_args, $request);
295             case 'plugin-form':
296                 return $plugin->makeForm($plugin_args, $request);
297         }
298     }
299
300     // Special treatment. Only called by Template.php:GeneratePage
301     function expandPI_head($pi, $request) {
302         if (!preg_match('/^\s*<\?plugin-head\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
303             return $this->_error(sprintf("Bad %s", 'PI'));
304
305         list(, $plugin_name, $plugin_args) = $m;
306         $plugin = $this->getPlugin($plugin_name);
307         if (!is_object($plugin)) {
308             return new HtmlElement('p', array('class' => 'plugin-error'),
309                                    $this->getErrorDetail());
310         }
311         $dbi = $request->getDbh();
312         return $plugin->run($dbi, $plugin_args, $request);
313     }
314
315     function getPlugin($plugin_name, $pi) {
316         global $ErrorManager;
317
318         // Note that there seems to be no way to trap parse errors
319         // from this include.  (At least not via set_error_handler().)
320         $plugin_source = "lib/plugin/$plugin_name.php";
321
322         $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
323         $include_failed = !include_once("lib/plugin/$plugin_name.php");
324         $ErrorManager->popErrorHandler();
325
326         $plugin_class = "WikiPlugin_$plugin_name";
327         if (!class_exists($plugin_class)) {
328             if ($include_failed)
329                 return $this->_error(sprintf(_("Include of '%s' failed"),
330                                              $plugin_source));
331             return $this->_error(sprintf("%s: no such class", $plugin_class));
332         }
333
334         $plugin = new $plugin_class;
335         if (!is_subclass_of($plugin, "WikiPlugin"))
336             return $this->_error(sprintf("%s: not a subclass of WikiPlugin",
337                                          $plugin_class));
338
339         $plugin->_pi = $pi;
340         return $plugin;
341     }
342
343     function _plugin_error_filter ($err) {
344         if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
345             return true;        // Ignore this error --- it's expected.
346         return false;
347     }
348
349     function getErrorDetail() {
350         return $this->_errors;
351     }
352
353     function _error($message) {
354         $this->_errors = $message;
355         return false;
356     }
357
358
359 };
360
361 // (c-file-style: "gnu")
362 // Local Variables:
363 // mode: php
364 // tab-width: 8
365 // c-basic-offset: 4
366 // c-hanging-comment-ender-p: nil
367 // indent-tabs-mode: nil
368 // End:
369 ?>