r23073 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23072‎ | r23073 | r23074 >
Date:15:37, 18 June 2007
Author:brion
Status:old
Tags:
Comment:
don't mix spaces and tabs in code examples, or the WHITESPACE GODS SHALL SMITE THEE
Modified paths:
  • /trunk/phase3/docs/hooks.txt (modified) (history)

Diff [purge]

Index: trunk/phase3/docs/hooks.txt
@@ -75,15 +75,15 @@
7676 option-specific stuff in our mainline code. Using hooks, the function
7777 becomes:
7878
79 - function showAnArticle($article) {
 79+ function showAnArticle($article) {
8080
81 - if (wfRunHooks('ArticleShow', array(&$article))) {
82 -
83 - # code to actually show the article goes here
84 -
85 - wfRunHooks('ArticleShowComplete', array(&$article));
 81+ if (wfRunHooks('ArticleShow', array(&$article))) {
 82+
 83+ # code to actually show the article goes here
 84+
 85+ wfRunHooks('ArticleShowComplete', array(&$article));
 86+ }
8687 }
87 - }
8888
8989 We've cleaned up the code here by removing clumps of weird,
9090 infrequently used code and moving them off somewhere else. It's much
@@ -96,25 +96,25 @@
9797 in showAnArticle, deleteAnArticle, exportArticle, etc., we can
9898 concentrate it all in an extension file:
9999
100 - function reverseArticleTitle($article) {
101 - # ...
102 - }
 100+ function reverseArticleTitle($article) {
 101+ # ...
 102+ }
103103
104 - function reverseForExport($article) {
105 - # ...
106 - }
 104+ function reverseForExport($article) {
 105+ # ...
 106+ }
107107
108108 The setup function for the extension just has to add its hook
109109 functions to the appropriate events:
110110
111 - setupTitleReversingExtension() {
112 - global $wgHooks;
113 -
114 - $wgHooks['ArticleShow'][] = 'reverseArticleTitle';
115 - $wgHooks['ArticleDelete'][] = 'reverseArticleTitle';
116 - $wgHooks['ArticleExport'][] = 'reverseForExport';
117 - }
 111+ setupTitleReversingExtension() {
 112+ global $wgHooks;
118113
 114+ $wgHooks['ArticleShow'][] = 'reverseArticleTitle';
 115+ $wgHooks['ArticleDelete'][] = 'reverseArticleTitle';
 116+ $wgHooks['ArticleExport'][] = 'reverseForExport';
 117+ }
 118+
119119 Having all this code related to the title-reversion option in one
120120 place means that it's easier to read and understand; you don't have to
121121 do a grep-find to see where the $wgReverseTitle variable is used, say.
@@ -124,8 +124,8 @@
125125 performance at runtime. Admins who want to have all the reversed
126126 titles can add:
127127
128 - require_once('extensions/ReverseTitle.php');
129 -
 128+ require_once('extensions/ReverseTitle.php');
 129+
130130 ...to their LocalSettings.php file; those of us who don't want or need
131131 it can just leave it out.
132132
@@ -143,31 +143,31 @@
144144 Hooks are registered by adding them to the global $wgHooks array for a
145145 given event. All the following are valid ways to define hooks:
146146
147 - $wgHooks['EventName'][] = 'someFunction'; # function, no data
148 - $wgHooks['EventName'][] = array('someFunction', $someData);
149 - $wgHooks['EventName'][] = array('someFunction'); # weird, but OK
150 -
151 - $wgHooks['EventName'][] = $object; # object only
152 - $wgHooks['EventName'][] = array($object, 'someMethod');
153 - $wgHooks['EventName'][] = array($object, 'someMethod', $someData);
154 - $wgHooks['EventName'][] = array($object); # weird but OK
 147+ $wgHooks['EventName'][] = 'someFunction'; # function, no data
 148+ $wgHooks['EventName'][] = array('someFunction', $someData);
 149+ $wgHooks['EventName'][] = array('someFunction'); # weird, but OK
155150
 151+ $wgHooks['EventName'][] = $object; # object only
 152+ $wgHooks['EventName'][] = array($object, 'someMethod');
 153+ $wgHooks['EventName'][] = array($object, 'someMethod', $someData);
 154+ $wgHooks['EventName'][] = array($object); # weird but OK
 155+
156156 When an event occurs, the function (or object method) will be called
157157 with the optional data provided as well as event-specific parameters.
158158 The above examples would result in the following code being executed
159159 when 'EventName' happened:
160160
161 - # function, no data
162 - someFunction($param1, $param2)
163 - # function with data
164 - someFunction($someData, $param1, $param2)
 161+ # function, no data
 162+ someFunction($param1, $param2)
 163+ # function with data
 164+ someFunction($someData, $param1, $param2)
165165
166 - # object only
167 - $object->onEventName($param1, $param2)
168 - # object with method
169 - $object->someMethod($param1, $param2)
170 - # object with method and data
171 - $object->someMethod($someData, $param1, $param2)
 166+ # object only
 167+ $object->onEventName($param1, $param2)
 168+ # object with method
 169+ $object->someMethod($param1, $param2)
 170+ # object with method and data
 171+ $object->someMethod($someData, $param1, $param2)
172172
173173 Note that when an object is the hook, and there's no specified method,
174174 the default method called is 'onEventName'. For different events this
@@ -176,8 +176,8 @@
177177 The extra data is useful if we want to use the same function or object
178178 for different purposes. For example:
179179
180 - $wgHooks['ArticleSaveComplete'][] = array('ircNotify', 'TimStarling');
181 - $wgHooks['ArticleSaveComplete'][] = array('ircNotify', 'brion');
 180+ $wgHooks['ArticleSaveComplete'][] = array('ircNotify', 'TimStarling');
 181+ $wgHooks['ArticleSaveComplete'][] = array('ircNotify', 'brion');
182182
183183 This code would result in ircNotify being run twice when an article is
184184 saved: once for 'TimStarling', and once for 'brion'.
@@ -195,12 +195,12 @@
196196 users to a custom system (LDAP, another PHP program, whatever), you
197197 could do:
198198
199 - $wgHooks['UserLogin'][] = array('ldapLogin', $ldapServer);
200 -
201 - function ldapLogin($username, $password) {
202 - # log user into LDAP
203 - return false;
204 - }
 199+ $wgHooks['UserLogin'][] = array('ldapLogin', $ldapServer);
 200+
 201+ function ldapLogin($username, $password) {
 202+ # log user into LDAP
 203+ return false;
 204+ }
205205
206206 Returning false makes less sense for events where the action is
207207 complete, and will normally be ignored.
@@ -210,14 +210,15 @@
211211 A calling function or method uses the wfRunHooks() function to run
212212 the hooks related to a particular event, like so:
213213
214 - class Article {
215 - # ...
216 - function protect() {
217 - global $wgUser;
218 - if (wfRunHooks('ArticleProtect', array(&$this, &$wgUser))) {
219 - # protect the article
220 - wfRunHooks('ArticleProtectComplete', array(&$this, &$wgUser));
221 - }
 214+ class Article {
 215+ # ...
 216+ function protect() {
 217+ global $wgUser;
 218+ if (wfRunHooks('ArticleProtect', array(&$this, &$wgUser))) {
 219+ # protect the article
 220+ wfRunHooks('ArticleProtectComplete', array(&$this, &$wgUser));
 221+ }
 222+ }
222223 }
223224
224225 wfRunHooks() returns true if the calling function should continue

Follow-up revisions

RevisionCommit summaryAuthorDate
r23087Merged revisions 23050-23086 via svnmerge from...david03:14, 19 June 2007

Status & tagging log