2 rcs_id('$Id: WikiPlugin.php,v 1.33 2003-09-13 22:20:55 carstenklapp 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, $basepage) {
34 trigger_error("WikiPlugin::run: pure virtual function",
38 /** Get wiki-pages linked to by plugin invocation.
40 * A plugin may override this method to add pages to the
41 * link database for the invoking page.
43 * For example, the IncludePage plugin should override this so
44 * that the including page shows up in the backlinks list for the
47 * Not all plugins which generate links to wiki-pages need list
50 * Note also that currently the links are calculated at page save
51 * time, so only static page links (e.g. those dependent on the PI
52 * args, not the rest of the wikidb state or any request query args)
53 * will work correctly here.
55 * @param string $argstr The plugin argument string.
56 * @param string $basepage The pagename the plugin is invoked from.
57 * @return array List of pagenames linked to (or false).
59 function getWikiPageLinks ($argstr, $basepage) {
66 * This is used (by default) by getDefaultLinkArguments and
67 * getDefaultFormArguments to compute the default link/form
70 * If you want to gettextify the name (probably a good idea),
71 * override this method in your plugin class, like:
73 * function getName() { return _("MyPlugin"); }
76 * @return string plugin name/target.
79 return preg_replace('/^.*_/', '', get_class($this));
82 function getDescription() {
83 return $this->getName();
86 // plugins should override this with the commented-out code
87 function getVersion() {
89 //return preg_replace("/[Revision: $]/", '',
90 // "\$Revision: 1.33 $");
93 function getArgs($argstr, $request=false, $defaults = false) {
94 if ($defaults === false)
95 $defaults = $this->getDefaultArguments();
97 list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
99 foreach ($defaults as $arg => $default_val) {
100 if (isset($argstr_args[$arg]))
101 $args[$arg] = $argstr_args[$arg];
102 elseif ( $request and ($argval = $request->getArg($arg)) !== false )
103 $args[$arg] = $argval;
104 elseif (isset($argstr_defaults[$arg]))
105 $args[$arg] = (string) $argstr_defaults[$arg];
107 $args[$arg] = $default_val;
110 $args[$arg] = $this->expandArg($args[$arg], $request);
112 unset($argstr_args[$arg]);
113 unset($argstr_defaults[$arg]);
116 foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
117 trigger_error(sprintf(_("argument '%s' not declared by plugin"),
118 $arg), E_USER_NOTICE);
124 function expandArg($argval, $request) {
125 return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
129 function parseArgStr($argstr) {
131 $op_p = '(?:\|\|)?=';
134 $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
135 //"<--kludge for brain-dead syntax coloring
136 $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
137 $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
138 $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
143 while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
144 @ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
145 $argstr = substr($argstr, strlen($m[0]));
147 // Remove quotes from string values.
149 $val = stripslashes($qq_val);
151 $val = stripslashes($q_val);
153 $val = _(stripslashes($gt_val));
161 // NOTE: This does work for multiple args. Use the
162 // separator character defined in your webserver
163 // configuration, usually & or & (See
164 // http://www.htmlhelp.com/faq/cgifaq.4.html)
165 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
166 // url: RecentChanges?days=1&show_all=1&show_minor=0
167 assert($op == '||=');
168 $defaults[$arg] = $val;
173 $this->handle_plugin_args_cruft($argstr, $args);
176 return array($args, $defaults);
179 /* A plugin can override this function to define how any remaining text is handled */
180 function handle_plugin_args_cruft($argstr, $args) {
181 trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),
182 $argstr), E_USER_NOTICE);
185 function getDefaultLinkArguments() {
186 return array('targetpage' => $this->getName(),
187 'linktext' => $this->getName(),
188 'description' => $this->getDescription(),
189 'class' => 'wikiaction');
192 function makeLink($argstr, $request) {
193 $defaults = $this->getDefaultArguments();
194 $link_defaults = $this->getDefaultLinkArguments();
195 $defaults = array_merge($defaults, $link_defaults);
197 $args = $this->getArgs($argstr, $request, $defaults);
198 $plugin = $this->getName();
200 $query_args = array();
201 foreach ($args as $arg => $val) {
202 if (isset($link_defaults[$arg]))
204 if ($val != $defaults[$arg])
205 $query_args[$arg] = $val;
208 $link = Button($query_args, $args['linktext'], $args['targetpage']);
209 if (!empty($args['description']))
210 $link->addTooltip($args['description']);
215 function getDefaultFormArguments() {
216 return array('targetpage' => $this->getName(),
217 'buttontext' => $this->getName(),
218 'class' => 'wikiaction',
221 'description' => $this->getDescription(),
225 function makeForm($argstr, $request) {
226 $form_defaults = $this->getDefaultFormArguments();
227 $defaults = array_merge($this->getDefaultArguments(),
230 $args = $this->getArgs($argstr, $request, $defaults);
231 $plugin = $this->getName();
232 $textinput = $args['textinput'];
233 assert(!empty($textinput) && isset($args['textinput']));
235 $form = HTML::form(array('action' => WikiURL($args['targetpage']),
236 'method' => $args['method'],
237 'class' => $args['class'],
238 'accept-charset' => CHARSET));
239 if (! USE_PATH_INFO ) {
240 $pagename = $request->get('pagename');
241 $form->pushContent(HTML::input(array('type' => 'hidden', 'name' => 'pagename',
242 'value' => $args['targetpage'])));
244 $contents = HTML::div();
245 $contents->setAttr('class', $args['class']);
247 foreach ($args as $arg => $val) {
248 if (isset($form_defaults[$arg]))
250 if ($arg != $textinput && $val == $defaults[$arg])
253 $i = HTML::input(array('name' => $arg, 'value' => $val));
255 if ($arg == $textinput) {
256 //if ($inputs[$arg] == 'file')
257 // $attr['type'] = 'file';
259 $i->setAttr('type', 'text');
260 $i->setAttr('size', $args['formsize']);
261 if ($args['description'])
262 $i->addTooltip($args['description']);
265 $i->setAttr('type', 'hidden');
267 $contents->pushContent($i);
270 if ($i->getAttr('type') == 'file') {
271 $form->setAttr('enctype', 'multipart/form-data');
272 $form->setAttr('method', 'post');
273 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
274 'value' => MAX_UPLOAD_SIZE,
275 'type' => 'hidden')));
279 if (!empty($args['buttontext']))
280 $contents->pushContent(HTML::input(array('type' => 'submit',
282 'value' => $args['buttontext'])));
284 $form->pushContent($contents);
288 function error ($message) {
289 return HTML::div(array('class' => 'errors'),
290 HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
294 function disabled ($message='') {
295 $html[] = HTML::div(array('class' => 'title'),
296 fmt("Plugin %s disabled.", $this->getName()),
298 $html[] = HTML::pre($this->_pi);
299 return HTML::div(array('class' => 'disabled-plugin'), $html);
303 class WikiPluginLoader {
306 function expandPI($pi, &$request, $basepage=false) {
307 if (!($ppi = $this->parsePi($pi)))
309 list($pi_name, $plugin, $plugin_args) = $ppi;
311 if (!is_object($plugin)) {
312 return new HtmlElement($pi_name == 'plugin-link' ? 'span' : 'p',
313 array('class' => 'plugin-error'),
314 $this->getErrorDetail());
318 // FIXME: change API for run() (no $dbi needed).
319 $dbi = $request->getDbh();
320 // FIXME: could do better here...
321 if (! $plugin->managesValidators()) {
322 // Output of plugin (potentially) depends on
323 // the state of the WikiDB (other than the current
326 // Lacking other information, we'll assume things
327 // changed last time the wikidb was touched.
329 // As an additional hack, mark the ETag weak, since,
330 // for all we know, the page might depend
331 // on things other than the WikiDB (e.g. PhpWeather,
334 $timestamp = $dbi->getTimestamp();
335 $request->appendValidators(array('dbi_timestamp' => $timestamp,
336 '%mtime' => (int)$timestamp,
339 return $plugin->run($dbi, $plugin_args, $request, $basepage);
341 return $plugin->makeLink($plugin_args, $request);
343 return $plugin->makeForm($plugin_args, $request);
347 function getWikiPageLinks($pi, $basepage) {
348 if (!($ppi = $this->parsePi($pi)))
350 list($pi_name, $plugin, $plugin_args) = $ppi;
351 if (!is_object($plugin))
353 if ($pi_name != 'plugin')
355 return $plugin->getWikiPageLinks($plugin_args, $basepage);
358 function parsePI($pi) {
359 if (!preg_match('/^\s*<\?(plugin(?:-form|-link)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
360 return $this->_error(sprintf("Bad %s", 'PI'));
362 list(, $pi_name, $plugin_name, $plugin_args) = $m;
363 $plugin = $this->getPlugin($plugin_name, $pi);
365 return array($pi_name, $plugin, $plugin_args);
368 function getPlugin($plugin_name, $pi) {
369 global $ErrorManager;
371 // Note that there seems to be no way to trap parse errors
372 // from this include. (At least not via set_error_handler().)
373 $plugin_source = "lib/plugin/$plugin_name.php";
375 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
376 // $include_failed = !@include_once("lib/plugin/$plugin_name.php");
377 $include_failed = !include_once("lib/plugin/$plugin_name.php");
378 $ErrorManager->popErrorHandler();
380 $plugin_class = "WikiPlugin_$plugin_name";
381 if (!class_exists($plugin_class)) {
383 return $this->_error(sprintf(_("Include of '%s' failed"),
385 return $this->_error(sprintf("%s: no such class", $plugin_class));
388 $plugin = new $plugin_class;
389 if (!is_subclass_of($plugin, "WikiPlugin"))
390 return $this->_error(sprintf("%s: not a subclass of WikiPlugin",
397 function _plugin_error_filter ($err) {
398 if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
399 return true; // Ignore this error --- it's expected.
403 function getErrorDetail() {
404 return $this->_errors;
407 function _error($message) {
408 $this->_errors = $message;
413 // (c-file-style: "gnu")
418 // c-hanging-comment-ender-p: nil
419 // indent-tabs-mode: nil