3 abstract class WikiPlugin
7 function getDefaultArguments()
9 return array('description' => $this->getDescription());
12 /** Does the plugin manage its own HTTP validators?
14 * This should be overwritten by (some) individual plugins.
16 * If the output of the plugin is static, depending only
17 * on the plugin arguments, query arguments and contents
18 * of the current page, this can (and should) return true.
20 * If the plugin can deduce a modification time, or equivalent
21 * sort of tag for it's content, then the plugin should
22 * call $request->appendValidators() with appropriate arguments,
23 * and should override this method to return true.
25 * When in doubt, the safe answer here is false.
26 * Unfortunately, returning false here will most likely make
27 * any page which invokes the plugin uncacheable (by HTTP proxies
30 function managesValidators()
37 * @param string $argstr
38 * @param WikiRequest $request
39 * @param string $basepage
42 abstract public function run($dbi, $argstr, &$request, $basepage);
44 /** Get wiki-pages linked to by plugin invocation.
46 * A plugin may override this method to add pages to the
47 * link database for the invoking page.
49 * For example, the IncludePage plugin should override this so
50 * that the including page shows up in the backlinks list for the
53 * Not all plugins which generate links to wiki-pages need list
56 * Note also that currently the links are calculated at page save
57 * time, so only static page links (e.g. those dependent on the PI
58 * args, not the rest of the wikidb state or any request query args)
59 * will work correctly here.
61 * @param string $argstr The plugin argument string.
62 * @param string $basepage The pagename the plugin is invoked from.
63 * @return array List of pagenames linked to (or false).
65 function getWikiPageLinks($argstr, $basepage)
73 * This is used (by default) by getDefaultLinkArguments and
74 * getDefaultFormArguments to compute the default link/form
77 * If you override this method in your plugin class,
78 * you MUST NOT translate the name.
80 * function getName() { return "MyPlugin"; }
83 * @return string plugin name/target.
87 return preg_replace('/^WikiPlugin_/', '', get_class($this));
91 * Get description of plugin.
93 * This method should be overriden in your plugin class, like:
95 * function getDescription() { return _("MyPlugin does this..."); }
98 * @return string plugin description
101 abstract protected function getDescription();
104 * @param string $argstr
105 * @param WikiRequest $request
106 * @param array $defaults
109 function getArgs($argstr, $request = false, $defaults = array())
111 if (empty($defaults)) {
112 $defaults = $this->getDefaultArguments();
114 //Fixme: on POST argstr is empty
115 list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
117 if (!empty($defaults))
118 foreach ($defaults as $arg => $default_val) {
119 if (isset($argstr_args[$arg])) {
120 $args[$arg] = $argstr_args[$arg];
121 } elseif ($request and ($argval = $request->getArg($arg)) !== false) {
122 $args[$arg] = $argval;
123 } elseif (isset($argstr_defaults[$arg])) {
124 $args[$arg] = (string)$argstr_defaults[$arg];
126 $args[$arg] = $default_val;
129 if ($request and is_string($args[$arg]) and strstr($args[$arg], "[")) {
130 $args[$arg] = $this->expandArg($args[$arg], $request);
133 unset($argstr_args[$arg]);
134 unset($argstr_defaults[$arg]);
137 foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
138 if ($this->allow_undeclared_arg($arg, $val)) {
143 // Add special handling of pages and exclude args to accept <! plugin-list !>
144 // and split explodePageList($args['exclude']) => array()
145 // TODO : handle p[] pagehash
146 foreach (array('pages', 'exclude') as $key) {
147 if (!empty($args[$key]) and array_key_exists($key, $defaults)) {
148 $args[$key] = is_string($args[$key])
149 ? explodePageList($args[$key])
150 : $args[$key]; // <! plugin-list !>
158 // Expand [arg] to $request->getArg("arg") unless preceded by ~
159 function expandArg($argval, &$request)
161 // Replace the arg unless it is preceded by a ~
162 $ret = preg_replace_callback('/([^~]|^)\[(\w[\w\d]*)\]/',
165 return "$m[1]" . $request->getArg("$m[2]");
168 // Ditch the ~ so later versions can be expanded if desired
169 return preg_replace('/~(\[\w[\w\d]*\])/', '$1', $ret);
172 function parseArgStr($argstr)
177 return array($args, $defaults);
180 $op_p = '(?:\|\|)?=';
183 $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
184 //"<--kludge for brain-dead syntax coloring
185 $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
186 $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
187 $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
189 // handle plugin-list arguments separately
190 $plugin_p = '<!plugin-list\s+\w+.*?!>';
191 while (preg_match("/^($arg_p) $opt_ws ($op_p) $opt_ws ($plugin_p) $opt_ws/x", $argstr, $m)) {
192 @ list(, $arg, $op, $plugin_val) = $m;
193 $argstr = substr($argstr, strlen($m[0]));
194 $loader = new WikiPluginLoader();
197 $plugin_val = preg_replace(array("/^<!/", "/!>$/"), array("<?", "?>"), $plugin_val);
198 $val = $loader->expandPI($plugin_val, $GLOBALS['request'], $markup, $basepage);
200 $args[$arg] = $val; // comma delimited pagenames or array()?
202 assert($op == '||=');
203 $defaults[$arg] = $val;
206 while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
215 list(, $arg, $op, $qq_val, $q_val, $gt_val, $word_val) = $m;
216 } elseif ($count == 6) {
217 list(, $arg, $op, $qq_val, $q_val, $gt_val) = $m;
218 } elseif ($count == 5) {
219 list(, $arg, $op, $qq_val, $q_val) = $m;
220 } elseif ($count == 4) {
221 list(, $arg, $op, $qq_val) = $m;
223 $argstr = substr($argstr, strlen($m[0]));
224 // Remove quotes from string values.
226 $val = stripslashes($qq_val);
227 elseif ($count > 4 and $q_val)
228 $val = stripslashes($q_val); elseif ($count >= 6 and $gt_val)
229 $val = _(stripslashes($gt_val)); elseif ($count >= 7)
230 $val = $word_val; else
236 // NOTE: This does work for multiple args. Use the
237 // separator character defined in your webserver
238 // configuration, usually & or & (See
239 // http://www.htmlhelp.com/faq/cgifaq.4.html)
240 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
241 // url: RecentChanges?days=1&show_all=1&show_minor=0
242 assert($op == '||=');
243 $defaults[$arg] = $val;
248 $this->handle_plugin_args_cruft($argstr, $args);
251 return array($args, $defaults);
254 /* A plugin can override this function to define how any remaining text is handled */
255 function handle_plugin_args_cruft($argstr, $args)
257 trigger_error(sprintf(_("trailing cruft in plugin args: ā%sā"),
258 $argstr), E_USER_NOTICE);
261 /* A plugin can override this to allow undeclared arguments.
262 Or to silence the warning.
264 function allow_undeclared_arg($name, $value)
266 trigger_error(sprintf(_("Argument ā%sā not declared by plugin."),
267 $name), E_USER_NOTICE);
271 /* handle plugin-list argument: use run(). */
272 function makeList($plugin_args, $request, $basepage)
274 $dbi = $request->getDbh();
275 $pagelist = $this->run($dbi, $plugin_args, $request, $basepage);
277 if (is_object($pagelist) and isa($pagelist, 'PageList'))
278 return $pagelist->pageNames();
279 elseif (is_array($pagelist))
285 function getDefaultLinkArguments()
287 return array('targetpage' => $this->getName(),
288 'linktext' => $this->getName(),
289 'description' => $this->getDescription(),
290 'class' => 'wikiaction');
293 function getDefaultFormArguments()
295 return array('targetpage' => $this->getName(),
296 'buttontext' => _($this->getName()),
297 'class' => 'wikiaction',
300 'description' => $this->getDescription(),
304 function makeForm($argstr, $request)
306 $form_defaults = $this->getDefaultFormArguments();
307 $defaults = array_merge($form_defaults,
308 array('start_debug' => $request->getArg('start_debug')),
309 $this->getDefaultArguments());
311 $args = $this->getArgs($argstr, $request, $defaults);
312 $textinput = $args['textinput'];
313 assert(!empty($textinput) && isset($args['textinput']));
315 $form = HTML::form(array('action' => WikiURL($args['targetpage']),
316 'method' => $args['method'],
317 'class' => $args['class'],
318 'accept-charset' => 'UTF-8'));
319 if (!USE_PATH_INFO) {
320 $pagename = $request->get('pagename');
321 $form->pushContent(HTML::input(array('type' => 'hidden',
322 'name' => 'pagename',
323 'value' => $args['targetpage'])));
325 if ($args['targetpage'] != $this->getName()) {
326 $form->pushContent(HTML::input(array('type' => 'hidden',
328 'value' => $this->getName())));
330 $contents = HTML::div();
331 $contents->setAttr('class', $args['class']);
333 foreach ($args as $arg => $val) {
334 if (isset($form_defaults[$arg]))
336 if ($arg != $textinput && $val == $defaults[$arg])
339 $i = HTML::input(array('name' => $arg, 'value' => $val));
341 if ($arg == $textinput) {
342 //if ($inputs[$arg] == 'file')
343 // $attr['type'] = 'file';
345 $i->setAttr('type', 'text');
346 $i->setAttr('size', $args['formsize']);
347 if ($args['description'])
348 $i->addTooltip($args['description']);
350 $i->setAttr('type', 'hidden');
352 $contents->pushContent($i);
355 if ($i->getAttr('type') == 'file') {
356 $form->setAttr('enctype', 'multipart/form-data');
357 $form->setAttr('method', 'post');
358 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
359 'value' => MAX_UPLOAD_SIZE,
360 'type' => 'hidden')));
364 if (!empty($args['buttontext']))
365 $contents->pushContent(HTML::input(array('type' => 'submit',
367 'value' => $args['buttontext'])));
368 $form->pushContent($contents);
372 // box is used to display a fixed-width, narrow version with common header
374 * @param string $args
375 * @param WikiRequest $request
376 * @param string $basepage
377 * @return $this|HtmlElement
379 function box($args = '', $request = null, $basepage = '')
382 $request =& $GLOBALS['request'];
383 } $dbi = $request->getDbh();
384 return $this->makeBox('', $this->run($dbi, $args, $request, $basepage));
387 function makeBox($title, $body)
390 $title = $this->getName();
392 return HTML::div(array('class' => 'box'),
393 HTML::div(array('class' => 'box-title'), $title),
394 HTML::div(array('class' => 'box-data'), $body));
397 function error($message)
399 return HTML::span(array('class' => 'error'),
400 HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
404 function disabled($message = '')
406 $html[] = HTML::div(array('class' => 'title'),
407 fmt("Plugin %s disabled.", $this->getName()),
409 $html[] = HTML::pre($this->_pi);
410 return HTML::div(array('class' => 'disabled-plugin'), $html);
413 // TODO: Not really needed, since our plugins generally initialize their own
414 // PageList object, which accepts options['types'].
415 // Register custom PageList types for special plugins, like
416 // 'hi_content' for WikiAdminSearcheplace, 'renamed_pagename' for WikiAdminRename, ...
417 function addPageListColumn($array)
419 global $customPageListColumns;
420 if (empty($customPageListColumns)) $customPageListColumns = array();
421 foreach ($array as $column => $obj) {
422 $customPageListColumns[$column] = $obj;
426 // provide a sample usage text for automatic edit-toolbar insertion
429 $args = $this->getDefaultArguments();
430 $string = '<<' . $this->getName() . ' ';
432 foreach ($args as $key => $value) {
433 $string .= ($key . "||=" . (string)$value . " ");
436 return $string . '>>';
439 function getArgumentsDescription()
442 foreach ($this->getDefaultArguments() as $arg => $default) {
443 // Work around UserPreferences plugin to avoid error
444 if ((is_array($default))) {
445 $default = '(array)';
446 // This is a bit flawed with UserPreferences object
447 //$default = sprintf("array('%s')",
448 // implode("', '", array_keys($default)));
450 if (stristr($default, ' '))
451 $default = "'$default'";
452 $arguments->pushcontent("$arg=$default", HTML::br());
459 class WikiPluginLoader
463 function expandPI($pi, &$request, &$markup, $basepage = false)
465 if (!($ppi = $this->parsePi($pi)))
467 list($pi_name, $plugin, $plugin_args) = $ppi;
469 if (!is_object($plugin)) {
470 return new HtmlElement('div',
471 array('class' => 'error'),
472 $this->getErrorDetail());
476 // FIXME: change API for run() (no $dbi needed).
477 $dbi = $request->getDbh();
478 // pass the parsed CachedMarkup context in dbi to the plugin
479 // to be able to know about itself, or even to change the markup XmlTree (CreateToc)
480 $dbi->_markup = &$markup;
481 // FIXME: could do better here...
482 if (!$plugin->managesValidators()) {
483 // Output of plugin (potentially) depends on
484 // the state of the WikiDB (other than the current
487 // Lacking other information, we'll assume things
488 // changed last time the wikidb was touched.
490 // As an additional hack, mark the ETag weak, since,
491 // for all we know, the page might depend
492 // on things other than the WikiDB (e.g. PhpWeather,
495 $timestamp = $dbi->getTimestamp();
496 $request->appendValidators(array('dbi_timestamp' => $timestamp,
497 '%mtime' => (int)$timestamp,
500 return $plugin->run($dbi, $plugin_args, $request, $basepage);
502 return $plugin->makeList($plugin_args, $request, $basepage);
504 return $plugin->makeForm($plugin_args, $request);
509 function getWikiPageLinks($pi, $basepage)
511 if (!($ppi = $this->parsePi($pi)))
513 list($pi_name, $plugin, $plugin_args) = $ppi;
514 if (!is_object($plugin))
516 if ($pi_name != 'plugin')
518 return $plugin->getWikiPageLinks($plugin_args, $basepage);
521 function parsePI($pi)
523 if (!preg_match('/^\s*<\?(plugin(?:-form|-link|-list)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
524 return $this->_error(sprintf("Bad %s", 'PI'));
526 list(, $pi_name, $plugin_name, $plugin_args) = $m;
527 $plugin = $this->getPlugin($plugin_name, $pi);
529 return array($pi_name, $plugin, $plugin_args);
532 function getPlugin($plugin_name, $pi = false)
534 global $ErrorManager;
535 global $AllAllowedPlugins;
537 if (in_array($plugin_name, $AllAllowedPlugins) === false) {
538 return $this->_error(sprintf(_("Plugin ā%sā does not exist."),
542 // Note that there seems to be no way to trap parse errors
543 // from this include. (At least not via set_error_handler().)
544 $plugin_source = "lib/plugin/$plugin_name.php";
546 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
547 $plugin_class = "WikiPlugin_$plugin_name";
548 if (!class_exists($plugin_class)) {
549 $include_failed = !include_once($plugin_source);
550 $ErrorManager->popErrorHandler();
552 if (!class_exists($plugin_class)) {
553 if ($include_failed) {
554 return $this->_error(sprintf(_("Plugin ā%sā does not exist."),
557 return $this->_error(sprintf(_("%s: no such class"), $plugin_class));
560 $ErrorManager->popErrorHandler();
561 $plugin = new $plugin_class;
562 if (!is_subclass_of($plugin, "WikiPlugin"))
563 return $this->_error(sprintf(_("%s: not a subclass of WikiPlugin."),
570 function _plugin_error_filter($err)
572 if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
573 return true; // Ignore this error --- it's expected.
577 function getErrorDetail()
579 return $this->_errors;
582 function _error($message)
584 $this->_errors = $message;
593 // c-hanging-comment-ender-p: nil
594 // indent-tabs-mode: nil