| 1 | Hooks {#hooks} |
| 2 | ===== |
| 3 | |
| 4 | ## Introduction |
| 5 | |
| 6 | Hooks allow MediaWiki Core to call extensions or allow one extension to call |
| 7 | another extension. For more information and a list of hooks, see |
| 8 | https://www.mediawiki.org/wiki/Manual:Hooks |
| 9 | |
| 10 | Starting in MediaWiki 1.35, each hook called by MediaWiki Core has an |
| 11 | associated interface with a single method. To call the hook, obtain a "hook |
| 12 | runner" object, which implements the relevant interface, and call the relevant |
| 13 | method. To handle a hook event in an extension, create a handler object which |
| 14 | implements the interface. |
| 15 | |
| 16 | The name of the interface is the name of the hook with "Hook" added to the end. |
| 17 | Interfaces are typically placed in the namespace of their primary caller. |
| 18 | |
| 19 | The method name for the hook is the name of the hook, prefixed with "on". |
| 20 | |
| 21 | Several hooks had colons in their name, which are invalid in an interface or |
| 22 | method name. These hooks have interfaces and method names in which the colons |
| 23 | are replaced with underscores. |
| 24 | |
| 25 | For example, if the hook is called `Mash`, we might have the interface: |
| 26 | |
| 27 | interface MashHook { |
| 28 | public function onMash( $banana ); |
| 29 | } |
| 30 | |
| 31 | Hooks can be defined and called by extensions. The extension should define a |
| 32 | hook interface for each hook, as described above. |
| 33 | |
| 34 | ## HookContainer |
| 35 | |
| 36 | HookContainer is a service which is responsible for maintaining a list of hook |
| 37 | handlers and calling those handlers when requested. HookContainer is not aware |
| 38 | of hook interfaces or parameter types. |
| 39 | |
| 40 | HookContainer provides hook metadata. For example, `isRegistered()` tells us |
| 41 | whether there are any handlers for a given hook event. |
| 42 | |
| 43 | A HookContainer instance can be obtained from the global service locator with |
| 44 | MediaWikiServices::getHookContainer(). When implementing a service that needs |
| 45 | to call a hook, a HookContainer object should be passed to the constructor of |
| 46 | the service. |
| 47 | |
| 48 | ## Hook runner classes |
| 49 | |
| 50 | A hook runner is a class which implements hook interfaces, proxying the calls |
| 51 | to `HookContainer::run()`. |
| 52 | |
| 53 | MediaWiki has three hook runner classes: HookRunner, ApiHookRunner and |
| 54 | ResourceLoader\HookRunner. |
| 55 | ApiHookRunner has proxy methods for all hooks which are called by the Action |
| 56 | API. HookRunner has proxy methods for all hooks which are called by other parts |
| 57 | of Core. ResourceLoader\HookRunner contains hooks specific to ResourceLoader. |
| 58 | Some hooks are implemented in two hook runner classes. |
| 59 | |
| 60 | Extensions which call hooks should create their own hook runner class, by |
| 61 | analogy with the ones in Core. Hook runner classes are effectively internal |
| 62 | to the module which calls the relevant hooks. Reorganisation of the hook |
| 63 | calling code may lead to methods being removed from hook runner classes. Thus, |
| 64 | it is safer for extensions to define their own hook runner classes even if |
| 65 | they are calling Core hooks. |
| 66 | |
| 67 | New code should typically be written in a service which takes a HookContainer |
| 68 | as a constructor parameter. In static functions it can fall back to the global |
| 69 | HookContainer instance. |
| 70 | For example, to call the hook `Mash`, as defined above, in static code: |
| 71 | |
| 72 | $hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) |
| 73 | $hookRunner->onMash( $banana ); |
| 74 | |
| 75 | ## How to handle a hook event in an extension |
| 76 | |
| 77 | In extension.json, there is a new attribute called `HookHandlers`. This is |
| 78 | an object mapping the handler name to an ObjectFactory specification describing |
| 79 | how to create the handler object. The specification will typically have a |
| 80 | `class` member with the name of the handler class. For example, in an extension |
| 81 | called `FoodProcessor`, we may have: |
| 82 | |
| 83 | "HookHandlers": { |
| 84 | "main": { |
| 85 | "class": "MediaWiki\\Extension\\FoodProcessor\\HookHandler" |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | Then in the Hooks attribute, instead of a function name, the value will be the |
| 90 | handler name: |
| 91 | |
| 92 | "Hooks": { |
| 93 | "Mash": "main" |
| 94 | } |
| 95 | |
| 96 | Or more explicitly, by using an object instead of a string for the handler: |
| 97 | |
| 98 | "Hooks": { |
| 99 | "Mash": { |
| 100 | "handler": "main" |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | Note that while your HookHandler class will implement an interface that ends |
| 105 | with the word "Hook", in `extension.json` you should omit the word "Hook" |
| 106 | from the key in the `Hooks` definition. For example, in the definitions above, |
| 107 | the key must be "Mash", not "MashHook". |
| 108 | |
| 109 | Then the extension will define a handler class: |
| 110 | |
| 111 | namespace MediaWiki\Extension\FoodProcessor; |
| 112 | |
| 113 | class HookHandler implements MashHook { |
| 114 | public function onMash( $banana ) { |
| 115 | // Implementation goes here |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | ## Service dependencies |
| 120 | |
| 121 | The ObjectFactory specification in HookHandlers can contain a list of services |
| 122 | which should be instantiated and provided to the constructor or factory |
| 123 | function for the handler. For example: |
| 124 | |
| 125 | "HookHandlers": { |
| 126 | "main": { |
| 127 | "class": "MediaWiki\\Extension\\FoodProcessor\\HookHandler", |
| 128 | "services": [ "ReadOnlyMode" ] |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | However, care should be taken with this feature. Some services have expensive |
| 133 | constructors, so requesting them when handling commonly-called hooks may damage |
| 134 | performance. Also, some services may not be safe to construct from within a hook |
| 135 | call. |
| 136 | |
| 137 | The safest pattern for service injection is to use a separate handler for each |
| 138 | hook, and to inject only the services needed by that hook. |
| 139 | |
| 140 | Calling a hook with the `noServices` option disables service injection. If a |
| 141 | handler for such a hook specifies services, an exception will be thrown when |
| 142 | the hook is called. |
| 143 | |
| 144 | ## Returning and aborting |
| 145 | |
| 146 | If a hook handler returns false, HookContainer will stop iterating through the |
| 147 | list of handlers and will immediately return false. |
| 148 | |
| 149 | If a hook handler returns true, or if there is no return value (causing it to |
| 150 | effectively return null), then HookContainer will continue to call any other |
| 151 | remaining handlers. Eventually HookContainer::run() will return true. |
| 152 | |
| 153 | If there were no registered handlers, HookContainer::run() will return true. |
| 154 | |
| 155 | Some hooks are declared to be "not abortable". If a handler for a non-abortable |
| 156 | hook returns false, an exception will be thrown. A hook is declared to be not |
| 157 | abortable by passing `[ "abortable" => false ]` in the $options parameter to |
| 158 | HookContainer::run(). |
| 159 | |
| 160 | Aborting is properly used to enforce a convention that only one extension |
| 161 | may handle a given hook call. |
| 162 | |
| 163 | Aborting is sometimes used as a generic return value, to indicate that the |
| 164 | caller should stop performing some action. |
| 165 | |
| 166 | Most hook callers do not check the return value from HookContainer::run() and |
| 167 | there is no real concept of aborting. The only effect of returning `false` from |
| 168 | a handler of these hooks is to break other extensions. |
| 169 | |
| 170 | Theoretically, extensions which are registered first in LocalSettings.php will |
| 171 | be called first, and thus will have the first opportunity to abort a hook call. |
| 172 | This behaviour should not be relied upon. In the new hook system, handlers |
| 173 | registered in the legacy way are called first, before handlers registered in |
| 174 | the new way. |
| 175 | |
| 176 | ## Parameters passed by reference |
| 177 | |
| 178 | The typical way for a handler to return data to the caller is by modifying a |
| 179 | parameter which was passed by reference. This is sometimes called "replacement". |
| 180 | |
| 181 | Reference parameters were somewhat overused in early versions of MediaWiki. You |
| 182 | may find that some parameters passed by reference cannot reasonably be modified. |
| 183 | Replacement either has no effect on the caller or would cause unexpected or |
| 184 | inconsistent effects. Handlers should generally only replace a parameter when it |
| 185 | is clear from the documentation that replacement is expected. |
| 186 | |
| 187 | ## How to define a new hook |
| 188 | |
| 189 | * Create a hook interface, typically in a subnamespace called `Hook` relative |
| 190 | to the caller namespace. For example, if the caller is in a namespace called |
| 191 | `MediaWiki\Foo`, the hook interface might be placed in `MediaWiki\Foo\Hook`. |
| 192 | * Add an implementation to the relevant HookRunner class. |
| 193 | |
| 194 | ## Hook deprecation |
| 195 | |
| 196 | Core hooks are deprecated by adding them to an array in the DeprecatedHooks |
| 197 | class. Hooks declared in extensions may be deprecated by listing them in the |
| 198 | `DeprecatedHooks` attribute: |
| 199 | |
| 200 | "DeprecatedHooks": { |
| 201 | "Mash": { |
| 202 | "deprecatedVersion": "2.0", |
| 203 | "component": "FoodProcessor" |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | If the `component` is not specified, it defaults to the name of the extension. |
| 208 | |
| 209 | The hook interface should be marked as deprecated by adding `@deprecated` to the |
| 210 | interface doc comment. The interface doc comment is a better place for |
| 211 | `@deprecated` than the method doc comment, because this causes the interface to |
| 212 | be deprecated for implementation. Deprecating the method only causes calling |
| 213 | to be deprecated, not handling. |
| 214 | |
| 215 | Deprecating a hook in this way activates a migration system called |
| 216 | **call filtering**. Extensions opt in to call filtering of deprecated hooks by |
| 217 | **acknowledging** deprecation. An extension acknowledges deprecation with the |
| 218 | `deprecated` parameter in the `Hooks` attribute: |
| 219 | |
| 220 | "Hooks": { |
| 221 | "Mash": { |
| 222 | "handler": "main", |
| 223 | "deprecated": true |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | If deprecation is acknowledged by the extension: |
| 228 | |
| 229 | * If MediaWiki knows that the hook is deprecated, the handler will not be |
| 230 | called. The call to the handler is filtered. |
| 231 | * If MediaWiki does not have the hook in its list of deprecated hooks, the |
| 232 | handler will be called anyway. |
| 233 | |
| 234 | Deprecation acknowledgement is a way for the extension to say that it has made |
| 235 | some other arrangement for implementing the relevant functionality and does |
| 236 | not need the handler for the deprecated hook to be called. |
| 237 | |
| 238 | ### Call filtering example |
| 239 | |
| 240 | Suppose the hook `Mash` is deprecated in MediaWiki 2.0, and is replaced by a |
| 241 | new one called `Slice`. In our example extension FoodProcessor 1.0, the |
| 242 | `Mash` hook is handled. In FoodProcessor 2.0, both `Mash` and `Slice` have |
| 243 | handlers, but deprecation of `Mash` is acknowledged. Thus: |
| 244 | |
| 245 | * With MediaWiki 2.0 and FoodProcessor 1.0, `onMash` is called but raises a |
| 246 | deprecation warning. |
| 247 | * With MediaWiki 2.0 and FoodProcessor 2.0, `onMash` is filtered, and `onSlice` |
| 248 | is called. |
| 249 | * With MediaWiki 1.0 and FoodProcessor 2.0, `onMash` is called, since it is not |
| 250 | yet deprecated in Core. `onSlice` is not called since it does not yet exist |
| 251 | in Core. |
| 252 | |
| 253 | So the call filtering system provides both forwards and backwards compatibility. |
| 254 | |
| 255 | ### Silent deprecation |
| 256 | |
| 257 | Developers sometimes use two stages of deprecation: "soft" deprecation in which |
| 258 | the deprecated entity is merely discouraged in documentation, and "hard" |
| 259 | deprecation in which a warning is raised. When you soft-deprecate a hook, it is |
| 260 | important to register it as deprecated so that call filtering is activated. |
| 261 | Activating call filtering simplifies the task of migrating extensions to the |
| 262 | new hook. |
| 263 | |
| 264 | To deprecate a hook without raising deprecation warnings, use the "silent" flag: |
| 265 | |
| 266 | "DeprecatedHooks": { |
| 267 | "Mash": { |
| 268 | "deprecatedVersion": "2.0", |
| 269 | "component": "FoodProcessor", |
| 270 | "silent": true |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | As with hard deprecation, `@deprecated` should be added to the interface. |