File: WikiPlugin.php

package info (click to toggle)
phpwiki 1.3.12p3-5etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 16,956 kB
  • ctags: 21,608
  • sloc: php: 82,335; xml: 3,840; sh: 1,522; sql: 1,198; perl: 625; makefile: 562; awk: 28
file content (559 lines) | stat: -rw-r--r-- 21,855 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<?php //-*-php-*-
rcs_id('$Id: WikiPlugin.php,v 1.61 2005/10/31 17:20:40 rurban Exp $');

class WikiPlugin
{
    function getDefaultArguments() {
        return array('description' => $this->getDescription());
    }

    /** Does the plugin manage its own HTTP validators?
     *
     * This should be overwritten by (some) individual plugins.
     *
     * If the output of the plugin is static, depending only
     * on the plugin arguments, query arguments and contents
     * of the current page, this can (and should) return true.
     *
     * If the plugin can deduce a modification time, or equivalent
     * sort of tag for it's content, then the plugin should
     * call $request->appendValidators() with appropriate arguments,
     * and should override this method to return true.
     *
     * When in doubt, the safe answer here is false.
     * Unfortunately, returning false here will most likely make
     * any page which invokes the plugin uncacheable (by HTTP proxies
     * or browsers).
     */
    function managesValidators() {
        return false;
    }
    
    // FIXME: args?
    function run ($dbi, $argstr, &$request, $basepage) {
        trigger_error("WikiPlugin::run: pure virtual function",
                      E_USER_ERROR);
    }

    /** Get wiki-pages linked to by plugin invocation.
     *
     * A plugin may override this method to add pages to the
     * link database for the invoking page.
     *
     * For example, the IncludePage plugin should override this so
     * that the including page shows up in the backlinks list for the
     * included page.
     *
     * Not all plugins which generate links to wiki-pages need list
     * those pages here.
     *
     * Note also that currently the links are calculated at page save
     * time, so only static page links (e.g. those dependent on the PI
     * args, not the rest of the wikidb state or any request query args)
     * will work correctly here.
     *
     * @param string $argstr The plugin argument string.
     * @param string $basepage The pagename the plugin is invoked from.
     * @return array List of pagenames linked to (or false).
     */
    function getWikiPageLinks ($argstr, $basepage) {
        return false;
    }
    
    /**
     * Get name of plugin.
     *
     * This is used (by default) by getDefaultLinkArguments and
     * getDefaultFormArguments to compute the default link/form
     * targets.
     *
     * If you want to gettextify the name (probably a good idea),
     * override this method in your plugin class, like:
     * <pre>
     *   function getName() { return _("MyPlugin"); }
     * </pre>
     *
     * @return string plugin name/target.
     */
    function getName() {
        return preg_replace('/^.*_/', '',  get_class($this));
    }

    function getDescription() {
        return $this->getName();
    }
    
    // plugins should override this with the commented-out code
    function getVersion() {
        return _("n/a");
        //return preg_replace("/[Revision: $]/", '',
        //                    "\$Revision: 1.61 $");
    }

    function getArgs($argstr, $request=false, $defaults=false) {
        if ($defaults === false) {
            $defaults = $this->getDefaultArguments();
        }
        //Fixme: on POST argstr is empty
        list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
        $args = array();
        if (!empty($defaults))
          foreach ($defaults as $arg => $default_val) {
            if (isset($argstr_args[$arg])) {
                $args[$arg] = $argstr_args[$arg];
            } elseif ( $request and ($argval = $request->getArg($arg)) !== false ) {
                $args[$arg] = $argval;
            } elseif (isset($argstr_defaults[$arg])) {
                $args[$arg] = (string) $argstr_defaults[$arg];
            } else {
                $args[$arg] = $default_val;
            }
            // expand [arg]
            if ($request and is_string($args[$arg]) and strstr($args[$arg], "[")) {
                $args[$arg] = $this->expandArg($args[$arg], $request);
            }

            unset($argstr_args[$arg]);
            unset($argstr_defaults[$arg]);
        }

        foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
            if ($request and $request->getArg('pagename') == _("PhpWikiAdministration") 
                and $arg == 'overwrite') // silence this warning
                ;
            else
                trigger_error(sprintf(_("Argument '%s' not declared by plugin."),
                                      $arg), E_USER_NOTICE);
        }

        // add special handling of pages and exclude args to accept <! plugin-list !>
        // and split explodePageList($args['exclude']) => array()
        // TODO : handle p[] pagehash
        foreach (array('pages', 'exclude') as $key) {
            if (!empty($args[$key]) and array_key_exists($key, $defaults))
                $args[$key] = is_string($args[$key])
                    ? explodePageList($args[$key])
                    : $args[$key]; // <! plugin-list !>
        }

