Jump to content

Extension:测试功能

From mediawiki.org
This page is a translated version of the page Extension:BetaFeatures and the translation is 89% complete.
MediaWiki扩展手册
BetaFeatures
发行状态: 稳定版
实现 媒体, 函数钩 , 数据库
描述 允许其他扩展作为测试功能在用户设置中启用
作者 Mark Holmquist (MarkTraceur留言)
最新版本 0.1 (Continous updates)
兼容性政策 快照跟随MediaWiki发布。 master分支不向后兼容。
MediaWiki 1.25+
PHP 5.4+
数据库更改
betafeatures_user_counts
许可协议 GNU通用公眾授權條款2.0或更新版本
下載 Template:WikimediaDownload/gerritonly
示例 Special:Preferences#mw-prefsection-betafeatures
Special
  • $wgBetaFeatures
  • $wgBetaFeaturesAllowList
季度下載量 42 (Ranked 91st)
正在使用的公开wiki数 1,031 (Ranked 249th)
前往translatewiki.net翻譯BetaFeatures扩展
問題 开启的任务 · 报告错误

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

安裝

  • 下载文件,并将解压后的BetaFeatures文件夹移动到extensions/目录中。
    开发者和代码贡献人员应从Git安装扩展,输入:cd extensions/
    git clone https://gerrit.wikimedia.org/r/mediawiki/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' );
        $betaPrefs['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'。请确保您使用了全部!

然后,您可以使用测试功能提供的便捷功能来检查该功能是否已启用。

// SpecialMyExtension.php
class SpecialMyExtension extends SpecialPage {

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

您也可以使用常规首选项检查,但不要检查真值或假值 - 使用HTMLFeatureField类中的值。

// SpecialMyExtension.php
class SpecialMyExtension extends SpecialPage {

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

由于BetaFeatures类应该无处不在,因此您可以在任何钩子、特殊页面或您想要的任何其他内容中使用便利函数。请注意这些更改可能会引入的潜在性能或缓存问题。

如果您还想不通过测试功能使用您的扩展,您还应该检查它是否存在,例如:

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

配置

$wgBetaFeaturesWhitelist配置变量可用于限制首选项中显示的测试功能。 默认情况下,它为空,并显示所有测试功能。

如果使用它,那么为了在参数设置中显示测试版功能,需要在白名单中列出。 此配置变量接受字符串数组。 每个字符串应是传递给onGetBetaFeaturePreferences()的首选项定义中指定的测试功能的名称。 例如,在上面给出的代码中,测试功能的名称是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',
        );
    }
}

附属管理

接下来,我们实际上可以定义每个功能的依赖关系管理。为此,我们首先用钩子“GetBetaFeatureDependencyHooks ”注册我们想要用于此目的的钩子的名称,然后我们注册一个该类型的钩子,该钩子检查某些依赖关系,如果满足,则返回,否则返回

// 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;
    }
}

如果功能被标记为依赖功能,您可以滥用此功能来禁用每个wiki的功能。但这听起来真的很骇人听闻。您可能不应该这样做。我能听见您现在在想它,停手吧。

数据库填充

有一个数据库表(betafeatures_user_counts)由测试功能定义和使用。但是,如果您尝试在本地使用它,您可能会对它感到困惑。

当缓存过期(30分钟TTL)时,我们使用作业队列来运行此表的更新。如果您的wiki配置为对每个请求运行作业,这将使每30分钟大约一个请求的速度非常慢,但其余的请求会相对较快。如果您将您的wiki配置为通过cron运行作业将会好很多。

Troubleshootings

Usage counts

The usage counts might not reflect exactly the number of users having some beta features activated at this precise moment. These should be interpreted as approximate numbers.

This number is computed by a job, which could be delayed or long-running. In the past, there was also a 30-minutes cache on these figures.

参见