--help ftw !
[lhc/web/wiklou.git] / docs / hooks.txt
1 hooks.txt
2
3 This document describes how event hooks work in MediaWiki; how to add
4 hooks for an event; and how to run hooks for an event.
5
6 ==Glossary==
7
8 event
9 Something that happens with the wiki. For example: a user logs
10 in. A wiki page is saved. A wiki page is deleted. Often there are
11 two events associated with a single action: one before the code
12 is run to make the event happen, and one after. Each event has a
13 name, preferably in CamelCase. For example, 'UserLogin',
14 'ArticleSave', 'ArticleSaveComplete', 'ArticleDelete'.
15
16 hook
17 A clump of code and data that should be run when an event
18 happens. This can be either a function and a chunk of data, or an
19 object and a method.
20
21 hook function
22 The function part of a hook.
23
24 ==Rationale==
25
26 Hooks allow us to decouple optionally-run code from code that is run
27 for everyone. It allows MediaWiki hackers, third-party developers and
28 local administrators to define code that will be run at certain points
29 in the mainline code, and to modify the data run by that mainline
30 code. Hooks can keep mainline code simple, and make it easier to
31 write extensions. Hooks are a principled alternative to local patches.
32
33 Consider, for example, two options in MediaWiki. One reverses the
34 order of a title before displaying the article; the other converts the
35 title to all uppercase letters. Currently, in MediaWiki code, we
36 would handle this as follows (note: not real code, here):
37
38 function showAnArticle($article) {
39 global $wgReverseTitle, $wgCapitalizeTitle;
40
41 if ($wgReverseTitle) {
42 wfReverseTitle($article);
43 }
44
45 if ($wgCapitalizeTitle) {
46 wfCapitalizeTitle($article);
47 }
48
49 # code to actually show the article goes here
50 }
51
52 An extension writer, or a local admin, will often add custom code to
53 the function -- with or without a global variable. For example,
54 someone wanting email notification when an article is shown may add:
55
56 function showAnArticle($article) {
57 global $wgReverseTitle, $wgCapitalizeTitle;
58
59 if ($wgReverseTitle) {
60 wfReverseTitle($article);
61 }
62
63 if ($wgCapitalizeTitle) {
64 wfCapitalizeTitle($article);
65 }
66
67 # code to actually show the article goes here
68
69 if ($wgNotifyArticle) {
70 wfNotifyArticleShow($article));
71 }
72 }
73
74 Using a hook-running strategy, we can avoid having all this
75 option-specific stuff in our mainline code. Using hooks, the function
76 becomes:
77
78 function showAnArticle($article) {
79
80 if (wfRunHooks('ArticleShow', array(&$article))) {
81
82 # code to actually show the article goes here
83
84 wfRunHooks('ArticleShowComplete', array(&$article));
85 }
86 }
87
88 We've cleaned up the code here by removing clumps of weird,
89 infrequently used code and moving them off somewhere else. It's much
90 easier for someone working with this code to see what's _really_ going
91 on, and make changes or fix bugs.
92
93 In addition, we can take all the code that deals with the little-used
94 title-reversing options (say) and put it in one place. Instead of
95 having little title-reversing if-blocks spread all over the codebase
96 in showAnArticle, deleteAnArticle, exportArticle, etc., we can
97 concentrate it all in an extension file:
98
99 function reverseArticleTitle($article) {
100 # ...
101 }
102
103 function reverseForExport($article) {
104 # ...
105 }
106
107 The setup function for the extension just has to add its hook
108 functions to the appropriate events:
109
110 setupTitleReversingExtension() {
111 global $wgHooks;
112
113 $wgHooks['ArticleShow'][] = 'reverseArticleTitle';
114 $wgHooks['ArticleDelete'][] = 'reverseArticleTitle';
115 $wgHooks['ArticleExport'][] = 'reverseForExport';
116 }
117
118 Having all this code related to the title-reversion option in one
119 place means that it's easier to read and understand; you don't have to
120 do a grep-find to see where the $wgReverseTitle variable is used, say.
121
122 If the code is well enough isolated, it can even be excluded when not
123 used -- making for some slight savings in memory and load-up
124 performance at runtime. Admins who want to have all the reversed
125 titles can add:
126
127 require_once('extensions/ReverseTitle.php');
128
129 ...to their LocalSettings.php file; those of us who don't want or need
130 it can just leave it out.
131
132 The extensions don't even have to be shipped with MediaWiki; they
133 could be provided by a third-party developer or written by the admin
134 him/herself.
135
136 ==Writing hooks==
137
138 A hook is a chunk of code run at some particular event. It consists of:
139
140 * a function with some optional accompanying data, or
141 * an object with a method and some optional accompanying data.
142
143 Hooks are registered by adding them to the global $wgHooks array for a
144 given event. All the following are valid ways to define hooks:
145
146 $wgHooks['EventName'][] = 'someFunction'; # function, no data
147 $wgHooks['EventName'][] = array('someFunction', $someData);
148 $wgHooks['EventName'][] = array('someFunction'); # weird, but OK
149
150 $wgHooks['EventName'][] = $object; # object only
151 $wgHooks['EventName'][] = array($object, 'someMethod');
152 $wgHooks['EventName'][] = array($object, 'someMethod', $someData);
153 $wgHooks['EventName'][] = array($object); # weird but OK
154
155 When an event occurs, the function (or object method) will be called
156 with the optional data provided as well as event-specific parameters.
157 The above examples would result in the following code being executed
158 when 'EventName' happened:
159
160 # function, no data
161 someFunction($param1, $param2)
162 # function with data
163 someFunction($someData, $param1, $param2)
164
165 # object only
166 $object->onEventName($param1, $param2)
167 # object with method
168 $object->someMethod($param1, $param2)
169 # object with method and data
170 $object->someMethod($someData, $param1, $param2)
171
172 Note that when an object is the hook, and there's no specified method,
173 the default method called is 'onEventName'. For different events this
174 would be different: 'onArticleSave', 'onUserLogin', etc.
175
176 The extra data is useful if we want to use the same function or object
177 for different purposes. For example:
178
179 $wgHooks['ArticleSaveComplete'][] = array('ircNotify', 'TimStarling');
180 $wgHooks['ArticleSaveComplete'][] = array('ircNotify', 'brion');
181
182 This code would result in ircNotify being run twice when an article is
183 saved: once for 'TimStarling', and once for 'brion'.
184
185 Hooks can return three possible values:
186
187 * true: the hook has operated successfully
188 * "some string": an error occurred; processing should
189 stop and the error should be shown to the user
190 * false: the hook has successfully done the work
191 necessary and the calling function should skip
192
193 The last result would be for cases where the hook function replaces
194 the main functionality. For example, if you wanted to authenticate
195 users to a custom system (LDAP, another PHP program, whatever), you
196 could do:
197
198 $wgHooks['UserLogin'][] = array('ldapLogin', $ldapServer);
199
200 function ldapLogin($username, $password) {
201 # log user into LDAP
202 return false;
203 }
204
205 Returning false makes less sense for events where the action is
206 complete, and will normally be ignored.
207
208 ==Using hooks==
209
210 A calling function or method uses the wfRunHooks() function to run
211 the hooks related to a particular event, like so:
212
213 class Article {
214 # ...
215 function protect() {
216 global $wgUser;
217 if (wfRunHooks('ArticleProtect', array(&$this, &$wgUser))) {
218 # protect the article
219 wfRunHooks('ArticleProtectComplete', array(&$this, &$wgUser));
220 }
221 }
222
223 wfRunHooks() returns true if the calling function should continue
224 processing (the hooks ran OK, or there are no hooks to run), or false
225 if it shouldn't (an error occurred, or one of the hooks handled the
226 action already). Checking the return value matters more for "before"
227 hooks than for "complete" hooks.
228
229 Note that hook parameters are passed in an array; this is a necessary
230 inconvenience to make it possible to pass reference values (that can
231 be changed) into the hook code. Also note that earlier versions of
232 wfRunHooks took a variable number of arguments; the array() calling
233 protocol came about after MediaWiki 1.4rc1.
234
235 ==Events and parameters==
236
237 This is a list of known events and parameters; please add to it if
238 you're going to add events to the MediaWiki code.
239
240 'AddNewAccount': after a user account is created
241 null: This hook passes null as an argument
242
243 'ArticleDelete': before an article is deleted
244 $article: the article (object) being deleted
245 $user: the user (object) deleting the article
246 $reason: the reason (string) the article is being deleted
247
248 'ArticleDeleteComplete': after an article is deleted
249 $article: the article that was deleted
250 $user: the user that deleted the article
251 $reason: the reason the article was deleted
252
253 'ArticleProtect': before an article is protected
254 $article: the article being protected
255 $user: the user doing the protection
256 $protect: boolean whether this is a protect or an unprotect
257 $reason: Reason for protect
258 $moveonly: boolean whether this is for move only or not
259
260 'ArticleProtectComplete': after an article is protected
261 $article: the article that was protected
262 $user: the user who did the protection
263 $protect: boolean whether it was a protect or an unprotect
264 $reason: Reason for protect
265 $moveonly: boolean whether it was for move only or not
266
267 'ArticleSave': before an article is saved
268 $article: the article (object) being saved
269 $user: the user (object) saving the article
270 $text: the new article text
271 $summary: the article summary (comment)
272 $isminor: minor flag
273 $iswatch: watch flag
274 $section: section #
275
276 'ArticleSaveComplete': after an article is saved
277 $article: the article (object) saved
278 $user: the user (object) who saved the article
279 $text: the new article text
280 $summary: the article summary (comment)
281 $isminor: minor flag
282 $iswatch: watch flag
283 $section: section #
284
285 'BlockIp': before an IP address or user is blocked
286 $block: the Block object about to be saved
287 $user: the user _doing_ the block (not the one being blocked)
288
289 'BlockIpComplete': after an IP address or user is blocked
290 $block: the Block object that was saved
291 $user: the user who did the block (not the one being blocked)
292
293 'EmailUser': before sending email from one user to another
294 $to: address of receiving user
295 $from: address of sending user
296 $subject: subject of the mail
297 $text: text of the mail
298
299 'EmailUserComplete': after sending email from one user to another
300 $to: address of receiving user
301 $from: address of sending user
302 $subject: subject of the mail
303 $text: text of the mail
304
305 'LogPageValidTypes': action being logged.
306 $type: array of strings
307
308 'LogPageLogName': name of the logging page(s).
309 $typeText: array of strings
310
311 'LogPageLogHeader': strings used by wfMsg as a header.
312 $headerText: array of strings
313
314 'TitleMoveComplete': after moving an article (title)
315 $old: old title
316 $nt: new title
317 $user: user who did the move
318 $pageid: database ID of the page that's been moved
319 $redirid: database ID of the created redirect
320
321 'UnknownAction': An unknown "action" has occured (useful for defining
322 your own actions)
323 $action: action name
324 $article: article "acted on"
325
326 'UnwatchArticle': before a watch is removed from an article
327 $user: user watching
328 $article: article object to be removed
329
330 'UnwatchArticle': after a watch is removed from an article
331 $user: user that was watching
332 $article: article object removed
333
334 'UploadVerification': additional chances to reject an uploaded file
335 string $saveName: destination file name
336 string $tempName: filesystem path to the temporary file for checks
337 string &$error: output: HTML error to show if upload canceled by returning false
338
339 'UserLoginComplete': after a user has logged in
340 $user: the user object that was created on login
341
342 'UserLogout': before a user logs out
343 $user: the user object that is about to be logged out
344
345 'UserLogoutComplete': after a user has logged out
346 $user: the user object _after_ logout (won't have name, ID, etc.)
347
348 'WatchArticle': before a watch is added to an article
349 $user: user that will watch
350 $article: article object to be watched
351
352 'WatchArticleComplete': after a watch is added to an article
353 $user: user that watched
354 $article: article object watched
355
356 'CategoryPageView': before viewing a categorypage in CategoryPage::view
357 $catpage: CategoryPage instance
358
359 'SkinTemplateContentActions': after building the $content_action array right
360 before returning it, see content_action.php in
361 the extension module for a demonstration of how
362 to use this hook.
363 $content_actions: The array of content actions
364
365
366 More hooks might be available but undocumented.