2 rcs_id('$Id: WikiPlugin.php,v 1.10 2002-01-21 06:55:47 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) {
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 // NOTE: This does work for multiple args. Use the
101 // separator character defined in your webserver
102 // configuration, usually & or & (See
103 // http://www.htmlhelp.com/faq/cgifaq.4.html)
104 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
105 // url: RecentChanges?days=1&show_all=1&show_minor=0
106 assert($op == '||=');
107 $defaults[$arg] = $val;
112 trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),$argstr), E_USER_WARNING);
115 return array($args, $defaults);
119 function getDefaultLinkArguments() {
120 return array('targetpage' => $this->getName(),
121 'linktext' => $this->getName(),
122 'description' => $this->getDescription(),
123 'class' => 'wikiaction');
126 function makeLink($argstr, $request) {
127 $defaults = $this->getDefaultArguments();
128 $link_defaults = $this->getDefaultLinkArguments();
129 $defaults = array_merge($defaults, $link_defaults);
131 $args = $this->getArgs($argstr, $request, $defaults);
132 $plugin = $this->getName();
134 $query_args = array();
135 foreach ($args as $arg => $val) {
136 if (isset($link_defaults[$arg]))
138 if ($val != $defaults[$arg])
139 $query_args[$arg] = $val;
142 $attr = array('href' => WikiURL($args['targetpage'], $query_args),
143 'class' => $args['class']);
145 if ($args['description']) {
146 $attr['title'] = $args['description'];
147 $attr['onmouseover'] = sprintf("window.status='%s';return true;",
148 str_replace("'", "\\'", $args['description']));
149 $attr['onmouseout'] = "window.status='';return true;";
151 return QElement('a', $attr, $args['linktext']);
154 function getDefaultFormArguments() {
155 return array('targetpage' => $this->getName(),
156 'buttontext' => $this->getName(),
157 'class' => 'wikiaction',
160 'description' => false,
164 function makeForm($argstr, $request) {
165 $form_defaults = $this->getDefaultFormArguments();
166 $defaults = array_merge($this->getDefaultArguments(),
169 $args = $this->getArgs($argstr, $request, $defaults);
170 $plugin = $this->getName();
171 $textinput = $args['textinput'];
172 assert(!empty($textinput) && isset($args['textinput']));
174 $formattr = array('action' => WikiURL($args['targetpage']),
175 'method' => $args['method'],
176 'class' => $args['class']);
178 foreach ($args as $arg => $val) {
179 if (isset($form_defaults[$arg]))
181 if ($arg != $textinput && $val == $defaults[$arg])
184 $attr = array('name' => $arg, 'value' => $val);
186 if ($arg == $textinput) {
187 //if ($inputs[$arg] == 'file')
188 // $attr['type'] = 'file';
190 $attr['type'] = 'text';
191 $attr['size'] = $args['formsize'];
192 if ($args['description']) {
193 $attr['title'] = $args['description'];
194 $attr['onmouseover'] = sprintf("window.status='%s';return true;",
195 str_replace("'", "\\'", $args['description']));
196 $attr['onmouseout'] = "window.status='';return true;";
200 $attr['type'] = 'hidden';
203 $contents .= Element('input', $attr);
206 if ($attr['type'] == 'file') {
207 $formattr['enctype'] = 'multipart/form-data';
208 $formattr['method'] = 'post';
209 $contents .= Element('input',
210 array('name' => 'MAX_FILE_SIZE',
211 'value' => MAX_UPLOAD_SIZE,
212 'type' => 'hidden'));
216 if (!empty($args['buttontext'])) {
217 $contents .= Element('input',
218 array('type' => 'submit',
220 'value' => $args['buttontext']));
223 //FIXME: can we do without this table?
224 return Element('form', $formattr,
227 Element('td', $contents))));
231 class WikiPluginLoader {
234 function expandPI($pi, $dbi, $request) {
235 if (!preg_match('/^\s*<\?(plugin(?:-form|-link)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
236 return $this->_error(sprintf(_("Bad %s"),'PI'));
238 list(, $pi_name, $plugin_name, $plugin_args) = $m;
239 $plugin = $this->getPlugin($plugin_name);
240 if (!is_object($plugin)) {
241 return QElement($pi_name == 'plugin-link' ? 'span' : 'p',
242 array('class' => 'plugin-error'),
243 $this->getErrorDetail());
247 return AsXml($plugin->run($dbi, $plugin_args, $request));
249 return $plugin->makeLink($plugin_args, $request);
251 return $plugin->makeForm($plugin_args, $request);
255 function getPlugin($plugin_name) {
257 // Note that there seems to be no way to trap parse errors
258 // from this include. (At least not via set_error_handler().)
259 $plugin_source = "lib/plugin/$plugin_name.php";
261 if (!include_once("lib/plugin/$plugin_name.php")) {
262 if (!empty($GLOBALS['php_errormsg']))
263 return $this->_error($GLOBALS['php_errormsg']);
264 // If the plugin source has already been included, the include_once()
265 // will fail, so we don't want to crap out just yet.
266 $include_failed = true;
268 // this avoids: lib/WikiPlugin.php:265: Notice[8]: Undefined variable: include_failed
269 $include_failed = false;
272 $plugin_class = "WikiPlugin_$plugin_name";
273 if (!class_exists($plugin_class)) {
275 return $this->_error(sprintf(_("Include of '%s' failed"),$plugin_source));
276 return $this->_error(sprintf(_("%s: no such class"),$plugin_class));
280 $plugin = new $plugin_class;
281 if (!is_subclass_of($plugin, "WikiPlugin"))
282 return $this->_error(sprintf(_("%s: not a subclass of WikiPlugin"),$plugin_class));
287 function getErrorDetail() {
288 return htmlspecialchars($this->_errors);
291 function _error($message) {
292 $this->_errors = $message;
299 // (c-file-style: "gnu")
304 // c-hanging-comment-ender-p: nil
305 // indent-tabs-mode: nil