Manual:Developing extensions/ja

MediaWiki 拡張機能

This page is a guide to developing extensions for MediaWiki. 始める前に、complete list in: Category:Extensions by category ja[[::Category:Extensions by category| ]] を参照して、あなたのユースケースに合った拡張機能が既に存在するか確認してください。

Extension development consists of these parts:

  1. セットアップ
  2. 実装
  3. 地域化
  4. 公表

セットアップ

To set up a new extension, start by setting up a local development environment for MediaWiki, and follow the instructions to install and copy the BoilerPlate extension.

開発段階ではシステムメッセージその他の変更が表示されないよう、$wgMainCacheType = CACHE_NONE$wgCacheDirectory = falseを設定してキャッシュを回避することができます。

構造

最小限の拡張機能は、以下の構造で構成されます:

MyExtension/extension.json
セットアップの指示を保持します。 ファイル名は必ず extension.json にします。 (MediaWiki 1.25以前では、セットアップの指示は拡張機能から名前を取った MyExtension/MyExtension.php ファイル内に記述されていました。 依然として拡張機能の多くには、このPHPファイルに下位互換性のあるシム (shim) があります。)
MyExtension/includes/ (or MyExtension/src/)
拡張機能のための PHP 実行コードを格納します。
MyExtension/resources/ (or MyExtension/modules/)
拡張機能に必要となるJavaScript、CSSやLESSのようなクライアント側に必要なリソースを保存します。
MyExtension/i18n/*.json
これは拡張機能の地域化に関わる情報を格納したページです。
MyExtension/README.md
いい実践としては、拡張機能のインストールと設定方法に関する基本情報が記載された README ファイルを追加することが推奨されています。 Use either plain text or Markdown. 例えば、Extension:Page Forms について Phabricator Diffusion ページを参照してください。 Markdown が使用される場合は、ファイル拡張子に .md を追加してください。 例えば、Phabricator Diffusion 上の Parsoid 向けの README.md ファイルを参照してください。

拡張機能の開発時には上記の MyExtension の代わりにご利用の拡張機能名を記入します。 ディレクトリ名と PHP ファイル名はアッパーキャメルケースにします。これは標準的なファイル命名規約に準じています。[1]

登録

MediaWiki バージョン:
1.25

The extension.json file contains the configuration data for the extension. Here is an example of a minimal extension.json:

{
	"name": "MyExtension",
	"author": "John Doe",
	"url": "https://www.mediawiki.org/wiki/Extension:MyExtension",
	"description": "This extension is an example.",
	"version": "1.5",
	"license-name": "GPL-2.0-or-later",
	"type": "validextensiontype",
	"manifest_version": 2
}

項目の多くは設定任意ですが、すべて指定しておく方がいいでしょう。 For a more complete example of extension.json, see the BoilerPlate extension.

manifest_versionextension.json のファイルが書き込まれるスキーマのバージョンを示しています。 この機能に関する文書はこちらを参照してください。 以前のMediaWikiバージョンをサポートする必要がない限り、最新バージョンを選択してください。

Once you have an extension.json file set up for your extension, the extension will appear on your local Special:Version page.

ライセンス

MediaWiki はオープンソース プロジェクトであり、オープンソース イニシアティブ (OSI) 承認済みライセンスの下で任意の MediaWiki extensions GPL-2.0-or-later (ウィキメディアの標準ソフトウェア ライセンス) と互換性のあるものにすることをお勧めします。

Gerrit のプロジェクトには、以下の互換ライセンスのいずれかを採用することをお勧めします:

互換性のあるライセンスを持つ拡張機能の場合、拡張機能の MediaWiki ソース リポジトリへの開発者アクセスを申請できます。 コード内で「license-name」でライセンスを指定するには、短い名前を提供するために、spdx.orgの識別子一覧に準拠するキー (例:「GPL-2.0-or-later」「MIT」) を使用する必要があります。

拡張機能を利用者が構成可能にする

Ideally, users should be able to install your extension by adding only one line to LocalSettings.php:

wfLoadExtension( 'MyExtension' );

ユーザーが拡張機能の設定を変更できるようにするには設定パラメータの設定と説明文書作成が必要で、ユーザ設定はたとえば下記の例のようになります。

wfLoadExtension( 'MyExtension' );
$wgMyExtensionConfigThis = 1;
$wgMyExtensionConfigThat = false;

利用者が拡張機能を構成できるようにするには、1 つ以上の構成変数を用意する必要があります。 これらの変数には、固有の名前を付けるといいでしょう。 また、MediaWiki の命名規約に従わなければなりません (例: グローバル変数は $wg で始まる)。

例えば、拡張機能の名前が MyExtension の場合、すべての設定変数を $wgMyExtension で始めるようにすることを検討するかもしれません。 重要なのは、MediaWikiのコアがそのような変数名を使用していないことを確認することであり、公開されている拡張機能の中でもそのような変数名を使用していないかを確認することです。 重複する変数名を選択したために、あなたの拡張機能と他の拡張機能のどちらかを選択しなければならなくなったとしたら、利用者は快く思わないでしょう。

また、インストールの注記には、構成変数の詳細を記載することをお勧めします。

Here is an example of how to set up a configuration variable in extension.json:

{
	"config": {
		"BoilerPlateEnableFoo": {
			"value": true,
			"description": "Enables the foo functionality"
		}
	}
}

wfLoadExtension( 'BoilerPlate' ); を呼び出した後、グローバル変数 $wgBoilerPlateEnableFoo は存在しないことに注意してください。 例えば LocalSettings.php で変数を設定した場合、extension.json に記載された既定値は使用されません。

カスタム拡張機能内でグローバル変数を使用する方法の詳細については、Configuration for developers を参照してください。

クラスのオートローディングの準備

拡張機能を実装するためにクラスを使用することを選択した場合、MediaWiki は PHP がクラスの定義が格納されているソース ファイルを見つけるのを支援する簡素化された仕組みを提供します。 多くの場合、__autoload($classname)の手順を自作しなくて済みます。

MediaWikiのオートローディング機構を使用するには、AutoloadClasses フィールドにエントリを追加します。 各エントリのキーはクラス名で、値はクラスの定義が格納されているファイルです。 シンプルな 1 クラスの拡張機能では、クラス名は通常拡張機能の名前と同じにされるため、オートローディングの節は以下のようになる場合があります (拡張機能名は MyExtension):

{
	"AutoloadClasses": {
		"MyExtension": "includes/MyExtension.php"
	}
}

ファイル名は extension.json ファイルが格納されているディレクトリに対して相対的です。

より複雑な拡張機能では、名前空間を考慮する必要があります。 詳細は Manual:Extension.json/Schema#AutoloadNamespaces を参照してください。

Handling dependencies

拡張機能が別の拡張機能の存在を必要とすると仮定してください。例えば、機能やデータベース テーブルを使用するため、存在しない場合のエラー メッセージを避けるためです。

For example the extension CountingMarker requires the presence of the extension HitCounters for certain functions.

One way to specify this would be by using the requires key in extension.json.

Another option is using ExtensionRegistry (available since MW 1.25):

	if ( ExtensionRegistry::getInstance()->isLoaded( 'HitCounters', '>=1.1' ) {
		/* do some extra stuff, if extension HitCounters is present in version 1.1 and above */
	}

