2 rcs_id('$Id: WikiPlugin.php,v 1.24 2002-08-27 21:51:31 rurban Exp $');
6 function getDefaultArguments() {
7 return array('description' => $this->getDescription());
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"),
65 $arg), E_USER_NOTICE);
71 function expandArg($argval, $request) {
72 return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
77 function parseArgStr($argstr) {
82 $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
83 //"<--kludge for brain-dead syntax coloring
84 $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
85 $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
86 $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
91 while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
92 @ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
93 $argstr = substr($argstr, strlen($m[0]));
95 // Remove quotes from string values.
97 $val = stripslashes($qq_val);
99 $val = stripslashes($q_val);
101 $val = _(stripslashes($gt_val));
109 // NOTE: This does work for multiple args. Use the
110 // separator character defined in your webserver
111 // configuration, usually & or & (See
112 // http://www.htmlhelp.com/faq/cgifaq.4.html)
113 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
114 // url: RecentChanges?days=1&show_all=1&show_minor=0
115 assert($op == '||=');
116 $defaults[$arg] = $val;
121 trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),
122 $argstr), E_USER_NOTICE);
125 return array($args, $defaults);
129 function getDefaultLinkArguments() {
130 return array('targetpage' => $this->getName(),
131 'linktext' => $this->getName(),
132 'description' => $this->getDescription(),
133 'class' => 'wikiaction');
136 function makeLink($argstr, $request) {
137 $defaults = $this->getDefaultArguments();
138 $link_defaults = $this->getDefaultLinkArguments();
139 $defaults = array_merge($defaults, $link_defaults);
141 $args = $this->getArgs($argstr, $request, $defaults);
142 $plugin = $this->getName();
144 $query_args = array();
145 foreach ($args as $arg => $val) {
146 if (isset($link_defaults[$arg]))
148 if ($val != $defaults[$arg])
149 $query_args[$arg] = $val;
152 $link = Button($query_args, $args['linktext'], $args['targetpage']);
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 if (! USE_PATH_INFO ) {
184 $pagename = $request->get('pagename');
185 $form->pushContent(HTML::input(array('type' => 'hidden', 'name' => 'pagename',
186 'value' => $args['targetpage'])));
188 $contents = HTML::div();
189 $contents->setAttr('class', $args['class']);
191 foreach ($args as $arg => $val) {
192 if (isset($form_defaults[$arg]))
194 if ($arg != $textinput && $val == $defaults[$arg])
197 $i = HTML::input(array('name' => $arg, 'value' => $val));
199 if ($arg == $textinput) {
200 //if ($inputs[$arg] == 'file')
201 // $attr['type'] = 'file';
203 $i->setAttr('type', 'text');
204 $i->setAttr('size', $args['formsize']);
205 if ($args['description'])
206 $i->addTooltip($args['description']);
209 $i->setAttr('type', 'hidden');
211 $contents->pushContent($i);
214 if ($i->getAttr('type') == 'file') {
215 $form->setAttr('enctype', 'multipart/form-data');
216 $form->setAttr('method', 'post');
217 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
218 'value' => MAX_UPLOAD_SIZE,
219 'type' => 'hidden')));
223 if (!empty($args['buttontext']))
224 $contents->pushContent(HTML::input(array('type' => 'submit',
226 'value' => $args['buttontext'])));
228 $form->pushContent($contents);
232 function error ($message) {
233 return HTML::div(array('class' => 'errors'),
234 HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
239 class WikiPluginLoader {
242 function expandPI($pi, $request) {
243 if (!preg_match('/^\s*<\?(plugin(?:-form|-link|-head)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
244 return $this->_error(sprintf("Bad %s", 'PI'));
246 list(, $pi_name, $plugin_name, $plugin_args) = $m;
247 $plugin = $this->getPlugin($plugin_name);
248 if (!is_object($plugin)) {
249 return new HtmlElement($pi_name == 'plugin-link' ? 'span' : 'p',
250 array('class' => 'plugin-error'),
251 $this->getErrorDetail());
256 // FIXME: change API for run() (no $dbi needed).
257 $dbi = $request->getDbh();
258 return $plugin->run($dbi, $plugin_args, $request);
260 return $plugin->makeLink($plugin_args, $request);
262 return $plugin->makeForm($plugin_args, $request);
266 function getPlugin($plugin_name) {
267 global $ErrorManager;
269 // Note that there seems to be no way to trap parse errors
270 // from this include. (At least not via set_error_handler().)
271 $plugin_source = "lib/plugin/$plugin_name.php";
273 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
274 $include_failed = !include_once("lib/plugin/$plugin_name.php");
275 $ErrorManager->popErrorHandler();
277 $plugin_class = "WikiPlugin_$plugin_name";
278 if (!class_exists($plugin_class)) {
280 return $this->_error(sprintf(_("Include of '%s' failed"),
282 return $this->_error(sprintf("%s: no such class", $plugin_class));
285 $plugin = new $plugin_class;
286 if (!is_subclass_of($plugin, "WikiPlugin"))
287 return $this->_error(sprintf("%s: not a subclass of WikiPlugin",
293 function _plugin_error_filter ($err) {
294 if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
295 return true; // Ignore this error --- it's expected.
299 function getErrorDetail() {
300 return $this->_errors;
303 function _error($message) {
304 $this->_errors = $message;
311 // (c-file-style: "gnu")
316 // c-hanging-comment-ender-p: nil
317 // indent-tabs-mode: nil