        // always override sortby,limit from the REQUEST. ignore defaults if defined as such.
        foreach (array('sortby', 'limit') as $key) {
            if (array_key_exists($key, $defaults)) {
                if ($val = $request->getArg($key))
                    $args[$key] = $val;
                elseif (!empty($args[$key])) {
                    $GLOBALS['request']->setArg($key, $args[$key]);
                }
            }
        }
        return $args;
    }

    // Patch by Dan F:
    // Expand [arg] to $request->getArg("arg") unless preceded by ~
    function expandArg($argval, &$request) {
        // return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
        // Replace the arg unless it is preceded by a ~
        $ret = preg_replace('/([^~]|^)\[(\w[\w\d]*)\]/e',
                            '"$1" . $request->getArg("$2")',
                           $argval);
        // Ditch the ~ so later versions can be expanded if desired
        return preg_replace('/~(\[\w[\w\d]*\])/', '$1', $ret);
    }

    function parseArgStr($argstr) {
        $args = array();
        $defaults = array();
	if (empty($argstr))
            return array($args,$defaults);
            
        $arg_p = '\w+';
        $op_p = '(?:\|\|)?=';
        $word_p = '\S+';
        $opt_ws = '\s*';
        $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
        //"<--kludge for brain-dead syntax coloring
        $q_p  = "' ( (?:[^'\\\\]|\\\\.)* ) '";
        $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
        $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";

        // handle plugin-list arguments seperately
        $plugin_p = '<!plugin-list\s+\w+.*?!>';
        while (preg_match("/^($arg_p) $opt_ws ($op_p) $opt_ws ($plugin_p) $opt_ws/x", $argstr, $m)) {
            @ list(,$arg, $op, $plugin_val) = $m;
            $argstr = substr($argstr, strlen($m[0]));
            $loader = new WikiPluginLoader();
            $markup = null;
            $basepage = null;
            $plugin_val = preg_replace(array("/^<!/","/!>$/"),array("<?","?>"),$plugin_val);
            $val = $loader->expandPI($plugin_val, $GLOBALS['request'], $markup, $basepage);
            if ($op == '=') {
                $args[$arg] = $val; // comma delimited pagenames or array()?
            } else {
                assert($op == '||=');
                $defaults[$arg] = $val;
            }
        }
        while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
            @ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
            $argstr = substr($argstr, strlen($m[0]));

            // Remove quotes from string values.
            if ($qq_val)
                $val = stripslashes($qq_val);
            elseif ($q_val)
                $val = stripslashes($q_val);
            elseif ($gt_val)
                $val = _(stripslashes($gt_val));
            else
                $val = $word_val;

            if ($op == '=') {
                $args[$arg] = $val;
            }
            else {
                // NOTE: This does work for multiple args. Use the
                // separator character defined in your webserver
                // configuration, usually & or &amp; (See
                // http://www.htmlhelp.com/faq/cgifaq.4.html)
                // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
                // url: RecentChanges?days=1&show_all=1&show_minor=0
                assert($op == '||=');
                $defaults[$arg] = $val;
            }
        }

        if ($argstr) {
           $this->handle_plugin_args_cruft($argstr, $args);
        }

        return array($args, $defaults);
    }

    /* A plugin can override this function to define how any remaining text is handled */
    function handle_plugin_args_cruft($argstr, $args) {
        trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),
                              $argstr), E_USER_NOTICE);
    }

    /* handle plugin-list argument: use run(). */
    function makeList($plugin_args, $request, $basepage) {
        $dbi = $request->getDbh();
        $pagelist = $this->run($dbi, $plugin_args, $request, $basepage);
        $list = array();
        if (is_object($pagelist) and isa($pagelist, 'PageList')) {
            // table or list?
            foreach ($pagelist->_pages as $page) {
            	$list[] = $page->getName();
            }
        }
        return $list;
    }

    function getDefaultLinkArguments() {
        return array('targetpage'  => $this->getName(),
                     'linktext'    => $this->getName(),
                     'description' => $this->getDescription(),
                     'class'       => 'wikiaction');
    }

    function makeLink($argstr, $request) {
        $defaults = $this->getDefaultArguments();
        $link_defaults = $this->getDefaultLinkArguments();
        $defaults = array_merge($defaults, $link_defaults);
    
        $args = $this->getArgs($argstr, $request, $defaults);
        $plugin = $this->getName();
    
        $query_args = array();
        foreach ($args as $arg => $val) {
            if (isset($link_defaults[$arg]))
                continue;
            if ($val != $defaults[$arg])
                $query_args[$arg] = $val;
        }
    
        $link = Button($query_args, $args['linktext'], $args['targetpage']);
        if (!empty($args['description']))
            $link->addTooltip($args['description']);
    
        return $link;
    }
    
    function getDefaultFormArguments() {
        return array('targetpage' => $this->getName(),
                     'buttontext' => $this->getName(),
                     'class'      => 'wikiaction',
                     'method'     => 'get',
                     'textinput'  => 's',
                     'description'=> $this->getDescription(),
                     'formsize'   => 30);
    }
    
    function makeForm($argstr, $request) {
        $form_defaults = $this->getDefaultFormArguments();
        $defaults = array_merge($form_defaults, 
                                array('start_debug' => $request->getArg('start_debug')),
        			$this->getDefaultArguments());
    
        $args = $this->getArgs($argstr, $request, $defaults);
        $plugin = $this->getName();
        $textinput = $args['textinput'];
        assert(!empty($textinput) && isset($args['textinput']));
    
        $form = HTML::form(array('action' => WikiURL($args['targetpage']),
                                 'method' => $args['method'],
                                 'class'  => $args['class'],
                                 'accept-charset' => $GLOBALS['charset']));
        if (! USE_PATH_INFO ) {
            $pagename = $request->get('pagename');
            $form->pushContent(HTML::input(array('type' => 'hidden', 
                                                 'name' => 'pagename', 
                                                 'value' => $args['targetpage'])));
        }
        if ($args['targetpage'] != $this->getName()) {
            $form->pushContent(HTML::input(array('type' => 'hidden', 
                                                 'name' => 'action', 
                                                 'value' => $this->getName())));
        }
        $contents = HTML::div();
        $contents->setAttr('class', $args['class']);
    
        foreach ($args as $arg => $val) {
            if (isset($form_defaults[$arg]))
                continue;
            if ($arg != $textinput && $val == $defaults[$arg])
                continue;
    
            $i = HTML::input(array('name' => $arg, 'value' => $val));
    
            if ($arg == $textinput) {
                //if ($inputs[$arg] == 'file')
                //    $attr['type'] = 'file';
                //else
                $i->setAttr('type', 'text');
                $i->setAttr('size', $args['formsize']);
                if ($args['description'])
                    $i->addTooltip($args['description']);
            }
            else {
                $i->setAttr('type', 'hidden');
            }
            $contents->pushContent($i);
    
            // FIXME: hackage
            if ($i->getAttr('type') == 'file') {
                $form->setAttr('enctype', 'multipart/form-data');
                $form->setAttr('method', 'post');
                $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
                                                         'value' => MAX_UPLOAD_SIZE,
                                                         'type' => 'hidden')));
            }
        }
    
        if (!empty($args['buttontext']))
            $contents->pushContent(HTML::input(array('type' => 'submit',
                                                     'class' => 'button',
                                                     'value' => $args['buttontext'])));
        $form->pushContent($contents);
        return $form;
    }

    // box is used to display a fixed-width, narrow version with common header
    function box($args=false, $request=false, $basepage=false) {
        if (!$request) $request =& $GLOBALS['request'];
        $dbi = $request->getDbh();
        return $this->makeBox('', $this->run($dbi, $args, $request, $basepage));
    }

    function makeBox($title, $body) {
        if (!$title) $title = $this->getName();
        return HTML::div(array('class'=>'box'),
                         HTML::div(array('class'=>'box-title'), $title),
                         HTML::div(array('class'=>'box-data'), $body));
    }
    
    function error ($message) {
        return HTML::div(array('class' => 'errors'),
                        HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
                        $message);
    }

    function disabled ($message='') {
        $html[] = HTML::div(array('class' => 'title'),
                            fmt("Plugin %s disabled.", $this->getName()),
                            ' ', $message);
        $html[] = HTML::pre($this->_pi);
        return HTML::div(array('class' => 'disabled-plugin'), $html);
    }

    // TODO: Not really needed, since our plugins generally initialize their own 
    // PageList object, which accepts options['types'].
    // Register custom PageList types for special plugins, like 
    // 'hi_content' for WikiAdminSearcheplace, 'renamed_pagename' for WikiAdminRename, ...
    function addPageListColumn ($array) {
        global $customPageListColumns;
        if (empty($customPageListColumns)) $customPageListColumns = array();
        foreach ($array as $column => $obj) {
            $customPageListColumns[$column] = $obj;
        }
    }
    
    // provide a sample usage text for automatic edit-toolbar insertion
    function getUsage() {
        $args = $this->getDefaultArguments();
        $string = '<'.'?plugin '.$this->getName().' ';
        if ($args) {
            foreach ($args as $key => $value) {
                $string .= ($key."||=".(string)$value." ");
            }
        }
        return $string . '?'.'>';
    }

    function getArgumentsDescription() {
        $arguments = HTML();
        foreach ($this->getDefaultArguments() as $arg => $default) {
            // Work around UserPreferences plugin to avoid error
            if ((is_array($default))) {
                $default = '(array)';
                // This is a bit flawed with UserPreferences object
                //$default = sprintf("array('%s')",
                //                   implode("', '", array_keys($default)));
            }
            else
                if (stristr($default, ' '))
                    $default = "'$default'";
            $arguments->pushcontent("$arg=$default", HTML::br());
        }
        return $arguments;
    }

}

