]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiPlugin.php
Added input 'size' arg to plugin-form.
[SourceForge/phpwiki.git] / lib / WikiPlugin.php
1 <?php //-*-php-*-
2 rcs_id('$Id: WikiPlugin.php,v 1.21 2002-02-27 19:04:30 carstenklapp Exp $');
3
4 class WikiPlugin
5 {
6     function getDefaultArguments() {
7         return array();
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
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             trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),
122                                     $argstr), E_USER_NOTICE);
123         }
124
125         return array($args, $defaults);
126     }
127
128
129     function getDefaultLinkArguments() {
130         return array('targetpage'  => $this->getName(),
131                     'linktext'    => $this->getName(),
132                     'description' => $this->getDescription(),
133                     'class'       => 'wikiaction');
134     }
135
136     function makeLink($argstr, $request) {
137         $defaults = $this->getDefaultArguments();
138         $link_defaults = $this->getDefaultLinkArguments();
139         $defaults = array_merge($defaults, $link_defaults);
140     
141         $args = $this->getArgs($argstr, $request, $defaults);
142         $plugin = $this->getName();
143     
144         $query_args = array();
145         foreach ($args as $arg => $val) {
146             if (isset($link_defaults[$arg]))
147                 continue;
148             if ($val != $defaults[$arg])
149                 $query_args[$arg] = $val;
150         }
151     
152         $link = Button($query_args, $args['linktext'], $args['targetpage']);
153         if (!empty($args['description']))
154             $link->addTooltip($args['description']);
155     
156         return $link;
157     }
158     
159     function getDefaultFormArguments() {
160         return array('targetpage'  => $this->getName(),
161                      'buttontext'  => $this->getName(),
162                      'class'       => 'wikiaction',
163                      'method'      => 'get',
164                      'textinput'   => 's',
165                      'description' => $this->getDescription(),
166                      'formsize'    => 'size');
167     }
168     
169     function makeForm($argstr, $request) {
170         $form_defaults = $this->getDefaultFormArguments();
171         $defaults = array_merge($this->getDefaultArguments(),
172                                 $form_defaults);
173     
174         $args = $this->getArgs($argstr, $request, $defaults);
175         $plugin = $this->getName();
176         $textinput = $args['textinput'];
177         assert(!empty($textinput) && isset($args['textinput']));
178     
179         $form = HTML::form(array('action' => WikiURL($args['targetpage']),
180                                 'method' => $args['method'],
181                                 'class'  => $args['class'],
182                                 'accept-charset' => CHARSET));
183         $contents = HTML::div();
184         $contents->setAttr('class', $args['class']);
185     
186         foreach ($args as $arg => $val) {
187             if (isset($form_defaults[$arg]))
188                 continue;
189             if ($arg != $textinput && $val == $defaults[$arg])
190                 continue;
191     
192             $i = HTML::input(array('name' => $arg, 'value' => $val));
193     
194             if ($arg == $textinput) {
195                 //if ($inputs[$arg] == 'file')
196                 //    $attr['type'] = 'file';
197                 //else
198                 $size = $args['size'] > 0 ? $args['size'] : 30;
199                 $i->setAttr('type', 'text');
200                 $i->setAttr('size', $size);
201                 if ($args['description'])
202                     $i->addTooltip($args['description']);
203             }
204             else {
205                 $i->setAttr('type', 'hidden');
206             }
207             $contents->pushContent($i);
208     
209             // FIXME: hackage
210             if ($i->getAttr('type') == 'file') {
211                 $form->setAttr('enctype', 'multipart/form-data');
212                 $form->setAttr('method', 'post');
213                 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
214                                                         'value' => MAX_UPLOAD_SIZE,
215                                                         'type' => 'hidden')));
216             }
217         }
218     
219         if (!empty($args['buttontext']))
220             $contents->pushContent(HTML::input(array('type' => 'submit',
221                                                     'class' => 'button',
222                                                     'value' => $args['buttontext'])));
223     
224         $form->pushContent($contents);
225         return $form;
226     }
227     
228     function error ($message) {
229         return HTML::div(array('class' => 'errors'),
230                         HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
231                         $message);
232     }
233 }
234
235 class WikiPluginLoader {
236     var $_errors;
237
238     function expandPI($pi, $request) {
239         if (!preg_match('/^\s*<\?(plugin(?:-form|-link)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
240             return $this->_error(sprintf("Bad %s", 'PI'));
241
242         list(, $pi_name, $plugin_name, $plugin_args) = $m;
243         $plugin = $this->getPlugin($plugin_name);
244         if (!is_object($plugin)) {
245             return new HtmlElement($pi_name == 'plugin-link' ? 'span' : 'p',
246                                    array('class' => 'plugin-error'),
247                                    $this->getErrorDetail());
248         }
249         switch ($pi_name) {
250             case 'plugin':
251                 // FIXME: change API for run() (no $dbi needed).
252                 $dbi = $request->getDbh();
253                 return $plugin->run($dbi, $plugin_args, $request);
254             case 'plugin-link':
255                 return $plugin->makeLink($plugin_args, $request);
256             case 'plugin-form':
257                 return $plugin->makeForm($plugin_args, $request);
258         }
259     }
260
261     function getPlugin($plugin_name) {
262         global $ErrorManager;
263
264         // Note that there seems to be no way to trap parse errors
265         // from this include.  (At least not via set_error_handler().)
266         $plugin_source = "lib/plugin/$plugin_name.php";
267
268         $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
269         $include_failed = !include_once("lib/plugin/$plugin_name.php");
270         $ErrorManager->popErrorHandler();
271
272         $plugin_class = "WikiPlugin_$plugin_name";
273         if (!class_exists($plugin_class)) {
274             if ($include_failed)
275                 return $this->_error(sprintf(_("Include of '%s' failed"),
276                                              $plugin_source));
277             return $this->_error(sprintf("%s: no such class", $plugin_class));
278         }
279
280         $plugin = new $plugin_class;
281         if (!is_subclass_of($plugin, "WikiPlugin"))
282             return $this->_error(sprintf("%s: not a subclass of WikiPlugin",
283                                          $plugin_class));
284
285         return $plugin;
286     }
287
288     function _plugin_error_filter ($err) {
289         if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
290             return true;        // Ignore this error --- it's expected.
291         return false;
292     }
293
294     function getErrorDetail() {
295         return $this->_errors;
296     }
297
298     function _error($message) {
299         $this->_errors = $message;
300         return false;
301     }
302
303
304 };
305
306 // (c-file-style: "gnu")
307 // Local Variables:
308 // mode: php
309 // tab-width: 8
310 // c-basic-offset: 4
311 // c-hanging-comment-ender-p: nil
312 // indent-tabs-mode: nil
313 // End:
314 ?>