1 # <a name="Package <code>TWiki::Plugins="></a> Package =TWiki::Plugins
3 This module defines the singleton object that handles Plugins loading, initialization and execution.
5 This class uses Chain of Responsibility (GOF) pattern to dispatch handler calls to registered plugins.
9 <li><a href="#Package =TWiki::Plugins="> Package TWiki::Plugins</a><ul>
10 <li><a href="#PUBLIC constant $VERSION"> PUBLIC constant $VERSION</a></li>
11 <li><a href="#PUBLIC $SESSION"> PUBLIC $SESSION</a></li>
12 <li><a href="#ClassMethod <strong>new</strong> ($session)"> ClassMethod new <tt>($session)</tt></a></li>
13 <li><a href="#ObjectMethod <strong>load</strong> ($allDisable"> ObjectMethod load <tt>($allDisabled) -> $loginName</tt></a></li>
14 <li><a href="#ObjectMethod <strong>settings</strong> ()"> ObjectMethod settings <tt>()</tt></a></li>
15 <li><a href="#ObjectMethod <strong>enable</strong> ()"> ObjectMethod enable <tt>()</tt></a></li>
16 <li><a href="#ObjectMethod *get_PluginVersion*"> ObjectMethod getPluginVersion <tt>() -> $number</tt></a></li>
17 <li><a href="#ObjectMethod <strong>addListener</strong> ($com"> ObjectMethod addListener <tt>($command,$handler)</tt></a></li>
18 <li><a href="#ObjectMethod <strong>have_HandlerFor</strong> ("> ObjectMethod haveHandlerFor <tt>($handlerName) -> $boolean</tt></a></li>
19 <li><a href="#ObjectMethod *registrationHandle"> ObjectMethod registrationHandler <tt>()</tt></a></li>
20 <li><a href="#ObjectMethod *before_CommonTagsH"> ObjectMethod beforeCommonTagsHandler <tt>()</tt></a></li>
21 <li><a href="#ObjectMethod *common_TagsHandler"> ObjectMethod commonTagsHandler <tt>()</tt></a></li>
22 <li><a href="#ObjectMethod *after_CommonTagsHa"> ObjectMethod afterCommonTagsHandler <tt>()</tt></a></li>
23 <li><a href="#ObjectMethod *pre_RenderingHandl"> ObjectMethod preRenderingHandler <tt>($text,\%map)</tt></a></li>
24 <li><a href="#ObjectMethod *post_RenderingHand"> ObjectMethod postRenderingHandler <tt>(\$text)</tt></a></li>
25 <li><a href="#ObjectMethod *start_RenderingHan"> ObjectMethod startRenderingHandler <tt>()</tt></a></li>
26 <li><a href="#ObjectMethod *outsidePREHandler*"> ObjectMethod outsidePREHandler <tt>()</tt></a></li>
27 <li><a href="#ObjectMethod *insidePREHandler*"> ObjectMethod insidePREHandler <tt>()</tt></a></li>
28 <li><a href="#ObjectMethod *end_RenderingHandl"> ObjectMethod endRenderingHandler <tt>()</tt></a></li>
29 <li><a href="#ObjectMethod *before_EditHandler"> ObjectMethod beforeEditHandler <tt>()</tt></a></li>
30 <li><a href="#ObjectMethod *after_EditHandler*"> ObjectMethod afterEditHandler <tt>()</tt></a></li>
31 <li><a href="#ObjectMethod *before_SaveHandler"> ObjectMethod beforeSaveHandler <tt>()</tt></a></li>
32 <li><a href="#ObjectMethod *after_SaveHandler*"> ObjectMethod afterSaveHandler <tt>()</tt></a></li>
33 <li><a href="#ObjectMethod *after_RenameHandle"> ObjectMethod afterRenameHandler <tt>($oldWeb,$oldTopic,$oldAttachment,$newWeb,$newTopic,$newAttachment)</tt></a></li>
34 <li><a href="#ObjectMethod <strong>mergeHandler</strong> ()"> ObjectMethod mergeHandler <tt>()</tt></a></li>
35 <li><a href="#ObjectMethod *before_AttachmentS"> ObjectMethod beforeAttachmentSaveHandler <tt>($attrHashRef,$topic,$web)</tt></a></li>
36 <li><a href="#ObjectMethod *after_AttachmentSa"> ObjectMethod afterAttachmentSaveHandler <tt>($attachmentAttrHash,$topic,$web,$error)</tt></a></li>
37 <li><a href="#ObjectMethod *write_HeaderHandle"> ObjectMethod writeHeaderHandler <tt>() -> $headers</tt></a></li>
38 <li><a href="#ObjectMethod *modify_HeaderHandl"> ObjectMethod modifyHeaderHandler <tt>(\@headers,$query)</tt></a></li>
39 <li><a href="#ObjectMethod *redirect_CgiQueryH"> ObjectMethod redirectCgiQueryHandler <tt>() -> $result</tt></a></li>
40 <li><a href="#ObjectMethod *render_FormFieldFo"> ObjectMethod renderFormFieldForEditHandler <tt>($name,$type,$size,$value,$attributes,$possibleValues) -> $html</tt></a></li>
41 <li><a href="#ObjectMethod *render_WikiWordHan"> ObjectMethod renderWikiWordHandler <tt>() -> $result</tt></a></li>
47 Note that as of version 1.026 of this module, TWiki internal methods are _no longer available_ to plugins. Any calls to TWiki internal methods must be replaced by calls via the `$SESSION` object in this package, or via the Func package. For example, the call:
49 `my $pref = TWiki::getPreferencesValue('URGH');`
51 should be replaced with
53 `my $pref = TWiki::Func::getPreferencesValue('URGH');`
57 `my $t = TWiki::writeWarning($message);`
59 should be replaced with
61 `my $pref = $TWiki::Plugins::SESSION->writeWarning($message);`
63 Methods in other modules such as Store must be accessed through the relevant TWiki sub-object, for example
65 `TWiki::Store::saveTopic(...)`
67 should be replaced with
69 `$TWiki::Plugins::SESSION->{store}->saveTopic(...)`
71 Note that calling TWiki internal methods is very very bad practice, and should be avoided wherever practical.
73 The developers of TWiki reserve the right to change internal methods without warning, unless those methods are clearly marked as PUBLIC. PUBLIC methods are part of the core specification of a module and can be trusted.
75 ## <a name="PUBLIC constant $VERSION"></a> PUBLIC constant $VERSION
77 This is the version number of the plugins package. Use it for checking if you have a recent enough version.
79 ## <a name="PUBLIC $SESSION"></a> PUBLIC $SESSION
81 This is a reference to the TWiki session object. It can be used in plugins to get at the methods of the TWiki kernel.
83 You are _highly_ recommended to only use the methods in the [[Func|Main/TWikiFuncDotPm]] interface, unless you have no other choice, as kernel methods may change between TWiki releases.
85 ## <a name="ClassMethod <strong>new</strong> ($session)"></a> [[ClassMethod]] **new** `($session)`
87 Construct new singleton plugins collection object. The object is a container for a list of plugins and the handlers registered by the plugins. The plugins and the handlers are carefully ordered.
89 ## <a name="ObjectMethod <strong>load</strong> ($allDisable"></a> [[ObjectMethod]] **load** `($allDisabled) -> $loginName`
91 Find all active plugins, and invoke the early initialisation. Has to be done _after_ prefs are read.
93 Returns the user returned by the last `initializeUserHandler` to be called.
95 If allDisabled is set, no plugin handlers will be called.
97 ## <a name="ObjectMethod <strong>settings</strong> ()"></a> [[ObjectMethod]] **settings** `()`
99 Push plugin settings onto preference stack
101 ## <a name="ObjectMethod <strong>enable</strong> ()"></a> [[ObjectMethod]] **enable** `()`
103 Initialisation that is done after the user is known.
105 ## <a name="ObjectMethod <strong>get_PluginVersion*"></a> [[ObjectMethod]] \*getPluginVersion `() -> $number`
107 Returns the $TWiki::Plugins::VERSION number if no parameter is specified, else returns the version number of a named Plugin. If the Plugin cannot be found or is not active, 0 is returned.
109 ## <a name="ObjectMethod <strong>addListener</strong> ($com"></a> [[ObjectMethod]] **addListener** `($command,$handler)`
111 - =$command\* - name of the event
112 - `$handler` - the handler object.
114 Add a listener to the end of the list of registered listeners for this event. The listener must implement `invoke($command,...)`, which will be triggered when the event is to be processed.
116 ## <a name="ObjectMethod <strong>have_HandlerFor</strong> ("></a> [[ObjectMethod]] **haveHandlerFor** `($handlerName) -> $boolean`
118 - `$handlerName` - name of the handler e.g. preRenderingHandler
120 Return: true if at least one plugin has registered a handler of this type.
122 ## <a name="ObjectMethod <strong>registrationHandle"></a> [[ObjectMethod]] \*registrationHandler `()`
124 Called by the register script
126 ## <a name="ObjectMethod <strong>before_CommonTagsH"></a> [[ObjectMethod]] \*beforeCommonTagsHandler `()`
128 Called at the beginning (for cache Plugins only)
130 ## <a name="ObjectMethod <strong>common_TagsHandler"></a> [[ObjectMethod]] \*commonTagsHandler `()`
132 Called after %INCLUDE:"..."%
134 ## <a name="ObjectMethod <strong>after_CommonTagsHa"></a> [[ObjectMethod]] \*afterCommonTagsHandler `()`
136 Called at the end (for cache Plugins only)
138 ## <a name="ObjectMethod <strong>pre_RenderingHandl"></a> [[ObjectMethod]] \*preRenderingHandler `($text,\%map)`
140 - `$text` - the text, with the head, verbatim and pre blocks replaced with placeholders
141 - `\%removed` - reference to a hash that maps the placeholders to the removed blocks.
143 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.
145 Each removed block is represented by the block text and the parameters passed to the tag (usually empty) e.g. for
147 <pre class='slobadob'>
150 the map will contain:
152 $removed->{'pre1'}{text}: XYZ
153 $removed->{'pre1'}{params}: class="slobadob"
156 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:
158 foreach my $placeholder ( keys %$map ) \{ if( $placeholder =~ /^pre/i ) \{ my $n = 1; $map->\{$placeholder\}\{text\} =~ s/^/$n++/gem; \} \}
160 ## <a name="ObjectMethod <strong>post_RenderingHand"></a> [[ObjectMethod]] \*postRenderingHandler `(\$text)`
162 - `\$text` - a reference to the HTML, with the head, verbatim and pre blocks replaced with placeholders
164 ## <a name="ObjectMethod <strong>start_RenderingHan"></a> [[ObjectMethod]] \*startRenderingHandler `()`
166 Called just before the line loop
168 **DEPRECATED** Use preRenderingHandler instead. This handler correctly handles verbatim and other TWiki ML block types, and exposes them to the plugin.
170 ## <a name="ObjectMethod <strong>outsidePREHandler*"></a> [[ObjectMethod]] \*outsidePREHandler `()`
172 Called in line loop outside of <PRE> tag
174 **DEPRECATED** Use preRenderingHandler instead. This handler correctly handles pre and other TWiki ML block types, and is called only once instead of line-by-line.
176 ## <a name="ObjectMethod <strong>insidePREHandler*"></a><a name="ObjectMethod *insidePREHandler</strong> "></a> [[ObjectMethod]] **insidePREHandler** `()`
178 Called in line loop inside of <PRE> tag
180 **DEPRECATED** Use preRenderingHandler instead. This handler correctly handles pre and other TWiki ML block types, and is called only once instead of line-by-line.
182 ## <a name="ObjectMethod <strong>end_RenderingHandl"></a> [[ObjectMethod]] \*endRenderingHandler `()`
184 Called just after the line loop
186 **DEPRECATED** Use postRenderingHandler instead.
188 ## <a name="ObjectMethod <strong>before_EditHandler"></a> [[ObjectMethod]] \*beforeEditHandler `()`
192 ## <a name="ObjectMethod <strong>after_EditHandler*"></a> [[ObjectMethod]] \*afterEditHandler `()`
196 ## <a name="ObjectMethod <strong>before_SaveHandler"></a> [[ObjectMethod]] \*beforeSaveHandler `()`
198 Called just before the save action
200 ## <a name="ObjectMethod <strong>after_SaveHandler*"></a> [[ObjectMethod]] \*afterSaveHandler `()`
202 Called just after the save action
204 ## <a name="ObjectMethod <strong>after_RenameHandle"></a> [[ObjectMethod]] \*afterRenameHandler `($oldWeb,$oldTopic,$oldAttachment,$newWeb,$newTopic,$newAttachment)`
206 Called just after the rename/move/delete action of a web, topic or attachment.
208 - `$oldWeb` - name of old web
209 - `$oldTopic` - name of old topic (empty string if web rename)
210 - `$oldAttachment` - name of old attachment (empty string if web or topic rename)
211 - `$newWeb` - name of new web
212 - `$newTopic` - name of new topic (empty string if web rename)
213 - `$newAttachment` - name of new attachment (empty string if web or topic rename)
215 ## <a name="ObjectMethod <strong>mergeHandler</strong> ()"></a> [[ObjectMethod]] **mergeHandler** `()`
217 Called to handle text merge.
219 ## <a name="ObjectMethod <strong>before_AttachmentS"></a> [[ObjectMethod]] \*beforeAttachmentSaveHandler `($attrHashRef,$topic,$web)`
221 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.
223 - `$attrHashRef` - Hash reference of attachment attributes (keys are indicated below)
224 - `$topic` - | Topic name
225 - `$web` - | Web name
227 Keys in $attrHashRef:
229 <table border="1" cellpadding="0" cellspacing="0">
231 <th bgcolor="#99CCCC"><strong> Key </strong></th>
232 <th bgcolor="#99CCCC"><strong> Value </strong></th>
235 <td> attachment </td>
236 <td> Name of the attachment </td>
239 <td> tmpFilename </td>
240 <td> Name of the local file that stores the upload </td>
244 <td> Comment to be associated with the upload </td>
248 <td> Login name of the person submitting the attachment, e.g. 'jsmith' </td>
252 Note: All keys should be used read-only, except for comment which can be modified.
256 my( $attrHashRef, $topic, $web ) = @_;
257 $$attrHashRef{'comment'} .= " (NOTE: Extracted from blah.tar.gz)";
259 ## <a name="ObjectMethod <strong>after_AttachmentSa"></a> [[ObjectMethod]] \*afterAttachmentSaveHandler `($attachmentAttrHash,$topic,$web,$error)`
261 deal with an uploaded attachment between the upload and save-to-store processes. It is invoked as per other plugins.
263 - `$attrHashRef` - Hash reference of attachment attributes (keys are indicated below)
264 - `$topic` - | Topic name
265 - `$web` - | Web name
266 - `$error` - | Error string of save action, empty if OK
268 Keys in $attrHashRef:
270 <table border="1" cellpadding="0" cellspacing="0">
272 <th bgcolor="#99CCCC"><strong> Key </strong></th>
273 <th bgcolor="#99CCCC"><strong> Value </strong></th>
276 <td> attachment </td>
277 <td> Name of the attachment </td>
280 <td> tmpFilename </td>
281 <td> Name of the local file that stores the upload </td>
285 <td> Comment to be associated with the upload </td>
289 <td> Login name of the person submitting the attachment, e.g. 'jsmith' </td>
293 Note: The hash is **read-only**
295 ## <a name="ObjectMethod <strong>write_HeaderHandle"></a> [[ObjectMethod]] \*writeHeaderHandler `() -> $headers`
297 Called by TWiki::writePageHeader. **DEPRECATED** do not use!
299 **DEPRECATED** Use modifyHeaderHandler instead. it is a lot more flexible, and allows you to modify existing headers as well as add new ones. It also works correctly when multiple plugins want to modify headers.
301 ## <a name="ObjectMethod <strong>modify_HeaderHandl"></a> [[ObjectMethod]] \*modifyHeaderHandler `(\@headers,$query)`
303 ## <a name="ObjectMethod <strong>redirect_CgiQueryH"></a> [[ObjectMethod]] \*redirectCgiQueryHandler `() -> $result`
305 Called by TWiki::redirect
307 ## <a name="ObjectMethod <strong>render_FormFieldFo"></a> [[ObjectMethod]] \*renderFormFieldForEditHandler `($name,$type,$size,$value,$attributes,$possibleValues) -> $html`
309 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.
311 - `$name` - name of form field
312 - `$type` - type of form field
313 - `$size` - size of form field
314 - `$value` - value held in the form field
315 - `$attributes` - attributes of form field
316 - `$possibleValues` - the values defined as options for form field, if any. May be a scalar (one legal value) or an array (several legal values)
318 Return HTML text that renders this field. If false, form rendering continues by considering the built-in types.
320 Note that a common application would be to generate formatting of the field involving generation of javascript. Such usually also requires the insertion of some common javascript into the page header. Unfortunately, there is currently no mechanism to pass that script to where the header of the page is visible. Consequentially, the common javascript may have to be emitted as part of the field formatting and might be duplicated many times throughout the page.
322 ## <a name="ObjectMethod <strong>render_WikiWordHan"></a> [[ObjectMethod]] \*renderWikiWordHandler `() -> $result`
324 Change how a [[WikiWord]] is rendered
326 Originated from the TWiki:Plugins.SpacedWikiWordPlugin hack