]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiPlugin.php
Big Template cleanup and refactor.
[SourceForge/phpwiki.git] / lib / WikiPlugin.php
1 <?php //-*-php-*-
2 rcs_id('$Id: WikiPlugin.php,v 1.14 2002-01-24 00:45:28 dairiki 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"),$arg),
65                           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")', $argval);
73     }
74     
75         
76     function parseArgStr($argstr) {
77         $arg_p = '\w+';
78         $op_p = '(?:\|\|)?=';
79         $word_p = '\S+';
80         $opt_ws = '\s*';
81         $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
82         $q_p  = "' ( (?:[^'\\\\]|\\\\.)* ) '";
83         $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
84         $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
85
86         $args = array();
87         $defaults = array();
88         
89         while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
90             @ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
91             $argstr = substr($argstr, strlen($m[0]));
92
93             // Remove quotes from string values.
94             if ($qq_val)
95                 $val = stripslashes($qq_val);
96             elseif ($q_val)
97                 $val = stripslashes($q_val);
98             elseif ($gt_val)
99                 $val = gettext(stripslashes($gt_val));
100             else
101                 $val = $word_val;
102
103             if ($op == '=') {
104                 $args[$arg] = $val;
105             }
106             else {
107                 // NOTE: This does work for multiple args. Use the
108                 // separator character defined in your webserver
109                 // configuration, usually & or &amp; (See
110                 // http://www.htmlhelp.com/faq/cgifaq.4.html)
111                 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
112                 // url: RecentChanges?days=1&show_all=1&show_minor=0
113                 assert($op == '||=');
114                 $defaults[$arg] = $val;
115             }
116         }
117         
118         if ($argstr) {
119             trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),$argstr), E_USER_WARNING);
120         }
121
122         return array($args, $defaults);
123     }
124     
125
126     function getDefaultLinkArguments() {
127         return array('targetpage' => $this->getName(),
128                      'linktext' => $this->getName(),
129                      'description' => $this->getDescription(),
130                      'class' => 'wikiaction');
131     }
132     
133     function makeLink($argstr, $request) {
134         $defaults = $this->getDefaultArguments();
135         $link_defaults = $this->getDefaultLinkArguments();
136         $defaults = array_merge($defaults, $link_defaults);
137
138         $args = $this->getArgs($argstr, $request, $defaults);
139         $plugin = $this->getName();
140         
141         $query_args = array();
142         foreach ($args as $arg => $val) {
143             if (isset($link_defaults[$arg]))
144                 continue;
145             if ($val != $defaults[$arg])
146                 $query_args[$arg] = $val;
147         }
148
149         global $Theme;
150         $link = $Theme->makeButton($args['linktext'],
151                                    WikiURL($args['targetpage'], $query_args),
152                                    $args['class']);
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' => 30);
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::td();
184         
185         foreach ($args as $arg => $val) {
186             if (isset($form_defaults[$arg]))
187                 continue;
188             if ($arg != $textinput && $val == $defaults[$arg])
189                 continue;
190
191             $i = HTML::input(array('name' => $arg, 'value' => $val));
192             
193             if ($arg == $textinput) {
194                 //if ($inputs[$arg] == 'file')
195                 //    $attr['type'] = 'file';
196                 //else
197                 $i->setAttr('type', 'text');
198                 $i->setAttr('size', $args['formsize']);
199                 if ($args['description'])
200                     $i->addTooltip($args['description']);
201             }
202             else {
203                 $i->setAttr('type', 'hidden');
204             }
205             $contents->pushContent($i);
206
207             // FIXME: hackage
208             if ($i->getAttr('type') == 'file') {
209                 $form->setAttr('enctype', 'multipart/form-data');
210                 $form->setAttr('method', 'post');
211                 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
212                                                          'value' => MAX_UPLOAD_SIZE,
213                                                          'type' => 'hidden')));
214             }
215         }
216
217         if (!empty($args['buttontext']))
218             $contents->pushContent(HTML::input(array('type' => 'submit',
219                                                      'class' => 'button',
220                                                      'value' => $args['buttontext'])));
221
222         //FIXME: can we do without this table?
223         $form->pushContent(HTML::table(HTML::tr($contents)));
224         return $form;
225     }
226 }
227
228 class WikiPluginLoader {
229     var $_errors;
230     
231     function expandPI($pi, $request) {
232         if (!preg_match('/^\s*<\?(plugin(?:-form|-link)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
233             return $this->_error(sprintf(_("Bad %s"),'PI'));
234
235         list(, $pi_name, $plugin_name, $plugin_args) = $m;
236         $plugin = $this->getPlugin($plugin_name);
237         if (!is_object($plugin)) {
238             return new HtmlElement($pi_name == 'plugin-link' ? 'span' : 'p',
239                                    array('class' => 'plugin-error'),
240                                    $this->getErrorDetail());
241         }
242         switch ($pi_name) {
243         case 'plugin':
244             // FIXME: change API for run() (no $dbi needed).
245             $dbi = $request->getDbh();
246             return $plugin->run($dbi, $plugin_args, $request);
247         case 'plugin-link':
248             return $plugin->makeLink($plugin_args, $request);
249         case 'plugin-form':
250             return $plugin->makeForm($plugin_args, $request);
251         }
252     }
253     
254     function getPlugin($plugin_name) {
255
256         // Note that there seems to be no way to trap parse errors
257         // from this include.  (At least not via set_error_handler().)
258         $plugin_source = "lib/plugin/$plugin_name.php";
259         
260         if (!include_once("lib/plugin/$plugin_name.php")) {
261             if (!empty($GLOBALS['php_errormsg']))
262                 return $this->_error($GLOBALS['php_errormsg']);
263             // If the plugin source has already been included, the include_once()
264             // will fail, so we don't want to crap out just yet.
265             $include_failed = true;
266         } else {
267             // this avoids: lib/WikiPlugin.php:265: Notice[8]: Undefined variable: include_failed
268             $include_failed = false;
269         }
270         
271         $plugin_class = "WikiPlugin_$plugin_name";
272         if (!class_exists($plugin_class)) {
273             if ($include_failed)
274                 return $this->_error(sprintf(_("Include of '%s' failed"),$plugin_source));
275             return $this->_error(sprintf(_("%s: no such class"),$plugin_class));
276         }
277         
278     
279         $plugin = new $plugin_class;
280         if (!is_subclass_of($plugin, "WikiPlugin"))
281             return $this->_error(sprintf(_("%s: not a subclass of WikiPlugin"),$plugin_class));
282
283         return $plugin;
284     }
285
286     function getErrorDetail() {
287         return $this->_errors;
288     }
289     
290     function _error($message) {
291         $this->_errors = $message;
292         return false;
293     }
294
295         
296 };
297
298 // (c-file-style: "gnu")
299 // Local Variables:
300 // mode: php
301 // tab-width: 8
302 // c-basic-offset: 4
303 // c-hanging-comment-ender-p: nil
304 // indent-tabs-mode: nil
305 // End:   
306 ?>