/
Plugin.pm
419 lines (357 loc) · 12.5 KB
/
Plugin.pm
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
# See bottom of file for license and copyright information
# PRIVATE CLASS Foswiki::Plugin
#
# Reference information for a single plugin.
package Foswiki::Plugin;
use strict;
use warnings;
use Assert;
use Error qw(:try);
use Foswiki::Plugins ();
use Foswiki::AccessControlException ();
use Foswiki::OopsException ();
use Foswiki::ValidationException ();
our @registrableHandlers = ( # Foswiki::Plugins::VERSION:
'afterAttachmentSaveHandler', # 1.022 DEPRECATED
'afterUploadHandler', # 2.1
'afterCommonTagsHandler', # 1.024
'afterEditHandler', # 1.010
'afterRenameHandler', # 1.110
'afterSaveHandler', # 1.020
'beforeAttachmentSaveHandler', # 1.022 DEPRECATED
'beforeCommonTagsHandler', # 1.024
'beforeEditHandler', # 1.010
'beforeMergeHandler', # 1.200
'beforeSaveHandler', # 1.010
'beforeUploadHandler', # 2.1
'commonTagsHandler', # 1.000
'completePageHandler', # 1.100
'earlyInitPlugin', # 1.020
'endRenderingHandler', # 1.000 DEPRECATED
'finishPlugin', # 2.100
'initPlugin', # 1.000
'initializeUserHandler', # 1.010
'insidePREHandler', # 1.000 DEPRECATED
'modifyHeaderHandler', # 1.026
'mergeHandler', # 1.026
'outsidePREHandler', # 1.000 DEPRECATED
'postRenderingHandler', # 1.026
'preRenderingHandler', # 1.026
'redirectCgiQueryHandler', # 1.010 DEPRECATED
'registrationHandler', # 1.010
'renderFormFieldForEditHandler', # ?
'renderWikiWordHandler', # 1.023
'startRenderingHandler', # 1.000 DEPRECATED
'writeHeaderHandler', # 1.010 DEPRECATED
);
# deprecated handlers
our %deprecated = (
afterAttachmentSaveHandler => 1,
beforeAttachmentSaveHandler => 1,
endRenderingHandler => 1,
insidePREHandler => 1,
outsidePREHandler => 1,
redirectCgiQueryHandler => 1,
startRenderingHandler => 1,
writeHeaderHandler => 1,
);
=begin TML
---++ ClassMethod new( $session, $name, [, $module] )
* =$session= - Foswiki object
* =$name= - name of the plugin e.g. MyPlugin
* =$module= - name of implementing package; optional, used for tests.
Normally =load= is used to discover the module from the config.
=cut
sub new {
my ( $class, $session, $name, $module ) = @_;
ASSERT( UNTAINTED($name), "Name $name is tainted!" ) if DEBUG;
my $this = bless(
{
session => $session,
name => $name || '',
module => $module, # if undef, use discovery
},
$class
);
return $this;
}
=begin TML
---++ ObjectMethod finish()
Break circular references.
=cut
# Note to developers; please undef *all* fields in the object explicitly,
# whether they are references or not. That way this method is "golden
# documentation" of the live fields in the object.
sub finish {
my $this = shift;
undef $this->{name};
undef $this->{topicWeb};
undef $this->{module};
undef $this->{errors};
undef $this->{disabled};
undef $this->{reason};
undef $this->{no_topic};
undef $this->{description};
undef $this->{session};
}
# Load and verify a plugin, invoking any early registration
# handlers. Return the user resulting from the user handler call.
sub load {
my ($this) = @_;
my $p = $Foswiki::cfg{Plugins}{ $this->{name} }{Module};
if ( defined $p ) {
eval "use $p";
if ($@) {
push(
@{ $this->{errors} },
"$p could not be loaded. Errors were:\n$@\n----"
);
$this->{disabled} = 1;
$this->{reason} =
$this->{session}->i18n->maketext(
'Error encountered loading the plugin. See errors below.');
return;
}
else {
$this->{module} = $p;
}
}
else {
push(
@{ $this->{errors} },
"$this->{name} could not be loaded. No \$Foswiki::cfg{Plugins}{$this->{name}}{Module} defined - re-run configure\n---"
);
$this->{disabled} = 1;
$this->{reason} =
$this->{session}->i18n->maketext(
'Plugin module was not defined. Check configuration. See errors below.'
);
return;
}
my $noTopic = eval '$' . $p . '::NO_PREFS_IN_TOPIC';
$this->{no_topic} = $noTopic;
$this->{topicWeb} = undef; # not known yet
unless ($noTopic) {
if ( !$this->topicWeb() ) {
# not found
push(
@{ $this->{errors} },
'Plugins: could not fully register '
. $this->{name}
. ', no plugin topic'
);
$noTopic = 1;
}
}
# Get the description from the code, if present. if it's not there, it'll
# be loaded as a preference from the plugin topic later
$this->{description} = eval '$' . $p . '::SHORTDESCRIPTION';
# Set the session for this call stack
local $Foswiki::Plugins::SESSION = $this->{session};
ASSERT( $Foswiki::Plugins::SESSION->isa('Foswiki') ) if DEBUG;
my $sub = $p . "::initPlugin";
if ( !defined(&$sub) ) {
push( @{ $this->{errors} }, $sub . ' is not defined' );
$this->{disabled} = 1;
$this->{reason} =
$this->{session}
->i18n->maketext('initPlugin was not defined. See errors below.');
return;
}
$sub = $p . '::earlyInitPlugin';
if ( defined(&$sub) ) {
no strict 'refs';
my $error = &$sub();
use strict 'refs';
if ($error) {
push( @{ $this->{errors} }, $sub . ' failed: ' . $error );
$this->{disabled} = 1;
$this->{reason} =
$this->{session}
->i18n->maketext('earlyInitPlugin failed. See errors below.');
return;
}
}
my $user;
$sub = $p . '::initializeUserHandler';
if ( defined(&$sub) ) {
no strict 'refs';
$user = &$sub(
$this->{session}->{remoteUser},
$this->{session}->{request}->url(),
$this->{session}->{request}->path_info()
);
use strict 'refs';
}
#print STDERR "Compile $p: ".timestr(timediff(new Benchmark, $begin))."\n";
return $user;
}
# register plugin settings
sub registerSettings {
my ( $this, $plugins ) = @_;
return if $this->{disabled};
my $prefs = $this->{session}->{prefs};
if ( !$this->{no_topic} ) {
$prefs->setPluginPreferences( $this->topicWeb(), $this->{name} );
}
}
# invoke plugin initialisation and register handlers.
sub registerHandlers {
my ( $this, $plugins ) = @_;
return if $this->{disabled};
my $p = $this->{module};
my $sub = $p . "::initPlugin";
my $users = $Foswiki::Plugins::SESSION->{users};
my $status = 0;
my $exception = '';
try {
no strict 'refs';
$status = &$sub(
$Foswiki::Plugins::SESSION->{topicName},
$Foswiki::Plugins::SESSION->{webName},
$users->getLoginName( $Foswiki::Plugins::SESSION->{user} ),
$this->topicWeb()
);
use strict 'refs';
}
catch Foswiki::AccessControlException with {
shift->throw(); # propagate
}
catch Foswiki::OopsException with {
shift->throw(); # propagate
}
catch Foswiki::ValidationException with {
shift->throw(); # propagate
}
otherwise {
my $e = shift;
$exception = $e->text() . ' ' . $e->stacktrace();
};
unless ($status) {
if ( !$exception ) {
$exception = <<MESSAGE;
$sub did not return true.
Check your Foswiki warning and error logs for more information.
MESSAGE
}
push( @{ $this->{errors} }, $exception );
$this->{disabled} = 1;
$this->{reason} =
$this->{session}->i18n->maketext(
'Plugin module did not return true. See errors below.');
return;
}
my $compat = eval '\%' . $p . '::FoswikiCompatibility';
foreach my $h (@registrableHandlers) {
my $sub = $p . '::' . $h;
if ( defined(&$sub) ) {
if ( $deprecated{$h}
&& $compat
&& $compat->{$h}
&& $compat->{$h} <= $Foswiki::Plugins::VERSION )
{
# Compatibility handler not required in this version
next;
}
$plugins->addListener( $h, $this );
}
}
$this->{session}->enterContext( $this->{name} . 'Enabled' );
}
# Invoke a handler
sub invoke {
my $this = shift; # remove from parameter vector
my $handlerName = shift;
my $handler = $this->{module} . '::' . $handlerName;
no strict 'refs';
return &$handler(@_);
use strict 'refs';
}
# Get the VERSION number of the specified plugin.
# SMELL: may die if the plugin doesn't compile
sub getVersion {
my $this = shift;
no strict 'refs';
return ${ $this->{module} . '::VERSION' } || '';
use strict 'refs';
}
# Get the RELEASE of the specified plugin.
# SMELL: may die if the plugin doesn't compile
sub getRelease {
my $this = shift;
no strict 'refs';
return ${ $this->{module} . '::RELEASE' } || '';
use strict 'refs';
}
# Get the description string for the given plugin
sub getDescription {
my $this = shift;
unless ( defined $this->{description} ) {
my $pref = uc( $this->{name} ) . '_SHORTDESCRIPTION';
my $prefs = $this->{session}->{prefs};
$this->{description} = $prefs->getPreference($pref) || '';
}
if ( $this->{disabled} ) {
my $reason = '';
if ( $this->{reason} ) {
$reason =
CGI::span( { class => 'foswikiAlert' }, ' ' . $this->{reason} );
}
return ' ' . $this->{name} . ': (disabled)' . $reason;
}
my $release = $this->getRelease();
my $version = $this->getVersion();
$version =~ s/\$Rev: (\d+) \$/$1/g;
$version = $release . ', ' . $version if $release;
my $web = $this->topicWeb();
my $result = ' ' . ( $web ? "$web." : '!' ) . $this->{name} . ' ';
$result .= CGI::span( { class => 'foswikiGrayText foswikiSmall' },
'(' . $version . ')' );
$result .= ': ' . $this->{description};
return $result;
}
=begin TML
---++ ObjectMethod topicWeb() -> $webname
Find the web that has the topic for this plugin by searching the
{Plugins}{WebSearchPath}. Returns undef if $NO_PREFS_IN_TOPIC=1
=cut
sub topicWeb {
my $this = shift;
unless ( defined( $this->{topicWeb} ) || $this->{no_topic} ) {
# Find the plugin topic, if required
my $session = $this->{session};
foreach
my $web ( split( /[, ]+/, $Foswiki::cfg{Plugins}{WebSearchPath} ),
$session->{webName} )
{
if ( $session->topicExists( $web, $this->{name} ) ) {
$this->{topicWeb} = $web;
last;
}
}
}
# If there is no web (probably because NO_PREFS_IN_TOPIC is set)
# then default to the system web name.
return $this->{topicWeb} || $Foswiki::cfg{SystemWebName};
}
1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2010 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
Additional copyrights apply to some or all of the code in this
file as follows:
Copyright (C) 2000-2001 Andrea Sterbini, a.sterbini@flashnet.it
Copyright (C) 2001-2007 Peter Thoeny, peter@thoeny.org
and TWiki Contributors. All Rights Reserved. TWiki Contributors
are listed in the AUTHORS file in the root of this distribution.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.