2 rcs_id('$Id: WikiPlugin.php,v 1.6 2001-12-16 18:33:25 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("$arg: argument not declared by plugin",
71 function expandArg($argval, $request) {
72 return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")', $argval);
76 function parseArgStr($argstr) {
83 $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws ($qq_p|$q_p|$word_p)";
88 while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
89 @ list(,$arg,$op,$val) = $m;
90 $argstr = substr($argstr, strlen($m[0]));
92 // Remove quotes from string values.
93 if ($val && ($val[0] == '"' || $val[0] == "'"))
94 $val = substr($val, 1, strlen($val) - 2);
100 assert($op == '||=');
101 $defaults[$arg] = $val;
106 trigger_error("trailing cruft in plugin args: '$argstr'", E_USER_WARNING);
109 return array($args, $defaults);
113 function getDefaultLinkArguments() {
114 return array('targetpage' => $this->getName(),
115 'linktext' => $this->getName(),
116 'description' => $this->getDescription(),
117 'class' => 'wikiaction');
120 function makeLink($argstr, $request) {
121 $defaults = $this->getDefaultArguments();
122 $link_defaults = $this->getDefaultLinkArguments();
123 $defaults = array_merge($defaults, $link_defaults);
125 $args = $this->getArgs($argstr, $request, $defaults);
126 $plugin = $this->getName();
128 $query_args = array();
129 foreach ($args as $arg => $val) {
130 if (isset($link_defaults[$arg]))
132 if ($val != $defaults[$arg])
133 $query_args[$arg] = $val;
136 $attr = array('href' => WikiURL($args['targetpage'], $query_args),
137 'class' => $args['class']);
139 if ($args['description']) {
140 $attr['title'] = $args['description'];
141 $attr['onmouseover'] = sprintf("window.status='%s';return true;",
142 str_replace("'", "\\'", $args['description']));
143 $attr['onmouseout'] = "window.status='';return true;";
145 return QElement('a', $attr, $args['linktext']);
148 function getDefaultFormArguments() {
149 return array('targetpage' => $this->getName(),
150 'buttontext' => $this->getName(),
151 'class' => 'wikiaction',
154 'description' => false,
158 function makeForm($argstr, $request) {
159 $form_defaults = $this->getDefaultFormArguments();
160 $defaults = array_merge($this->getDefaultArguments(),
163 $args = $this->getArgs($argstr, $request, $defaults);
164 $plugin = $this->getName();
165 $textinput = $args['textinput'];
166 assert(!empty($textinput) && isset($args['textinput']));
168 $formattr = array('action' => WikiURL($args['targetpage']),
169 'method' => $args['method'],
170 'class' => $args['class']);
172 foreach ($args as $arg => $val) {
173 if (isset($form_defaults[$arg]))
175 if ($arg != $textinput && $val == $defaults[$arg])
178 $attr = array('name' => $arg, 'value' => $val);
180 if ($arg == $textinput) {
181 //if ($inputs[$arg] == 'file')
182 // $attr['type'] = 'file';
184 $attr['type'] = 'text';
185 $attr['size'] = $args['formsize'];
186 if ($args['description']) {
187 $attr['title'] = $args['description'];
188 $attr['onmouseover'] = sprintf("window.status='%s';return true;",
189 str_replace("'", "\\'", $args['description']));
190 $attr['onmouseout'] = "window.status='';return true;";
194 $attr['type'] = 'hidden';
197 $contents .= Element('input', $attr);
200 if ($attr['type'] == 'file') {
201 $formattr['enctype'] = 'multipart/form-data';
202 $formattr['method'] = 'post';
203 $contents .= Element('input',
204 array('name' => 'MAX_FILE_SIZE',
205 'value' => MAX_UPLOAD_SIZE,
206 'type' => 'hidden'));
210 if (!empty($args['buttontext'])) {
211 $contents .= Element('input',
212 array('type' => 'submit',
214 'value' => $args['buttontext']));
217 //FIXME: can we do without this table?
218 return Element('form', $formattr,
221 Element('td', $contents))));
225 class WikiPluginLoader {
228 function expandPI($pi, $dbi, $request) {
229 if (!preg_match('/^\s*<\?(plugin(?:-form|-link)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
230 return $this->_error("Bad PI");
232 list(, $pi_name, $plugin_name, $plugin_args) = $m;
233 $plugin = $this->getPlugin($plugin_name);
234 if (!is_object($plugin)) {
235 return QElement($pi_name == 'plugin-link' ? 'span' : 'p',
236 array('class' => 'plugin-error'),
237 $this->getErrorDetail());
241 return $plugin->run($dbi, $plugin_args, $request);
243 return $plugin->makeLink($plugin_args, $request);
245 return $plugin->makeForm($plugin_args, $request);
249 function getPlugin($plugin_name) {
251 // Note that there seems to be no way to trap parse errors
252 // from this include. (At least not via set_error_handler().)
253 $plugin_source = "lib/plugin/$plugin_name.php";
255 if (!include_once("lib/plugin/$plugin_name.php")) {
256 if (!empty($GLOBALS['php_errormsg']))
257 return $this->_error($GLOBALS['php_errormsg']);
258 // If the plugin source has already been included, the include_once()
259 // will fail, so we don't want to crap out just yet.
260 $include_failed = true;
263 $plugin_class = "WikiPlugin_$plugin_name";
264 if (!class_exists($plugin_class)) {
266 return $this->_error("Include of '$plugin_source' failed");
267 return $this->_error("$plugin_class: no such class");
271 $plugin = new $plugin_class;
272 if (!is_subclass_of($plugin, "WikiPlugin"))
273 return $this->_error("$plugin_class: not a subclass of WikiPlugin");
278 function getErrorDetail() {
279 return htmlspecialchars($this->_errors);
282 function _error($message) {
283 $this->_errors = $message;
290 // (c-file-style: "gnu")
295 // c-hanging-comment-ender-p: nil
296 // indent-tabs-mode: nil