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()
36 function run($dbi, $argstr, &$request, $basepage)
38 trigger_error("WikiPlugin::run: pure virtual function", E_USER_ERROR);
42 /** Get wiki-pages linked to by plugin invocation.
44 * A plugin may override this method to add pages to the
45 * link database for the invoking page.
47 * For example, the IncludePage plugin should override this so
48 * that the including page shows up in the backlinks list for the
51 * Not all plugins which generate links to wiki-pages need list
54 * Note also that currently the links are calculated at page save
55 * time, so only static page links (e.g. those dependent on the PI
56 * args, not the rest of the wikidb state or any request query args)
57 * will work correctly here.
59 * @param string $argstr The plugin argument string.
60 * @param string $basepage The pagename the plugin is invoked from.
61 * @return array List of pagenames linked to (or false).
63 function getWikiPageLinks($argstr, $basepage)
71 * This is used (by default) by getDefaultLinkArguments and
72 * getDefaultFormArguments to compute the default link/form
75 * If you override this method in your plugin class,
76 * you MUST NOT translate the name.
78 * function getName() { return "MyPlugin"; }
81 * @return string plugin name/target.
85 return preg_replace('/^WikiPlugin_/', '', get_class($this));
89 * Get description of plugin.
91 * This method should be overriden in your plugin class, like:
93 * function getDescription() { return _("MyPlugin does this..."); }
96 * @return string plugin description
99 function getDescription()
101 return _('This plugin has no description.');
104 function getArgs($argstr, $request = false, $defaults = array())
106 if (empty($defaults)) {
107 $defaults = $this->getDefaultArguments();
109 //Fixme: on POST argstr is empty
110 list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
112 if (!empty($defaults))
113 foreach ($defaults as $arg => $default_val) {
114 if (isset($argstr_args[$arg])) {
115 $args[$arg] = $argstr_args[$arg];
116 } elseif ($request and ($argval = $request->getArg($arg)) !== false) {
117 $args[$arg] = $argval;
118 } elseif (isset($argstr_defaults[$arg])) {
119 $args[$arg] = (string)$argstr_defaults[$arg];
121 $args[$arg] = $default_val;
124 if ($request and is_string($args[$arg]) and strstr($args[$arg], "[")) {
125 $args[$arg] = $this->expandArg($args[$arg], $request);
128 unset($argstr_args[$arg]);
129 unset($argstr_defaults[$arg]);
132 foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
133 if ($this->allow_undeclared_arg($arg, $val)) {
138 // Add special handling of pages and exclude args to accept <! plugin-list !>
139 // and split explodePageList($args['exclude']) => array()
140 // TODO : handle p[] pagehash
141 foreach (array('pages', 'exclude') as $key) {
142 if (!empty($args[$key]) and array_key_exists($key, $defaults)) {
143 $args[$key] = is_string($args[$key])
144 ? explodePageList($args[$key])
145 : $args[$key]; // <! plugin-list !>
153 // Expand [arg] to $request->getArg("arg") unless preceded by ~
154 function expandArg($argval, &$request)
156 // return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
157 // Replace the arg unless it is preceded by a ~
158 $ret = preg_replace('/([^~]|^)\[(\w[\w\d]*)\]/e',
159 '"$1" . $request->getArg("$2")',
161 // Ditch the ~ so later versions can be expanded if desired
162 return preg_replace('/~(\[\w[\w\d]*\])/', '$1', $ret);
165 function parseArgStr($argstr)
170 return array($args, $defaults);
173 $op_p = '(?:\|\|)?=';
176 $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
177 //"<--kludge for brain-dead syntax coloring
178 $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
179 $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
180 $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
182 // handle plugin-list arguments separately
183 $plugin_p = '<!plugin-list\s+\w+.*?!>';
184 while (preg_match("/^($arg_p) $opt_ws ($op_p) $opt_ws ($plugin_p) $opt_ws/x", $argstr, $m)) {
185 @ list(, $arg, $op, $plugin_val) = $m;
186 $argstr = substr($argstr, strlen($m[0]));
187 $loader = new WikiPluginLoader();
190 $plugin_val = preg_replace(array("/^<!/", "/!>$/"), array("<?", "?>"), $plugin_val);
191 $val = $loader->expandPI($plugin_val, $GLOBALS['request'], $markup, $basepage);
193 $args[$arg] = $val; // comma delimited pagenames or array()?
195 assert($op == '||=');
196 $defaults[$arg] = $val;
199 while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
208 list(, $arg, $op, $qq_val, $q_val, $gt_val, $word_val) = $m;
209 } elseif ($count == 6) {
210 list(, $arg, $op, $qq_val, $q_val, $gt_val) = $m;
211 } elseif ($count == 5) {
212 list(, $arg, $op, $qq_val, $q_val) = $m;
213 } elseif ($count == 4) {
214 list(, $arg, $op, $qq_val) = $m;
216 $argstr = substr($argstr, strlen($m[0]));
217 // Remove quotes from string values.
219 $val = stripslashes($qq_val);
220 elseif ($count > 4 and $q_val)
221 $val = stripslashes($q_val); elseif ($count >= 6 and $gt_val)
222 $val = _(stripslashes($gt_val)); elseif ($count >= 7)
223 $val = $word_val; else
229 // NOTE: This does work for multiple args. Use the
230 // separator character defined in your webserver
231 // configuration, usually & or & (See
232 // http://www.htmlhelp.com/faq/cgifaq.4.html)
233 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
234 // url: RecentChanges?days=1&show_all=1&show_minor=0
235 assert($op == '||=');
236 $defaults[$arg] = $val;
241 $this->handle_plugin_args_cruft($argstr, $args);
244 return array($args, $defaults);
247 /* A plugin can override this function to define how any remaining text is handled */
248 function handle_plugin_args_cruft($argstr, $args)
250 trigger_error(sprintf(_("trailing cruft in plugin args: ā%sā"),
251 $argstr), E_USER_NOTICE);
254 /* A plugin can override this to allow undeclared arguments.
255 Or to silence the warning.
257 function allow_undeclared_arg($name, $value)
259 trigger_error(sprintf(_("Argument ā%sā not declared by plugin."),
260 $name), E_USER_NOTICE);
264 /* handle plugin-list argument: use run(). */
265 function makeList($plugin_args, $request, $basepage)
267 $dbi = $request->getDbh();
268 $pagelist = $this->run($dbi, $plugin_args, $request, $basepage);
270 if (is_object($pagelist) and isa($pagelist, 'PageList'))
271 return $pagelist->pageNames();
272 elseif (is_array($pagelist))
273 return $pagelist; else
277 function getDefaultLinkArguments()
279 return array('targetpage' => $this->getName(),
280 'linktext' => $this->getName(),
281 'description' => $this->getDescription(),
282 'class' => 'wikiaction');
285 function getDefaultFormArguments()
287 return array('targetpage' => $this->getName(),
288 'buttontext' => _($this->getName()),
289 'class' => 'wikiaction',
292 'description' => $this->getDescription(),
296 function makeForm($argstr, $request)
298 $form_defaults = $this->getDefaultFormArguments();
299 $defaults = array_merge($form_defaults,
300 array('start_debug' => $request->getArg('start_debug')),
301 $this->getDefaultArguments());
303 $args = $this->getArgs($argstr, $request, $defaults);
304 $textinput = $args['textinput'];
305 assert(!empty($textinput) && isset($args['textinput']));
307 $form = HTML::form(array('action' => WikiURL($args['targetpage']),
308 'method' => $args['method'],
309 'class' => $args['class'],
310 'accept-charset' => 'UTF-8'));
311 if (!USE_PATH_INFO) {
312 $pagename = $request->get('pagename');
313 $form->pushContent(HTML::input(array('type' => 'hidden',
314 'name' => 'pagename',
315 'value' => $args['targetpage'])));
317 if ($args['targetpage'] != $this->getName()) {
318 $form->pushContent(HTML::input(array('type' => 'hidden',
320 'value' => $this->getName())));
322 $contents = HTML::div();
323 $contents->setAttr('class', $args['class']);
325 foreach ($args as $arg => $val) {
326 if (isset($form_defaults[$arg]))
328 if ($arg != $textinput && $val == $defaults[$arg])
331 $i = HTML::input(array('name' => $arg, 'value' => $val));
333 if ($arg == $textinput) {
334 //if ($inputs[$arg] == 'file')
335 // $attr['type'] = 'file';
337 $i->setAttr('type', 'text');
338 $i->setAttr('size', $args['formsize']);
339 if ($args['description'])
340 $i->addTooltip($args['description']);
342 $i->setAttr('type', 'hidden');
344 $contents->pushContent($i);
347 if ($i->getAttr('type') == 'file') {
348 $form->setAttr('enctype', 'multipart/form-data');
349 $form->setAttr('method', 'post');
350 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
351 'value' => MAX_UPLOAD_SIZE,
352 'type' => 'hidden')));
356 if (!empty($args['buttontext']))
357 $contents->pushContent(HTML::input(array('type' => 'submit',
359 'value' => $args['buttontext'])));
360 $form->pushContent($contents);
364 // box is used to display a fixed-width, narrow version with common header
365 function box($args = false, $request = false, $basepage = false)
367 if (!$request) $request =& $GLOBALS['request'];
368 $dbi = $request->getDbh();
369 return $this->makeBox('', $this->run($dbi, $args, $request, $basepage));
372 function makeBox($title, $body)
374 if (!$title) $title = $this->getName();
375 return HTML::div(array('class' => 'box'),
376 HTML::div(array('class' => 'box-title'), $title),
377 HTML::div(array('class' => 'box-data'), $body));
380 function error($message)
382 return HTML::span(array('class' => 'error'),
383 HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
387 function disabled($message = '')
389 $html[] = HTML::div(array('class' => 'title'),
390 fmt("Plugin %s disabled.", $this->getName()),
392 $html[] = HTML::pre($this->_pi);
393 return HTML::div(array('class' => 'disabled-plugin'), $html);
396 // TODO: Not really needed, since our plugins generally initialize their own
397 // PageList object, which accepts options['types'].
398 // Register custom PageList types for special plugins, like
399 // 'hi_content' for WikiAdminSearcheplace, 'renamed_pagename' for WikiAdminRename, ...
400 function addPageListColumn($array)
402 global $customPageListColumns;
403 if (empty($customPageListColumns)) $customPageListColumns = array();
404 foreach ($array as $column => $obj) {
405 $customPageListColumns[$column] = $obj;
409 // provide a sample usage text for automatic edit-toolbar insertion
412 $args = $this->getDefaultArguments();
413 $string = '<<' . $this->getName() . ' ';
415 foreach ($args as $key => $value) {
416 $string .= ($key . "||=" . (string)$value . " ");
419 return $string . '>>';
422 function getArgumentsDescription()
425 foreach ($this->getDefaultArguments() as $arg => $default) {
426 // Work around UserPreferences plugin to avoid error
427 if ((is_array($default))) {
428 $default = '(array)';
429 // This is a bit flawed with UserPreferences object
430 //$default = sprintf("array('%s')",
431 // implode("', '", array_keys($default)));
433 if (stristr($default, ' '))
434 $default = "'$default'";
435 $arguments->pushcontent("$arg=$default", HTML::br());
442 class WikiPluginLoader
446 function expandPI($pi, &$request, &$markup, $basepage = false)
448 if (!($ppi = $this->parsePi($pi)))
450 list($pi_name, $plugin, $plugin_args) = $ppi;
452 if (!is_object($plugin)) {
453 return new HtmlElement('div',
454 array('class' => 'error'),
455 $this->getErrorDetail());
459 // FIXME: change API for run() (no $dbi needed).
460 $dbi = $request->getDbh();
461 // pass the parsed CachedMarkup context in dbi to the plugin
462 // to be able to know about itself, or even to change the markup XmlTree (CreateToc)
463 $dbi->_markup = &$markup;
464 // FIXME: could do better here...
465 if (!$plugin->managesValidators()) {
466 // Output of plugin (potentially) depends on
467 // the state of the WikiDB (other than the current
470 // Lacking other information, we'll assume things
471 // changed last time the wikidb was touched.
473 // As an additional hack, mark the ETag weak, since,
474 // for all we know, the page might depend
475 // on things other than the WikiDB (e.g. PhpWeather,
478 $timestamp = $dbi->getTimestamp();
479 $request->appendValidators(array('dbi_timestamp' => $timestamp,
480 '%mtime' => (int)$timestamp,
483 return $plugin->run($dbi, $plugin_args, $request, $basepage);
485 return $plugin->makeList($plugin_args, $request, $basepage);
487 return $plugin->makeForm($plugin_args, $request);
492 function getWikiPageLinks($pi, $basepage)
494 if (!($ppi = $this->parsePi($pi)))
496 list($pi_name, $plugin, $plugin_args) = $ppi;
497 if (!is_object($plugin))
499 if ($pi_name != 'plugin')
501 return $plugin->getWikiPageLinks($plugin_args, $basepage);
504 function parsePI($pi)
506 if (!preg_match('/^\s*<\?(plugin(?:-form|-link|-list)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
507 return $this->_error(sprintf("Bad %s", 'PI'));
509 list(, $pi_name, $plugin_name, $plugin_args) = $m;
510 $plugin = $this->getPlugin($plugin_name, $pi);
512 return array($pi_name, $plugin, $plugin_args);
515 function getPlugin($plugin_name, $pi = false)
517 global $ErrorManager;
518 global $AllAllowedPlugins;
520 if (in_array($plugin_name, $AllAllowedPlugins) === false) {
521 return $this->_error(sprintf(_("Plugin ā%sā does not exist."),
525 // Note that there seems to be no way to trap parse errors
526 // from this include. (At least not via set_error_handler().)
527 $plugin_source = "lib/plugin/$plugin_name.php";
529 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
530 $plugin_class = "WikiPlugin_$plugin_name";
531 if (!class_exists($plugin_class)) {
532 // $include_failed = !@include_once("lib/plugin/$plugin_name.php");
533 $include_failed = !include_once($plugin_source);
534 $ErrorManager->popErrorHandler();
536 if (!class_exists($plugin_class)) {
537 if ($include_failed) {
538 return $this->_error(sprintf(_("Plugin ā%sā does not exist."),
541 return $this->_error(sprintf(_("%s: no such class"), $plugin_class));
544 $ErrorManager->popErrorHandler();
545 $plugin = new $plugin_class;
546 if (!is_subclass_of($plugin, "WikiPlugin"))
547 return $this->_error(sprintf(_("%s: not a subclass of WikiPlugin."),
554 function _plugin_error_filter($err)
556 if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
557 return true; // Ignore this error --- it's expected.
561 function getErrorDetail()
563 return $this->_errors;
566 function _error($message)
568 $this->_errors = $message;
577 // c-hanging-comment-ender-p: nil
578 // indent-tabs-mode: nil