2 rcs_id('$Id: WikiPlugin.php,v 1.1 2001-09-18 19:16:23 dairiki Exp $');
6 function getDefaultArguments() {
12 function run ($argstr, $request) {
13 trigger_error("WikiPlugin::run: pure virtual function",
22 function getArgs($argstr, $request, $defaults = false) {
23 if ($defaults === false)
24 $defaults = $this->getDefaultArguments();
26 list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
28 foreach ($defaults as $arg => $default_val) {
29 if (isset($argstr_args[$arg]))
30 $args[$arg] = $argstr_args[$arg];
31 elseif ( ($argval = $request->getArg($arg)) )
32 $args[$arg] = $argval;
33 elseif (isset($argstr_defaults[$arg]))
34 $args[$arg] = (string) $argstr_defaults[$arg];
36 $args[$arg] = $default_val;
38 $args[$arg] = $this->expandArg($args[$arg], $request);
40 unset($argstr_args[$arg]);
41 unset($argstr_defaults[$arg]);
44 foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
45 trigger_error("$arg: argument not declared by plugin",
52 function expandArg($argval, $request) {
53 return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")', $argval);
57 function parseArgStr($argstr) {
64 $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws ($qq_p|$q_p|$word_p)";
69 while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
70 @ list(,$arg,$op,$val) = $m;
71 $argstr = substr($argstr, strlen($m[0]));
73 // Remove quotes from string values.
74 if ($val && ($val[0] == '"' || $val[0] == "'"))
75 $val = substr($val, 1, strlen($val) - 2);
82 $defaults[$arg] = $val;
87 trigger_error("trailing cruft in plugin args: '$argstr'", E_USER_WARNING);
90 return array($args, $defaults);
94 function getDefaultLinkArguments() {
95 return array('targetpage' => $this->getName(),
96 'linktext' => $this->getName(),
97 'description' => false,
98 'class' => 'wikiaction');
101 function makeLink($argstr, $request) {
102 $defaults = $this->getDefaultArguments();
103 $link_defaults = $this->getDefaultLinkArguments();
104 $defaults = array_merge($defaults, $link_defaults);
106 $args = $this->getArgs($argstr, $request, $defaults);
107 $plugin = $this->getName();
109 $query_args = array();
110 foreach ($args as $arg => $val) {
111 if (isset($link_defaults[$arg]))
113 if ($val != $defaults[$arg])
114 $query_args[$arg] = $val;
117 $attr = array('href' => WikiURL($args['targetpage'], $query_args),
118 'class' => $args['class']);
120 if ($args['description']) {
121 $attr['title'] = $args['description'];
122 $attr['onmouseover'] = sprintf("window.status='%s';return true;",
123 str_replace("'", "\\'", $args['description']));
124 $attr['onmouseout'] = "window.status='';return true;";
126 return QElement('a', $attr, $args['linktext']);
129 function getDefaultFormArguments() {
130 return array('targetpage' => $this->getName(),
131 'buttontext' => $this->getName(),
132 'class' => 'wikiaction',
135 'description' => false,
139 function makeForm($argstr, $request) {
140 $form_defaults = $this->getDefaultFormArguments();
141 $defaults = array_merge($this->getDefaultArguments(),
144 $args = $this->getArgs($argstr, $request, $defaults);
145 $plugin = $this->getName();
146 $textinput = $args['textinput'];
147 assert(!empty($textinput) && isset($args['textinput']));
149 $formattr = array('action' => WikiURL($args['targetpage']),
150 'method' => $args['method'],
151 'class' => $args['class']);
153 foreach ($args as $arg => $val) {
154 if (isset($form_defaults[$arg]))
156 if ($arg != $textinput && $val == $defaults[$arg])
159 $attr = array('name' => $arg, 'value' => $val);
161 if ($arg == $textinput) {
162 //if ($inputs[$arg] == 'file')
163 // $attr['type'] = 'file';
165 $attr['type'] = 'text';
166 $attr['size'] = $args['formsize'];
167 if ($args['description']) {
168 $attr['title'] = $args['description'];
169 $attr['onmouseover'] = sprintf("window.status='%s';return true;",
170 str_replace("'", "\\'", $args['description']));
171 $attr['onmouseout'] = "window.status='';return true;";
175 $attr['type'] = 'hidden';
178 $contents .= Element('input', $attr);
181 if ($attr['type'] == 'file') {
182 $formattr['enctype'] = 'multipart/form-data';
183 $formattr['method'] = 'post';
184 $contents .= Element('input',
185 array('name' => 'MAX_FILE_SIZE',
186 'value' => MAX_UPLOAD_SIZE,
187 'type' => 'hidden'));
191 if (!empty($args['buttontext'])) {
192 $contents .= Element('input',
193 array('type' => 'submit',
195 'value' => $args['buttontext']));
198 //FIXME: can we do without this table?
199 return Element('form', $formattr,
202 Element('td', $contents))));
206 class WikiPluginLoader {
209 function expandPI($pi, $dbi, $request) {
210 if (!preg_match('/^\s*<\?(plugin(?:-form|-link)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
211 return $this->_error("Bad PI");
213 list(, $pi_name, $plugin_name, $plugin_args) = $m;
214 $plugin = $this->getPlugin($plugin_name);
215 if (!is_object($plugin)) {
216 return QElement($pi_name == 'plugin-link' ? 'span' : 'p',
217 array('class' => 'plugin-error'),
218 $this->getErrorDetail());
222 return $plugin->run($dbi, $plugin_args, $request);
224 return $plugin->makeLink($plugin_args, $request);
226 return $plugin->makeForm($plugin_args, $request);
230 function getPlugin($plugin_name) {
232 // Note that there seems to be no way to trap parse errors
233 // from this include. (At least not via set_error_handler().)
234 $plugin_source = "lib/plugin/$plugin_name.php";
236 if (!include_once("lib/plugin/$plugin_name.php")) {
237 if (!empty($GLOBALS['php_errormsg']))
238 return $this->_error($GLOBALS['php_errormsg']);
239 // If the plugin source has already been included, the include_once()
240 // will fail, so we don't want to crap out just yet.
241 $include_failed = true;
244 $plugin_class = "WikiPlugin_$plugin_name";
245 if (!class_exists($plugin_class)) {
247 return $this->_error("Include of '$plugin_source' failed");
248 return $this->_error("$plugin_class: no such class");
252 $plugin = new $plugin_class;
253 if (!is_subclass_of($plugin, "WikiPlugin"))
254 return $this->_error("$plugin_class: not a subclass of WikiPlugin");
259 function getErrorDetail() {
260 return htmlspecialchars($this->_errors);
263 function _error($message) {
264 $this->_errors = $message;
271 // (c-file-style: "gnu")
276 // c-hanging-comment-ender-p: nil
277 // indent-tabs-mode: nil