2 rcs_id('$Id: WikiPlugin.php,v 1.45 2004-04-18 01:11:52 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.45 $");
93 function getArgs($argstr, $request=false, $defaults = false) {
94 if ($defaults === false)
95 $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 $args[$arg] = $this->expandArg($args[$arg], $request);
113 unset($argstr_args[$arg]);
114 unset($argstr_defaults[$arg]);
117 foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
118 trigger_error(sprintf(_("argument '%s' not declared by plugin"),
119 $arg), E_USER_NOTICE);
126 // Expand [arg] to $request->getArg("arg") unless preceded by ~
127 function expandArg($argval, $request) {
128 // return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
129 // Replace the arg unless it is preceded by a ~
130 $ret = preg_replace('/([^~]|^)\[(\w[\w\d]*)\]/e',
131 '"$1" . $request->getArg("$2")',
133 // Ditch the ~ so later versions can be expanded if desired
134 return preg_replace('/~(\[\w[\w\d]*\])/', '$1', $ret);
137 function parseArgStr($argstr) {
139 $op_p = '(?:\|\|)?=';
142 $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
143 //"<--kludge for brain-dead syntax coloring
144 $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
145 $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
146 $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
151 while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
152 @ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
153 $argstr = substr($argstr, strlen($m[0]));
155 // Remove quotes from string values.
157 $val = stripslashes($qq_val);
159 $val = stripslashes($q_val);
161 $val = _(stripslashes($gt_val));
169 // NOTE: This does work for multiple args. Use the
170 // separator character defined in your webserver
171 // configuration, usually & or & (See
172 // http://www.htmlhelp.com/faq/cgifaq.4.html)
173 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
174 // url: RecentChanges?days=1&show_all=1&show_minor=0
175 assert($op == '||=');
176 $defaults[$arg] = $val;
181 $this->handle_plugin_args_cruft($argstr, $args);
184 return array($args, $defaults);
187 /* A plugin can override this function to define how any remaining text is handled */
188 function handle_plugin_args_cruft($argstr, $args) {
189 trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),
190 $argstr), E_USER_NOTICE);
193 function getDefaultLinkArguments() {
194 return array('targetpage' => $this->getName(),
195 'linktext' => $this->getName(),
196 'description' => $this->getDescription(),
197 'class' => 'wikiaction');
200 function makeLink($argstr, $request) {
201 $defaults = $this->getDefaultArguments();
202 $link_defaults = $this->getDefaultLinkArguments();
203 $defaults = array_merge($defaults, $link_defaults);
205 $args = $this->getArgs($argstr, $request, $defaults);
206 $plugin = $this->getName();
208 $query_args = array();
209 foreach ($args as $arg => $val) {
210 if (isset($link_defaults[$arg]))
212 if ($val != $defaults[$arg])
213 $query_args[$arg] = $val;
216 $link = Button($query_args, $args['linktext'], $args['targetpage']);
217 if (!empty($args['description']))
218 $link->addTooltip($args['description']);
223 function getDefaultFormArguments() {
224 return array('targetpage' => $this->getName(),
225 'buttontext' => $this->getName(),
226 'class' => 'wikiaction',
229 'description'=> $this->getDescription(),
233 function makeForm($argstr, $request) {
234 $form_defaults = $this->getDefaultFormArguments();
235 $defaults = array_merge($form_defaults,
236 $this->getDefaultArguments());
238 $args = $this->getArgs($argstr, $request, $defaults);
239 $plugin = $this->getName();
240 $textinput = $args['textinput'];
241 assert(!empty($textinput) && isset($args['textinput']));
243 $form = HTML::form(array('action' => WikiURL($args['targetpage']),
244 'method' => $args['method'],
245 'class' => $args['class'],
246 'accept-charset' => $GLOBALS['charset']));
247 if (! USE_PATH_INFO ) {
248 $pagename = $request->get('pagename');
249 $form->pushContent(HTML::input(array('type' => 'hidden', 'name' => 'pagename',
250 'value' => $args['targetpage'])));
252 if ($args['targetpage'] != $this->getName()) {
253 $form->pushContent(HTML::input(array('type' => 'hidden',
255 'value' => $this->getName())));
257 $contents = HTML::div();
258 $contents->setAttr('class', $args['class']);
260 foreach ($args as $arg => $val) {
261 if (isset($form_defaults[$arg]))
263 if ($arg != $textinput && $val == $defaults[$arg])
266 $i = HTML::input(array('name' => $arg, 'value' => $val));
268 if ($arg == $textinput) {
269 //if ($inputs[$arg] == 'file')
270 // $attr['type'] = 'file';
272 $i->setAttr('type', 'text');
273 $i->setAttr('size', $args['formsize']);
274 if ($args['description'])
275 $i->addTooltip($args['description']);
278 $i->setAttr('type', 'hidden');
280 $contents->pushContent($i);
283 if ($i->getAttr('type') == 'file') {
284 $form->setAttr('enctype', 'multipart/form-data');
285 $form->setAttr('method', 'post');
286 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
287 'value' => MAX_UPLOAD_SIZE,
288 'type' => 'hidden')));
292 if (!empty($args['buttontext']))
293 $contents->pushContent(HTML::input(array('type' => 'submit',
295 'value' => $args['buttontext'])));
296 $form->pushContent($contents);
300 function makeBox($title,$body) {
301 if (!$title) $title = $this->_getName();
302 return HTML::div(array('class'=>'box'),
303 HTML::div(array('class'=>'box-title'),$title),
304 HTML::div(array('class'=>'box-data'),$body));
307 function error ($message) {
308 return HTML::div(array('class' => 'errors'),
309 HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
313 function disabled ($message='') {
314 $html[] = HTML::div(array('class' => 'title'),
315 fmt("Plugin %s disabled.", $this->getName()),
317 $html[] = HTML::pre($this->_pi);
318 return HTML::div(array('class' => 'disabled-plugin'), $html);
321 // TODO: Not really needed, since our plugins generally initialize their own
322 // PageList object, which accepts options['types'].
323 // Register custom PageList types for special plugins, like
324 // 'hi_content' for WikiAdminSearcheplace, 'renamed_pagename' for WikiAdminRename, ...
325 function addPageListColumn ($array) {
326 global $customPageListColumns;
327 if (empty($customPageListColumns)) $customPageListColumns = array();
328 foreach ($array as $column => $obj) {
329 $customPageListColumns[$column] = $obj;
333 // provide a sample usage text for automatic edit-toolbar insertion
334 function getUsage() {
335 $args = $this->getDefaultArguments();
336 $string = '<'.'?plugin '.$this->getName().' ';
338 foreach ($args as $key => $value) {
339 $string .= ($key."||=".(string)$value." ");
342 return $string . '?'.'>';
346 class WikiPluginLoader {
349 function expandPI($pi, &$request, &$markup, $basepage=false) {
350 if (!($ppi = $this->parsePi($pi)))
352 list($pi_name, $plugin, $plugin_args) = $ppi;
354 if (!is_object($plugin)) {
355 return new HtmlElement($pi_name == 'plugin-link' ? 'span' : 'p',
356 array('class' => 'plugin-error'),
357 $this->getErrorDetail());
361 // FIXME: change API for run() (no $dbi needed).
362 $dbi = $request->getDbh();
363 // pass the parsed CachedMarkup context in dbi to the plugin
364 // to be able to know about itself, or even to change the markup XmlTree (CreateToc)
365 $dbi->_markup = &$markup;
366 // FIXME: could do better here...
367 if (! $plugin->managesValidators()) {
368 // Output of plugin (potentially) depends on
369 // the state of the WikiDB (other than the current
372 // Lacking other information, we'll assume things
373 // changed last time the wikidb was touched.
375 // As an additional hack, mark the ETag weak, since,
376 // for all we know, the page might depend
377 // on things other than the WikiDB (e.g. PhpWeather,
380 $timestamp = $dbi->getTimestamp();
381 $request->appendValidators(array('dbi_timestamp' => $timestamp,
382 '%mtime' => (int)$timestamp,
385 return $plugin->run($dbi, $plugin_args, $request, $basepage);
387 return $plugin->makeLink($plugin_args, $request);
389 return $plugin->makeForm($plugin_args, $request);
393 function getWikiPageLinks($pi, $basepage) {
394 if (!($ppi = $this->parsePi($pi)))
396 list($pi_name, $plugin, $plugin_args) = $ppi;
397 if (!is_object($plugin))
399 if ($pi_name != 'plugin')
401 return $plugin->getWikiPageLinks($plugin_args, $basepage);
404 function parsePI($pi) {
405 if (!preg_match('/^\s*<\?(plugin(?:-form|-link)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
406 return $this->_error(sprintf("Bad %s", 'PI'));
408 list(, $pi_name, $plugin_name, $plugin_args) = $m;
409 $plugin = $this->getPlugin($plugin_name, $pi);
411 return array($pi_name, $plugin, $plugin_args);
414 function getPlugin($plugin_name, $pi=false) {
415 global $ErrorManager;
417 // Note that there seems to be no way to trap parse errors
418 // from this include. (At least not via set_error_handler().)
419 $plugin_source = "lib/plugin/$plugin_name.php";
421 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
422 $plugin_class = "WikiPlugin_$plugin_name";
423 if (!class_exists($plugin_class)) {
424 // $include_failed = !@include_once("lib/plugin/$plugin_name.php");
425 $include_failed = !include_once("lib/plugin/$plugin_name.php");
426 $ErrorManager->popErrorHandler();
428 if (!class_exists($plugin_class)) {
430 return $this->_error(sprintf(_("Include of '%s' failed"),
432 return $this->_error(sprintf(_("%s: no such class"), $plugin_class));
435 $plugin = new $plugin_class;
436 if (!is_subclass_of($plugin, "WikiPlugin"))
437 return $this->_error(sprintf(_("%s: not a subclass of WikiPlugin"),
444 function _plugin_error_filter ($err) {
445 if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
446 return true; // Ignore this error --- it's expected.
450 function getErrorDetail() {
451 return $this->_errors;
454 function _error($message) {
455 $this->_errors = $message;
460 // (c-file-style: "gnu")
465 // c-hanging-comment-ender-p: nil
466 // indent-tabs-mode: nil