9 date 2008.01.22.03.21.25; author TWikiContributor; state Exp;
14 date 2007.01.16.04.11.57; author TWikiContributor; state Exp;
19 date 2006.02.01.12.01.25; author TWikiContributor; state Exp;
34 @---+ Package =TWiki::Plugins=
36 This module defines the singleton object that handles Plugins
37 loading, initialization and execution.
39 This class uses Chain of Responsibility (GOF) pattern to dispatch
40 handler calls to registered plugins.
45 Note that as of version 1.026 of this module, TWiki internal
46 methods are _no longer available_ to plugins. Any calls to
47 TWiki internal methods must be replaced by calls via the
48 =$SESSION= object in this package, or via the Func package.
49 For example, the call:
51 =my $pref = TWiki::getPreferencesValue('URGH');=
53 should be replaced with
55 =my $pref = TWiki::Func::getPreferencesValue('URGH');=
59 =my $t = TWiki::writeWarning($message);=
61 should be replaced with
63 =my $pref = $TWiki::Plugins::SESSION->writeWarning($message);=
65 Methods in other modules such as Store must be accessed through
66 the relevant TWiki sub-object, for example
68 =TWiki::Store::saveTopic(...)=
70 should be replaced with
72 =$TWiki::Plugins::SESSION->{store}->saveTopic(...)=
74 Note that calling TWiki internal methods is very very bad practice,
75 and should be avoided wherever practical.
77 The developers of TWiki reserve the right to change internal
78 methods without warning, unless those methods are clearly
79 marked as PUBLIC. PUBLIC methods are part of the core specification
80 of a module and can be trusted.
83 ---++ PUBLIC constant $VERSION
85 This is the version number of the plugins package. Use it for checking
86 if you have a recent enough version.
90 This is a reference to the TWiki session object. It can be used in
91 plugins to get at the methods of the TWiki kernel.
93 You are _highly_ recommended to only use the methods in the
94 [[TWikiFuncDotPm][Func]] interface, unless you have no other choice,
95 as kernel methods may change between TWiki releases.
98 ---++ ClassMethod *new* <tt>($session)</tt>
100 Construct new singleton plugins collection object. The object is a
101 container for a list of plugins and the handlers registered by the plugins.
102 The plugins and the handlers are carefully ordered.
106 ---++ ObjectMethod *finish* <tt>()</tt>
107 Break circular references.
111 ---++ ObjectMethod *load* <tt>($allDisabled) -> $loginName</tt>
113 Find all active plugins, and invoke the early initialisation.
114 Has to be done _after_ prefs are read.
116 Returns the user returned by the last =initializeUserHandler= to be
119 If allDisabled is set, no plugin handlers will be called.
123 ---++ ObjectMethod *settings* <tt>()</tt>
125 Push plugin settings onto preference stack
129 ---++ ObjectMethod *enable* <tt>()</tt>
131 Initialisation that is done after the user is known.
135 ---++ ObjectMethod *getPluginVersion* <tt>() -> $number</tt>
137 Returns the $TWiki::Plugins::VERSION number if no parameter is specified,
138 else returns the version number of a named Plugin. If the Plugin cannot
139 be found or is not active, 0 is returned.
143 ---++ ObjectMethod *addListener* <tt>($command,$handler)</tt>
145 * =$command= - name of the event
146 * =$handler= - the handler object.
148 Add a listener to the end of the list of registered listeners for this event.
149 The listener must implement =invoke($command,...)=, which will be triggered
150 when the event is to be processed.
154 ---++ ObjectMethod *haveHandlerFor* <tt>($handlerName) -> $boolean</tt>
156 * =$handlerName= - name of the handler e.g. preRenderingHandler
157 Return: true if at least one plugin has registered a handler of
162 ---++ ObjectMethod *registrationHandler* <tt>()</tt>
164 Called by the register script
168 ---++ ObjectMethod *beforeCommonTagsHandler* <tt>()</tt>
170 Called at the beginning (for cache Plugins only)
174 ---++ ObjectMethod *commonTagsHandler* <tt>()</tt>
176 Called after %INCLUDE:"..."%
180 ---++ ObjectMethod *afterCommonTagsHandler* <tt>()</tt>
182 Called at the end (for cache Plugins only)
186 ---++ ObjectMethod *preRenderingHandler* <tt>($text,\%map)</tt>
188 * =$text= - the text, with the head, verbatim and pre blocks replaced with placeholders
189 * =\%removed= - reference to a hash that maps the placeholders to the removed blocks.
191 Placeholders are text strings constructed using the tag name and a sequence number e.g. 'pre1', "verbatim6", "head1" etc. Placeholders are inserted into the text inside \1 characters so the text will contain \1_pre1\1 for placeholder pre1.
193 Each removed block is represented by the block text and the parameters passed to the tag (usually empty) e.g. for
195 <pre class='slobadob'>
199 the map will contain:
201 $removed->{'pre1'}{text}: XYZ
202 $removed->{'pre1'}{params}: class="slobadob"
205 Iterating over blocks for a single tag is easy. For example, to prepend a line number to every line of a pre block you might use this code:
208 foreach my $placeholder ( keys %$map ) {
209 if( $placeholder =~ /^pre/i ) {
211 $map->{$placeholder}{text} =~ s/^/$n++/gem;
218 ---++ ObjectMethod *postRenderingHandler* <tt>(\$text)</tt>
220 * =\$text= - a reference to the HTML, with the head, verbatim and pre blocks replaced with placeholders
224 ---++ ObjectMethod *startRenderingHandler* <tt>()</tt>
226 Called just before the line loop
228 *DEPRECATED* Use preRenderingHandler instead. This handler correctly
229 handles verbatim and other TWiki ML block types, and exposes them to
234 ---++ ObjectMethod *outsidePREHandler* <tt>()</tt>
236 Called in line loop outside of <PRE> tag
238 *DEPRECATED* Use preRenderingHandler instead.
239 This handler correctly handles pre and other
240 TWiki ML block types, and is called only once
241 instead of line-by-line.
245 ---++ ObjectMethod *insidePREHandler* <tt>()</tt>
247 Called in line loop inside of <PRE> tag
249 *DEPRECATED* Use preRenderingHandler instead.
250 This handler correctly handles pre and other
251 TWiki ML block types, and is called only once
252 instead of line-by-line.
256 ---++ ObjectMethod *endRenderingHandler* <tt>()</tt>
258 Called just after the line loop
260 *DEPRECATED* Use postRenderingHandler instead.
264 ---++ ObjectMethod *beforeEditHandler* <tt>()</tt>
270 ---++ ObjectMethod *afterEditHandler* <tt>()</tt>
276 ---++ ObjectMethod *beforeSaveHandler* <tt>()</tt>
278 Called just before the save action
282 ---++ ObjectMethod *afterSaveHandler* <tt>()</tt>
284 Called just after the save action
288 ---++ ObjectMethod *afterRenameHandler* <tt>($oldWeb,$oldTopic,$oldAttachment,$newWeb,$newTopic,$newAttachment)</tt>
290 Called just after the rename/move/delete action of a web, topic or attachment.
292 * =$oldWeb= - name of old web
293 * =$oldTopic= - name of old topic (empty string if web rename)
294 * =$oldAttachment= - name of old attachment (empty string if web or topic rename)
295 * =$newWeb= - name of new web
296 * =$newTopic= - name of new topic (empty string if web rename)
297 * =$newAttachment= - name of new attachment (empty string if web or topic rename)
301 ---++ ObjectMethod *mergeHandler* <tt>()</tt>
303 Called to handle text merge.
307 ---++ ObjectMethod *beforeAttachmentSaveHandler* <tt>($attrHashRef,$topic,$web)</tt>
309 This code provides Plugins with the opportunity to alter an uploaded attachment between the upload and save-to-store processes. It is invoked as per other Plugins.
310 * =$attrHashRef= - Hash reference of attachment attributes (keys are indicated below)
311 * =$topic= - Topic name
314 Keys in $attrHashRef:
316 | attachment | Name of the attachment |
317 | tmpFilename | Name of the local file that stores the upload |
318 | comment | Comment to be associated with the upload |
319 | user | Login name of the person submitting the attachment, e.g. 'jsmith' |
321 Note: All keys should be used read-only, except for comment which can be modified.
326 my( $attrHashRef, $topic, $web ) = @@_;
327 $$attrHashRef{'comment'} .= " (NOTE: Extracted from blah.tar.gz)";
332 ---++ ObjectMethod *afterAttachmentSaveHandler* <tt>($attachmentAttrHash,$topic,$web,$error)</tt>
334 deal with an uploaded attachment between the upload and save-to-store processes. It is invoked as per other plugins.
336 * =$attrHashRef= - Hash reference of attachment attributes (keys are indicated below)
337 * =$topic= - Topic name
339 * =$error= - Error string of save action, empty if OK
341 Keys in $attrHashRef:
343 | attachment | Name of the attachment |
344 | tmpFilename | Name of the local file that stores the upload |
345 | comment | Comment to be associated with the upload |
346 | user | Login name of the person submitting the attachment, e.g. 'jsmith' |
348 Note: The hash is *read-only*
352 ---++ ObjectMethod *writeHeaderHandler* <tt>() -> $headers</tt>
354 *DEPRECATED* Use modifyHeaderHandler instead. it is a lot
355 more flexible, and allows you to modify existing headers
356 as well as add new ones. It also works correctly when
357 multiple plugins want to modify headers.
361 ---++ ObjectMethod *modifyHeaderHandler* <tt>(\@@headers,$query)</tt>
365 ---++ ObjectMethod *completePageHandler* <tt>($text,$pageType,$contentType)</tt>
369 ---++ ObjectMethod *redirectCgiQueryHandler* <tt>() -> $result</tt>
371 Called by TWiki::redirect
375 ---++ ObjectMethod *renderFormFieldForEditHandler* <tt>($name,$type,$size,$value,$attributes,$possibleValues) -> $html</tt>
377 This handler is called before built-in types are considered. It generates the HTML text rendering this form field, or false, if the rendering should be done by the built-in type handlers.
378 * =$name= - name of form field
379 * =$type= - type of form field
380 * =$size= - size of form field
381 * =$value= - value held in the form field
382 * =$attributes= - attributes of form field
383 * =$possibleValues= - the values defined as options for form field, if any. May be a scalar (one legal value) or an array (several legal values)
384 Return HTML text that renders this field. If false, form rendering continues by considering the built-in types.
386 Note that a common application would be to generate formatting of the
387 field involving generation of javascript. Such usually also requires
388 the insertion of some common javascript into the page header. Unfortunately,
389 there is currently no mechanism to pass that script to where the header of
390 the page is visible. Consequentially, the common javascript may have to
391 be emitted as part of the field formatting and might be duplicated many
392 times throughout the page.
396 ---++ ObjectMethod *renderWikiWordHandler* <tt>() -> $result</tt>
398 Change how a WikiWord is rendered
400 Originated from the TWiki:Plugins.SpacedWikiWordPlugin hack
414 * =$command* - name of the event
425 * =$topic= - | Topic name
426 * =$web= - | Web name
429 * =$topic= - | Topic name
430 * =$web= - | Web name
431 * =$error= - | Error string of save action, empty if OK
433 Called by TWiki::writePageHeader. *DEPRECATED* do not use!
446 $SESSION object in this package, or via the Func package.
449 my $pref = TWiki::getPreferencesValue('URGH');
452 my $pref = TWiki::Func::getPreferencesValue('URGH');
455 my $t = TWiki::writeWarning($message);
458 my $pref = $TWiki::Plugins::SESSION->writeWarning($message);
461 TWiki::Store::saveTopic(...)
464 $TWiki::Plugins::SESSION->{store}->saveTopic(...)
472 ---++ ObjectMethd preRenderingHandler( $text, \%map )