2 rcs_id('$Id: WikiPlugin.php,v 1.56 2004-11-29 17:56:51 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.56 $");
93 function getArgs($argstr, $request=false, $defaults=false) {
94 if ($defaults === false) {
95 $defaults = $this->getDefaultArguments();
97 //Fixme: on POST argstr is empty
98 list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
100 if (!empty($defaults))
101 foreach ($defaults as $arg => $default_val) {
102 if (isset($argstr_args[$arg])) {
103 $args[$arg] = $argstr_args[$arg];
104 } elseif ( $request and ($argval = $request->getArg($arg)) !== false ) {
105 $args[$arg] = $argval;
106 } elseif (isset($argstr_defaults[$arg])) {
107 $args[$arg] = (string) $argstr_defaults[$arg];
109 $args[$arg] = $default_val;
112 if ($request and is_string($args[$arg]) and strstr($args[$arg], "[")) {
113 $args[$arg] = $this->expandArg($args[$arg], $request);
116 unset($argstr_args[$arg]);
117 unset($argstr_defaults[$arg]);
120 foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
121 if ($request and $request->getArg('pagename') == _("PhpWikiAdministration")
122 and $arg == 'overwrite') // silence this warning
125 trigger_error(sprintf(_("argument '%s' not declared by plugin"),
126 $arg), E_USER_NOTICE);
129 // add special handling of pages and exclude args to accept <! plugin-list !>
130 // and split explodePageList($args['exclude']) => array()
131 // TODO : handle p[] pagehash
132 foreach (array('pages', 'exclude') as $key) {
133 if (!empty($args[$key]) and array_key_exists($key, $defaults))
134 $args[$key] = is_string($args[$key])
135 ? explodePageList($args[$key])
136 : $args[$key]; // <! plugin-list !>
139 // always override sortby,limit from the REQUEST. ignore defaults if defined as such.
140 foreach (array('sortby', 'limit') as $key) {
141 if (array_key_exists($key, $defaults)) {
142 if ($val = $request->getArg($key))
144 elseif (!empty($args[$key])) {
145 $GLOBALS['request']->setArg($key, $args[$key]);
153 // Expand [arg] to $request->getArg("arg") unless preceded by ~
154 function expandArg($argval, &$request) {
155 // return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
156 // Replace the arg unless it is preceded by a ~
157 $ret = preg_replace('/([^~]|^)\[(\w[\w\d]*)\]/e',
158 '"$1" . $request->getArg("$2")',
160 // Ditch the ~ so later versions can be expanded if desired
161 return preg_replace('/~(\[\w[\w\d]*\])/', '$1', $ret);
164 function parseArgStr($argstr) {
168 return array($args,$defaults);
171 $op_p = '(?:\|\|)?=';
174 $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
175 //"<--kludge for brain-dead syntax coloring
176 $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
177 $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
178 $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
180 // handle plugin-list arguments seperately
181 $plugin_p = '<!plugin-list\s+\w+.*?!>';
182 while (preg_match("/^($arg_p) $opt_ws ($op_p) $opt_ws ($plugin_p) $opt_ws/x", $argstr, $m)) {
183 @ list(,$arg, $op, $plugin_val) = $m;
184 $argstr = substr($argstr, strlen($m[0]));
185 $loader = new WikiPluginLoader();
188 $plugin_val = preg_replace(array("/^<!/","/!>$/"),array("<?","?>"),$plugin_val);
189 $val = $loader->expandPI($plugin_val, $GLOBALS['request'], $markup, $basepage);
191 $args[$arg] = $val; // comma delimited pagenames or array()?
193 assert($op == '||=');
194 $defaults[$arg] = $val;
197 while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
198 @ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
199 $argstr = substr($argstr, strlen($m[0]));
201 // Remove quotes from string values.
203 $val = stripslashes($qq_val);
205 $val = stripslashes($q_val);
207 $val = _(stripslashes($gt_val));
215 // NOTE: This does work for multiple args. Use the
216 // separator character defined in your webserver
217 // configuration, usually & or & (See
218 // http://www.htmlhelp.com/faq/cgifaq.4.html)
219 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
220 // url: RecentChanges?days=1&show_all=1&show_minor=0
221 assert($op == '||=');
222 $defaults[$arg] = $val;
227 $this->handle_plugin_args_cruft($argstr, $args);
230 return array($args, $defaults);
233 /* A plugin can override this function to define how any remaining text is handled */
234 function handle_plugin_args_cruft($argstr, $args) {
235 trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),
236 $argstr), E_USER_NOTICE);
239 /* handle plugin-list argument: use run(). */
240 function makeList($plugin_args, $request, $basepage) {
241 $dbi = $request->getDbh();
242 $pagelist = $this->run($dbi, $plugin_args, $request, $basepage);
244 if (is_object($pagelist) and isa($pagelist, 'PageList')) {
246 foreach ($pagelist->_pages as $page) {
247 $list[] = $page->getName();
253 function getDefaultLinkArguments() {
254 return array('targetpage' => $this->getName(),
255 'linktext' => $this->getName(),
256 'description' => $this->getDescription(),
257 'class' => 'wikiaction');
260 function makeLink($argstr, $request) {
261 $defaults = $this->getDefaultArguments();
262 $link_defaults = $this->getDefaultLinkArguments();
263 $defaults = array_merge($defaults, $link_defaults);
265 $args = $this->getArgs($argstr, $request, $defaults);
266 $plugin = $this->getName();
268 $query_args = array();
269 foreach ($args as $arg => $val) {
270 if (isset($link_defaults[$arg]))
272 if ($val != $defaults[$arg])
273 $query_args[$arg] = $val;
276 $link = Button($query_args, $args['linktext'], $args['targetpage']);
277 if (!empty($args['description']))
278 $link->addTooltip($args['description']);
283 function getDefaultFormArguments() {
284 return array('targetpage' => $this->getName(),
285 'buttontext' => $this->getName(),
286 'class' => 'wikiaction',
289 'description'=> $this->getDescription(),
293 function makeForm($argstr, $request) {
294 $form_defaults = $this->getDefaultFormArguments();
295 $defaults = array_merge($form_defaults,
296 array('start_debug' => $request->getArg('start_debug')),
297 $this->getDefaultArguments());
299 $args = $this->getArgs($argstr, $request, $defaults);
300 $plugin = $this->getName();
301 $textinput = $args['textinput'];
302 assert(!empty($textinput) && isset($args['textinput']));
304 $form = HTML::form(array('action' => WikiURL($args['targetpage']),
305 'method' => $args['method'],
306 'class' => $args['class'],
307 'accept-charset' => $GLOBALS['charset']));
308 if (! USE_PATH_INFO ) {
309 $pagename = $request->get('pagename');
310 $form->pushContent(HTML::input(array('type' => 'hidden',
311 'name' => 'pagename',
312 'value' => $args['targetpage'])));
314 if ($args['targetpage'] != $this->getName()) {
315 $form->pushContent(HTML::input(array('type' => 'hidden',
317 'value' => $this->getName())));
319 $contents = HTML::div();
320 $contents->setAttr('class', $args['class']);
322 foreach ($args as $arg => $val) {
323 if (isset($form_defaults[$arg]))
325 if ($arg != $textinput && $val == $defaults[$arg])
328 $i = HTML::input(array('name' => $arg, 'value' => $val));
330 if ($arg == $textinput) {
331 //if ($inputs[$arg] == 'file')
332 // $attr['type'] = 'file';
334 $i->setAttr('type', 'text');
335 $i->setAttr('size', $args['formsize']);
336 if ($args['description'])
337 $i->addTooltip($args['description']);
340 $i->setAttr('type', 'hidden');
342 $contents->pushContent($i);
345 if ($i->getAttr('type') == 'file') {
346 $form->setAttr('enctype', 'multipart/form-data');
347 $form->setAttr('method', 'post');
348 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
349 'value' => MAX_UPLOAD_SIZE,
350 'type' => 'hidden')));
354 if (!empty($args['buttontext']))
355 $contents->pushContent(HTML::input(array('type' => 'submit',
357 'value' => $args['buttontext'])));
358 $form->pushContent($contents);
362 function makeBox($title,$body) {
363 if (!$title) $title = $this->_getName();
364 return HTML::div(array('class'=>'box'),
365 HTML::div(array('class'=>'box-title'),$title),
366 HTML::div(array('class'=>'box-data'),$body));
369 function error ($message) {
370 return HTML::div(array('class' => 'errors'),
371 HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
375 function disabled ($message='') {
376 $html[] = HTML::div(array('class' => 'title'),
377 fmt("Plugin %s disabled.", $this->getName()),
379 $html[] = HTML::pre($this->_pi);
380 return HTML::div(array('class' => 'disabled-plugin'), $html);
383 // TODO: Not really needed, since our plugins generally initialize their own
384 // PageList object, which accepts options['types'].
385 // Register custom PageList types for special plugins, like
386 // 'hi_content' for WikiAdminSearcheplace, 'renamed_pagename' for WikiAdminRename, ...
387 function addPageListColumn ($array) {
388 global $customPageListColumns;
389 if (empty($customPageListColumns)) $customPageListColumns = array();
390 foreach ($array as $column => $obj) {
391 $customPageListColumns[$column] = $obj;
395 // provide a sample usage text for automatic edit-toolbar insertion
396 function getUsage() {
397 $args = $this->getDefaultArguments();
398 $string = '<'.'?plugin '.$this->getName().' ';
400 foreach ($args as $key => $value) {
401 $string .= ($key."||=".(string)$value." ");
404 return $string . '?'.'>';
408 class WikiPluginLoader {
411 function expandPI($pi, &$request, &$markup, $basepage=false) {
412 if (!($ppi = $this->parsePi($pi)))
414 list($pi_name, $plugin, $plugin_args) = $ppi;
416 if (!is_object($plugin)) {
417 return new HtmlElement($pi_name == 'plugin-link' ? 'span' : 'p',
418 array('class' => 'plugin-error'),
419 $this->getErrorDetail());
423 // FIXME: change API for run() (no $dbi needed).
424 $dbi = $request->getDbh();
425 // pass the parsed CachedMarkup context in dbi to the plugin
426 // to be able to know about itself, or even to change the markup XmlTree (CreateToc)
427 $dbi->_markup = &$markup;
428 // FIXME: could do better here...
429 if (! $plugin->managesValidators()) {
430 // Output of plugin (potentially) depends on
431 // the state of the WikiDB (other than the current
434 // Lacking other information, we'll assume things
435 // changed last time the wikidb was touched.
437 // As an additional hack, mark the ETag weak, since,
438 // for all we know, the page might depend
439 // on things other than the WikiDB (e.g. PhpWeather,
442 $timestamp = $dbi->getTimestamp();
443 $request->appendValidators(array('dbi_timestamp' => $timestamp,
444 '%mtime' => (int)$timestamp,
447 return $plugin->run($dbi, $plugin_args, $request, $basepage);
449 return $plugin->makeList($plugin_args, $request, $basepage);
451 return $plugin->makeLink($plugin_args, $request);
453 return $plugin->makeForm($plugin_args, $request);
457 function getWikiPageLinks($pi, $basepage) {
458 if (!($ppi = $this->parsePi($pi)))
460 list($pi_name, $plugin, $plugin_args) = $ppi;
461 if (!is_object($plugin))
463 if ($pi_name != 'plugin')
465 return $plugin->getWikiPageLinks($plugin_args, $basepage);
468 function parsePI($pi) {
469 if (!preg_match('/^\s*<\?(plugin(?:-form|-link|-list)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
470 return $this->_error(sprintf("Bad %s", 'PI'));
472 list(, $pi_name, $plugin_name, $plugin_args) = $m;
473 $plugin = $this->getPlugin($plugin_name, $pi);
475 return array($pi_name, $plugin, $plugin_args);
478 function getPlugin($plugin_name, $pi=false) {
479 global $ErrorManager;
481 // Note that there seems to be no way to trap parse errors
482 // from this include. (At least not via set_error_handler().)
483 $plugin_source = "lib/plugin/$plugin_name.php";
485 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
486 $plugin_class = "WikiPlugin_$plugin_name";
487 if (!class_exists($plugin_class)) {
488 // $include_failed = !@include_once("lib/plugin/$plugin_name.php");
489 $include_failed = !include_once("lib/plugin/$plugin_name.php");
490 $ErrorManager->popErrorHandler();
492 if (!class_exists($plugin_class)) {
494 return $this->_error(sprintf(_("Include of '%s' failed"),
496 return $this->_error(sprintf(_("%s: no such class"), $plugin_class));
499 $ErrorManager->popErrorHandler();
500 $plugin = new $plugin_class;
501 if (!is_subclass_of($plugin, "WikiPlugin"))
502 return $this->_error(sprintf(_("%s: not a subclass of WikiPlugin"),
509 function _plugin_error_filter ($err) {
510 if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
511 return true; // Ignore this error --- it's expected.
515 function getErrorDetail() {
516 return $this->_errors;
519 function _error($message) {
520 $this->_errors = $message;
525 // (c-file-style: "gnu")
530 // c-hanging-comment-ender-p: nil
531 // indent-tabs-mode: nil