Manual:Hooks/ja



フックは特定のイベント（ページ保存や利用者ログインなど）が発生したとき、カスタム コードの実行を認めます. たとえば下記のコード スニペットにより、フックの が走る場面で必ず関数 を呼び出し、$PageContentSaveCompleteに特有の関数引数を渡します. For example, the following code snippet will trigger a call to the function  whenever the   hook runs, passing it function arguments specific to.

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

""

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

フックに機能を割り当てるには の末尾またはファイル スコープの拡張機能 ( 関数や  フックではなく) で割り当てます. 拡張機能の場合、LocalSettings.php の設定がフックの機能の挙動に条件を付けるなら、フックに機能を割り当て、条件が適合しないときは早めに関数を停止する必要があります.

ご利用の拡張機能で新しくフックを作成することもできます. 作成したフックは拡張機能フックのレジストリに追加します.

背景
A hook is triggered by a call to HookContainer::run, usually via a method in HookRunner. HookContainer will find the hook handlers to run and call them with the parameters given to HookContainer::run. Hook handlers are registered via.

も参照してください.

この使用事例は の   関数で、HookRunner::onArticlePurge は doPurge に呼び出されると  フックを実行、引数として   を当てます:

""

多数のフックを呼び出すのは ですが、 からもフックを呼び出せます.

イベントハンドラーを書く
イベントハンドラーはフックに設定する関数で、 フックが代表するイベントが発生するたびに実行されます. 構成要素は次のとおりです.


 * 関数と、設定で有効にするデータを添えたもの.
 * method変数と、設定で有効にするデータを添えたオブジェクト.

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:

拡張機能の場合、構文は ファイルと同様（前述の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:

(Before MediaWiki 1.23, returning true was required.) This will prevent further handlers from being run, and in some cases tells the calling function to skip normal processing.
 * no return value (or null): the hook handler has operated successfully.
 * "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.

操作が完了した場合はfalseを返しても無意味であり、通常、callerに無視されます.

Handling hooks in MediaWiki 1.35 and later
MediaWiki 1.35 introduces a system for handling hooks based on individual hook interfaces. This system allows for dependency injection, provides machine-readable parameter names and types, and integrates hook documentation with code editors.

For extensions, methods of registering and handling hooks in MediaWiki 1.34 and earlier are not being deprecated and will continue to work as expected. Extension authors who want to pilot the new system are welcome to do so. To learn more, see the hook specification in MediaWiki Core and the announcement on wikitech-l.

 Changes to hook names 

Prior to MediaWiki 1.35, several hooks included colons in their names. With the implementation of hook interfaces, colons in hook names have been replaced with underscores. For example, the interface for ApiFeedContributions::feedItem is. This does not impact extensions using the hook system in MediaWiki 1.34 and earlier.

 Registering hooks using HookHandlers 

To register a hook in an extension's extension.json file, the new system uses HookHandlers to specify how the handler object is created. Instead of mapping a hook directly to a function, a hook maps to a HookHandlers object which specifies the  and has the option to inject.

For example, to register the BeforePageDisplay hook:

 Handling hooks using interfaces 

To work with registration using HookHandlers, extensions should define an event handler class that implements the hook interface. Hook interfaces are named with the hook name with "Hook" appended.

Hook behavior before MediaWiki 1.22 vs after
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.

MediaWiki 1.22 以前

または

MediaWiki 1.22+

説明文書
MediaWiki コアのフックは現状ではここMediaWiki.orgのほか フック インターフェイス (ソースコード リポジトリ内) の2箇所に説明文書を置くことになっています. 場合によってはどちらかで作業が未完了なことがあるため、フックの説明文書の確認は両方の場所を調べてください.

Each hook provided by MediaWiki Core is defined in a hook interface. Typically, hook interfaces are located in a "Hook" sub-namespace inside the caller namespace. 例:. You can find a list of hook interfaces in the generated MediaWiki PHP documentation.

オンウィキでフックを開設するにはMediaWikiHookを使います.

 Hook interface doc template 

In hook interfaces, doc comments specify the status, purpose, parameters, and behavior of the hook.

機能ごとに分類したフック
ここに一覧したフックの中には、機能ごとにいくつかのグループに分類できます.
 * 小分類: 記事の管理 - ページ編集 - ページのレンダリング - UI - 特別ページ - 利用者管理 - ログ - 外装 / テンプレート - API - インポートとエクスポート - その他

Alphabetical list of hooks
フックの完全な一覧は、を参照してください. こちらはさらに最新に近い状態に維持されています.

関連項目

 * — specification of the hooks system
 * List of hook interfaces in MediaWiki Core
 * — contains examples of hooks
 * — the JavaScript/front-end system of hooks
 * — specification of the hooks system
 * List of hook interfaces in MediaWiki Core
 * — contains examples of hooks
 * — the JavaScript/front-end system of hooks