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 // TODO: where the heck comes this from? Put the new method over there and peace.
134 /*if ($request and $request->getArg('pagename') == _("PhpWikiAdministration")
135 and $arg == 'overwrite') // silence this warning
137 if ($this->allow_undeclared_arg($arg, $val))
141 // Add special handling of pages and exclude args to accept <! plugin-list !>
142 // and split explodePageList($args['exclude']) => array()
143 // TODO : handle p[] pagehash
144 foreach (array('pages', 'exclude') as $key) {
145 if (!empty($args[$key]) and array_key_exists($key, $defaults))
146 $args[$key] = is_string($args[$key])
147 ? explodePageList($args[$key])
148 : $args[$key]; // <! plugin-list !>
155 // Expand [arg] to $request->getArg("arg") unless preceded by ~
156 function expandArg($argval, &$request)
158 // return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
159 // Replace the arg unless it is preceded by a ~
160 $ret = preg_replace('/([^~]|^)\[(\w[\w\d]*)\]/e',
161 '"$1" . $request->getArg("$2")',
163 // Ditch the ~ so later versions can be expanded if desired
164 return preg_replace('/~(\[\w[\w\d]*\])/', '$1', $ret);
167 function parseArgStr($argstr)
172 return array($args, $defaults);
175 $op_p = '(?:\|\|)?=';
178 $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
179 //"<--kludge for brain-dead syntax coloring
180 $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
181 $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
182 $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
184 // handle plugin-list arguments separately
185 $plugin_p = '<!plugin-list\s+\w+.*?!>';
186 while (preg_match("/^($arg_p) $opt_ws ($op_p) $opt_ws ($plugin_p) $opt_ws/x", $argstr, $m)) {
187 @ list(, $arg, $op, $plugin_val) = $m;
188 $argstr = substr($argstr, strlen($m[0]));
189 $loader = new WikiPluginLoader();
192 $plugin_val = preg_replace(array("/^<!/", "/!>$/"), array("<?", "?>"), $plugin_val);
193 $val = $loader->expandPI($plugin_val, $GLOBALS['request'], $markup, $basepage);
195 $args[$arg] = $val; // comma delimited pagenames or array()?
197 assert($op == '||=');
198 $defaults[$arg] = $val;
201 while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
210 list(, $arg, $op, $qq_val, $q_val, $gt_val, $word_val) = $m;
211 } elseif ($count == 6) {
212 list(, $arg, $op, $qq_val, $q_val, $gt_val) = $m;
213 } elseif ($count == 5) {
214 list(, $arg, $op, $qq_val, $q_val) = $m;
215 } elseif ($count == 4) {
216 list(, $arg, $op, $qq_val) = $m;
218 $argstr = substr($argstr, strlen($m[0]));
219 // Remove quotes from string values.
221 $val = stripslashes($qq_val);
222 elseif ($count > 4 and $q_val)
223 $val = stripslashes($q_val); elseif ($count >= 6 and $gt_val)
224 $val = _(stripslashes($gt_val)); elseif ($count >= 7)
225 $val = $word_val; else
231 // NOTE: This does work for multiple args. Use the
232 // separator character defined in your webserver
233 // configuration, usually & or & (See
234 // http://www.htmlhelp.com/faq/cgifaq.4.html)
235 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
236 // url: RecentChanges?days=1&show_all=1&show_minor=0
237 assert($op == '||=');
238 $defaults[$arg] = $val;
243 $this->handle_plugin_args_cruft($argstr, $args);
246 return array($args, $defaults);
249 /* A plugin can override this function to define how any remaining text is handled */
250 function handle_plugin_args_cruft($argstr, $args)
252 trigger_error(sprintf(_("trailing cruft in plugin args: ā%sā"),
253 $argstr), E_USER_NOTICE);
256 /* A plugin can override this to allow undeclared arguments.
257 Or to silence the warning.
259 function allow_undeclared_arg($name, $value)
261 trigger_error(sprintf(_("Argument ā%sā not declared by plugin."),
262 $name), E_USER_NOTICE);
266 /* handle plugin-list argument: use run(). */
267 function makeList($plugin_args, $request, $basepage)
269 $dbi = $request->getDbh();
270 $pagelist = $this->run($dbi, $plugin_args, $request, $basepage);
272 if (is_object($pagelist) and isa($pagelist, 'PageList'))
273 return $pagelist->pageNames();
274 elseif (is_array($pagelist))
275 return $pagelist; else
279 function getDefaultLinkArguments()
281 return array('targetpage' => $this->getName(),
282 'linktext' => $this->getName(),
283 'description' => $this->getDescription(),
284 'class' => 'wikiaction');
287 function getDefaultFormArguments()
289 return array('targetpage' => $this->getName(),
290 'buttontext' => _($this->getName()),
291 'class' => 'wikiaction',
294 'description' => $this->getDescription(),
298 function makeForm($argstr, $request)
300 $form_defaults = $this->getDefaultFormArguments();
301 $defaults = array_merge($form_defaults,
302 array('start_debug' => $request->getArg('start_debug')),
303 $this->getDefaultArguments());
305 $args = $this->getArgs($argstr, $request, $defaults);
306 $textinput = $args['textinput'];
307 assert(!empty($textinput) && isset($args['textinput']));
309 $form = HTML::form(array('action' => WikiURL($args['targetpage']),
310 'method' => $args['method'],
311 'class' => $args['class'],
312 'accept-charset' => 'UTF-8'));
313 if (!USE_PATH_INFO) {
314 $pagename = $request->get('pagename');
315 $form->pushContent(HTML::input(array('type' => 'hidden',
316 'name' => 'pagename',
317 'value' => $args['targetpage'])));
319 if ($args['targetpage'] != $this->getName()) {
320 $form->pushContent(HTML::input(array('type' => 'hidden',
322 'value' => $this->getName())));
324 $contents = HTML::div();
325 $contents->setAttr('class', $args['class']);
327 foreach ($args as $arg => $val) {
328 if (isset($form_defaults[$arg]))
330 if ($arg != $textinput && $val == $defaults[$arg])
333 $i = HTML::input(array('name' => $arg, 'value' => $val));
335 if ($arg == $textinput) {
336 //if ($inputs[$arg] == 'file')
337 // $attr['type'] = 'file';
339 $i->setAttr('type', 'text');
340 $i->setAttr('size', $args['formsize']);
341 if ($args['description'])
342 $i->addTooltip($args['description']);
344 $i->setAttr('type', 'hidden');
346 $contents->pushContent($i);
349 if ($i->getAttr('type') == 'file') {
350 $form->setAttr('enctype', 'multipart/form-data');
351 $form->setAttr('method', 'post');
352 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
353 'value' => MAX_UPLOAD_SIZE,
354 'type' => 'hidden')));
358 if (!empty($args['buttontext']))
359 $contents->pushContent(HTML::input(array('type' => 'submit',
361 'value' => $args['buttontext'])));
362 $form->pushContent($contents);
366 // box is used to display a fixed-width, narrow version with common header
367 function box($args = false, $request = false, $basepage = false)
369 if (!$request) $request =& $GLOBALS['request'];
370 $dbi = $request->getDbh();
371 return $this->makeBox('', $this->run($dbi, $args, $request, $basepage));
374 function makeBox($title, $body)
376 if (!$title) $title = $this->getName();
377 return HTML::div(array('class' => 'box'),
378 HTML::div(array('class' => 'box-title'), $title),
379 HTML::div(array('class' => 'box-data'), $body));
382 function error($message)
384 return HTML::span(array('class' => 'error'),
385 HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
389 function disabled($message = '')
391 $html[] = HTML::div(array('class' => 'title'),
392 fmt("Plugin %s disabled.", $this->getName()),
394 $html[] = HTML::pre($this->_pi);
395 return HTML::div(array('class' => 'disabled-plugin'), $html);
398 // TODO: Not really needed, since our plugins generally initialize their own
399 // PageList object, which accepts options['types'].
400 // Register custom PageList types for special plugins, like
401 // 'hi_content' for WikiAdminSearcheplace, 'renamed_pagename' for WikiAdminRename, ...
402 function addPageListColumn($array)
404 global $customPageListColumns;
405 if (empty($customPageListColumns)) $customPageListColumns = array();
406 foreach ($array as $column => $obj) {
407 $customPageListColumns[$column] = $obj;
411 // provide a sample usage text for automatic edit-toolbar insertion
414 $args = $this->getDefaultArguments();
415 $string = '<<' . $this->getName() . ' ';
417 foreach ($args as $key => $value) {
418 $string .= ($key . "||=" . (string)$value . " ");
421 return $string . '>>';
424 function getArgumentsDescription()
427 foreach ($this->getDefaultArguments() as $arg => $default) {
428 // Work around UserPreferences plugin to avoid error
429 if ((is_array($default))) {
430 $default = '(array)';
431 // This is a bit flawed with UserPreferences object
432 //$default = sprintf("array('%s')",
433 // implode("', '", array_keys($default)));
435 if (stristr($default, ' '))
436 $default = "'$default'";
437 $arguments->pushcontent("$arg=$default", HTML::br());
444 class WikiPluginLoader
448 function expandPI($pi, &$request, &$markup, $basepage = false)
450 if (!($ppi = $this->parsePi($pi)))
452 list($pi_name, $plugin, $plugin_args) = $ppi;
454 if (!is_object($plugin)) {
455 return new HtmlElement('div',
456 array('class' => 'error'),
457 $this->getErrorDetail());
461 // FIXME: change API for run() (no $dbi needed).
462 $dbi = $request->getDbh();
463 // pass the parsed CachedMarkup context in dbi to the plugin
464 // to be able to know about itself, or even to change the markup XmlTree (CreateToc)
465 $dbi->_markup = &$markup;
466 // FIXME: could do better here...
467 if (!$plugin->managesValidators()) {
468 // Output of plugin (potentially) depends on
469 // the state of the WikiDB (other than the current
472 // Lacking other information, we'll assume things
473 // changed last time the wikidb was touched.
475 // As an additional hack, mark the ETag weak, since,
476 // for all we know, the page might depend
477 // on things other than the WikiDB (e.g. PhpWeather,
480 $timestamp = $dbi->getTimestamp();
481 $request->appendValidators(array('dbi_timestamp' => $timestamp,
482 '%mtime' => (int)$timestamp,
485 return $plugin->run($dbi, $plugin_args, $request, $basepage);
487 return $plugin->makeList($plugin_args, $request, $basepage);
489 return $plugin->makeForm($plugin_args, $request);
494 function getWikiPageLinks($pi, $basepage)
496 if (!($ppi = $this->parsePi($pi)))
498 list($pi_name, $plugin, $plugin_args) = $ppi;
499 if (!is_object($plugin))
501 if ($pi_name != 'plugin')
503 return $plugin->getWikiPageLinks($plugin_args, $basepage);
506 function parsePI($pi)
508 if (!preg_match('/^\s*<\?(plugin(?:-form|-link|-list)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
509 return $this->_error(sprintf("Bad %s", 'PI'));
511 list(, $pi_name, $plugin_name, $plugin_args) = $m;
512 $plugin = $this->getPlugin($plugin_name, $pi);
514 return array($pi_name, $plugin, $plugin_args);
517 function getPlugin($plugin_name, $pi = false)
519 global $ErrorManager;
520 global $AllAllowedPlugins;
522 if (in_array($plugin_name, $AllAllowedPlugins) === false) {
523 return $this->_error(sprintf(_("Plugin ā%sā does not exist."),
527 // Note that there seems to be no way to trap parse errors
528 // from this include. (At least not via set_error_handler().)
529 $plugin_source = "lib/plugin/$plugin_name.php";
531 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
532 $plugin_class = "WikiPlugin_$plugin_name";
533 if (!class_exists($plugin_class)) {
534 // $include_failed = !@include_once("lib/plugin/$plugin_name.php");
535 $include_failed = !include_once($plugin_source);
536 $ErrorManager->popErrorHandler();
538 if (!class_exists($plugin_class)) {
539 if ($include_failed) {
540 return $this->_error(sprintf(_("Plugin ā%sā does not exist."),
543 return $this->_error(sprintf(_("%s: no such class"), $plugin_class));
546 $ErrorManager->popErrorHandler();
547 $plugin = new $plugin_class;
548 if (!is_subclass_of($plugin, "WikiPlugin"))
549 return $this->_error(sprintf(_("%s: not a subclass of WikiPlugin."),
556 function _plugin_error_filter($err)
558 if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
559 return true; // Ignore this error --- it's expected.
563 function getErrorDetail()
565 return $this->_errors;
568 function _error($message)
570 $this->_errors = $message;
579 // c-hanging-comment-ender-p: nil
580 // indent-tabs-mode: nil