class WikiPluginLoader {
    var $_errors;

    function expandPI($pi, &$request, &$markup, $basepage=false) {
        if (!($ppi = $this->parsePi($pi)))
            return false;
        list($pi_name, $plugin, $plugin_args) = $ppi;

        if (!is_object($plugin)) {
            return new HtmlElement($pi_name == 'plugin-link' ? 'span' : 'p',
                                   array('class' => 'plugin-error'),
                                   $this->getErrorDetail());
        }
        switch ($pi_name) {
            case 'plugin':
                // FIXME: change API for run() (no $dbi needed).
                $dbi = $request->getDbh();
                // pass the parsed CachedMarkup context in dbi to the plugin 
                // to be able to know about itself, or even to change the markup XmlTree (CreateToc)
                $dbi->_markup = &$markup; 
                // FIXME: could do better here...
                if (! $plugin->managesValidators()) {
                    // Output of plugin (potentially) depends on
                    // the state of the WikiDB (other than the current
                    // page.)
                    
                    // Lacking other information, we'll assume things
                    // changed last time the wikidb was touched.
                    
                    // As an additional hack, mark the ETag weak, since,
                    // for all we know, the page might depend
                    // on things other than the WikiDB (e.g. PhpWeather,
                    // Calendar...)
                    
                    $timestamp = $dbi->getTimestamp();
                    $request->appendValidators(array('dbi_timestamp' => $timestamp,
                                                     '%mtime' => (int)$timestamp,
                                                     '%weak' => true));
                }
                return $plugin->run($dbi, $plugin_args, $request, $basepage);
            case 'plugin-list':
                return $plugin->makeList($plugin_args, $request, $basepage);
            case 'plugin-link':
                return $plugin->makeLink($plugin_args, $request);
            case 'plugin-form':
                return $plugin->makeForm($plugin_args, $request);
        }
    }

