2 rcs_id('$Id: WikiPlugin.php,v 1.14 2002-01-24 00:45:28 dairiki Exp $');
6 function getDefaultArguments() {
12 function run ($argstr, $request) {
13 trigger_error("WikiPlugin::run: pure virtual function",
20 * This is used (by default) by getDefaultLinkArguments and
21 * getDefaultFormArguments to compute the default link/form
24 * If you want to gettextify the name (probably a good idea),
25 * override this method in your plugin class, like:
27 * function getName() { return _("MyPlugin"); }
30 * @return string plugin name/target.
33 return preg_replace('/^.*_/', '', get_class($this));
36 function getDescription() {
37 return $this->getName();
41 function getArgs($argstr, $request, $defaults = false) {
42 if ($defaults === false)
43 $defaults = $this->getDefaultArguments();
45 list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
47 foreach ($defaults as $arg => $default_val) {
48 if (isset($argstr_args[$arg]))
49 $args[$arg] = $argstr_args[$arg];
50 elseif ( ($argval = $request->getArg($arg)) !== false )
51 $args[$arg] = $argval;
52 elseif (isset($argstr_defaults[$arg]))
53 $args[$arg] = (string) $argstr_defaults[$arg];
55 $args[$arg] = $default_val;
57 $args[$arg] = $this->expandArg($args[$arg], $request);
59 unset($argstr_args[$arg]);
60 unset($argstr_defaults[$arg]);
63 foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
64 trigger_error(sprintf(_("argument '%s' not declared by plugin"),$arg),
71 function expandArg($argval, $request) {
72 return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")', $argval);
76 function parseArgStr($argstr) {
81 $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
82 $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
83 $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
84 $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
89 while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
90 @ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
91 $argstr = substr($argstr, strlen($m[0]));
93 // Remove quotes from string values.
95 $val = stripslashes($qq_val);
97 $val = stripslashes($q_val);
99 $val = gettext(stripslashes($gt_val));
107 // NOTE: This does work for multiple args. Use the
108 // separator character defined in your webserver
109 // configuration, usually & or & (See
110 // http://www.htmlhelp.com/faq/cgifaq.4.html)
111 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
112 // url: RecentChanges?days=1&show_all=1&show_minor=0
113 assert($op == '||=');
114 $defaults[$arg] = $val;
119 trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),$argstr), E_USER_WARNING);
122 return array($args, $defaults);
126 function getDefaultLinkArguments() {
127 return array('targetpage' => $this->getName(),
128 'linktext' => $this->getName(),
129 'description' => $this->getDescription(),
130 'class' => 'wikiaction');
133 function makeLink($argstr, $request) {
134 $defaults = $this->getDefaultArguments();
135 $link_defaults = $this->getDefaultLinkArguments();
136 $defaults = array_merge($defaults, $link_defaults);
138 $args = $this->getArgs($argstr, $request, $defaults);
139 $plugin = $this->getName();
141 $query_args = array();
142 foreach ($args as $arg => $val) {
143 if (isset($link_defaults[$arg]))
145 if ($val != $defaults[$arg])
146 $query_args[$arg] = $val;
150 $link = $Theme->makeButton($args['linktext'],
151 WikiURL($args['targetpage'], $query_args),
153 if (!empty($args['description']))
154 $link->addTooltip($args['description']);
159 function getDefaultFormArguments() {
160 return array('targetpage' => $this->getName(),
161 'buttontext' => $this->getName(),
162 'class' => 'wikiaction',
165 'description' => $this->getDescription(),
169 function makeForm($argstr, $request) {
170 $form_defaults = $this->getDefaultFormArguments();
171 $defaults = array_merge($this->getDefaultArguments(),
174 $args = $this->getArgs($argstr, $request, $defaults);
175 $plugin = $this->getName();
176 $textinput = $args['textinput'];
177 assert(!empty($textinput) && isset($args['textinput']));
179 $form = HTML::form(array('action' => WikiURL($args['targetpage']),
180 'method' => $args['method'],
181 'class' => $args['class'],
182 'accept-charset' => CHARSET));
183 $contents = HTML::td();
185 foreach ($args as $arg => $val) {
186 if (isset($form_defaults[$arg]))
188 if ($arg != $textinput && $val == $defaults[$arg])
191 $i = HTML::input(array('name' => $arg, 'value' => $val));
193 if ($arg == $textinput) {
194 //if ($inputs[$arg] == 'file')
195 // $attr['type'] = 'file';
197 $i->setAttr('type', 'text');
198 $i->setAttr('size', $args['formsize']);
199 if ($args['description'])
200 $i->addTooltip($args['description']);
203 $i->setAttr('type', 'hidden');
205 $contents->pushContent($i);
208 if ($i->getAttr('type') == 'file') {
209 $form->setAttr('enctype', 'multipart/form-data');
210 $form->setAttr('method', 'post');
211 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
212 'value' => MAX_UPLOAD_SIZE,
213 'type' => 'hidden')));
217 if (!empty($args['buttontext']))
218 $contents->pushContent(HTML::input(array('type' => 'submit',
220 'value' => $args['buttontext'])));
222 //FIXME: can we do without this table?
223 $form->pushContent(HTML::table(HTML::tr($contents)));
228 class WikiPluginLoader {
231 function expandPI($pi, $request) {
232 if (!preg_match('/^\s*<\?(plugin(?:-form|-link)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
233 return $this->_error(sprintf(_("Bad %s"),'PI'));
235 list(, $pi_name, $plugin_name, $plugin_args) = $m;
236 $plugin = $this->getPlugin($plugin_name);
237 if (!is_object($plugin)) {
238 return new HtmlElement($pi_name == 'plugin-link' ? 'span' : 'p',
239 array('class' => 'plugin-error'),
240 $this->getErrorDetail());
244 // FIXME: change API for run() (no $dbi needed).
245 $dbi = $request->getDbh();
246 return $plugin->run($dbi, $plugin_args, $request);
248 return $plugin->makeLink($plugin_args, $request);
250 return $plugin->makeForm($plugin_args, $request);
254 function getPlugin($plugin_name) {
256 // Note that there seems to be no way to trap parse errors
257 // from this include. (At least not via set_error_handler().)
258 $plugin_source = "lib/plugin/$plugin_name.php";
260 if (!include_once("lib/plugin/$plugin_name.php")) {
261 if (!empty($GLOBALS['php_errormsg']))
262 return $this->_error($GLOBALS['php_errormsg']);
263 // If the plugin source has already been included, the include_once()
264 // will fail, so we don't want to crap out just yet.
265 $include_failed = true;
267 // this avoids: lib/WikiPlugin.php:265: Notice[8]: Undefined variable: include_failed
268 $include_failed = false;
271 $plugin_class = "WikiPlugin_$plugin_name";
272 if (!class_exists($plugin_class)) {
274 return $this->_error(sprintf(_("Include of '%s' failed"),$plugin_source));
275 return $this->_error(sprintf(_("%s: no such class"),$plugin_class));
279 $plugin = new $plugin_class;
280 if (!is_subclass_of($plugin, "WikiPlugin"))
281 return $this->_error(sprintf(_("%s: not a subclass of WikiPlugin"),$plugin_class));
286 function getErrorDetail() {
287 return $this->_errors;
290 function _error($message) {
291 $this->_errors = $message;
298 // (c-file-style: "gnu")
303 // c-hanging-comment-ender-p: nil
304 // indent-tabs-mode: nil