Currently (as of February 2024, MediaWiki 1.41.0) the name of the extension-to-be-checked needs to exactly match the name in their extension.json.[2][3]

例: if you want to check the load status of extension OpenIDConnect, you have to use it with a space

	if ( ExtensionRegistry::getInstance()->isLoaded( 'OpenID Connect' ) {
    ...
	}

実装

For an overview of code architecture, structure, and conventions for extensions, see 拡張機能の最善手法.

Extension points

MediaWiki core provides several ways for extensions to change the behavior, functionality, and appearance of a wiki. Most extensions use more than one of these extension points. For a complete list of extension points supported in extension.json, see the schema reference.

General

  • フック: Inject custom code at key points in MediaWiki core code, such as when a user logs in or saves a page. Extensions can also define new hooks.
  • Domain events: React to changes to the wiki's state, such as when a page is edited. Extensions can also define their own events. 1.44
  • API モジュール: Define API modules based on the Action API or REST API. These modules can be called by bots or clients.
  • ジョブ: Add jobs to MediaWiki's JobQueue to perform process-intensive tasks asynchronously, such as sending notification emails.

ページ

  • Display a special page: 特別ページは、システムの状態、データベースのクエリ、利用者からの入力などに基づいて動的に生成されるコンテンツを提供します。
  • Perform a page action: The action URL parameter generates a custom page based on the current page, usually to provide information (such as page history) or to perform an action (such as edit the page). In addition to the default actions provided by MediaWiki core, extensions can define a new page action.
  • Add a tracking category: Help users find pages with similar characteristics by automatically adding pages to custom categories.

Content

  • Extend wiki markup: Extensions can add custom functionality to MediaWiki's wikitext markup using template syntax ({{...}}) or tags (<example />). These customizations are used to generate content and to interact with MediaWiki during page rendering.
  • Support a content model: By default, wiki pages can be stored using a few standard content models, such as wikitext and JSON. Extensions can provide support for new content models by adding a content handler.
  • Support a media type: Extensions can add to the default set of supported media file types by adding a media handler.

Moderation tools

  • Log a user or system action: On wiki, actions are tracked for transparency and collaboration. To support this feature, extensions can add custom entries and functionality to Special:Log.
  • Add a recent-changes flag: Extensions can add custom flags to the following special pages to help moderators track page changes: Special:RecentChanges, Special:Watchlist, Special:RecentChangesLinked
  • Add a revision tag: 拡張機能は、例えばビジュアルエディター拡張機能を使って行われた編集に対してなど、版や記録項目に関連付けられた注釈を追加できます。

Authentication

  • Add a provider: Extensions can add support for new login mechanisms and session management methods.

データベース テーブルの追加

Make sure the extension doesn't modify the core database tables. Instead, extension should create new tables with foreign keys to the relevant MW tables.

警告 警告: If your extension is used on any production WMF-hosted wiki please follow the Schema change guide.

If your extension needs to add its own database tables, use the LoadExtensionSchemaUpdates hook. 使用法のより詳細な情報はマニュアルページを参照してください。

Registering attributes for other extensions

Attributes allow extensions to register something, such as a module, with another extension. For example, extensions can use attributes to register a plugin module with the VisualEditor extension. For more information, see 拡張機能の登録.

地域化

While developing, you may want to disable both cache by setting $wgMainCacheType = CACHE_NONE and $wgCacheDirectory = false, otherwise your system message changes may not show up.

多言語の読者を持つウィキで拡張機能を使用してもらいたい場合は、その拡張機能に地域化対応を追加する必要があります。

Store messages in <language-key>.json

Store message definitions in a localisation JSON file, one for each language key your extension is translated in. The messages are saved with a message key and the message itself using standard JSON format. Each message id should be lowercase and may not contain spaces. Each key should begin with the lowercased extension name. An example you can find in the MobileFrontend extension. Here is an example of a minimal JSON file (in this case en.json):

en.json

{
	"myextension-desc": "Adds the MyExtension great functionality.",
	"myextension-action-message": "This is a test message"
}

Store message documentation in qqq.json

The documentation for message keys can be stored in the JSON file for the pseudo language with code qqq. A documentation of the example above can be:

qqq.json:

{
	"myextension-desc": "The description of MyExtension used in Extension credits.",
	"myextension-action-message": "Adds 'message' after 'action' triggered by user."
}

地域化ファイルの読み込み

In your extension.json, define the location of your messages files (e.g. in directory i18n/):

{
	"MessagesDirs": {
		"MyExtension": [
			"i18n"
		]
	}
}

PHP での wfMessage の使用

In your setup and implementation code, replace each literal use of the message with a call to wfMessage( $msgID, $param1, $param2, ... ). In classes that implement IContextSource (as well as some others such as subclasses of SpecialPage), you can use $this->msg( $msgID, $param1, $param2, ... ) instead. 例:

wfMessage( 'myextension-addition', '1', '2', '3' )->parse()

JavaScript での mw.message の使用

It's possible to use i18n functions in JavaScript too. 詳細は Manual:メッセージAPI を参照してください。


Publishing

To autocategorize and standardize the documentation of your existing extension, please see Template:Extension. To add your new extension to this Wiki:


A developer sharing their code in the MediaWiki code repository should expect:

フィードバック / 批評 / コード レビュー
Review and comments by other developers on things like framework use, security, efficiency and usability.
Developer tweaking
Other developers modifying your submission to improve or clean-up your code to meet new framework classes and methods, coding conventions and translations.
Improved access for wiki sysadmins
If you do decide to put your code on the wiki, another developer may decide to move it to the MediaWiki code repository for easier maintenance. You may then create a 開発者アカウント to continue maintaining it.
他の開発者による将来のバージョン
New branches of your code being created automatically as new versions of MediaWiki are released. You should backport to these branches if you want to support older versions.
Incorporation of your code into other extensions with duplicate or similar purposes — incorporating the best features from each extension.
帰属
Credit for your work being preserved in future versions — including any merged extensions.
Similarly, you should credit the developers of any extensions whose code you borrow from — especially when performing a merger.

Any developer who is uncomfortable with any of these actions occurring should not host in the code repository. You are still encouraged to create a summary page for your extension on the wiki to let people know about the extension, and where to download it.

Deploying and registering

拡張機能をウィキメディアのサイト群 (ウィキペディアを含む) に導入する予定がある場合は、パフォーマンスとセキュリティの面で追加の精査が必要になります。 展開のための拡張機能の作成 を参照してください。

If your extension adds namespaces, you may wish to register its default namespaces; likewise, if it adds database tables or fields, you may want to register those at データベース フィールドの接頭辞.

Please be aware that review and deployment of new extensions on Wikimedia sites can be extremely slow, and in some cases has taken more than two years.[4]

ヘルプの説明文書

You should provide public domain help documentation for features provided by your extension. The convention is for extensions to have their user-focused help pages under a pseudo-namespace of Help:Extension:<ExtensionName>, with whatever subpages are required (the top level page will be automatically linked from the extension infobox if it exists). Help:CirrusSearch is a example of good documentation. You should give users a link to the documentation via the addHelpLink() function.

Releasing updates

There are a number of common approaches to releasing updates to extensions. These are generally defined according to the compatibility policy of the extension (master, rel, or ltsrel):

  • master Releases may be tagged with version numbers on the master branch, and documentation provided on the extension's homepage describing which extension versions are compatible with which core versions. Release branches will still be created automatically, and you may wish to delete these if they are not intended to be used.
  • rel および ltsrel Release by backporting changes to the REL1_* branches (either all changes, or only critical ones). Version numbers are generally not needed unless the extension is a dependency of another (the version number can then be provided in the other extension's configuration to ensure that incompatible combinations aren't installed). Many extensions will stay at the same version number for years.

異なるコアバージョンのサポート

There are two widespread conventions for supporting older versions of MediaWiki core:

  • Master: the master branch of the extension is compatible with as many old versions of core as possible. This results in a maintenance burden (backwards-compatibility hacks need to be kept around for a long time, and changes to the extension need to be tested with several versions of MediaWiki), but sites running old MediaWiki versions benefit from functionality recently added to the extension.
  • Release branches: release branches of the extension are compatible with matching branches of core, e.g. sites using MediaWiki 1.43 need to use the REL1_43 branch of the extension. (For extensions hosted on Gerrit, these branches are automatically created when new versions of MediaWiki are released.) This results in cleaner code and faster development but users on old core versions do not benefit from bugfixes and new features unless they are backported manually.

Extension maintainers should declare with the compatibility policy parameter of the {{Extension }} template which convention they follow.

Providing support / collaboration

Extension developers should open an account on Wikimedia's Phabricator, and request a new project for the extension. これは、利用者が問題や提案を提出できる公開の場を提供し、利用者や他の開発者と協力してバグの優先順位付けや拡張機能の機能計画を行えます。


関連項目

Learn by example

参考資料

Category:Extension creation/ja#Developing%20extensions/ja
Category:Extension creation/ja