Manual:Hooks/de



Hooks erlauben benutzerdefinierten Code auszuführen, wenn eins von vielen definierten Ereignissen (wie das Speichern eines Artikels oder wenn sich ein Benutzer anmeldet) auftreten werden. Zum Beispiel: Der folgende Code-Schnipsel, wenn zur LocalSettings.php hinzugefügt, würde die Funktion  aufrufen, wann immer der   Hook läuft (function arguments):

""

MediaWiki bietet viele Hooks, die verwendet werden können, um die Funktionalität der MediaWiki-Software zu erweitern. Zuweisen einer Funktion (auch bekannt als ein Event-Handler) zu einem Hook führt dazu, dass diese Funktion am entsprechenden Punkt im Haupt-MediaWiki-Code aufgerufen wird, um was auch immer für zusätzliche Aufgabe(n) der Entwickler denkt durchzuführen, wäre an dieser Stelle sinnvoll. Jeder Hook kann mehreren Handlern zugewiesen werden, wobei sie die Funktionen in der Reihenfolge aufrufen, der sie zugeordnet sind, mit jeglichen Änderungen gemacht durch eine Funktion, werden an nachfolgende Funktionen in der Kette weitergegeben.

Hooks sollten am Ende der LocalSettings.php oder in Ihrer eigenen Erweiterungs-Datei auf dem Datei-Umfang (nicht in einer $wgExtensionFunctions oder dem ParserFirstCallInit Hook) zugeordnet werden. Der einfachste Weg, um eine Funktion zu einem Hook zuzuweisen ist:

""

die ein Element an das Array $wgHooks hinzufügen. Sie können auch neue Hooks in Ihrer eigenen Erweiterung erstellen. Hooks die auf diese Weise erstellt werden, sollten der Extension Hook Registry hinzugefügt werden.

Hintergrund
Each hook is represented in the code by a call of function wfRunHooks, which is defined in file Hooks.php. The first argument of wfRunHooks is the name of the hook, the second is the array of arguments of the hook. Function wfRunHooks finds the tasks to be done from the array $wgHooks. It calls the PHP function call_user_func_array with arguments being the function to be called and its arguments.

See also the hook specification in Gerrit.

In this example from the  function in Article.php, wfRunHooks is used to call the ArticleSaveComplete hook using several arguments:

""

The core calls many hooks, but extensions can also call hooks.

Schreiben eines Ereignishandler
An event handler is a function that is assigned 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.

Event handlers are registered by adding them to the global $wgHooks 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 LocalSettings.php or its included files. All the following are valid ways to define hooks, with the code that will be executed when 'EventName' happens:

When an event occurs, the function (or object method) will be called with the optional data provided as well as event-specific parameters. Note that when an object is the hook, and there's no specified method, the default method called is 'onEventName'. For different events this would be different: 'onArticleSave', 'onUserLogin', etc.

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

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

Event handlers can return one of three possible values:


 * true: the hook has operated successfully
 * "some string": an error occurred; processing should stop and the error should be shown to the user
 * false: the hook has successfully done the work necessary and the calling function should skip

The last result would be for cases where the hook function replaces the main functionality. For example, if you wanted to authenticate users to a custom system (LDAP, another PHP program, whatever), you could do:

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

Verfügbare hooks
This page contains a list of hooks that are made available by the MediaWiki software, and is known to be complete to version 1.8.2. There is a lot of detail missing for the more recent hooks in particular, as their purpose/usage has not yet been documented by the developers. If you have any further information on any of these then please add it in the appropriate place.

In the tables, the first column gives the MediaWiki version that the hook was introduced in; use the link in the second column to find out more information about the hook and how to use it.

Hooks gruppiert nach Funktion
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 - Miscellaneous

Hooks gruppiert nach Version
To see hooks grouped by version go to the table above and click the arrow symbol in the version table header.

Siehe auch

 * Manual:$wgHooks
 * Category:Hook extensions/de
 * Manual:Tag Extensions/de
 * Manual:Parser functions/de
 * hooks.txt
 * Some Examples