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