为 mediawiki 中的各个页面设置默认皮肤
Setting default skin for individual pages in mediawiki
我可以使用 LocalSettings.php 中的 $wgDefaultSkin 为 mediawiki 中的所有页面设置默认外观。但是,我想做的是更改特定页面的外观。
例如,我想将特定个别页面的皮肤设置为 "chick",同时将所有其他页面的默认皮肤设置为 "vector"。
这可能吗?
默认情况下,这是不可能的。 MediaWiki 支持为所有页面设置默认皮肤。此外,任何用户都可以在首选项中更改所有页面的外观。拥有不同皮肤的不同页面听起来让用户很困惑,顺便说一句。
但是,您可以使用 SkinPerPage extension to achieve this in a on-wiki configurable way; or the SkinPerNamespace extension, which does the same, but per namespace (like the name says ;)). Furthermore, with some developing time you can achieve this in a wiki-sysadmin (using the LocalSettings.php settings) configurable way (so your users can't change the skin preference using a parser function). E.g. you could create an extension, which uses the RequestContextCreateSkin
hook,根据给定的标题更改皮肤。例如:
<?php
/**
* RequestContextCreateSkin handler. Used to change the skin, based on the given title.
*
* @param IContextSource $context The context, in which this hook was called.
* @param Skin|null|string $skin The Skin object or the skin name, if a skin was
* created already, null if not.
*/
public static function onRequestContextCreateSkin( $context, &$skin ) {
// get the Config object of your extension to get the configuration we need
$config = ConfigFactory::getDefaultInstance()->makeConfig( 'your-extension-config' );
// that's the Title object of the request, from which this hook was called, mostly the Title
// of the page requested by the user. getPrefixedText() returns the full text of the title, including
// the namespace but without the fragment hash (e.g. Category:TestTitle)
$title = $context->getTitle()->getPrefixedText();
// get the configuration variable $wgFakeExtensionSkinTitleMap, which should be an array map of
// titles to skin names in the following format:
// array(
// 'TestTitle' => 'chick',
// 'TestTitle2' => 'monobook',
// );
$skinTitleMap = $config->get( 'FakeExtensionSkinTitleMap' );
if ( !is_array( $skinTitleMap ) ) {
// if the map isn't an array, throw an exception. You could also just log a debug message or do anything else,
// for this example the exception is good enough
throw new InvalidArgumentException(
'$wgFakeExtensionSkinTitleMap needs to be an array, ' . gettype( $skinTitleMap ) . ' given.' );
}
// check, if the current title is configured in our map, which would indicate, that we use our own skin for it
if ( isset( $skinTitleMap[$title] ) ) {
// set the skin. You could handle the skin object creation here, too, but if you return a string, the caller
// of the RequestContextCreateSkin will handle the creation. Probably it's wise to run Skin::normalizeKey()
// to be sure, that the skin name can be loaded, see the docs for it:
// https://doc.wikimedia.org/mediawiki-core/master/php/classSkin.html#af36919e77cfd51eb35767eb311155077
$skin = $skinTitleMap[$title];
}
}
我希望评论尽可能多地解释代码:)
但是,正如我之前所说:您应该考虑一下这种变化的影响。对于用户打开的不同标题获得不同的页面外观可能真的很混乱。为每个页面设置一致的皮肤可能更明智(MediaWiki 开发人员尚未实现此类功能是有原因的;))。
我可以使用 LocalSettings.php 中的 $wgDefaultSkin 为 mediawiki 中的所有页面设置默认外观。但是,我想做的是更改特定页面的外观。
例如,我想将特定个别页面的皮肤设置为 "chick",同时将所有其他页面的默认皮肤设置为 "vector"。
这可能吗?
默认情况下,这是不可能的。 MediaWiki 支持为所有页面设置默认皮肤。此外,任何用户都可以在首选项中更改所有页面的外观。拥有不同皮肤的不同页面听起来让用户很困惑,顺便说一句。
但是,您可以使用 SkinPerPage extension to achieve this in a on-wiki configurable way; or the SkinPerNamespace extension, which does the same, but per namespace (like the name says ;)). Furthermore, with some developing time you can achieve this in a wiki-sysadmin (using the LocalSettings.php settings) configurable way (so your users can't change the skin preference using a parser function). E.g. you could create an extension, which uses the RequestContextCreateSkin
hook,根据给定的标题更改皮肤。例如:
<?php
/**
* RequestContextCreateSkin handler. Used to change the skin, based on the given title.
*
* @param IContextSource $context The context, in which this hook was called.
* @param Skin|null|string $skin The Skin object or the skin name, if a skin was
* created already, null if not.
*/
public static function onRequestContextCreateSkin( $context, &$skin ) {
// get the Config object of your extension to get the configuration we need
$config = ConfigFactory::getDefaultInstance()->makeConfig( 'your-extension-config' );
// that's the Title object of the request, from which this hook was called, mostly the Title
// of the page requested by the user. getPrefixedText() returns the full text of the title, including
// the namespace but without the fragment hash (e.g. Category:TestTitle)
$title = $context->getTitle()->getPrefixedText();
// get the configuration variable $wgFakeExtensionSkinTitleMap, which should be an array map of
// titles to skin names in the following format:
// array(
// 'TestTitle' => 'chick',
// 'TestTitle2' => 'monobook',
// );
$skinTitleMap = $config->get( 'FakeExtensionSkinTitleMap' );
if ( !is_array( $skinTitleMap ) ) {
// if the map isn't an array, throw an exception. You could also just log a debug message or do anything else,
// for this example the exception is good enough
throw new InvalidArgumentException(
'$wgFakeExtensionSkinTitleMap needs to be an array, ' . gettype( $skinTitleMap ) . ' given.' );
}
// check, if the current title is configured in our map, which would indicate, that we use our own skin for it
if ( isset( $skinTitleMap[$title] ) ) {
// set the skin. You could handle the skin object creation here, too, but if you return a string, the caller
// of the RequestContextCreateSkin will handle the creation. Probably it's wise to run Skin::normalizeKey()
// to be sure, that the skin name can be loaded, see the docs for it:
// https://doc.wikimedia.org/mediawiki-core/master/php/classSkin.html#af36919e77cfd51eb35767eb311155077
$skin = $skinTitleMap[$title];
}
}
我希望评论尽可能多地解释代码:)
但是,正如我之前所说:您应该考虑一下这种变化的影响。对于用户打开的不同标题获得不同的页面外观可能真的很混乱。为每个页面设置一致的皮肤可能更明智(MediaWiki 开发人员尚未实现此类功能是有原因的;))。