Manual:Developing extensions/vi

Phần mở rộng MediaWiki

This page is a guide to developing extensions for MediaWiki. Before you start, browse the complete list in: Category:Extensions by category vi[[::Category:Extensions by category| ]] to see if an extension already exists for your use case.

Extension development consists of these parts:

  1. Cài đặt
  2. Implementation
  3. Bản địa hóa
  4. Publication

Thiết lập

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.

Trong khi phát triển, bạn có thể muốn tắt bộ nhớ đệm bằng cách đặt $wgMainCacheType = CACHE_NONE$wgCacheDirectory = false, nếu không, thông báo hệ thống và các thay đổi khác có thể không hiển thị.

Structure

Một phần mở rộng tối thiểu sẽ bao gồm cấu trúc sau:

MyExtension/extension.json
Lưu trữ cài đặt hướng dẫn. Tên tệp phải là extension.json. (Trước MediaWiki 1.25, hướng dẫn thiết lập nằm trong tệp MyExtension/MyExtension.php được đặt tên theo phần mở rộng. Nhiều tiện ích mở rộng vẫn có phần đệm tương thích ngược trong tệp PHP này.)
MyExtension/includes/ (or MyExtension/src/)
Lưu trữ mã PHP thực thi cho tiện ích mở rộng.
MyExtension/resources/ (or MyExtension/modules/)
Lưu trữ các tài nguyên phía-máy-khách(client-side) như JavaScript, CSS và LESS cho tiện ích mở rộng.
MyExtension/i18n/*.json
Lưu trữ bản địa hóa thông tin cho tiện ích mở rộng.
MyExtension/README.md
Good practice is to add a README file with basic info about how to install and configure the extension. Use either plain text or Markdown. For a plain text example, see the Phabricator Diffusion page for the Extension:Page Forms. If markdown is used, add the file extension .md. For example, see the README.md file for Parsoid on Phabricator Diffusion.

Khi bạn phát triển tiện ích mở rộng, hãy thay MyExtension ở trên bằng tên tiện ích mở rộng của bạn. Sử dụng UpperCamelCase tên cho thư mục và (các) tệp PHP của nó; đây là quy ước chung đặt tên tệp.[1]

Registration

Phiên bản 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
}

Nhiều trường là tùy chọn, nhưng bạn vẫn nên điền vào các trường đó. For a more complete example of extension.json, see the BoilerPlate extension.

manifest_version đề cập đến phiên bản của lược đồ mà tệp extension.json được viết dựa trên đó. Các phiên bản có sẵn là 1 và 2. Xem tại đây để biết tài liệu về tính năng này. Trừ khi bạn cần hỗ trợ phiên bản cũ hơn của MediaWiki, hãy chọn phiên bản mới nhất.

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

Giấy phép

MediaWiki is an open-source project and users are encouraged to make any MediaWiki extensions under an Open Source Initiative (OSI) approved license compatible with GPL-2.0-or-later (Wikimedia's standard software license).

We recommend adopting one of the following compatible licenses for your projects in Gerrit:

For extensions that have a compatible license, you can request developer access to the MediaWiki source repositories for extensions. To specify the licence in code and with "license-name" a key should be used to provide its short name, e.g. "GPL-2.0-or-later" or "MIT" adhering to the list of identifiers at spdx.org.

Làm cho phần mở rộng của bạn do người dùng có thể định cấu hình

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

wfLoadExtension( 'MyExtension' );

Nếu bạn muốn tạo ra phần mở rộng của mình do người dùng có thể cấu hình, bạn cần xác định và ghi lại một số tham số cấu hình và thiết lập người dùng của bạn sẽ trông giống như:

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

Nếu bạn muốn người dùng có thể định cấu hình tiện ích mở rộng của mình, thì bạn cần cung cấp một hoặc nhiều biến cấu hình. Bạn nên đặt tên riêng cho các biến đó. Họ cũng nên tuân theo MediaWiki quy ước đặt tên (ví dụ: các biến toàn cục nên bắt đầu bằng $wg).

Ví dụ: nếu tiện ích mở rộng của bạn có tên là "MyExtension", bạn có thể muốn đặt tên cho tất cả các biến cấu hình của mình để bắt đầu bằng $wgMyExtension. Điều quan trọng là không có lõi MediaWiki bắt đầu các biến của nó theo cách này và bạn đã thực hiện một công việc hợp lý là kiểm tra để thấy rằng không có phần mở rộng nào được xuất bản bắt đầu các biến của chúng theo cách này. Người dùng sẽ không hài lòng khi phải chọn giữa tiện ích mở rộng của bạn và một số tiện ích mở rộng khác vì bạn đã chọn tên biến trùng lặp.

Bạn cũng nên bao gồm tài liệu mở rộng về bất kỳ biến cấu hình nào trong ghi chú cài đặt của mình.

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

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

Lưu ý rằng sau khi gọi wfLoadExtension( 'BoilerPlate' ); thì biến toàn cục $wgBoilerPlateEnableFoo không tồn tại. Nếu bạn đặt biến, ví dụ: trong LocalSettings.php thì giá trị mặc định được cung cấp trong extension.json sẽ không được sử dụng.

Để biết thêm chi tiết về cách sử dụng biến toàn cục bên trong tiện ích mở rộng tùy chỉnh, vui lòng tham khảo Configuration for developers.

Chuẩn bị các lớp để tự động tải

Nếu bạn chọn sử dụng các lớp để triển khai tiện ích mở rộng của mình, MediaWiki cung cấp một cơ chế đơn giản hóa để giúp PHP tìm tệp nguồn nơi đặt lớp của bạn. Trong hầu hết các trường hợp, điều này sẽ loại bỏ nhu cầu viết phương thức __autoload($classname) của riêng bạn.

Để sử dụng cơ chế tự động tải của MediaWiki, bạn thêm các mục nhập vào trường AutoloadClasses. Chìa khóa của mỗi mục là tên lớp; giá trị là tệp lưu trữ định nghĩa của lớp. Đối với tiện ích mở rộng một lớp đơn giản, lớp thường được đặt cùng tên với tiện ích mở rộng, vì vậy phần tự động tải của bạn có thể trông như thế này (tiện ích mở rộng có tên MyExtension):

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

Tên tệp có liên quan đến thư mục chứa tệp extension.json.

Đối với các tiện ích mở rộng phức tạp hơn, không gian tên nên được xem xét. Xem Manual:Extension.json/Schema#AutoloadNamespaces để biết chi tiết.

Xử lý các phụ thuộc

Giả sử rằng một tiện ích mở rộng yêu cầu sự hiện diện của một tiện ích mở rộng khác, chẳng hạn vì các chức năng hoặc bảng cơ sở dữ liệu sẽ được sử dụng và cần tránh các thông báo lỗi trong trường hợp không tồn tại.

Ví dụ: phần mở rộng CountingMarker yêu cầu sự hiện diện của phần mở rộng HitCounters cho các chức năng nhất định.

Một cách để chỉ định điều này là sử dụng khóa requires trong 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]

Example: 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' ) {
    ...
	}

Implementation

For an overview of code architecture, structure, and conventions for extensions, see Best practices for extensions.

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

  • Hooks: 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 modules: Define API modules based on the Action API or REST API. These modules can be called by bots or clients.
  • Jobs: Add jobs to MediaWiki's JobQueue to perform process-intensive tasks asynchronously, such as sending notification emails.

Pages

  • Display a special page: Special pages provide dynamically generated content, often based on system state, database queries, and user inputs.
  • 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: Extensions can add annotations associated with a revision or log entry, such as for edits made using the VisualEditor extension.

Authentication

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

Thêm bảng cơ sở dữ liệu

Đảm bảo tiện ích mở rộng không sửa đổi các bảng cơ sở dữ liệu cốt lõi. Thay vào đó, tiện ích mở rộng sẽ tạo các bảng mới có khóa ngoại cho các bảng MW có liên quan.

Cảnh báo Cảnh báo: Nếu tiện ích mở rộng của bạn được sử dụng trên bất kỳ wiki sản xuất nào do WMF lưu trữ, vui lòng làm theo [hướng dẫn thay đổi lược đồ https://wikitech.wikimedia.org/wiki/Schema_changes].

Nếu tiện ích mở rộng của bạn cần thêm các bảng cơ sở dữ liệu riêng, hãy sử dụng móc nối(hook) LoadExtensionSchemaUpdates. Xem trang hướng dẫn để biết thêm thông tin về cách sử dụng.

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 Extension registration.

Bản địa hóa

Trong khi phát triển, bạn có thể muốn tắt cả hai bộ nhớ đệm bằng cách đặt $wgMainCacheType = CACHE_NONE$wgCacheDirectory = false, nếu không các thay đổi thông báo hệ thống của bạn có thể không hiển thị.

Nếu bạn muốn phần mở rộng của mình được sử dụng trên wiki có lượng độc giả đa ngôn ngữ, bạn sẽ cần thêm hỗ trợ bản địa hóa cho phần mở rộng của mình.

Lưu trữ tin nhắn trong <language-key>.json

Lưu trữ các định nghĩa thông báo trong tệp JSON bản địa hóa, một tệp cho mỗi khóa ngôn ngữ mà tiện ích mở rộng của bạn được dịch sang. Các tin nhắn được lưu bằng khóa tin nhắn và chính tin nhắn đó sử dụng định dạng JSON tiêu chuẩn. Mỗi thông điệp id phải là chữ thường và có thể không chứa dấu cách. Mỗi khóa phải bắt đầu bằng tên mở rộng viết thường. Một ví dụ bạn có thể tìm thấy trong [$url phần mở rộng GiaodiệnMobile (MobileFrontend) ]. Đây là một ví dụ về tệp JSON tối thiểu (trong trường hợp này là en.json):

en.json

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

Lưu trữ tài liệu tin nhắn trong qqq.json

Tài liệu về khóa thông báo có thể được lưu trữ trong tệp JSON dành cho ngôn ngữ giả có mã qqq. Một tài liệu của ví dụ trên có thể là:

qqq.json:

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

Tải tệp bản địa hóa

Trong extension.json của bạn, hãy xác định vị trí của các tệp tin nhắn của bạn (ví dụ: trong thư mục i18n/):

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

Sử dụng wfMessage trong PHP

Trong mã triển khai và thiết lập của bạn, hãy thay thế từng cách sử dụng thông báo theo nghĩa đen bằng lệnh gọi wfMessage( $msgID, $param1, $param2, ... ). Trong các lớp triển khai IContextSource (cũng như một số lớp khác, chẳng hạn như các lớp con của Trang đặc biệt), bạn có thể sử dụng $this->msg( $msgID, $param1, $param2, ... ) để thay thế. Ví dụ:

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

Sử dụng mw.message trong JavaScript

Cũng có thể sử dụng các hàm i18n trong JavaScript. Xem tại Manual:Messages API để biết chi tiết.


Xuất bản

Để tự động phân loại và chuẩn hóa tài liệu của tiện ích mở rộng hiện tại của bạn, vui lòng xem Bản mẫu:Phần mở rộng. Để thêm phần mở rộng mới của bạn vào Wiki này:


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

Feedback / Criticism / Code reviews
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 developer account to continue maintaining it.
Future versions by other developers
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
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.

Triển khai và đăng ký

Nếu bạn có ý định triển khai tiện ích mở rộng của mình trên các trang Wikimedia (bao gồm cả Wikipedia), thì cần phải xem xét kỹ lưỡng hơn về mặt hiệu suất và bảo mật. Tham khảo Writing an extension for deployment.

Nếu tiện ích mở rộng của bạn thêm không gian tên, bạn có thể muốn đăng ký không gian tên mặc định của nó; tương tự như vậy, nếu nó thêm các bảng hoặc trường cơ sở dữ liệu, bạn có thể muốn đăng ký chúng tại database field prefixes.

Xin lưu ý rằng việc xem xét và triển khai các tiện ích mở rộng mới trên các trang Wikimedia có thể cực kỳ chậm và trong một số trường hợp phải mất hơn hai năm.[4][5]

Hỗ trợ tài liệu

Bạn nên cung cấp nơi có hỗ trợ tài liệu cho các tính năng do phần mở rộng của bạn cung cấp. 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 là một ví dụ điển hình. Bạn nên cung cấp cho người dùng một liên kết đến tài liệu thông qua chức năng addHelpLink().

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.

Hỗ trợ các phiên bản cốt lõi khác

Có hai quy ước phổ biến để hỗ trợ các phiên bản lõi MediaWiki cũ hơn:

  • Master: nhánh chính của tiện ích mở rộng tương thích với càng nhiều phiên bản lõi cũ càng tốt. Điều này dẫn đến gánh nặng bảo trì (các bản hack tương thích ngược cần được duy trì trong một thời gian dài và các thay đổi đối với tiện ích mở rộng cần được thử nghiệm với một số phiên bản MediaWiki), nhưng các trang web chạy phiên bản MediaWiki cũ được hưởng lợi từ chức năng được thêm gần đây vào sự mở rộng.
  • Release branches: các nhánh phát hành của tiện ích mở rộng tương thích với các nhánh phù hợp của lõi, ví dụ: các trang web sử dụng MediaWiki 1.43 cần sử dụng nhánh REL1_43 của tiện ích mở rộng. (Đối với các tiện ích mở rộng được lưu trữ trên gerrit, các nhánh này được tạo tự động khi các phiên bản MediaWiki mới được phát hành.) Điều này dẫn đến mã sạch hơn và phát triển nhanh hơn nhưng người dùng trên các phiên bản cốt lõi cũ không được hưởng lợi từ các bản sửa lỗi và tính năng mới trừ khi chúng đã nhập ngược theo cách thủ công.

Người bảo trì tiện ích mở rộng nên khai báo với tham số compatibility policy của mẫu {{Phần mở rộng }} mà họ tuân theo quy ước.

Cung cấp hỗ trợ/hợp tác

Các nhà phát triển phần mở rộng nên mở một tài khoản trên {Phabricator của Wikimedia, và yêu cầu một dự án mới cho phần mở rộng. Điều này cung cấp một địa điểm công cộng nơi người dùng có thể gửi các vấn đề và đề xuất, đồng thời bạn có thể cộng tác với người dùng và các nhà phát triển khác để phân loại lỗi và lập kế hoạch cho các tính năng của tiện ích mở rộng của bạn.


Xem thêm

Learn by example

Tham khảo

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