Manual:Hooks/pt



Hooks permite que o código personalizado seja executado quando ocorre algum evento (tais como guardar uma página ou um início de sessão) definido. Por exemplo, o seguinte código snippet irá ativar uma chamada para a função  sempre que o hook   é executado, e passar os argumentos da função específica para.

Hooks can be registered by mapping the name of the hook to the callback in the extension's file:

""

MediaWiki provides many hooks like this to extend the functionality of the MediaWiki software. Assigning a function (known as an event handler) to a hook will cause that function to be called at the appropriate point in the main MediaWiki code, to perform whatever additional task(s) the developer thinks would be useful at that point. Each hook can have multiple handlers assigned to it, in which case it will call the functions in the order that they are assigned, with any modifications made by one function passed on to subsequent functions in the chain.

Assign functions to hooks at the end of or in your own extension file at the file scope (not in a  function or the  hook). For extensions, if the hook function's behavior is conditioned on a setting in LocalSettings.php, the hook should be assigned and the function should terminate early if the condition was not met.

You can also create new hooks in your own extension; if you do so, add them to the Extension hook registry.

Contexto
A hook is triggered by a call to the function Hooks::run (described in file, and defined in . The first argument to Hooks::run is the name of the hook, the second is the array of arguments for that hook. It will find the event handlers to run in the array . It calls the PHP function call_user_func_array with arguments being the function to be called and its arguments.

See also the.

In this example from the  function in, doEditContent calls Hooks::run to run the  hook, passing   as argument:

""

The calls many hooks, but  can also call hooks.

Writing an event handler
An event handler is a function you assign to a hook, which will be run whenever the event represented by that hook occurs. It consists of:


 * a function with some optional accompanying data, or
 * an object with a method and some optional accompanying data.

Register the event handler by adding it to the global array for a given event. Hooks can be added from any point in the execution before the hook is called, but are most commonly added in, its included files, or, for extensions, in the file extension.json. All the following are valid ways to define a hook function for the event EventName that is passed two parameters, showing the code that will be executed when EventName happens:

For extensions, the syntax is similar in the file  (corresponding to the first and second case above):

When an event occurs, the function (or object method) that you registered will be called, the event's parameters, along with any optional data you provided at registration. Note that when an object is the hook and you didn't specify a method, the method called is 'onEventName'. For other events this would be 'onArticleSave', 'onUserLogin', etc.

The optional data is useful if you want to use the same function or object for different purposes. For example:

This code would result in ircNotify being run twice when a page is saved: once for 'TimStarling', and once for 'brion'.

Event handlers can return one of three possible values:


 * no return value (or null): the hook handler has operated successfully. (Before MediaWiki 1.23, returning true was required.)
 * "some string": an error occurred; processing should stop and the error should be shown to the user
 * false: the hook handler has done all the work necessary, or replaced normal handling. This will prevent further handlers from being run, and in some cases tells the calling function to skip normal processing.

Returning false makes less sense for events where the action is complete, and will normally be ignored by the caller.

Comportamento de Hook antes do MediaWiki 1.22 vs depois
Extracted from: change 500542: for non-abortable hooks (most hooks) returning true has been redundant since MediaWiki 1.22 (in 2015). This was done to reduce chances of accidental failure because we had experienced several outages and broken features due to silent failures where e.g. one hook callback somewhere accidentally returned a non-bool or false instead of true/void and thus short-circuits the whole system.

(Returning non-true/non-void in a MediaWiki Hook is equivalent to  and   in JavaScript events, it kills other listeners for the same event).

For example, if  hook were to return false in MobileFrontend, it would mean Popups stops because its callback would no longer run. See differences below, assuming the hook.

Antes do MediaWiki 1.22

ou

MediaWiki 1.22+

Documentação
Currently, hooks in MediaWiki core have to be documented both in (in the source code repository) and here on MediaWiki.org. In some cases, one of these steps may not yet have been completed, so if a hook appears undocumented, check both.

To document a hook on-wiki, use MediaWikiHook.

Hooks agrupados por função
Some of these hooks can be grouped into multiple functions.
 * Sections: Article Management - Edit Page - Page Rendering - User Interface - Special Pages - User Management - Logging - Skinning Templates - API - Import/Export - Miscellaneous

Lista alfabética de hooks
For a complete list of hooks, use the, which should be kept more up to date.

Consulte também

 * — documentation of the hooks in MediaWiki core
 * — contém exemplos de hooks
 * — the JavaScript/front-end system of hooks
 * — documentation of the hooks in MediaWiki core
 * — contém exemplos de hooks
 * — the JavaScript/front-end system of hooks
 * — the JavaScript/front-end system of hooks