2 rcs_id('$Id: WikiPlugin.php,v 1.30 2003-02-16 20:04:46 dairiki Exp $');
6 function getDefaultArguments() {
7 return array('description' => $this->getDescription());
10 /** Does the plugin manage its own HTTP validators?
12 * This should be overwritten by (some) individual plugins.
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.
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.
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
28 function managesValidators() {
33 function run ($dbi, $argstr, &$request) {
34 trigger_error("WikiPlugin::run: pure virtual function",
41 * This is used (by default) by getDefaultLinkArguments and
42 * getDefaultFormArguments to compute the default link/form
45 * If you want to gettextify the name (probably a good idea),
46 * override this method in your plugin class, like:
48 * function getName() { return _("MyPlugin"); }
51 * @return string plugin name/target.
54 return preg_replace('/^.*_/', '', get_class($this));
57 function getDescription() {
58 return $this->getName();
62 function getArgs($argstr, $request, $defaults = false) {
63 if ($defaults === false)
64 $defaults = $this->getDefaultArguments();
66 list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
68 foreach ($defaults as $arg => $default_val) {
69 if (isset($argstr_args[$arg]))
70 $args[$arg] = $argstr_args[$arg];
71 elseif ( ($argval = $request->getArg($arg)) !== false )
72 $args[$arg] = $argval;
73 elseif (isset($argstr_defaults[$arg]))
74 $args[$arg] = (string) $argstr_defaults[$arg];
76 $args[$arg] = $default_val;
78 $args[$arg] = $this->expandArg($args[$arg], $request);
80 unset($argstr_args[$arg]);
81 unset($argstr_defaults[$arg]);
84 foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
85 trigger_error(sprintf(_("argument '%s' not declared by plugin"),
86 $arg), E_USER_NOTICE);
92 function expandArg($argval, $request) {
93 return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
98 function parseArgStr($argstr) {
100 $op_p = '(?:\|\|)?=';
103 $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
104 //"<--kludge for brain-dead syntax coloring
105 $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
106 $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
107 $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
112 while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
113 @ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
114 $argstr = substr($argstr, strlen($m[0]));
116 // Remove quotes from string values.
118 $val = stripslashes($qq_val);
120 $val = stripslashes($q_val);
122 $val = _(stripslashes($gt_val));
130 // NOTE: This does work for multiple args. Use the
131 // separator character defined in your webserver
132 // configuration, usually & or & (See
133 // http://www.htmlhelp.com/faq/cgifaq.4.html)
134 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
135 // url: RecentChanges?days=1&show_all=1&show_minor=0
136 assert($op == '||=');
137 $defaults[$arg] = $val;
142 $this->handle_plugin_args_cruft($argstr, $args);
145 return array($args, $defaults);
148 /* A plugin can override this function to define how any remaining text is handled */
149 function handle_plugin_args_cruft($argstr, $args) {
150 trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),
151 $argstr), E_USER_NOTICE);
154 function getDefaultLinkArguments() {
155 return array('targetpage' => $this->getName(),
156 'linktext' => $this->getName(),
157 'description' => $this->getDescription(),
158 'class' => 'wikiaction');
161 function makeLink($argstr, $request) {
162 $defaults = $this->getDefaultArguments();
163 $link_defaults = $this->getDefaultLinkArguments();
164 $defaults = array_merge($defaults, $link_defaults);
166 $args = $this->getArgs($argstr, $request, $defaults);
167 $plugin = $this->getName();
169 $query_args = array();
170 foreach ($args as $arg => $val) {
171 if (isset($link_defaults[$arg]))
173 if ($val != $defaults[$arg])
174 $query_args[$arg] = $val;
177 $link = Button($query_args, $args['linktext'], $args['targetpage']);
178 if (!empty($args['description']))
179 $link->addTooltip($args['description']);
184 function getDefaultFormArguments() {
185 return array('targetpage' => $this->getName(),
186 'buttontext' => $this->getName(),
187 'class' => 'wikiaction',
190 'description' => $this->getDescription(),
194 function makeForm($argstr, $request) {
195 $form_defaults = $this->getDefaultFormArguments();
196 $defaults = array_merge($this->getDefaultArguments(),
199 $args = $this->getArgs($argstr, $request, $defaults);
200 $plugin = $this->getName();
201 $textinput = $args['textinput'];
202 assert(!empty($textinput) && isset($args['textinput']));
204 $form = HTML::form(array('action' => WikiURL($args['targetpage']),
205 'method' => $args['method'],
206 'class' => $args['class'],
207 'accept-charset' => CHARSET));
208 if (! USE_PATH_INFO ) {
209 $pagename = $request->get('pagename');
210 $form->pushContent(HTML::input(array('type' => 'hidden', 'name' => 'pagename',
211 'value' => $args['targetpage'])));
213 $contents = HTML::div();
214 $contents->setAttr('class', $args['class']);
216 foreach ($args as $arg => $val) {
217 if (isset($form_defaults[$arg]))
219 if ($arg != $textinput && $val == $defaults[$arg])
222 $i = HTML::input(array('name' => $arg, 'value' => $val));
224 if ($arg == $textinput) {
225 //if ($inputs[$arg] == 'file')
226 // $attr['type'] = 'file';
228 $i->setAttr('type', 'text');
229 $i->setAttr('size', $args['formsize']);
230 if ($args['description'])
231 $i->addTooltip($args['description']);
234 $i->setAttr('type', 'hidden');
236 $contents->pushContent($i);
239 if ($i->getAttr('type') == 'file') {
240 $form->setAttr('enctype', 'multipart/form-data');
241 $form->setAttr('method', 'post');
242 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
243 'value' => MAX_UPLOAD_SIZE,
244 'type' => 'hidden')));
248 if (!empty($args['buttontext']))
249 $contents->pushContent(HTML::input(array('type' => 'submit',
251 'value' => $args['buttontext'])));
253 $form->pushContent($contents);
257 function error ($message) {
258 return HTML::div(array('class' => 'errors'),
259 HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
263 function disabled ($message='') {
264 $html[] = HTML::div(array('class' => 'title'),
265 fmt("Plugin %s disabled.", $this->getName()),
267 $html[] = HTML::pre($this->_pi);
268 return HTML::div(array('class' => 'disabled-plugin'), $html);
272 class WikiPluginLoader {
275 function expandPI($pi, &$request) {
276 if (!preg_match('/^\s*<\?(plugin(?:-form|-link)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
277 return $this->_error(sprintf("Bad %s", 'PI'));
279 list(, $pi_name, $plugin_name, $plugin_args) = $m;
280 $plugin = $this->getPlugin($plugin_name, $pi);
281 if (!is_object($plugin)) {
282 return new HtmlElement($pi_name == 'plugin-link' ? 'span' : 'p',
283 array('class' => 'plugin-error'),
284 $this->getErrorDetail());
288 // FIXME: change API for run() (no $dbi needed).
289 $dbi = $request->getDbh();
290 // FIXME: could do better here...
291 if (! $plugin->managesValidators()) {
292 // Output of plugin (potentially) depends on
293 // the state of the WikiDB (other than the current
296 // Lacking other information, we'll assume things
297 // changed last time the wikidb was touched.
299 // As an additional hack, mark the ETag weak, since,
300 // for all we know, the page might depend
301 // on things other than the WikiDB (e.g. PhpWeather,
304 $timestamp = $dbi->getTimestamp();
305 $request->appendValidators(array('dbi_timestamp' => $timestamp,
306 '%mtime' => (int)$timestamp,
309 return $plugin->run($dbi, $plugin_args, $request);
311 return $plugin->makeLink($plugin_args, $request);
313 return $plugin->makeForm($plugin_args, $request);
317 // Special treatment. Only called by Template.php:GeneratePage
318 function expandPI_head($pi, $request) {
319 if (!preg_match('/^\s*<\?plugin-head\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
320 return $this->_error(sprintf("Bad %s", 'PI'));
322 list(, $plugin_name, $plugin_args) = $m;
323 $plugin = $this->getPlugin($plugin_name);
324 if (!is_object($plugin)) {
325 return new HtmlElement('p', array('class' => 'plugin-error'),
326 $this->getErrorDetail());
328 $dbi = $request->getDbh();
329 return $plugin->run($dbi, $plugin_args, $request);
332 function getPlugin($plugin_name, $pi) {
333 global $ErrorManager;
335 // Note that there seems to be no way to trap parse errors
336 // from this include. (At least not via set_error_handler().)
337 $plugin_source = "lib/plugin/$plugin_name.php";
339 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
340 $include_failed = !include_once("lib/plugin/$plugin_name.php");
341 $ErrorManager->popErrorHandler();
343 $plugin_class = "WikiPlugin_$plugin_name";
344 if (!class_exists($plugin_class)) {
346 return $this->_error(sprintf(_("Include of '%s' failed"),
348 return $this->_error(sprintf("%s: no such class", $plugin_class));
351 $plugin = new $plugin_class;
352 if (!is_subclass_of($plugin, "WikiPlugin"))
353 return $this->_error(sprintf("%s: not a subclass of WikiPlugin",
360 function _plugin_error_filter ($err) {
361 if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
362 return true; // Ignore this error --- it's expected.
366 function getErrorDetail() {
367 return $this->_errors;
370 function _error($message) {
371 $this->_errors = $message;
376 // (c-file-style: "gnu")
381 // c-hanging-comment-ender-p: nil
382 // indent-tabs-mode: nil