]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiPlugin.php
Fix warnings
[SourceForge/phpwiki.git] / lib / WikiPlugin.php
1 <?php
2
3 class WikiPlugin
4 {
5     function getDefaultArguments()
6     {
7         return array('description' => $this->getDescription());
8     }
9
10     /** Does the plugin manage its own HTTP validators?
11      *
12      * This should be overwritten by (some) individual plugins.
13      *
14      * If the output of the plugin is static, depending only
15      * on the plugin arguments, query arguments and contents
16      * of the current page, this can (and should) return true.
17      *
18      * If the plugin can deduce a modification time, or equivalent
19      * sort of tag for it's content, then the plugin should
20      * call $request->appendValidators() with appropriate arguments,
21      * and should override this method to return true.
22      *
23      * When in doubt, the safe answer here is false.
24      * Unfortunately, returning false here will most likely make
25      * any page which invokes the plugin uncacheable (by HTTP proxies
26      * or browsers).
27      */
28     function managesValidators()
29     {
30         return false;
31     }
32
33     // FIXME: args?
34     function run($dbi, $argstr, &$request, $basepage)
35     {
36         trigger_error("WikiPlugin::run: pure virtual function", E_USER_ERROR);
37         return false;
38     }
39
40     /** Get wiki-pages linked to by plugin invocation.
41      *
42      * A plugin may override this method to add pages to the
43      * link database for the invoking page.
44      *
45      * For example, the IncludePage plugin should override this so
46      * that the including page shows up in the backlinks list for the
47      * included page.
48      *
49      * Not all plugins which generate links to wiki-pages need list
50      * those pages here.
51      *
52      * Note also that currently the links are calculated at page save
53      * time, so only static page links (e.g. those dependent on the PI
54      * args, not the rest of the wikidb state or any request query args)
55      * will work correctly here.
56      *
57      * @param  string $argstr   The plugin argument string.
58      * @param  string $basepage The pagename the plugin is invoked from.
59      * @return array  List of pagenames linked to (or false).
60      */
61     function getWikiPageLinks($argstr, $basepage)
62     {
63         return false;
64     }
65
66     /**
67      * Get name of plugin.
68      *
69      * This is used (by default) by getDefaultLinkArguments and
70      * getDefaultFormArguments to compute the default link/form
71      * targets.
72      *
73      * If you want to gettextify the name (probably a good idea),
74      * override this method in your plugin class, like:
75      * <pre>
76      *   function getName() { return _("MyPlugin"); }
77      * </pre>
78      *
79      * @return string plugin name/target.
80      */
81     function getName()
82     {
83         return preg_replace('/^.*_/', '', get_class($this));
84     }
85
86     function getDescription()
87     {
88         return $this->getName();
89     }
90
91     function getArgs($argstr, $request = false, $defaults = array())
92     {
93         if (empty($defaults)) {
94             $defaults = $this->getDefaultArguments();
95         }
96         //Fixme: on POST argstr is empty
97         list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
98         $args = array();
99         if (!empty($defaults))
100             foreach ($defaults as $arg => $default_val) {
101                 if (isset($argstr_args[$arg])) {
102                     $args[$arg] = $argstr_args[$arg];
103                 } elseif ($request and ($argval = $request->getArg($arg)) !== false) {
104                     $args[$arg] = $argval;
105                 } elseif (isset($argstr_defaults[$arg])) {
106                     $args[$arg] = (string)$argstr_defaults[$arg];
107                 } else {
108                     $args[$arg] = $default_val;
109                 }
110                 // expand [arg]
111                 if ($request and is_string($args[$arg]) and strstr($args[$arg], "[")) {
112                     $args[$arg] = $this->expandArg($args[$arg], $request);
113                 }
114
115                 unset($argstr_args[$arg]);
116                 unset($argstr_defaults[$arg]);
117             }
118
119         foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
120             // TODO: where the heck comes this from? Put the new method over there and peace.
121             /*if ($request and $request->getArg('pagename') == _("PhpWikiAdministration")
122                 and $arg == 'overwrite') // silence this warning
123                 ;*/
124             if ($this->allow_undeclared_arg($arg, $val))
125                 $args[$arg] = $val;
126         }
127
128         // Add special handling of pages and exclude args to accept <! plugin-list !>
129         // and split explodePageList($args['exclude']) => array()
130         // TODO : handle p[] pagehash
131         foreach (array('pages', 'exclude') as $key) {
132             if (!empty($args[$key]) and array_key_exists($key, $defaults))
133                 $args[$key] = is_string($args[$key])
134                     ? explodePageList($args[$key])
135                     : $args[$key]; // <! plugin-list !>
136         }
137
138         return $args;
139     }
140
141     // Patch by Dan F:
142     // Expand [arg] to $request->getArg("arg") unless preceded by ~
143     function expandArg($argval, &$request)
144     {
145         // return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
146         // Replace the arg unless it is preceded by a ~
147         $ret = preg_replace('/([^~]|^)\[(\w[\w\d]*)\]/e',
148             '"$1" . $request->getArg("$2")',
149             $argval);
150         // Ditch the ~ so later versions can be expanded if desired
151         return preg_replace('/~(\[\w[\w\d]*\])/', '$1', $ret);
152     }
153
154     function parseArgStr($argstr)
155     {
156         $args = array();
157         $defaults = array();
158         if (empty($argstr))
159             return array($args, $defaults);
160
161         $arg_p = '\w+';
162         $op_p = '(?:\|\|)?=';
163         $word_p = '\S+';
164         $opt_ws = '\s*';
165         $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
166         //"<--kludge for brain-dead syntax coloring
167         $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
168         $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
169         $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
170
171         // handle plugin-list arguments separately
172         $plugin_p = '<!plugin-list\s+\w+.*?!>';
173         while (preg_match("/^($arg_p) $opt_ws ($op_p) $opt_ws ($plugin_p) $opt_ws/x", $argstr, $m)) {
174             @ list(, $arg, $op, $plugin_val) = $m;
175             $argstr = substr($argstr, strlen($m[0]));
176             $loader = new WikiPluginLoader();
177             $markup = null;
178             $basepage = null;
179             $plugin_val = preg_replace(array("/^<!/", "/!>$/"), array("<?", "?>"), $plugin_val);
180             $val = $loader->expandPI($plugin_val, $GLOBALS['request'], $markup, $basepage);
181             if ($op == '=') {
182                 $args[$arg] = $val; // comma delimited pagenames or array()?
183             } else {
184                 assert($op == '||=');
185                 $defaults[$arg] = $val;
186             }
187         }
188         while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
189             //@ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
190             $count = count($m);
191             if ($count >= 7) {
192                 list(, $arg, $op, $qq_val, $q_val, $gt_val, $word_val) = $m;
193             } elseif ($count == 6) {
194                 list(, $arg, $op, $qq_val, $q_val, $gt_val) = $m;
195             } elseif ($count == 5) {
196                 list(, $arg, $op, $qq_val, $q_val) = $m;
197             } elseif ($count == 4) {
198                 list(, $arg, $op, $qq_val) = $m;
199             }
200             $argstr = substr($argstr, strlen($m[0]));
201             // Remove quotes from string values.
202             if ($qq_val)
203                 $val = stripslashes($qq_val);
204             elseif ($count > 4 and $q_val)
205                 $val = stripslashes($q_val); elseif ($count >= 6 and $gt_val)
206                 $val = _(stripslashes($gt_val)); elseif ($count >= 7)
207                 $val = $word_val; else
208                 $val = '';
209
210             if ($op == '=') {
211                 $args[$arg] = $val;
212             } else {
213                 // NOTE: This does work for multiple args. Use the
214                 // separator character defined in your webserver
215                 // configuration, usually & or &amp; (See
216                 // http://www.htmlhelp.com/faq/cgifaq.4.html)
217                 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
218                 // url: RecentChanges?days=1&show_all=1&show_minor=0
219                 assert($op == '||=');
220                 $defaults[$arg] = $val;
221             }
222         }
223
224         if ($argstr) {
225             $this->handle_plugin_args_cruft($argstr, $args);
226         }
227
228         return array($args, $defaults);
229     }
230
231     /* A plugin can override this function to define how any remaining text is handled */
232     function handle_plugin_args_cruft($argstr, $args)
233     {
234         trigger_error(sprintf(_("trailing cruft in plugin args: ā€œ%sā€"),
235             $argstr), E_USER_NOTICE);
236     }
237
238     /* A plugin can override this to allow undeclared arguments.
239        Or to silence the warning.
240      */
241     function allow_undeclared_arg($name, $value)
242     {
243         trigger_error(sprintf(_("Argument ā€œ%sā€ not declared by plugin."),
244             $name), E_USER_NOTICE);
245         return false;
246     }
247
248     /* handle plugin-list argument: use run(). */
249     function makeList($plugin_args, $request, $basepage)
250     {
251         $dbi = $request->getDbh();
252         $pagelist = $this->run($dbi, $plugin_args, $request, $basepage);
253         $list = array();
254         if (is_object($pagelist) and isa($pagelist, 'PageList'))
255             return $pagelist->pageNames();
256         elseif (is_array($pagelist))
257             return $pagelist; else
258             return $list;
259     }
260
261     function getDefaultLinkArguments()
262     {
263         return array('targetpage' => $this->getName(),
264             'linktext' => $this->getName(),
265             'description' => $this->getDescription(),
266             'class' => 'wikiaction');
267     }
268
269     function getDefaultFormArguments()
270     {
271         return array('targetpage' => $this->getName(),
272             'buttontext' => $this->getName(),
273             'class' => 'wikiaction',
274             'method' => 'get',
275             'textinput' => 's',
276             'description' => $this->getDescription(),
277             'formsize' => 30);
278     }
279
280     function makeForm($argstr, $request)
281     {
282         $form_defaults = $this->getDefaultFormArguments();
283         $defaults = array_merge($form_defaults,
284             array('start_debug' => $request->getArg('start_debug')),
285             $this->getDefaultArguments());
286
287         $args = $this->getArgs($argstr, $request, $defaults);
288         $plugin = $this->getName();
289         $textinput = $args['textinput'];
290         assert(!empty($textinput) && isset($args['textinput']));
291
292         $form = HTML::form(array('action' => WikiURL($args['targetpage']),
293             'method' => $args['method'],
294             'class' => $args['class'],
295             'accept-charset' => 'UTF-8'));
296         if (!USE_PATH_INFO) {
297             $pagename = $request->get('pagename');
298             $form->pushContent(HTML::input(array('type' => 'hidden',
299                 'name' => 'pagename',
300                 'value' => $args['targetpage'])));
301         }
302         if ($args['targetpage'] != $this->getName()) {
303             $form->pushContent(HTML::input(array('type' => 'hidden',
304                 'name' => 'action',
305                 'value' => $this->getName())));
306         }
307         $contents = HTML::div();
308         $contents->setAttr('class', $args['class']);
309
310         foreach ($args as $arg => $val) {
311             if (isset($form_defaults[$arg]))
312                 continue;
313             if ($arg != $textinput && $val == $defaults[$arg])
314                 continue;
315
316             $i = HTML::input(array('name' => $arg, 'value' => $val));
317
318             if ($arg == $textinput) {
319                 //if ($inputs[$arg] == 'file')
320                 //    $attr['type'] = 'file';
321                 //else
322                 $i->setAttr('type', 'text');
323                 $i->setAttr('size', $args['formsize']);
324                 if ($args['description'])
325                     $i->addTooltip($args['description']);
326             } else {
327                 $i->setAttr('type', 'hidden');
328             }
329             $contents->pushContent($i);
330
331             // FIXME: hackage
332             if ($i->getAttr('type') == 'file') {
333                 $form->setAttr('enctype', 'multipart/form-data');
334                 $form->setAttr('method', 'post');
335                 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
336                     'value' => MAX_UPLOAD_SIZE,
337                     'type' => 'hidden')));
338             }
339         }
340
341         if (!empty($args['buttontext']))
342             $contents->pushContent(HTML::input(array('type' => 'submit',
343                 'class' => 'button',
344                 'value' => $args['buttontext'])));
345         $form->pushContent($contents);
346         return $form;
347     }
348
349     // box is used to display a fixed-width, narrow version with common header
350     function box($args = false, $request = false, $basepage = false)
351     {
352         if (!$request) $request =& $GLOBALS['request'];
353         $dbi = $request->getDbh();
354         return $this->makeBox('', $this->run($dbi, $args, $request, $basepage));
355     }
356
357     function makeBox($title, $body)
358     {
359         if (!$title) $title = $this->getName();
360         return HTML::div(array('class' => 'box'),
361             HTML::div(array('class' => 'box-title'), $title),
362             HTML::div(array('class' => 'box-data'), $body));
363     }
364
365     function error($message)
366     {
367         return HTML::span(array('class' => 'error'),
368             HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
369             $message);
370     }
371
372     function disabled($message = '')
373     {
374         $html[] = HTML::div(array('class' => 'title'),
375             fmt("Plugin %s disabled.", $this->getName()),
376             ' ', $message);
377         $html[] = HTML::pre($this->_pi);
378         return HTML::div(array('class' => 'disabled-plugin'), $html);
379     }
380
381     // TODO: Not really needed, since our plugins generally initialize their own
382     // PageList object, which accepts options['types'].
383     // Register custom PageList types for special plugins, like
384     // 'hi_content' for WikiAdminSearcheplace, 'renamed_pagename' for WikiAdminRename, ...
385     function addPageListColumn($array)
386     {
387         global $customPageListColumns;
388         if (empty($customPageListColumns)) $customPageListColumns = array();
389         foreach ($array as $column => $obj) {
390             $customPageListColumns[$column] = $obj;
391         }
392     }
393
394     // provide a sample usage text for automatic edit-toolbar insertion
395     function getUsage()
396     {
397         $args = $this->getDefaultArguments();
398         $string = '<<' . $this->getName() . ' ';
399         if ($args) {
400             foreach ($args as $key => $value) {
401                 $string .= ($key . "||=" . (string)$value . " ");
402             }
403         }
404         return $string . '>>';
405     }
406
407     function getArgumentsDescription()
408     {
409         $arguments = HTML();
410         foreach ($this->getDefaultArguments() as $arg => $default) {
411             // Work around UserPreferences plugin to avoid error
412             if ((is_array($default))) {
413                 $default = '(array)';
414                 // This is a bit flawed with UserPreferences object
415                 //$default = sprintf("array('%s')",
416                 //                   implode("', '", array_keys($default)));
417             } else
418                 if (stristr($default, ' '))
419                     $default = "'$default'";
420             $arguments->pushcontent("$arg=$default", HTML::br());
421         }
422         return $arguments;
423     }
424
425 }
426
427 class WikiPluginLoader
428 {
429     var $_errors;
430
431     function expandPI($pi, &$request, &$markup, $basepage = false)
432     {
433         if (!($ppi = $this->parsePi($pi)))
434             return false;
435         list($pi_name, $plugin, $plugin_args) = $ppi;
436
437         if (!is_object($plugin)) {
438             return new HtmlElement('div',
439                 array('class' => 'error'),
440                 $this->getErrorDetail());
441         }
442         switch ($pi_name) {
443             case 'plugin':
444                 // FIXME: change API for run() (no $dbi needed).
445                 $dbi = $request->getDbh();
446                 // pass the parsed CachedMarkup context in dbi to the plugin
447                 // to be able to know about itself, or even to change the markup XmlTree (CreateToc)
448                 $dbi->_markup = &$markup;
449                 // FIXME: could do better here...
450                 if (!$plugin->managesValidators()) {
451                     // Output of plugin (potentially) depends on
452                     // the state of the WikiDB (other than the current
453                     // page.)
454
455                     // Lacking other information, we'll assume things
456                     // changed last time the wikidb was touched.
457
458                     // As an additional hack, mark the ETag weak, since,
459                     // for all we know, the page might depend
460                     // on things other than the WikiDB (e.g. PhpWeather,
461                     // Calendar...)
462
463                     $timestamp = $dbi->getTimestamp();
464                     $request->appendValidators(array('dbi_timestamp' => $timestamp,
465                         '%mtime' => (int)$timestamp,
466                         '%weak' => true));
467                 }
468                 return $plugin->run($dbi, $plugin_args, $request, $basepage);
469             case 'plugin-list':
470                 return $plugin->makeList($plugin_args, $request, $basepage);
471             case 'plugin-form':
472                 return $plugin->makeForm($plugin_args, $request);
473         }
474         return false;
475     }
476
477     function getWikiPageLinks($pi, $basepage)
478     {
479         if (!($ppi = $this->parsePi($pi)))
480             return false;
481         list($pi_name, $plugin, $plugin_args) = $ppi;
482         if (!is_object($plugin))
483             return false;
484         if ($pi_name != 'plugin')
485             return false;
486         return $plugin->getWikiPageLinks($plugin_args, $basepage);
487     }
488
489     function parsePI($pi)
490     {
491         if (!preg_match('/^\s*<\?(plugin(?:-form|-link|-list)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
492             return $this->_error(sprintf("Bad %s", 'PI'));
493
494         list(, $pi_name, $plugin_name, $plugin_args) = $m;
495         $plugin = $this->getPlugin($plugin_name, $pi);
496
497         return array($pi_name, $plugin, $plugin_args);
498     }
499
500     function getPlugin($plugin_name, $pi = false)
501     {
502         global $ErrorManager;
503         global $AllAllowedPlugins;
504
505         if (in_array($plugin_name, $AllAllowedPlugins) === false) {
506             return $this->_error(sprintf(_("Plugin ā€œ%sā€ does not exist."),
507                 $plugin_name));
508         }
509
510         // Note that there seems to be no way to trap parse errors
511         // from this include.  (At least not via set_error_handler().)
512         $plugin_source = "lib/plugin/$plugin_name.php";
513
514         $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
515         $plugin_class = "WikiPlugin_$plugin_name";
516         if (!class_exists($plugin_class)) {
517             // $include_failed = !@include_once("lib/plugin/$plugin_name.php");
518             $include_failed = !include_once($plugin_source);
519             $ErrorManager->popErrorHandler();
520
521             if (!class_exists($plugin_class)) {
522                 if ($include_failed) {
523                     return $this->_error(sprintf(_("Plugin ā€œ%sā€ does not exist."),
524                         $plugin_name));
525                 }
526                 return $this->_error(sprintf(_("%s: no such class"), $plugin_class));
527             }
528         }
529         $ErrorManager->popErrorHandler();
530         $plugin = new $plugin_class;
531         if (!is_subclass_of($plugin, "WikiPlugin"))
532             return $this->_error(sprintf(_("%s: not a subclass of WikiPlugin."),
533                 $plugin_class));
534
535         $plugin->_pi = $pi;
536         return $plugin;
537     }
538
539     function _plugin_error_filter($err)
540     {
541         if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
542             return true; // Ignore this error --- it's expected.
543         return false;
544     }
545
546     function getErrorDetail()
547     {
548         return $this->_errors;
549     }
550
551     function _error($message)
552     {
553         $this->_errors = $message;
554         return false;
555     }
556 }
557
558 // Local Variables:
559 // mode: php
560 // tab-width: 8
561 // c-basic-offset: 4
562 // c-hanging-comment-ender-p: nil
563 // indent-tabs-mode: nil
564 // End: