本頁使用了標題或全文手工轉換

手册:如何制作MediaWiki皮肤

From mediawiki.org
Jump to navigation Jump to search
This page is a translated version of the page Manual:How to make a MediaWiki skin and the translation is 100% complete.

制作皮肤是了解并熟悉MediaWiki内部机制,并开始为维基媒体运动贡献自己力量的绝佳方式!

如果你熟悉CSS、JavaScript、JSON等前端技术,你就可以制作MediaWiki皮肤!完全不会PHP也可以制作,只是高端操作通常都需要您会一些PHP。

虽然并不需要精通以下知识,但如果您熟悉LESS CSS,这将帮助您制作皮肤。 本教程假定您有一份已安装且可正常工作的MediaWiki副本,并且正在运行当前最新开发版本,若尚不满足,建议先去准备一下。

If you having an existing skin using the PHP BaseTemplate, this guide is not for you. Please see Manual:How to make a MediaWiki skin/Migrating SkinTemplate based skins to SkinMustache instead.

Preparation

Skin development will be a lot easier with a familiarity with Mustache templates.

At minimum, you should know be familiar with the following Mustache basics described in the example below:

{{ str }} <!-- renders escaped string -->
{{{ html-str }}} <!-- renders RAW HTML -->

<!-- accesses data inside an associative array of the form { 'key' => 'hello' } -->
{{#array-data}} 
{{ key }} <!-- renders hello -->
{{/array-data}}

<!-- for boolean values -->
{{#is-talk-page}}
<!-- conditional rendering if is-talk-page is true -->
{{/is-talk-page}}

A skin is responsible for the rendering of user-visible content - the HTML inside the BODY tag. It is restricted from editing anything inside the HEAD. Code will be automatically generated for the skin, so that the skin can focus on presentation. If you need to modify the HEAD this is considered outside the scope of the skin, please use Hooks, extensions or configuration to do that see the FAQ for best practices on how to do that.

入门

要制作你的第一款皮肤,我们推荐以下两个选项。


选项 1:复刻示例皮肤

示例皮肤 https://github.com/wikimedia/mediawiki-skins-Example 提供了一款皮肤需要实现的基本骨架。 克隆仓库到您的皮肤文件夹,确保文件夹名称为“Example”,并添加以下代码到您的LocalSettings.php中:

wfLoadSkin( 'Example' );

若一切顺利,您的皮肤应该会在您wiki的Special:Preferences上显示。

选项 2:使用皮肤实验室

The skins lab tool allows you to setup a skin with basic CSS and templates. 在调整完毕后,你可以点击“下载为ZIP(download as ZIP)”,网站会自动为您编译所需的皮肤模板。 希望生成的皮肤仓库对你来说还算简单。 在你下载完ZIP后,将其放入你的MediaWiki皮肤文件夹,并用以下代码更新您的LocalSettings.php:

wfLoadSkin( 'FolderName' );

若一切顺利,您的皮肤应该会在您wiki的Special:Preferences上显示。

广而告之!

和更多的人制作你的皮肤会更有趣也更容易!在你的皮肤有了一个可以用的原型之后,可以考虑发布到GitHub或Gerrit 。 Once the code is publicly available, you should create a skin page (make sure you change the title!) to let people know you are open for collaboration!

创建一个wiki页面也有很多其他好处。 You'll be able to handle bug reports in Phabricator or GitHub issues and receive patches from other volunteers in the MediaWiki community. 也会有人可以帮助你设置好翻译。

Mustache

在1.35版本中我们为皮肤加入了Mustache的支持。 We found using Mustache to be very helpful in the development of the Skin:Minerva and Skin:Vector skins as it allows you to separate data from presentation. Mustache partial(插值模板/子模块)可用作皮肤的可重用部分。 To use a partial Partial.mustache in MediaWiki, simply add them to the folder you are working in and reference them using {{>Partial}} in the master template `skin.mustache`.

发送至Mustache模板的数据相对比较灵活。 If something is missing, you can use PHP to add data by extending the SkinMustache::getTemplateData function.

The SkinJson skin can be added to a MediaWiki development instance, to inspect the data available to skins. Note that arrays are prefixed "array-", booleans are prefixed with "is-" and objects are prefixed "data-" to help you conceptualize the data you are working with.

The data available, and in which MediaWiki versions it is available is documented on Manual:SkinMustache.php.

让皮肤文本可被翻译(国际化)

In skin.json under ValidSkinNames, you can use the `messages` option to define translateable message keys. These keys should correspond to entries inside the i18n/en.json folder. Once registered for the skin, these will be available in your template with a prefix "msg-".

"example": {
	"class": "SkinMustache",
	"args": [ {
			"name": "example",
			"messages": [
				"sitetitle",
				"search",
				"tagline",
				"navigation-heading"
			]
	} ]
}

For example in the example below, the message "sitetitle" can be rendered in a Mustache template using

<p>{{msg-sitetitle}}</p>

See Localisation for more information on why this is important.

Rendering template partials

Template partials can be used to render different parts of the skin and to avoid the problem with having a large unmaintainable skin.mustache file. In the following example, the skin renders the contents of the templates in the 3 files with the filenames Logo.mustache, Content.mustache and Footer.mustache. These files must exist in the same folder as the skin.mustache file or a subfolder of the directory containing skin.mustache.

{{>subfolder/Logo}}
{{>Content}}
{{>Footer}}

Template partials are a great way to break up your skin to make it more readable. No template partials are predefined and available by default, however you can look at existing skins using SkinMustache for inspiration.

Read more about Mustache template partials.

Logo.mustache

Following code block is being used to show the site logo in the Example skin and you will also see the same if you create the skin from the SkinLabs.

<!-- Logo.mustache -->
<div id="p-logo" class="mw-portlet" role="banner">
    <a href="{{link-mainpage}}">
    {{#data-logos}}
        {{#icon}}<img src="{{.}}" width="40" height="40">{{/icon}}
        {{#wordmark}}<img src="{{src}}" width="{{width}}" height="{{height}}">{{/wordmark}}
        {{^wordmark}}<h1>{{msg-sitetitle}}</h1>{{/wordmark}}
    {{/data-logos}}
    </a>
</div>

From the code block mentioned above, the following line is responsible to show the logo `icon`:

{{#icon}}<img src="{{.}}" width="40" height="40">{{/icon}}

This line assumes that, there is a key icon in the array $wgLogos. So in your LocalSettings.php file, if there is a line similar as $wgLogos = [ 'icon' => "$wgResourceBasePath/resources/assets/wiki.png" ];, then the logo/ icon image will be displayed. The default MediaWiki LocalSettings.php exports a 1x key in the $wgLogos array.

So to show the logo you need to update the LocalSettings.php and add a key icon.

If you want to change the logo do not just replace the default logo with a new one in the resources/assets/wiki.png path. Because it will be changed to default, when you update the MediaWiki. The recommended way to change the logo is to add a new logo image file and add that path to the LocalSettings.php.

脚本与样式

默认

A skin at minimum requires a single style ResourceLoader module. It will look a bit like this:

       "ResourceModules": {
                "skins.example": {
                        "class": "ResourceLoaderSkinModule",
                        "features": [ "normalize" ],
                        "styles": [ "resources/skins.example.less" ]
                }
        },

The features key allows you to use useful boiler plate defaults for a variety of things including i18n and normalize which are documented in the MediaWiki core php documentation. Features can be a list of keys (opt-in policy) or in 1.36 an object (opt-out policy). If you are not sure, please omit the features key to use the recommended defaults.

CSS / LESS

The skin.json is a manifest of all the assets your skin uses. Under the `ResourceModules` key you should find the style module for your skin listed under `skins.example`. Here, under the "styles" key you can add LESS or CSS files. They will be loaded in the order they are listed. With LESS you can use @import statements in the same folder. More information about what's possible can be found in ResourceLoader/Developing_with_ResourceLoader.

When using images you should be able to use relative URIs to access the image assets.

Responsive skins / adding a meta viewport

If you are building a responsive skin, make sure to use the responsive skin option when declaring your skin in skin.json.

{
    "ValidSkinNames": {
        "my-responsive-skin": {
            "class": "SkinMustache",
            "args": [
                {
                   "name": "my-responsive-skin",
                   "responsive": true
                }
            ]
        }
    }
}

Images

You can extend Manual:$wgLogos with any data you choose to. This will allow site admins to configure images as they choose, but you must always conditionally render them.

In cases where images must be hardcoded for some reason., and cannot use a CSS background-image, or wgLogos for any reason you will need to extend the data sent to the template

JavaScript

JavaScript code in skins, runs in an environment where you can rely on the `mw` and `jQuery` objects having been loaded. We recommend using ResourceLoader/Package_files which will allow you to require file assets.

For information on the available API and libraries see core JS documentation.

高级操作

More advanced information will provided on an as requested basis. Please ask a question on the talk page to accelerate the addition of documentation!

i18n

Messages defined in i18n/en.json can be passed directly to your Mustache template by inclusion in skin.json. Note, that you can use any messages defined inside MediaWiki core.

skin.json i18n/en.json skin.mustache
{
    "ValidSkinNames": {
        "mymustacheskin": {
            "class": "SkinMustache",
            "args": [
                {
                   "name": "mymustacheskin",
                   "messages": [
                        "createaccount"
                    ]
                }
            ]
        }
    }
}
{
        "@metadata": {
                "authors": []
        },
        "skinname-mymustacheskin": "My Mustache Skin",
        "mymustacheskin-hello": "Hello"
}
<div>
    {{msg-mymustacheskin-hello}} <!-- prints hello in the current language -->
</div>

Extending data

The data available is documented on Manual:SkinMustache.php.

If you need to add additional data for rendering inside your skin's template that cannot be served by messages (as in the i18n section) e.g. raw HTML or complex data structures you must use a dedicated PHP class and extend the SkinMustache::getTemplateData method.

<?php

class MySkin extends SkinMustache {
    
    /**
     * Extends the getTemplateData function to add a template key 'html-myskin-hello-world'
     * which can be rendered in skin.mustache using {{{html-myskin-hello-world}}}
     */
    public function getTemplateData() {
        $data = parent::getTemplateData();
        $data['html-myskin-hello-world'] = '<strong>Hello world!</strong>';
        return $data;
    }
}

使用ResourceLoaderSkinModule类载入默认样式

All skins should define a single style module with the class ResourceLoaderSkinModule. The module defines various default styles to take care of MediaWiki internals. If you want, you can disable these features and provide your own styles. Define features as an empty object to tie yourself into the recommended MediaWiki defaults. A list of support features is provided in our docs.

Example ResourceLoaderSkinModule that disables the logo feature but enables several others:

{
    "skins.vector.styles": {
                        "class": "ResourceLoaderSkinModule",
                        "features": {
                                "normalize": true,
                                "elements": true,
                                "content": true,
                                "logo": false,
                                "interface": true,
                                "legacy": true
                        },
                        "targets": [
                                "desktop",
                                "mobile"
                        ],
                        "styles": [ "resources/skins.vector.styles/skin.less" ]
                }
    }
}

整合其他擴充功能

Extensions should integrate with you, not the other way round! Try to forget about extensions when writing your skin. User:Jdlrobson/Skins for extension developers is provided for extension developers to ensure they get the best compatibility. The starter templates in Manual:How_to_make_a_MediaWiki_skin#Getting_started will render all possible UI elements. If you omit certain pieces of data you may break support with extensions.

For certain extensions you may want to tweak the styles of the default UI elements, notably Extension:Echo. To do this you will need to read Manual:$wgResourceModuleSkinStyles.

更改菜单内容

The composition of menus can be changed by using hooks. For example in Vector, the SkinTemplateNavigation hook is used to relocate the watch star icon. When doing this, remember to check the skin being operated on, to avoid side effects in other skins.

I want to change elements in the head of the HTML page

Skin developers should not concern themselves with modifying anything in the HEAD of a document. Modifications of the HEAD are better served via extensions and configuration inside LocalSettings.php. For any questions please use the

The following links may be helpful:

I am writing an extension that needs to style itself differently depending on the skin

Extensions can make use of skin styles to ship skin-specific styles using the skinStyles key. See Manual:$wgResourceModuleSkinStyles.

Building skins for 1.35

In 1.35 support for building skins was not as straightforward as in 1.36. If you wish to build a skin for 1.35, using template data provided in 1.36, you will need to extend the SkinMustache PHP class. A polyfill for the Example skin is provided.


你的意见很重要

If something is not easy, we'd like to make it easier, so your feedback is important. If you run into problems, then please file a bug report in the mediawiki core skin architecture project in Phabricator, and we'll try and find an elegant solution.

Please feel free to ask a question on the talk page. There is no such thing as a stupid question.