扩展:测试功能

From mediawiki.org
Jump to navigation Jump to search
This page is a translated version of the page Extension:BetaFeatures and the translation is 70% complete.
Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎Türkçe • ‎español • ‎français • ‎italiano • ‎polski • ‎português do Brasil • ‎sicilianu • ‎русский • ‎עברית • ‎العربية • ‎中文 • ‎日本語 • ‎한국어
MediaWiki擴充功能手冊
OOjs UI icon advanced.svg
BetaFeatures
发布状态: 稳定版
BetaFeatures 2013-09-06.png
实现 媒体, 钩子 , 資料庫
描述 允许其他扩展作为测试功能在用户设置中启用
作者 Mark Holmquist (MarkTraceur讨论)
最新版本 0.1 (Continous updates)
兼容性方针 发行分支
MediaWiki 1.25+
PHP 5.4+
数据庫更改
betafeatures_user_counts
许可协议 GNU通用公眾授權條款2.0或更新版本
下载 Template:WikimediaDownload/gerritonly
例子 Special:Preferences#mw-prefsection-betafeatures
Special
  • $wgBetaFeatures
  • $wgBetaFeaturesWhitelist
翻譯BetaFeatures擴充功能如在translatewiki.net可用
检查使用和版本矩阵。
问题 尚未完成的工作 · 回報錯誤

测试功能扩展允许其他MediaWiki扩展向wiki用户提供测试反馈。它通过用户设置界面和一些特殊页面记载测试中的一些数据。

安装

  • 下载文件,并将其放置在您extensions/文件夹中的BetaFeatures目录内。
  • 将下列代码放置在您的LocalSettings.php的底部:
    wfLoadExtension( 'BetaFeatures' );
    
  • 运行更新脚本,它将自动创建此扩展必须依赖的数据库表。
  • 根据需要配置。
  • Yes 完成 – 在您的wiki上导航至Special:Version,以验证扩展已成功安装。

对您的扩展使用新钩子

使用此扩展程序支持您的Beta功能非常简单。在您的extension.json 文件中注册一个类型为“GetBetaFeaturePreferences ”的钩子——语法几乎与GetPreferences钩子相同,只需稍作更改即可支持本例中需要的首选项类型。

extension.json中:

    "Hooks": {
        "GetBetaFeaturePreferences": "MediaWiki\\Extension\\MyExtension\\Hooks::onGetBetaFeaturePreferences"
    },

MyExtension/includes/Hooks.php中:

namespace MediaWiki\Extension\MyExtension;
class Hooks {
    public static function onGetBetaFeaturePreferences( User $user, array &$betaPrefs ) {
        $extensionAssetsPath = MediaWikiServices::getInstance()
			->getMainConfig()
			->get( 'ExtensionAssetsPath' );
        $prefs['myextension-awesome-feature'] = [
            // The first two are message keys
            'label-message' => 'myextension-awesome-feature-message',
            'desc-message' => 'myextension-awesome-feature-description',
            // Paths to images that represents the feature.
            // The image is usually different for ltr and rtl languages.
            // Images for specific languages can also specified using the language code.
            'screenshot' => array(
                'ru' => "$extensionAssetsPath/MyExtension/images/screenshot-ru.png",
                'ltr' => "$extensionAssetsPath/MyExtension/images/screenshot-ltr.png",
                'rtl' => "$extensionAssetsPath/MyExtension/images/screenshot-rtl.png",
            ),
            // Link to information on the feature - use subpages on mw.org, maybe?
            'info-link' => 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:MyExtension/MyFeature',
            // Link to discussion about the feature - talk pages might work
            'discussion-link' => 'https://www.mediawiki.org/wiki/Special:MyLanguage/Help_talk:Extension:MyExtension/MyFeature',
        ];
    }
}
目前需要“label-message”、“desc-message”、“info-link”和“discussion-link”。请确保您使用了全部!

Then, you can use the convenience function provided by BetaFeatures to check whether the feature is enabled.

// SpecialMyExtension.php
class SpecialMyExtension extends SpecialPage {

    public function execute() {
        if ( BetaFeatures::isFeatureEnabled( $this->getUser(), 'my-awesome-feature' ) ) {
            // Implement the feature!
        }
    }
}

You can also use a normal preference check, but don't check against truthy or falsy values - use the values from the HTMLFeatureField class.

// SpecialMyExtension.php
class SpecialMyExtension extends SpecialPage {

    public function execute() {
        if ( $this->getUser()->getOption( 'my-awesome-feature' ) === HTMLFeatureField::OPTION_ENABLED ) {
            // Implement the feature!
        }
    }
}

Because the BetaFeatures class should be present everywhere, you could use the convenience function in any hook, special page, or anything else you wanted. Just be aware of potential performance or caching issues you may introduce with those changes.

If you want to also use your extension without BetaFeatures, you should also check for its existence, e.g.:

if (
    !ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' )
    || \BetaFeatures::isFeatureEnabled( $user, 'my-awesome-feature' )
) {
    // Implement the feature!
}

配置

The $wgBetaFeaturesWhitelist config variable can be used to limit which beta features are shown in preferences. 默认情况下,它为空,并显示所有测试功能。

