2 rcs_id('$Id: WikiPlugin.php,v 1.29 2003-02-16 05:09:43 dairiki Exp $');
6 function getDefaultArguments() {
7 return array('description' => $this->getDescription());
10 /** Does plugin have static output?
12 * Should return true iff the output of the plugin only depends
13 * on its arguments and/or the contents of the current page.
15 * The safe answer is false, but that will probably make the page
16 * output uncacheable. Plugins should override this method to return
19 function hasStaticOutput() {
25 function run ($dbi, $argstr, &$request) {
26 trigger_error("WikiPlugin::run: pure virtual function",
33 * This is used (by default) by getDefaultLinkArguments and
34 * getDefaultFormArguments to compute the default link/form
37 * If you want to gettextify the name (probably a good idea),
38 * override this method in your plugin class, like:
40 * function getName() { return _("MyPlugin"); }
43 * @return string plugin name/target.
46 return preg_replace('/^.*_/', '', get_class($this));
49 function getDescription() {
50 return $this->getName();
54 function getArgs($argstr, $request, $defaults = false) {
55 if ($defaults === false)
56 $defaults = $this->getDefaultArguments();
58 list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
60 foreach ($defaults as $arg => $default_val) {
61 if (isset($argstr_args[$arg]))
62 $args[$arg] = $argstr_args[$arg];
63 elseif ( ($argval = $request->getArg($arg)) !== false )
64 $args[$arg] = $argval;
65 elseif (isset($argstr_defaults[$arg]))
66 $args[$arg] = (string) $argstr_defaults[$arg];
68 $args[$arg] = $default_val;
70 $args[$arg] = $this->expandArg($args[$arg], $request);
72 unset($argstr_args[$arg]);
73 unset($argstr_defaults[$arg]);
76 foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
77 trigger_error(sprintf(_("argument '%s' not declared by plugin"),
78 $arg), E_USER_NOTICE);
84 function expandArg($argval, $request) {
85 return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
90 function parseArgStr($argstr) {
95 $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
96 //"<--kludge for brain-dead syntax coloring
97 $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
98 $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
99 $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
104 while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
105 @ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
106 $argstr = substr($argstr, strlen($m[0]));
108 // Remove quotes from string values.
110 $val = stripslashes($qq_val);
112 $val = stripslashes($q_val);
114 $val = _(stripslashes($gt_val));
122 // NOTE: This does work for multiple args. Use the
123 // separator character defined in your webserver
124 // configuration, usually & or & (See
125 // http://www.htmlhelp.com/faq/cgifaq.4.html)
126 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
127 // url: RecentChanges?days=1&show_all=1&show_minor=0
128 assert($op == '||=');
129 $defaults[$arg] = $val;
134 $this->handle_plugin_args_cruft($argstr, $args);
137 return array($args, $defaults);
140 /* A plugin can override this function to define how any remaining text is handled */
141 function handle_plugin_args_cruft($argstr, $args) {
142 trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),
143 $argstr), E_USER_NOTICE);
146 function getDefaultLinkArguments() {
147 return array('targetpage' => $this->getName(),
148 'linktext' => $this->getName(),
149 'description' => $this->getDescription(),
150 'class' => 'wikiaction');
153 function makeLink($argstr, $request) {
154 $defaults = $this->getDefaultArguments();
155 $link_defaults = $this->getDefaultLinkArguments();
156 $defaults = array_merge($defaults, $link_defaults);
158 $args = $this->getArgs($argstr, $request, $defaults);
159 $plugin = $this->getName();
161 $query_args = array();
162 foreach ($args as $arg => $val) {
163 if (isset($link_defaults[$arg]))
165 if ($val != $defaults[$arg])
166 $query_args[$arg] = $val;
169 $link = Button($query_args, $args['linktext'], $args['targetpage']);
170 if (!empty($args['description']))
171 $link->addTooltip($args['description']);
176 function getDefaultFormArguments() {
177 return array('targetpage' => $this->getName(),
178 'buttontext' => $this->getName(),
179 'class' => 'wikiaction',
182 'description' => $this->getDescription(),
186 function makeForm($argstr, $request) {
187 $form_defaults = $this->getDefaultFormArguments();
188 $defaults = array_merge($this->getDefaultArguments(),
191 $args = $this->getArgs($argstr, $request, $defaults);
192 $plugin = $this->getName();
193 $textinput = $args['textinput'];
194 assert(!empty($textinput) && isset($args['textinput']));
196 $form = HTML::form(array('action' => WikiURL($args['targetpage']),
197 'method' => $args['method'],
198 'class' => $args['class'],
199 'accept-charset' => CHARSET));
200 if (! USE_PATH_INFO ) {
201 $pagename = $request->get('pagename');
202 $form->pushContent(HTML::input(array('type' => 'hidden', 'name' => 'pagename',
203 'value' => $args['targetpage'])));
205 $contents = HTML::div();
206 $contents->setAttr('class', $args['class']);
208 foreach ($args as $arg => $val) {
209 if (isset($form_defaults[$arg]))
211 if ($arg != $textinput && $val == $defaults[$arg])
214 $i = HTML::input(array('name' => $arg, 'value' => $val));
216 if ($arg == $textinput) {
217 //if ($inputs[$arg] == 'file')
218 // $attr['type'] = 'file';
220 $i->setAttr('type', 'text');
221 $i->setAttr('size', $args['formsize']);
222 if ($args['description'])
223 $i->addTooltip($args['description']);
226 $i->setAttr('type', 'hidden');
228 $contents->pushContent($i);
231 if ($i->getAttr('type') == 'file') {
232 $form->setAttr('enctype', 'multipart/form-data');
233 $form->setAttr('method', 'post');
234 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
235 'value' => MAX_UPLOAD_SIZE,
236 'type' => 'hidden')));
240 if (!empty($args['buttontext']))
241 $contents->pushContent(HTML::input(array('type' => 'submit',
243 'value' => $args['buttontext'])));
245 $form->pushContent($contents);
249 function error ($message) {
250 return HTML::div(array('class' => 'errors'),
251 HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
255 function disabled ($message='') {
256 $html[] = HTML::div(array('class' => 'title'),
257 fmt("Plugin %s disabled.", $this->getName()),
259 $html[] = HTML::pre($this->_pi);
260 return HTML::div(array('class' => 'disabled-plugin'), $html);
264 class WikiPluginLoader {
267 function expandPI($pi, &$request) {
268 if (!preg_match('/^\s*<\?(plugin(?:-form|-link)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
269 return $this->_error(sprintf("Bad %s", 'PI'));
271 list(, $pi_name, $plugin_name, $plugin_args) = $m;
272 $plugin = $this->getPlugin($plugin_name, $pi);
273 if (!is_object($plugin)) {
274 return new HtmlElement($pi_name == 'plugin-link' ? 'span' : 'p',
275 array('class' => 'plugin-error'),
276 $this->getErrorDetail());
280 // FIXME: change API for run() (no $dbi needed).
281 $dbi = $request->getDbh();
282 // FIXME: could do better here...
283 if (! $plugin->hasStaticOutput()) {
284 // Output of plugin (potentially) depends on
285 // the state of the WikiDB (other than the current
286 // page.) Individual plugins should try to add
287 // there own bits to ETag --- but for now, we'll
288 // at least mark ETag as a weak validator.
289 $request->setETagIsWeak();
292 return $plugin->run($dbi, $plugin_args, $request);
294 return $plugin->makeLink($plugin_args, $request);
296 return $plugin->makeForm($plugin_args, $request);
300 // Special treatment. Only called by Template.php:GeneratePage
301 function expandPI_head($pi, $request) {
302 if (!preg_match('/^\s*<\?plugin-head\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
303 return $this->_error(sprintf("Bad %s", 'PI'));
305 list(, $plugin_name, $plugin_args) = $m;
306 $plugin = $this->getPlugin($plugin_name);
307 if (!is_object($plugin)) {
308 return new HtmlElement('p', array('class' => 'plugin-error'),
309 $this->getErrorDetail());
311 $dbi = $request->getDbh();
312 return $plugin->run($dbi, $plugin_args, $request);
315 function getPlugin($plugin_name, $pi) {
316 global $ErrorManager;
318 // Note that there seems to be no way to trap parse errors
319 // from this include. (At least not via set_error_handler().)
320 $plugin_source = "lib/plugin/$plugin_name.php";
322 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
323 $include_failed = !include_once("lib/plugin/$plugin_name.php");
324 $ErrorManager->popErrorHandler();
326 $plugin_class = "WikiPlugin_$plugin_name";
327 if (!class_exists($plugin_class)) {
329 return $this->_error(sprintf(_("Include of '%s' failed"),
331 return $this->_error(sprintf("%s: no such class", $plugin_class));
334 $plugin = new $plugin_class;
335 if (!is_subclass_of($plugin, "WikiPlugin"))
336 return $this->_error(sprintf("%s: not a subclass of WikiPlugin",
343 function _plugin_error_filter ($err) {
344 if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
345 return true; // Ignore this error --- it's expected.
349 function getErrorDetail() {
350 return $this->_errors;
353 function _error($message) {
354 $this->_errors = $message;
361 // (c-file-style: "gnu")
366 // c-hanging-comment-ender-p: nil
367 // indent-tabs-mode: nil