    function getWikiPageLinks($pi, $basepage) {
        if (!($ppi = $this->parsePi($pi)))
            return false;
        list($pi_name, $plugin, $plugin_args) = $ppi;
        if (!is_object($plugin))
            return false;
        if ($pi_name != 'plugin')
            return false;
        return $plugin->getWikiPageLinks($plugin_args, $basepage);
    }
    
    function parsePI($pi) {
        if (!preg_match('/^\s*<\?(plugin(?:-form|-link|-list)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
            return $this->_error(sprintf("Bad %s", 'PI'));

        list(, $pi_name, $plugin_name, $plugin_args) = $m;
        $plugin = $this->getPlugin($plugin_name, $pi);

        return array($pi_name, $plugin, $plugin_args);
    }
    
    function getPlugin($plugin_name, $pi=false) {
        global $ErrorManager;

        // Note that there seems to be no way to trap parse errors
        // from this include.  (At least not via set_error_handler().)
        $plugin_source = "lib/plugin/$plugin_name.php";

        $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
        $plugin_class = "WikiPlugin_$plugin_name";
        if (!class_exists($plugin_class)) {
            // $include_failed = !@include_once("lib/plugin/$plugin_name.php");
            $include_failed = !include_once("lib/plugin/$plugin_name.php");
            $ErrorManager->popErrorHandler();
            
            if (!class_exists($plugin_class)) {
                if ($include_failed)
                    return $this->_error(sprintf(_("Include of '%s' failed."),
                                                 $plugin_source));
                return $this->_error(sprintf(_("%s: no such class"), $plugin_class));
            }
        }
        $ErrorManager->popErrorHandler();
        $plugin = new $plugin_class;
        if (!is_subclass_of($plugin, "WikiPlugin"))
            return $this->_error(sprintf(_("%s: not a subclass of WikiPlugin."),
                                         $plugin_class));

        $plugin->_pi = $pi;
        return $plugin;
    }

    function _plugin_error_filter ($err) {
        if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
            return true;        // Ignore this error --- it's expected.
        return false;
    }

    function getErrorDetail() {
        return $this->_errors;
    }

    function _error($message) {
        $this->_errors = $message;
        return false;
    }
};

// (c-file-style: "gnu")
// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>