Merge "Introduce Opt-In option on user preferences page" into mpga

This commit is contained in:
jenkins-bot 2016-12-16 00:11:55 +00:00 committed by Gerrit Code Review
commit cacce7b03c
6 changed files with 163 additions and 16 deletions

View file

@ -21,24 +21,52 @@
use MediaWiki\Logger\LoggerFactory;
class PopupsHooks {
static function getPreferences( User $user, array &$prefs ){
const PREVIEWS_ENABLED = 'enabled';
const PREVIEWS_DISABLED = 'disabled';
const PREVIEWS_OPTIN_PREFERENCE_NAME = 'popups-enable';
const PREVIEWS_PREFERENCES_SECTION = 'rendering/reading';
static function getPreferences( User $user, array &$prefs ) {
global $wgExtensionAssetsPath;
if ( self::getConfig()->get( 'PopupsBetaFeature' ) !== true ) {
return;
}
$prefs['popups'] = array(
$prefs['popups'] = [
'label-message' => 'popups-message',
'desc-message' => 'popups-desc',
'screenshot' => array(
'screenshot' => [
'ltr' => "$wgExtensionAssetsPath/Popups/images/popups-ltr.svg",
'rtl' => "$wgExtensionAssetsPath/Popups/images/popups-rtl.svg",
),
],
'info-link' => 'https://www.mediawiki.org/wiki/Beta_Features/Hovercards',
'discussion-link' => 'https://www.mediawiki.org/wiki/Talk:Beta_Features/Hovercards',
'requirements' => array(
'requirements' => [
'javascript' => true,
),
);
],
];
}
/**
* Add Page Previews options to user Preferences page
*
* @param User $user
* @param array $prefs
*/
static function onGetPreferences( User $user, array &$prefs ) {
$module = new \Popups\Module( self::getConfig() );
if ( !$module->showPreviewsOptInOnPreferencesPage() ) {
return;
}
$prefs[self::PREVIEWS_OPTIN_PREFERENCE_NAME] = [
'type' => 'radio',
'label-message' => 'popups-prefs-optin-title',
'options' => [
wfMessage( 'popups-prefs-optin-enabled-label' )->text() => self::PREVIEWS_ENABLED,
wfMessage( 'popups-prefs-optin-disabled-label' )->text() => self::PREVIEWS_DISABLED
],
'section' => self::PREVIEWS_PREFERENCES_SECTION
];
}
/**
@ -52,7 +80,7 @@ class PopupsHooks {
return $config;
}
public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin) {
public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) {
// Enable only if the user has turned it on in Beta Preferences, or BetaFeatures is not installed.
// Will only be loaded if PageImages & TextExtracts extensions are installed.
@ -83,7 +111,7 @@ class PopupsHooks {
}
}
$out->addModules( array( 'ext.popups' ) );
$out->addModules( [ 'ext.popups' ] );
return true;
}
@ -93,7 +121,8 @@ class PopupsHooks {
* @param ResourceLoader $resourceLoader
* @return bool
*/
public static function onResourceLoaderTestModules( array &$testModules, ResourceLoader &$resourceLoader ) {
public static function onResourceLoaderTestModules( array &$testModules,
ResourceLoader &$resourceLoader ) {
$localBasePath = __DIR__;
$scripts = glob( "{$localBasePath}/tests/qunit/ext.popups/{,**/}*.test.js", GLOB_BRACE );
$start = strlen( $localBasePath ) + 1;
@ -129,7 +158,7 @@ class PopupsHooks {
* @param array $vars
*/
public static function onResourceLoaderGetConfigVars( array &$vars ) {
$conf = ConfigFactory::getDefaultInstance()->makeConfig( 'popups' );
$conf = self::getConfig();
$vars['wgPopupsSchemaPopupsSamplingRate'] = $conf->get( 'SchemaPopupsSamplingRate' );
if ( $conf->get( 'PopupsExperiment' ) ) {
@ -147,7 +176,7 @@ class PopupsHooks {
* @param OutputPage $out
*/
public static function onMakeGlobalVariablesScript( array &$vars, OutputPage $out ) {
$config = ConfigFactory::getDefaultInstance()->makeConfig( 'popups' );
$config = self::getConfig();
$user = $out->getUser();
if ( $config->get( 'PopupsExperiment' ) ) {
@ -155,4 +184,21 @@ class PopupsHooks {
class_exists( 'BetaFeatures' ) && BetaFeatures::isFeatureEnabled( $user, 'popups' );
}
}
/**
* Register default preferences for popups
*/
public static function onExtensionRegistration() {
global $wgDefaultUserOptions;
/**
* We use MainConfig because PopupConfig is not available yet. We cannot use
* ExtensionFunctions as it's called too late (see T153280)
*
* @todo Use ConfigFactory() - when T153280 gets fixed switch it to ExtensionFunctions hook
* or when ConfigRegistry gets populated before calling `callback` ExtensionRegistry hook
*/
$config = \MediaWiki\MediaWikiServices::getInstance()->getMainConfig();
$wgDefaultUserOptions[ self::PREVIEWS_OPTIN_PREFERENCE_NAME ] =
$config->get( 'PopupsOptInDefaultState' );
}
}

View file

@ -9,7 +9,8 @@
"license-name": "GPL-2.0+",
"type": "betafeatures",
"AutoloadClasses": {
"PopupsHooks": "Popups.hooks.php"
"PopupsHooks": "Popups.hooks.php",
"Popups\\Module": "includes/Module.php"
},
"ConfigRegistry": {
"popups": "GlobalVarConfig::newInstance"
@ -29,6 +30,9 @@
],
"MakeGlobalVariablesScript": [
"PopupsHooks::onMakeGlobalVariablesScript"
],
"GetPreferences": [
"PopupsHooks::onGetPreferences"
]
},
"MessagesDirs": {
@ -39,12 +43,17 @@
"EventLoggingSchemas": {
"Popups": 16112163
},
"callback": "PopupsHooks::onExtensionRegistration",
"config": {
"@PopupsBetaFeature": "@var bool: Whether the extension should be enabled as an opt-in beta feature. If true, the BetaFeatures extension must be installed. False by default.",
"PopupsBetaFeature": false,
"@SchemaPopupsSamplingRate": "@var number: Sample rate for logging events to Schema:Popups.",
"SchemaPopupsSamplingRate": 0,
"PopupsExperiment": false
"PopupsExperiment": false,
"@PopupsHideOptInOnPreferencesPage": "@var bool: Whether the option to enable/disable Page Previews should be hidden on Preferences page. Please note if PopupsBetaFeature is set to true this option will be always hidden. False by default",
"PopupsHideOptInOnPreferencesPage": true,
"@PopupsOptInDefaultState" : "@var string:[enabled|disabled] Default Page Previews visibility",
"PopupsOptInDefaultState" : "disabled"
},
"ResourceModules": {
"ext.popups.images": {

View file

@ -20,5 +20,9 @@
"popups-settings-enable": "Enable previews",
"popups-send-feedback": "Send Feedback (external link)",
"popups-preview-no-preview": "Looks like there isn't a preview for this topic",
"popups-preview-footer-read": "Read article"
"popups-preview-footer-read": "Read article",
"prefs-reading": "Reading preferences",
"popups-prefs-optin-title": "Page previews\n\n<em>Get quick previews of a topic while reading an article</em>",
"popups-prefs-optin-enabled-label": "Enable previews",
"popups-prefs-optin-disabled-label": "Disable"
}

View file

@ -26,5 +26,9 @@
"popups-settings-enable": "Link on the footer to enable hovercards if its disabled.\n\nSee also:\n* {{msg-mw|Popups-settings-option-off}}",
"popups-send-feedback": "Tooltip for the send feedback icon on the hovercard. Should mention that its an external link.\n{{Identical|Send feedback}}",
"popups-preview-no-preview": "The message shown to the user when a preview can't be generated.",
"popups-preview-footer-read": "The link shown to the user when a preview can't be generated."
"popups-preview-footer-read": "The link shown to the user when a preview can't be generated.",
"prefs-reading": "Title for 'Reading preferences' section on preferences page",
"popups-prefs-optin-title": "Title for Page Previews option\n\n<em>Description for Page previews option</em>",
"popups-prefs-optin-enabled-label": "Label for Page Previews opt in",
"popups-prefs-optin-disabled-label": "Label for Page previews opt out"
}

35
includes/Module.php Normal file
View file

@ -0,0 +1,35 @@
<?php
/**
* Module.php
*/
namespace Popups;
/**
* Popups Module
*
* @package Popups
*/
final class Module {
/**
* @var \Config
*/
private $config;
/**
* Module constructor.
* @param \Config $config
*/
public function __construct( \Config $config ) {
$this->config = $config;
}
/**
* Are Page previews visible on User Preferences Page
*
* return @bool
*/
public function showPreviewsOptInOnPreferencesPage() {
return $this->config->get( 'PopupsBetaFeature' ) === false
&& $this->config->get( 'PopupsHideOptInOnPreferencesPage' ) === false;
}
}

View file

@ -0,0 +1,49 @@
<?php
use Popups\Module;
/**
* Popups module tests
*
* @group Popups
*/
class ModuleTest extends MediaWikiTestCase {
/**
* @covers Popups\Module::showPreviewsOptInOnPreferencesPage
* @dataProvider provideConfigForShowPreviewsInOptIn
*/
public function testShowPreviewsPreferencesWhenBetaIsOn( $config, $enabled ) {
$options = new HashConfig( $config );
$module = new Module( $options );
$this->assertEquals( $enabled, $module->showPreviewsOptInOnPreferencesPage() );
}
/**
* @return array
*/
public function provideConfigForShowPreviewsInOptIn() {
return [
[
"options" => [
"PopupsBetaFeature" => false,
"PopupsHideOptInOnPreferencesPage" => false
],
"enabled" => true
],
[
"options" => [
"PopupsBetaFeature" => true,
"PopupsHideOptInOnPreferencesPage" => false
],
"enabled" => false
],
[
"options" => [
"PopupsBetaFeature" => false,
"PopupsHideOptInOnPreferencesPage" => true
],
"enabled" => false
]
];
}
}