如果使用它,那么为了在参数设置中显示测试版功能,需要在白名单中列出。 此配置变量接受字符串数组。 Each string should be the name of a beta feature as specified in the preference definition passed to onGetBetaFeaturePreferences(). 例如,在上面给出的代码中,beta功能的名称是myextension-awesome-feature,因此您需要将该字符串添加到您的维基配置的$wgBetaFeaturesWhitelist数组中:

$wgBetaFeaturesWhitelist = [
        'myextension-awesome-feature' 
];

高级用法

想尝试更酷的玩法?!

自动参与组

在此示例中,我们定义了一个“自动启用所有新测试功能”的选项 - 如果用户选中此选项,并且特定组中出现新功能,则用户将自动开启这些功能。

// MyExtensionHooks.php
class MyExtensionHooks {

    static function getPreferences( $user, &$prefs ) {
        global $wgExtensionAssetsPath;

        $prefs['my-awesome-feature-auto-enroll'] = array(
            // The first two are message keys
            'label-message' => 'beta-feature-autoenroll-message',
            'desc-message' => 'beta-feature-autoenroll-description',
            // Link to information on the feature - use subpages on mw.org, maybe?
            'info-link' => 'https://wwww.mediawiki.org/wiki/Special:MyLanguage/MyFeature',
            // Link to discussion about the feature - talk pages might work
            'discussion-link' => 'https://www.mediawiki.org/wiki/Talk:MyFeature',
            // Enable auto-enroll for this group
            'auto-enrollment' => 'my-awesome-feature-group',
        );

        $prefs['my-awesome-feature'] = array(
            // The first two are message keys
            'label-message' => 'beta-feature-message',
            'desc-message' => 'beta-feature-description',
            // Paths to images that represents the feature.
            // The image is usually different for ltr and rtl languages.
            // Images for specific languages can also specified using the language code.
            'screenshot' => array(
                'ru' => "$wgExtensionAssetsPath/MyExtension/images/screenshot-ru.png",
                'ltr' => "$wgExtensionAssetsPath/MyExtension/images/screenshot-ltr.png",
                'rtl' => "$wgExtensionAssetsPath/MyExtension/images/screenshot-rtl.png",
            ),
            // Link to information on the feature - use subpages on mw.org, maybe?
            'info-link' => 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:MyExtension/SomeFeature',
            // Link to discussion about the feature - talk pages might work
            'discussion-link' => 'https://www.mediawiki.org/wiki/Extension_talk:MyExtension/SomeFeature',
            // Add feature to this group
            'group' => 'my-awesome-feature-group',
        );
    }
}

附属管理

Next up, we can actually define dependency management per-feature. To do this we first register the name of a hook that we want to use for this with the hook "GetBetaFeatureDependencyHooks", then we register a hook of that type that checks some dependency, and returns true if it's met or false if not.

// MyExtension.php
$wgAutoloadClasses['MyExtensionHooks'] = __DIR__ . '/MyExtensionHooks.php';
Hooks::register( 'GetBetaFeaturePreferences', 'MyExtensionHooks::getPreferences' );
Hooks::register( 'GetBetaFeatureDependencyHooks', 'MyExtensionHooks::getDependencyCallbacks' );
Hooks::register( 'CheckDependenciesForMyExtensionFeature', 'MyExtensionHooks::checkDependencies' );
// MyExtensionHooks.php
class MyExtensionHooks {

    static function getPreferences( $user, &$prefs ) {
        global $wgExtensionAssetsPath;

        $prefs['my-awesome-feature'] = array(
            // The first two are message keys
            'label-message' => 'beta-feature-message',
            'desc-message' => 'beta-feature-description',
            // Paths to images that represents the feature.
            // The image is usually different for ltr and rtl languages.
            // Images for specific languages can also specified using the language code.
            'screenshot' => array(
                'ru' => "$wgExtensionAssetsPath/MyExtension/images/screenshot-ru.png",
                'ltr' => "$wgExtensionAssetsPath/MyExtension/images/screenshot-ltr.png",
                'rtl' => "$wgExtensionAssetsPath/MyExtension/images/screenshot-rtl.png",
            ),
            // Link to information on the feature - use subpages on mw.org, maybe?
            'info-link' => 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:MyExtension/SomeFeature',
            // Link to discussion about the feature - talk pages might work
            'discussion-link' => 'https://www.mediawiki.org/wiki/Extension_talk:MyExtension/SomeFeature',
            // Mark as dependent on something
            'dependent' => true,
        );
    }

    static function getDependencyCallbacks( &$depHooks ) {
        $depHooks['my-awesome-feature'] = 'CheckDependenciesForMyExtensionFeature';
        return true;
    }

    static function checkDependencies() {
        $dependenciesMet = false;
        // Do some fancy checking and return the result
        return $dependenciesMet;
    }
}

You can abuse use this feature to do per-wiki disabling of features, if they're marked as dependent. But that sounds really hacky. You probably shouldn't. I can hear you thinking about it right now, just stop it.

数据库填充

There's a database table (betafeatures_user_counts) defined, and used, by BetaFeatures. But you might get confused by it if you try to use it locally.

We use the job queue to run updates for this table, when the cache expires (30 minutes TTL). If your wiki is configured to run jobs on each request, this will make about one request every 30 minutes reeeeeeally slow, but the rest will be relatively fast. If you configure your wiki to run jobs via cron, things will work much better.

参见