none
[openafs-wiki.git] / TWiki / TWikiPluginsDotPm.mdwn
1 # <a name="Package &lt;code&gt;TWiki::Plugins="></a> Package =TWiki::Plugins
2
3 This module defines the singleton object that handles Plugins loading, initialization and execution.
4
5 This class uses Chain of Responsibility (GOF) pattern to dispatch handler calls to registered plugins.
6
7 <div>
8   <ul>
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) -&gt; $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>() -&gt; $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) -&gt; $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>() -&gt; $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>() -&gt; $result</tt></a></li>
40         <li><a href="#ObjectMethod *render_FormFieldFo"> ObjectMethod renderFormFieldForEditHandler <tt>($name,$type,$size,$value,$attributes,$possibleValues) -&gt; $html</tt></a></li>
41         <li><a href="#ObjectMethod *render_WikiWordHan"> ObjectMethod renderWikiWordHandler <tt>() -&gt; $result</tt></a></li>
42       </ul>
43     </li>
44   </ul>
45 </div>
46
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:
48
49 `my $pref = TWiki::getPreferencesValue('URGH');`
50
51 should be replaced with
52
53 `my $pref = TWiki::Func::getPreferencesValue('URGH');`
54
55 and the call
56
57 `my $t = TWiki::writeWarning($message);`
58
59 should be replaced with
60
61 `my $pref = $TWiki::Plugins::SESSION->writeWarning($message);`
62
63 Methods in other modules such as Store must be accessed through the relevant TWiki sub-object, for example
64
65 `TWiki::Store::saveTopic(...)`
66
67 should be replaced with
68
69 `$TWiki::Plugins::SESSION->{store}->saveTopic(...)`
70
71 Note that calling TWiki internal methods is very very bad practice, and should be avoided wherever practical.
72
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.
74
75 ## <a name="PUBLIC constant $VERSION"></a> PUBLIC constant $VERSION
76
77 This is the version number of the plugins package. Use it for checking if you have a recent enough version.
78
79 ## <a name="PUBLIC $SESSION"></a> PUBLIC $SESSION
80
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.
82
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.
84
85 ## <a name="ClassMethod &lt;strong&gt;new&lt;/strong&gt; ($session)"></a> [[ClassMethod]] **new** `($session)`
86
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.
88
89 ## <a name="ObjectMethod &lt;strong&gt;load&lt;/strong&gt; ($allDisable"></a> [[ObjectMethod]] **load** `($allDisabled) -> $loginName`
90
91 Find all active plugins, and invoke the early initialisation. Has to be done _after_ prefs are read.
92
93 Returns the user returned by the last `initializeUserHandler` to be called.
94
95 If allDisabled is set, no plugin handlers will be called.
96
97 ## <a name="ObjectMethod &lt;strong&gt;settings&lt;/strong&gt; ()"></a> [[ObjectMethod]] **settings** `()`
98
99 Push plugin settings onto preference stack
100
101 ## <a name="ObjectMethod &lt;strong&gt;enable&lt;/strong&gt; ()"></a> [[ObjectMethod]] **enable** `()`
102
103 Initialisation that is done after the user is known.
104
105 ## <a name="ObjectMethod &lt;strong&gt;get_PluginVersion*"></a> [[ObjectMethod]] \*getPluginVersion `() -> $number`
106
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.
108
109 ## <a name="ObjectMethod &lt;strong&gt;addListener&lt;/strong&gt; ($com"></a> [[ObjectMethod]] **addListener** `($command,$handler)`
110
111 - =$command\* - name of the event
112 - `$handler` - the handler object.
113
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.
115
116 ## <a name="ObjectMethod &lt;strong&gt;have_HandlerFor&lt;/strong&gt; ("></a> [[ObjectMethod]] **haveHandlerFor** `($handlerName) -> $boolean`
117
118 - `$handlerName` - name of the handler e.g. preRenderingHandler
119
120 Return: true if at least one plugin has registered a handler of this type.
121
122 ## <a name="ObjectMethod &lt;strong&gt;registrationHandle"></a> [[ObjectMethod]] \*registrationHandler `()`
123
124 Called by the register script
125
126 ## <a name="ObjectMethod &lt;strong&gt;before_CommonTagsH"></a> [[ObjectMethod]] \*beforeCommonTagsHandler `()`
127
128 Called at the beginning (for cache Plugins only)
129
130 ## <a name="ObjectMethod &lt;strong&gt;common_TagsHandler"></a> [[ObjectMethod]] \*commonTagsHandler `()`
131
132 Called after %INCLUDE:"..."%
133
134 ## <a name="ObjectMethod &lt;strong&gt;after_CommonTagsHa"></a> [[ObjectMethod]] \*afterCommonTagsHandler `()`
135
136 Called at the end (for cache Plugins only)
137
138 ## <a name="ObjectMethod &lt;strong&gt;pre_RenderingHandl"></a> [[ObjectMethod]] \*preRenderingHandler `($text,\%map)`
139
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.
142
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.
144
145 Each removed block is represented by the block text and the parameters passed to the tag (usually empty) e.g. for
146
147     <pre class='slobadob'>
148     XYZ
149     </pre>
150     the map will contain:
151     <pre>
152     $removed->{'pre1'}{text}:   XYZ
153     $removed->{'pre1'}{params}: class="slobadob"
154     </pre>
155
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:
157
158 foreach my $placeholder ( keys %$map ) \{ if( $placeholder =~ /^pre/i ) \{ my $n = 1; $map-&gt;\{$placeholder\}\{text\} =~ s/^/$n++/gem; \} \}
159
160 ## <a name="ObjectMethod &lt;strong&gt;post_RenderingHand"></a> [[ObjectMethod]] \*postRenderingHandler `(\$text)`
161
162 - `\$text` - a reference to the HTML, with the head, verbatim and pre blocks replaced with placeholders
163
164 ## <a name="ObjectMethod &lt;strong&gt;start_RenderingHan"></a> [[ObjectMethod]] \*startRenderingHandler `()`
165
166 Called just before the line loop
167
168 **DEPRECATED** Use preRenderingHandler instead. This handler correctly handles verbatim and other TWiki ML block types, and exposes them to the plugin.
169
170 ## <a name="ObjectMethod &lt;strong&gt;outsidePREHandler*"></a> [[ObjectMethod]] \*outsidePREHandler `()`
171
172 Called in line loop outside of &lt;PRE&gt; tag
173
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.
175
176 ## <a name="ObjectMethod &lt;strong&gt;insidePREHandler*"></a><a name="ObjectMethod *insidePREHandler&lt;/strong&gt; "></a> [[ObjectMethod]] **insidePREHandler** `()`
177
178 Called in line loop inside of &lt;PRE&gt; tag
179
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.
181
182 ## <a name="ObjectMethod &lt;strong&gt;end_RenderingHandl"></a> [[ObjectMethod]] \*endRenderingHandler `()`
183
184 Called just after the line loop
185
186 **DEPRECATED** Use postRenderingHandler instead.
187
188 ## <a name="ObjectMethod &lt;strong&gt;before_EditHandler"></a> [[ObjectMethod]] \*beforeEditHandler `()`
189
190 Called by edit
191
192 ## <a name="ObjectMethod &lt;strong&gt;after_EditHandler*"></a> [[ObjectMethod]] \*afterEditHandler `()`
193
194 Called by edit
195
196 ## <a name="ObjectMethod &lt;strong&gt;before_SaveHandler"></a> [[ObjectMethod]] \*beforeSaveHandler `()`
197
198 Called just before the save action
199
200 ## <a name="ObjectMethod &lt;strong&gt;after_SaveHandler*"></a> [[ObjectMethod]] \*afterSaveHandler `()`
201
202 Called just after the save action
203
204 ## <a name="ObjectMethod &lt;strong&gt;after_RenameHandle"></a> [[ObjectMethod]] \*afterRenameHandler `($oldWeb,$oldTopic,$oldAttachment,$newWeb,$newTopic,$newAttachment)`
205
206 Called just after the rename/move/delete action of a web, topic or attachment.
207
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)
214
215 ## <a name="ObjectMethod &lt;strong&gt;mergeHandler&lt;/strong&gt; ()"></a> [[ObjectMethod]] **mergeHandler** `()`
216
217 Called to handle text merge.
218
219 ## <a name="ObjectMethod &lt;strong&gt;before_AttachmentS"></a> [[ObjectMethod]] \*beforeAttachmentSaveHandler `($attrHashRef,$topic,$web)`
220
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.
222
223 - `$attrHashRef` - Hash reference of attachment attributes (keys are indicated below)
224 - `$topic` - | Topic name
225 - `$web` - | Web name
226
227 Keys in $attrHashRef:
228
229 <table border="1" cellpadding="0" cellspacing="0">
230   <tr>
231     <th bgcolor="#99CCCC"><strong> Key </strong></th>
232     <th bgcolor="#99CCCC"><strong> Value </strong></th>
233   </tr>
234   <tr>
235     <td> attachment </td>
236     <td> Name of the attachment </td>
237   </tr>
238   <tr>
239     <td> tmpFilename </td>
240     <td> Name of the local file that stores the upload </td>
241   </tr>
242   <tr>
243     <td> comment </td>
244     <td> Comment to be associated with the upload </td>
245   </tr>
246   <tr>
247     <td> user </td>
248     <td> Login name of the person submitting the attachment, e.g. 'jsmith' </td>
249   </tr>
250 </table>
251
252 Note: All keys should be used read-only, except for comment which can be modified.
253
254 Example usage:
255
256        my( $attrHashRef, $topic, $web ) = @_;
257        $$attrHashRef{'comment'} .= " (NOTE: Extracted from blah.tar.gz)";
258
259 ## <a name="ObjectMethod &lt;strong&gt;after_AttachmentSa"></a> [[ObjectMethod]] \*afterAttachmentSaveHandler `($attachmentAttrHash,$topic,$web,$error)`
260
261 deal with an uploaded attachment between the upload and save-to-store processes. It is invoked as per other plugins.
262
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
267
268 Keys in $attrHashRef:
269
270 <table border="1" cellpadding="0" cellspacing="0">
271   <tr>
272     <th bgcolor="#99CCCC"><strong> Key </strong></th>
273     <th bgcolor="#99CCCC"><strong> Value </strong></th>
274   </tr>
275   <tr>
276     <td> attachment </td>
277     <td> Name of the attachment </td>
278   </tr>
279   <tr>
280     <td> tmpFilename </td>
281     <td> Name of the local file that stores the upload </td>
282   </tr>
283   <tr>
284     <td> comment </td>
285     <td> Comment to be associated with the upload </td>
286   </tr>
287   <tr>
288     <td> user </td>
289     <td> Login name of the person submitting the attachment, e.g. 'jsmith' </td>
290   </tr>
291 </table>
292
293 Note: The hash is **read-only**
294
295 ## <a name="ObjectMethod &lt;strong&gt;write_HeaderHandle"></a> [[ObjectMethod]] \*writeHeaderHandler `() -> $headers`
296
297 Called by TWiki::writePageHeader. **DEPRECATED** do not use!
298
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.
300
301 ## <a name="ObjectMethod &lt;strong&gt;modify_HeaderHandl"></a> [[ObjectMethod]] \*modifyHeaderHandler `(\@headers,$query)`
302
303 ## <a name="ObjectMethod &lt;strong&gt;redirect_CgiQueryH"></a> [[ObjectMethod]] \*redirectCgiQueryHandler `() -> $result`
304
305 Called by TWiki::redirect
306
307 ## <a name="ObjectMethod &lt;strong&gt;render_FormFieldFo"></a> [[ObjectMethod]] \*renderFormFieldForEditHandler `($name,$type,$size,$value,$attributes,$possibleValues) -> $html`
308
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.
310
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)
317
318 Return HTML text that renders this field. If false, form rendering continues by considering the built-in types.
319
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.
321
322 ## <a name="ObjectMethod &lt;strong&gt;render_WikiWordHan"></a> [[ObjectMethod]] \*renderWikiWordHandler `() -> $result`
323
324 Change how a [[WikiWord]] is rendered
325
326 Originated from the TWiki:Plugins.SpacedWikiWordPlugin hack