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 function getArgs($argstr, $request=false, $defaults=false) {
87 if ($defaults === false) {
88 $defaults = $this->getDefaultArguments();
90 //Fixme: on POST argstr is empty
91 list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
93 if (!empty($defaults))
94 foreach ($defaults as $arg => $default_val) {
95 if (isset($argstr_args[$arg])) {
96 $args[$arg] = $argstr_args[$arg];
97 } elseif ( $request and ($argval = $request->getArg($arg)) !== false ) {
98 $args[$arg] = $argval;
99 } elseif (isset($argstr_defaults[$arg])) {
100 $args[$arg] = (string) $argstr_defaults[$arg];
102 $args[$arg] = $default_val;
105 if ($request and is_string($args[$arg]) and strstr($args[$arg], "[")) {
106 $args[$arg] = $this->expandArg($args[$arg], $request);
109 unset($argstr_args[$arg]);
110 unset($argstr_defaults[$arg]);
113 foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
114 // TODO: where the heck comes this from? Put the new method over there and peace.
115 /*if ($request and $request->getArg('pagename') == _("PhpWikiAdministration")
116 and $arg == 'overwrite') // silence this warning
118 if ($this->allow_undeclared_arg($arg, $val))
122 // Add special handling of pages and exclude args to accept <! plugin-list !>
123 // and split explodePageList($args['exclude']) => array()
124 // TODO : handle p[] pagehash
125 foreach (array('pages', 'exclude') as $key) {
126 if (!empty($args[$key]) and array_key_exists($key, $defaults))
127 $args[$key] = is_string($args[$key])
128 ? explodePageList($args[$key])
129 : $args[$key]; // <! plugin-list !>
136 // Expand [arg] to $request->getArg("arg") unless preceded by ~
137 function expandArg($argval, &$request) {
138 // return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
139 // Replace the arg unless it is preceded by a ~
140 $ret = preg_replace('/([^~]|^)\[(\w[\w\d]*)\]/e',
141 '"$1" . $request->getArg("$2")',
143 // Ditch the ~ so later versions can be expanded if desired
144 return preg_replace('/~(\[\w[\w\d]*\])/', '$1', $ret);
147 function parseArgStr($argstr) {
151 return array($args, $defaults);
154 $op_p = '(?:\|\|)?=';
157 $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
158 //"<--kludge for brain-dead syntax coloring
159 $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
160 $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
161 $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
163 // handle plugin-list arguments seperately
164 $plugin_p = '<!plugin-list\s+\w+.*?!>';
165 while (preg_match("/^($arg_p) $opt_ws ($op_p) $opt_ws ($plugin_p) $opt_ws/x", $argstr, $m)) {
166 @ list(,$arg, $op, $plugin_val) = $m;
167 $argstr = substr($argstr, strlen($m[0]));
168 $loader = new WikiPluginLoader();
171 $plugin_val = preg_replace(array("/^<!/","/!>$/"),array("<?","?>"),$plugin_val);
172 $val = $loader->expandPI($plugin_val, $GLOBALS['request'], $markup, $basepage);
174 $args[$arg] = $val; // comma delimited pagenames or array()?
176 assert($op == '||=');
177 $defaults[$arg] = $val;
180 while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
181 //@ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
184 list(, $arg, $op, $qq_val, $q_val, $gt_val, $word_val) = $m;
185 } elseif ($count == 6) {
186 list(, $arg, $op, $qq_val, $q_val, $gt_val) = $m;
187 } elseif ($count == 5) {
188 list(, $arg, $op, $qq_val, $q_val) = $m;
189 } elseif ($count == 4) {
190 list(, $arg, $op, $qq_val) = $m;
192 $argstr = substr($argstr, strlen($m[0]));
193 // Remove quotes from string values.
195 $val = stripslashes($qq_val);
196 elseif ($count > 4 and $q_val)
197 $val = stripslashes($q_val);
198 elseif ($count >= 6 and $gt_val)
199 $val = _(stripslashes($gt_val));
209 // NOTE: This does work for multiple args. Use the
210 // separator character defined in your webserver
211 // configuration, usually & or & (See
212 // http://www.htmlhelp.com/faq/cgifaq.4.html)
213 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
214 // url: RecentChanges?days=1&show_all=1&show_minor=0
215 assert($op == '||=');
216 $defaults[$arg] = $val;
221 $this->handle_plugin_args_cruft($argstr, $args);
224 return array($args, $defaults);
227 /* A plugin can override this function to define how any remaining text is handled */
228 function handle_plugin_args_cruft($argstr, $args) {
229 trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),
230 $argstr), E_USER_NOTICE);
233 /* A plugin can override this to allow undeclared arguments.
234 Or to silence the warning.
236 function allow_undeclared_arg($name, $value) {
237 trigger_error(sprintf(_("Argument '%s' not declared by plugin."),
238 $name), E_USER_NOTICE);
242 /* handle plugin-list argument: use run(). */
243 function makeList($plugin_args, $request, $basepage) {
244 $dbi = $request->getDbh();
245 $pagelist = $this->run($dbi, $plugin_args, $request, $basepage);
247 if (is_object($pagelist) and isa($pagelist, 'PageList'))
248 return $pagelist->pageNames();
249 elseif (is_array($pagelist))
255 function getDefaultLinkArguments() {
256 return array('targetpage' => $this->getName(),
257 'linktext' => $this->getName(),
258 'description' => $this->getDescription(),
259 'class' => 'wikiaction');
262 function getDefaultFormArguments() {
263 return array('targetpage' => $this->getName(),
264 'buttontext' => $this->getName(),
265 'class' => 'wikiaction',
268 'description'=> $this->getDescription(),
272 function makeForm($argstr, $request) {
273 $form_defaults = $this->getDefaultFormArguments();
274 $defaults = array_merge($form_defaults,
275 array('start_debug' => $request->getArg('start_debug')),
276 $this->getDefaultArguments());
278 $args = $this->getArgs($argstr, $request, $defaults);
279 $plugin = $this->getName();
280 $textinput = $args['textinput'];
281 assert(!empty($textinput) && isset($args['textinput']));
283 $form = HTML::form(array('action' => WikiURL($args['targetpage']),
284 'method' => $args['method'],
285 'class' => $args['class'],
286 'accept-charset' => $GLOBALS['charset']));
287 if (! USE_PATH_INFO ) {
288 $pagename = $request->get('pagename');
289 $form->pushContent(HTML::input(array('type' => 'hidden',
290 'name' => 'pagename',
291 'value' => $args['targetpage'])));
293 if ($args['targetpage'] != $this->getName()) {
294 $form->pushContent(HTML::input(array('type' => 'hidden',
296 'value' => $this->getName())));
298 $contents = HTML::div();
299 $contents->setAttr('class', $args['class']);
301 foreach ($args as $arg => $val) {
302 if (isset($form_defaults[$arg]))
304 if ($arg != $textinput && $val == $defaults[$arg])
307 $i = HTML::input(array('name' => $arg, 'value' => $val));
309 if ($arg == $textinput) {
310 //if ($inputs[$arg] == 'file')
311 // $attr['type'] = 'file';
313 $i->setAttr('type', 'text');
314 $i->setAttr('size', $args['formsize']);
315 if ($args['description'])
316 $i->addTooltip($args['description']);
319 $i->setAttr('type', 'hidden');
321 $contents->pushContent($i);
324 if ($i->getAttr('type') == 'file') {
325 $form->setAttr('enctype', 'multipart/form-data');
326 $form->setAttr('method', 'post');
327 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
328 'value' => MAX_UPLOAD_SIZE,
329 'type' => 'hidden')));
333 if (!empty($args['buttontext']))
334 $contents->pushContent(HTML::input(array('type' => 'submit',
336 'value' => $args['buttontext'])));
337 $form->pushContent($contents);
341 // box is used to display a fixed-width, narrow version with common header
342 function box($args=false, $request=false, $basepage=false) {
343 if (!$request) $request =& $GLOBALS['request'];
344 $dbi = $request->getDbh();
345 return $this->makeBox('', $this->run($dbi, $args, $request, $basepage));
348 function makeBox($title, $body) {
349 if (!$title) $title = $this->getName();
350 return HTML::div(array('class'=>'box'),
351 HTML::div(array('class'=>'box-title'), $title),
352 HTML::div(array('class'=>'box-data'), $body));
355 function error ($message) {
356 return HTML::span(array('class' => 'error'),
357 HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
361 function disabled ($message='') {
362 $html[] = HTML::div(array('class' => 'title'),
363 fmt("Plugin %s disabled.", $this->getName()),
365 $html[] = HTML::pre($this->_pi);
366 return HTML::div(array('class' => 'disabled-plugin'), $html);
369 // TODO: Not really needed, since our plugins generally initialize their own
370 // PageList object, which accepts options['types'].
371 // Register custom PageList types for special plugins, like
372 // 'hi_content' for WikiAdminSearcheplace, 'renamed_pagename' for WikiAdminRename, ...
373 function addPageListColumn ($array) {
374 global $customPageListColumns;
375 if (empty($customPageListColumns)) $customPageListColumns = array();
376 foreach ($array as $column => $obj) {
377 $customPageListColumns[$column] = $obj;
381 // provide a sample usage text for automatic edit-toolbar insertion
382 function getUsage() {
383 $args = $this->getDefaultArguments();
384 $string = '<<'.$this->getName().' ';
386 foreach ($args as $key => $value) {
387 $string .= ($key."||=".(string)$value." ");
390 return $string . '>>';
393 function getArgumentsDescription() {
395 foreach ($this->getDefaultArguments() as $arg => $default) {
396 // Work around UserPreferences plugin to avoid error
397 if ((is_array($default))) {
398 $default = '(array)';
399 // This is a bit flawed with UserPreferences object
400 //$default = sprintf("array('%s')",
401 // implode("', '", array_keys($default)));
404 if (stristr($default, ' '))
405 $default = "'$default'";
406 $arguments->pushcontent("$arg=$default", HTML::br());
413 class WikiPluginLoader {
416 function expandPI($pi, &$request, &$markup, $basepage=false) {
417 if (!($ppi = $this->parsePi($pi)))
419 list($pi_name, $plugin, $plugin_args) = $ppi;
421 if (!is_object($plugin)) {
422 return new HtmlElement('div',
423 array('class' => 'error'),
424 $this->getErrorDetail());
428 // FIXME: change API for run() (no $dbi needed).
429 $dbi = $request->getDbh();
430 // pass the parsed CachedMarkup context in dbi to the plugin
431 // to be able to know about itself, or even to change the markup XmlTree (CreateToc)
432 $dbi->_markup = &$markup;
433 // FIXME: could do better here...
434 if (! $plugin->managesValidators()) {
435 // Output of plugin (potentially) depends on
436 // the state of the WikiDB (other than the current
439 // Lacking other information, we'll assume things
440 // changed last time the wikidb was touched.
442 // As an additional hack, mark the ETag weak, since,
443 // for all we know, the page might depend
444 // on things other than the WikiDB (e.g. PhpWeather,
447 $timestamp = $dbi->getTimestamp();
448 $request->appendValidators(array('dbi_timestamp' => $timestamp,
449 '%mtime' => (int)$timestamp,
452 return $plugin->run($dbi, $plugin_args, $request, $basepage);
454 return $plugin->makeList($plugin_args, $request, $basepage);
456 return $plugin->makeForm($plugin_args, $request);
460 function getWikiPageLinks($pi, $basepage) {
461 if (!($ppi = $this->parsePi($pi)))
463 list($pi_name, $plugin, $plugin_args) = $ppi;
464 if (!is_object($plugin))
466 if ($pi_name != 'plugin')
468 return $plugin->getWikiPageLinks($plugin_args, $basepage);
471 function parsePI($pi) {
472 if (!preg_match('/^\s*<\?(plugin(?:-form|-link|-list)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
473 return $this->_error(sprintf("Bad %s", 'PI'));
475 list(, $pi_name, $plugin_name, $plugin_args) = $m;
476 $plugin = $this->getPlugin($plugin_name, $pi);
478 return array($pi_name, $plugin, $plugin_args);
481 function getPlugin($plugin_name, $pi=false) {
482 global $ErrorManager;
483 global $AllAllowedPlugins;
485 if (in_array($plugin_name, $AllAllowedPlugins) === false) {
486 return $this->_error(sprintf(_("Plugin '%s' does not exist."),
490 // Note that there seems to be no way to trap parse errors
491 // from this include. (At least not via set_error_handler().)
492 $plugin_source = "lib/plugin/$plugin_name.php";
494 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
495 $plugin_class = "WikiPlugin_$plugin_name";
496 if (!class_exists($plugin_class)) {
497 // $include_failed = !@include_once("lib/plugin/$plugin_name.php");
498 $include_failed = !include_once("lib/plugin/$plugin_name.php");
499 $ErrorManager->popErrorHandler();
501 if (!class_exists($plugin_class)) {
502 if ($include_failed) {
503 return $this->_error(sprintf(_("Plugin '%s' does not exist."),
506 return $this->_error(sprintf(_("%s: no such class"), $plugin_class));
509 $ErrorManager->popErrorHandler();
510 $plugin = new $plugin_class;
511 if (!is_subclass_of($plugin, "WikiPlugin"))
512 return $this->_error(sprintf(_("%s: not a subclass of WikiPlugin."),
519 function _plugin_error_filter ($err) {
520 if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
521 return true; // Ignore this error --- it's expected.
525 function getErrorDetail() {
526 return $this->_errors;
529 function _error($message) {
530 $this->_errors = $message;
539 // c-hanging-comment-ender-p: nil
540 // indent-tabs-mode: nil