5 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()
34 function run($dbi, $argstr, &$request, $basepage)
36 trigger_error("WikiPlugin::run: pure virtual function",
40 /** Get wiki-pages linked to by plugin invocation.
42 * A plugin may override this method to add pages to the
43 * link database for the invoking page.
45 * For example, the IncludePage plugin should override this so
46 * that the including page shows up in the backlinks list for the
49 * Not all plugins which generate links to wiki-pages need list
52 * Note also that currently the links are calculated at page save
53 * time, so only static page links (e.g. those dependent on the PI
54 * args, not the rest of the wikidb state or any request query args)
55 * will work correctly here.
57 * @param string $argstr The plugin argument string.
58 * @param string $basepage The pagename the plugin is invoked from.
59 * @return array List of pagenames linked to (or false).
61 function getWikiPageLinks($argstr, $basepage)
69 * This is used (by default) by getDefaultLinkArguments and
70 * getDefaultFormArguments to compute the default link/form
73 * If you want to gettextify the name (probably a good idea),
74 * override this method in your plugin class, like:
76 * function getName() { return _("MyPlugin"); }
79 * @return string plugin name/target.
83 return preg_replace('/^.*_/', '', get_class($this));
86 function getDescription()
88 return $this->getName();
91 function getArgs($argstr, $request = false, $defaults = false)
93 if ($defaults === false) {
94 $defaults = $this->getDefaultArguments();
96 //Fixme: on POST argstr is empty
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 if ($request and is_string($args[$arg]) and strstr($args[$arg], "[")) {
112 $args[$arg] = $this->expandArg($args[$arg], $request);
115 unset($argstr_args[$arg]);
116 unset($argstr_defaults[$arg]);
119 foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
120 // TODO: where the heck comes this from? Put the new method over there and peace.
121 /*if ($request and $request->getArg('pagename') == _("PhpWikiAdministration")
122 and $arg == 'overwrite') // silence this warning
124 if ($this->allow_undeclared_arg($arg, $val))
128 // Add special handling of pages and exclude args to accept <! plugin-list !>
129 // and split explodePageList($args['exclude']) => array()
130 // TODO : handle p[] pagehash
131 foreach (array('pages', 'exclude') as $key) {
132 if (!empty($args[$key]) and array_key_exists($key, $defaults))
133 $args[$key] = is_string($args[$key])
134 ? explodePageList($args[$key])
135 : $args[$key]; // <! plugin-list !>
142 // Expand [arg] to $request->getArg("arg") unless preceded by ~
143 function expandArg($argval, &$request)
145 // return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
146 // Replace the arg unless it is preceded by a ~
147 $ret = preg_replace('/([^~]|^)\[(\w[\w\d]*)\]/e',
148 '"$1" . $request->getArg("$2")',
150 // Ditch the ~ so later versions can be expanded if desired
151 return preg_replace('/~(\[\w[\w\d]*\])/', '$1', $ret);
154 function parseArgStr($argstr)
159 return array($args, $defaults);
162 $op_p = '(?:\|\|)?=';
165 $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
166 //"<--kludge for brain-dead syntax coloring
167 $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
168 $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
169 $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
171 // handle plugin-list arguments separately
172 $plugin_p = '<!plugin-list\s+\w+.*?!>';
173 while (preg_match("/^($arg_p) $opt_ws ($op_p) $opt_ws ($plugin_p) $opt_ws/x", $argstr, $m)) {
174 @ list(, $arg, $op, $plugin_val) = $m;
175 $argstr = substr($argstr, strlen($m[0]));
176 $loader = new WikiPluginLoader();
179 $plugin_val = preg_replace(array("/^<!/", "/!>$/"), array("<?", "?>"), $plugin_val);
180 $val = $loader->expandPI($plugin_val, $GLOBALS['request'], $markup, $basepage);
182 $args[$arg] = $val; // comma delimited pagenames or array()?
184 assert($op == '||=');
185 $defaults[$arg] = $val;
188 while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
189 //@ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
192 list(, $arg, $op, $qq_val, $q_val, $gt_val, $word_val) = $m;
193 } elseif ($count == 6) {
194 list(, $arg, $op, $qq_val, $q_val, $gt_val) = $m;
195 } elseif ($count == 5) {
196 list(, $arg, $op, $qq_val, $q_val) = $m;
197 } elseif ($count == 4) {
198 list(, $arg, $op, $qq_val) = $m;
200 $argstr = substr($argstr, strlen($m[0]));
201 // Remove quotes from string values.
203 $val = stripslashes($qq_val);
204 elseif ($count > 4 and $q_val)
205 $val = stripslashes($q_val); elseif ($count >= 6 and $gt_val)
206 $val = _(stripslashes($gt_val)); elseif ($count >= 7)
207 $val = $word_val; else
213 // NOTE: This does work for multiple args. Use the
214 // separator character defined in your webserver
215 // configuration, usually & or & (See
216 // http://www.htmlhelp.com/faq/cgifaq.4.html)
217 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
218 // url: RecentChanges?days=1&show_all=1&show_minor=0
219 assert($op == '||=');
220 $defaults[$arg] = $val;
225 $this->handle_plugin_args_cruft($argstr, $args);
228 return array($args, $defaults);
231 /* A plugin can override this function to define how any remaining text is handled */
232 function handle_plugin_args_cruft($argstr, $args)
234 trigger_error(sprintf(_("trailing cruft in plugin args: ā%sā"),
235 $argstr), E_USER_NOTICE);
238 /* A plugin can override this to allow undeclared arguments.
239 Or to silence the warning.
241 function allow_undeclared_arg($name, $value)
243 trigger_error(sprintf(_("Argument ā%sā not declared by plugin."),
244 $name), E_USER_NOTICE);
248 /* handle plugin-list argument: use run(). */
249 function makeList($plugin_args, $request, $basepage)
251 $dbi = $request->getDbh();
252 $pagelist = $this->run($dbi, $plugin_args, $request, $basepage);
254 if (is_object($pagelist) and isa($pagelist, 'PageList'))
255 return $pagelist->pageNames();
256 elseif (is_array($pagelist))
257 return $pagelist; else
261 function getDefaultLinkArguments()
263 return array('targetpage' => $this->getName(),
264 'linktext' => $this->getName(),
265 'description' => $this->getDescription(),
266 'class' => 'wikiaction');
269 function getDefaultFormArguments()
271 return array('targetpage' => $this->getName(),
272 'buttontext' => $this->getName(),
273 'class' => 'wikiaction',
276 'description' => $this->getDescription(),
280 function makeForm($argstr, $request)
282 $form_defaults = $this->getDefaultFormArguments();
283 $defaults = array_merge($form_defaults,
284 array('start_debug' => $request->getArg('start_debug')),
285 $this->getDefaultArguments());
287 $args = $this->getArgs($argstr, $request, $defaults);
288 $plugin = $this->getName();
289 $textinput = $args['textinput'];
290 assert(!empty($textinput) && isset($args['textinput']));
292 $form = HTML::form(array('action' => WikiURL($args['targetpage']),
293 'method' => $args['method'],
294 'class' => $args['class'],
295 'accept-charset' => 'UTF-8'));
296 if (!USE_PATH_INFO) {
297 $pagename = $request->get('pagename');
298 $form->pushContent(HTML::input(array('type' => 'hidden',
299 'name' => 'pagename',
300 'value' => $args['targetpage'])));
302 if ($args['targetpage'] != $this->getName()) {
303 $form->pushContent(HTML::input(array('type' => 'hidden',
305 'value' => $this->getName())));
307 $contents = HTML::div();
308 $contents->setAttr('class', $args['class']);
310 foreach ($args as $arg => $val) {
311 if (isset($form_defaults[$arg]))
313 if ($arg != $textinput && $val == $defaults[$arg])
316 $i = HTML::input(array('name' => $arg, 'value' => $val));
318 if ($arg == $textinput) {
319 //if ($inputs[$arg] == 'file')
320 // $attr['type'] = 'file';
322 $i->setAttr('type', 'text');
323 $i->setAttr('size', $args['formsize']);
324 if ($args['description'])
325 $i->addTooltip($args['description']);
327 $i->setAttr('type', 'hidden');
329 $contents->pushContent($i);
332 if ($i->getAttr('type') == 'file') {
333 $form->setAttr('enctype', 'multipart/form-data');
334 $form->setAttr('method', 'post');
335 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
336 'value' => MAX_UPLOAD_SIZE,
337 'type' => 'hidden')));
341 if (!empty($args['buttontext']))
342 $contents->pushContent(HTML::input(array('type' => 'submit',
344 'value' => $args['buttontext'])));
345 $form->pushContent($contents);
349 // box is used to display a fixed-width, narrow version with common header
350 function box($args = false, $request = false, $basepage = false)
352 if (!$request) $request =& $GLOBALS['request'];
353 $dbi = $request->getDbh();
354 return $this->makeBox('', $this->run($dbi, $args, $request, $basepage));
357 function makeBox($title, $body)
359 if (!$title) $title = $this->getName();
360 return HTML::div(array('class' => 'box'),
361 HTML::div(array('class' => 'box-title'), $title),
362 HTML::div(array('class' => 'box-data'), $body));
365 function error($message)
367 return HTML::span(array('class' => 'error'),
368 HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
372 function disabled($message = '')
374 $html[] = HTML::div(array('class' => 'title'),
375 fmt("Plugin %s disabled.", $this->getName()),
377 $html[] = HTML::pre($this->_pi);
378 return HTML::div(array('class' => 'disabled-plugin'), $html);
381 // TODO: Not really needed, since our plugins generally initialize their own
382 // PageList object, which accepts options['types'].
383 // Register custom PageList types for special plugins, like
384 // 'hi_content' for WikiAdminSearcheplace, 'renamed_pagename' for WikiAdminRename, ...
385 function addPageListColumn($array)
387 global $customPageListColumns;
388 if (empty($customPageListColumns)) $customPageListColumns = array();
389 foreach ($array as $column => $obj) {
390 $customPageListColumns[$column] = $obj;
394 // provide a sample usage text for automatic edit-toolbar insertion
397 $args = $this->getDefaultArguments();
398 $string = '<<' . $this->getName() . ' ';
400 foreach ($args as $key => $value) {
401 $string .= ($key . "||=" . (string)$value . " ");
404 return $string . '>>';
407 function getArgumentsDescription()
410 foreach ($this->getDefaultArguments() as $arg => $default) {
411 // Work around UserPreferences plugin to avoid error
412 if ((is_array($default))) {
413 $default = '(array)';
414 // This is a bit flawed with UserPreferences object
415 //$default = sprintf("array('%s')",
416 // implode("', '", array_keys($default)));
418 if (stristr($default, ' '))
419 $default = "'$default'";
420 $arguments->pushcontent("$arg=$default", HTML::br());
427 class WikiPluginLoader
431 function expandPI($pi, &$request, &$markup, $basepage = false)
433 if (!($ppi = $this->parsePi($pi)))
435 list($pi_name, $plugin, $plugin_args) = $ppi;
437 if (!is_object($plugin)) {
438 return new HtmlElement('div',
439 array('class' => 'error'),
440 $this->getErrorDetail());
444 // FIXME: change API for run() (no $dbi needed).
445 $dbi = $request->getDbh();
446 // pass the parsed CachedMarkup context in dbi to the plugin
447 // to be able to know about itself, or even to change the markup XmlTree (CreateToc)
448 $dbi->_markup = &$markup;
449 // FIXME: could do better here...
450 if (!$plugin->managesValidators()) {
451 // Output of plugin (potentially) depends on
452 // the state of the WikiDB (other than the current
455 // Lacking other information, we'll assume things
456 // changed last time the wikidb was touched.
458 // As an additional hack, mark the ETag weak, since,
459 // for all we know, the page might depend
460 // on things other than the WikiDB (e.g. PhpWeather,
463 $timestamp = $dbi->getTimestamp();
464 $request->appendValidators(array('dbi_timestamp' => $timestamp,
465 '%mtime' => (int)$timestamp,
468 return $plugin->run($dbi, $plugin_args, $request, $basepage);
470 return $plugin->makeList($plugin_args, $request, $basepage);
472 return $plugin->makeForm($plugin_args, $request);
477 function getWikiPageLinks($pi, $basepage)
479 if (!($ppi = $this->parsePi($pi)))
481 list($pi_name, $plugin, $plugin_args) = $ppi;
482 if (!is_object($plugin))
484 if ($pi_name != 'plugin')
486 return $plugin->getWikiPageLinks($plugin_args, $basepage);
489 function parsePI($pi)
491 if (!preg_match('/^\s*<\?(plugin(?:-form|-link|-list)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
492 return $this->_error(sprintf("Bad %s", 'PI'));
494 list(, $pi_name, $plugin_name, $plugin_args) = $m;
495 $plugin = $this->getPlugin($plugin_name, $pi);
497 return array($pi_name, $plugin, $plugin_args);
500 function getPlugin($plugin_name, $pi = false)
502 global $ErrorManager;
503 global $AllAllowedPlugins;
505 if (in_array($plugin_name, $AllAllowedPlugins) === false) {
506 return $this->_error(sprintf(_("Plugin ā%sā does not exist."),
510 // Note that there seems to be no way to trap parse errors
511 // from this include. (At least not via set_error_handler().)
512 $plugin_source = "lib/plugin/$plugin_name.php";
514 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
515 $plugin_class = "WikiPlugin_$plugin_name";
516 if (!class_exists($plugin_class)) {
517 // $include_failed = !@include_once("lib/plugin/$plugin_name.php");
518 $include_failed = !include_once($plugin_source);
519 $ErrorManager->popErrorHandler();
521 if (!class_exists($plugin_class)) {
522 if ($include_failed) {
523 return $this->_error(sprintf(_("Plugin ā%sā does not exist."),
526 return $this->_error(sprintf(_("%s: no such class"), $plugin_class));
529 $ErrorManager->popErrorHandler();
530 $plugin = new $plugin_class;
531 if (!is_subclass_of($plugin, "WikiPlugin"))
532 return $this->_error(sprintf(_("%s: not a subclass of WikiPlugin."),
539 function _plugin_error_filter($err)
541 if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
542 return true; // Ignore this error --- it's expected.
546 function getErrorDetail()
548 return $this->_errors;
551 function _error($message)
553 $this->_errors = $message;
562 // c-hanging-comment-ender-p: nil
563 // indent-tabs-mode: nil