Manual:Hooks/ja



フックは特定のイベント（ページ保存やユーザログインなど）が発生したとき、カスタムコードの実行を認めます. たとえば下記のコードスニペットにより、フックの が走る場面で必ず関数 を呼び出し、PageContentSaveCompleteに特有の関数引数を渡します.

""

MediaWikiはこのようなフックを用意し、MediaWiki ソフトウェアの機能を拡張しています. 特定の関数（ユーザー処理つまりイベントハンドラー）をフックに定義すると、メインのMediaWikiコードにおいて適切なタイミングでその関数を呼び出し、その時点で開発者が有効と判断した追加のタスクをいくつでも実行します. フックに定義できるハンドラーは複数で、定義順に呼び出した定義が行った変更を、一連の後続の関数に渡していきます.

Assign functions to hooks at the end of LocalSettings.php or in your own extension file at the file scope (not in a $wgExtensionFunctions function or the ParserFirstCallInit 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.

背景
A hook is triggered by a call to the function Hooks::run (described in file, and defined in GlobalFunctions.php}. 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 $wgHooks. 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 WikiPage.php, doEditContent calls Hooks::run to run the PageContentSaveComplete hook, passing   as argument:

""

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

イベントハンドラーを書く
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 LocalSettings.php, 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:

拡張機能の場合、構文は ファイルと同様（前述の1番目と2番目の事例に対応）:

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.

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 by the caller.

説明文書
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.

利用できるフック
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.

関数ごとに分類したフック
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

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