2 rcs_id('$Id: WikiPlugin.php,v 1.34 2004-01-26 09:17:48 rurban 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.34 $");
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 if (!empty($defaults))
100 foreach ($defaults as $arg => $default_val) {
101 if (isset($argstr_args[$arg]))
102 $args[$arg] = $argstr_args[$arg];
103 elseif ( $request and ($argval = $request->getArg($arg)) !== false )
104 $args[$arg] = $argval;
105 elseif (isset($argstr_defaults[$arg]))
106 $args[$arg] = (string) $argstr_defaults[$arg];
108 $args[$arg] = $default_val;
111 $args[$arg] = $this->expandArg($args[$arg], $request);
113 unset($argstr_args[$arg]);
114 unset($argstr_defaults[$arg]);
117 foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
118 trigger_error(sprintf(_("argument '%s' not declared by plugin"),
119 $arg), E_USER_NOTICE);
125 function expandArg($argval, $request) {
126 return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
130 function parseArgStr($argstr) {
132 $op_p = '(?:\|\|)?=';
135 $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
136 //"<--kludge for brain-dead syntax coloring
137 $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
138 $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
139 $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
144 while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
145 @ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
146 $argstr = substr($argstr, strlen($m[0]));
148 // Remove quotes from string values.
150 $val = stripslashes($qq_val);
152 $val = stripslashes($q_val);
154 $val = _(stripslashes($gt_val));
162 // NOTE: This does work for multiple args. Use the
163 // separator character defined in your webserver
164 // configuration, usually & or & (See
165 // http://www.htmlhelp.com/faq/cgifaq.4.html)
166 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
167 // url: RecentChanges?days=1&show_all=1&show_minor=0
168 assert($op == '||=');
169 $defaults[$arg] = $val;
174 $this->handle_plugin_args_cruft($argstr, $args);
177 return array($args, $defaults);
180 /* A plugin can override this function to define how any remaining text is handled */
181 function handle_plugin_args_cruft($argstr, $args) {
182 trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),
183 $argstr), E_USER_NOTICE);
186 function getDefaultLinkArguments() {
187 return array('targetpage' => $this->getName(),
188 'linktext' => $this->getName(),
189 'description' => $this->getDescription(),
190 'class' => 'wikiaction');
193 function makeLink($argstr, $request) {
194 $defaults = $this->getDefaultArguments();
195 $link_defaults = $this->getDefaultLinkArguments();
196 $defaults = array_merge($defaults, $link_defaults);
198 $args = $this->getArgs($argstr, $request, $defaults);
199 $plugin = $this->getName();
201 $query_args = array();
202 foreach ($args as $arg => $val) {
203 if (isset($link_defaults[$arg]))
205 if ($val != $defaults[$arg])
206 $query_args[$arg] = $val;
209 $link = Button($query_args, $args['linktext'], $args['targetpage']);
210 if (!empty($args['description']))
211 $link->addTooltip($args['description']);
216 function getDefaultFormArguments() {
217 return array('targetpage' => $this->getName(),
218 'buttontext' => $this->getName(),
219 'class' => 'wikiaction',
222 'description' => $this->getDescription(),
226 function makeForm($argstr, $request) {
227 $form_defaults = $this->getDefaultFormArguments();
228 $defaults = array_merge($this->getDefaultArguments(),
231 $args = $this->getArgs($argstr, $request, $defaults);
232 $plugin = $this->getName();
233 $textinput = $args['textinput'];
234 assert(!empty($textinput) && isset($args['textinput']));
236 $form = HTML::form(array('action' => WikiURL($args['targetpage']),
237 'method' => $args['method'],
238 'class' => $args['class'],
239 'accept-charset' => CHARSET));
240 if (! USE_PATH_INFO ) {
241 $pagename = $request->get('pagename');
242 $form->pushContent(HTML::input(array('type' => 'hidden', 'name' => 'pagename',
243 'value' => $args['targetpage'])));
245 $contents = HTML::div();
246 $contents->setAttr('class', $args['class']);
248 foreach ($args as $arg => $val) {
249 if (isset($form_defaults[$arg]))
251 if ($arg != $textinput && $val == $defaults[$arg])
254 $i = HTML::input(array('name' => $arg, 'value' => $val));
256 if ($arg == $textinput) {
257 //if ($inputs[$arg] == 'file')
258 // $attr['type'] = 'file';
260 $i->setAttr('type', 'text');
261 $i->setAttr('size', $args['formsize']);
262 if ($args['description'])
263 $i->addTooltip($args['description']);
266 $i->setAttr('type', 'hidden');
268 $contents->pushContent($i);
271 if ($i->getAttr('type') == 'file') {
272 $form->setAttr('enctype', 'multipart/form-data');
273 $form->setAttr('method', 'post');
274 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
275 'value' => MAX_UPLOAD_SIZE,
276 'type' => 'hidden')));
280 if (!empty($args['buttontext']))
281 $contents->pushContent(HTML::input(array('type' => 'submit',
283 'value' => $args['buttontext'])));
285 $form->pushContent($contents);
289 function error ($message) {
290 return HTML::div(array('class' => 'errors'),
291 HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
295 function disabled ($message='') {
296 $html[] = HTML::div(array('class' => 'title'),
297 fmt("Plugin %s disabled.", $this->getName()),
299 $html[] = HTML::pre($this->_pi);
300 return HTML::div(array('class' => 'disabled-plugin'), $html);
304 class WikiPluginLoader {
307 function expandPI($pi, &$request, $basepage=false) {
308 if (!($ppi = $this->parsePi($pi)))
310 list($pi_name, $plugin, $plugin_args) = $ppi;
312 if (!is_object($plugin)) {
313 return new HtmlElement($pi_name == 'plugin-link' ? 'span' : 'p',
314 array('class' => 'plugin-error'),
315 $this->getErrorDetail());
319 // FIXME: change API for run() (no $dbi needed).
320 $dbi = $request->getDbh();
321 // FIXME: could do better here...
322 if (! $plugin->managesValidators()) {
323 // Output of plugin (potentially) depends on
324 // the state of the WikiDB (other than the current
327 // Lacking other information, we'll assume things
328 // changed last time the wikidb was touched.
330 // As an additional hack, mark the ETag weak, since,
331 // for all we know, the page might depend
332 // on things other than the WikiDB (e.g. PhpWeather,
335 $timestamp = $dbi->getTimestamp();
336 $request->appendValidators(array('dbi_timestamp' => $timestamp,
337 '%mtime' => (int)$timestamp,
340 return $plugin->run($dbi, $plugin_args, $request, $basepage);
342 return $plugin->makeLink($plugin_args, $request);
344 return $plugin->makeForm($plugin_args, $request);
348 function getWikiPageLinks($pi, $basepage) {
349 if (!($ppi = $this->parsePi($pi)))
351 list($pi_name, $plugin, $plugin_args) = $ppi;
352 if (!is_object($plugin))
354 if ($pi_name != 'plugin')
356 return $plugin->getWikiPageLinks($plugin_args, $basepage);
359 function parsePI($pi) {
360 if (!preg_match('/^\s*<\?(plugin(?:-form|-link)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
361 return $this->_error(sprintf("Bad %s", 'PI'));
363 list(, $pi_name, $plugin_name, $plugin_args) = $m;
364 $plugin = $this->getPlugin($plugin_name, $pi);
366 return array($pi_name, $plugin, $plugin_args);
369 function getPlugin($plugin_name, $pi) {
370 global $ErrorManager;
372 // Note that there seems to be no way to trap parse errors
373 // from this include. (At least not via set_error_handler().)
374 $plugin_source = "lib/plugin/$plugin_name.php";
376 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
377 // $include_failed = !@include_once("lib/plugin/$plugin_name.php");
378 $include_failed = !include_once("lib/plugin/$plugin_name.php");
379 $ErrorManager->popErrorHandler();
381 $plugin_class = "WikiPlugin_$plugin_name";
382 if (!class_exists($plugin_class)) {
384 return $this->_error(sprintf(_("Include of '%s' failed"),
386 return $this->_error(sprintf("%s: no such class", $plugin_class));
389 $plugin = new $plugin_class;
390 if (!is_subclass_of($plugin, "WikiPlugin"))
391 return $this->_error(sprintf("%s: not a subclass of WikiPlugin",
398 function _plugin_error_filter ($err) {
399 if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
400 return true; // Ignore this error --- it's expected.
404 function getErrorDetail() {
405 return $this->_errors;
408 function _error($message) {
409 $this->_errors = $message;
414 // (c-file-style: "gnu")
419 // c-hanging-comment-ender-p: nil
420 // indent-tabs-mode: nil