Define sub-features in arrays and use where possible

* HookUtils:FEATURES lists all features
* CommentFormatter::USE_WITH_FEATURES are all features
  which require the comment formatter

Change-Id: Idbbe8bdd910b9c7b23c7fee76af7bb7ee13c2759
This commit is contained in:
Ed Sanders 2021-02-17 17:16:17 +00:00
parent fa484e0c4a
commit bb8d2b691e
4 changed files with 67 additions and 47 deletions

View file

@ -12,6 +12,11 @@ use Wikimedia\Parsoid\Utils\DOMUtils;
use Wikimedia\Parsoid\Wt2Html\XMLSerializer;
class CommentFormatter {
// List of features which, when enabled, cause the comment formatter to run
public const USE_WITH_FEATURES = [
'replytool',
];
protected const REPLY_LINKS_COMMENT = '<!-- DiscussionTools addReplyLinks called -->';
/**

View file

@ -19,11 +19,22 @@ use Title;
use User;
class HookUtils {
/**
* @var string[] List of all sub-features. Will be used to generate:
* - Feature override global: $wgDiscussionTools_FEATURE
* - Body class: dt-FEATURE-enabled
* - User option: discussiontools-FEATURE
*/
public const FEATURES = [
'replytool',
'newtopictool',
];
/**
* Check if a DiscussionTools feature is available to this user
*
* @param User $user
* @param string|null $feature Feature to check for: 'replytool' or 'newtopictool'.
* @param string|null $feature Feature to check for (one of static::FEATURES)
* Null will check for any DT feature.
* @return bool
*/
@ -37,21 +48,20 @@ class HookUtils {
$optionsLookup = $services->getUserOptionsLookup();
// Feature-specific override
if ( $feature ) {
// Feature-specific override
if ( $dtConfig->get( 'DiscussionTools_' . $feature ) !== 'default' ) {
// Feature setting can be 'available' or 'unavailable', overriding any BetaFeatures settings
return $dtConfig->get( 'DiscussionTools_' . $feature ) === 'available';
}
} else {
// Non-feature-specific override
if (
$dtConfig->get( 'DiscussionTools_replytool' ) === 'available' ||
$dtConfig->get( 'DiscussionTools_newtopictool' ) === 'available'
) {
// Non-feature-specific override, check for any feature
foreach ( static::FEATURES as $feat ) {
if ( $dtConfig->get( 'DiscussionTools_' . $feat ) === 'available' ) {
return true;
}
}
}
// No feature-specific override found.
@ -65,7 +75,7 @@ class HookUtils {
}
// Otherwise, being in the "test" group for this feature means
// it's effectively beta-enabled.
return self::determineUserABTestBucket( $user, $feature ) === 'test';
return static::determineUserABTestBucket( $user, $feature ) === 'test';
}
// Assume that if BetaFeature is turned off, or user has it enabled, that
@ -78,22 +88,28 @@ class HookUtils {
* Check if a DiscussionTools feature is enabled by this user
*
* @param User $user
* @param string|null $feature Feature to check for: 'replytool' or 'newtopictool'.
* @param string|null $feature Feature to check for (one of static::FEATURES)
* Null will check for any DT feature.
* @return bool
*/
public static function isFeatureEnabledForUser( User $user, ?string $feature = null ) : bool {
if ( !static::isFeatureAvailableToUser( $user, $feature ) ) {
return false;
}
$services = MediaWikiServices::getInstance();
$optionsLookup = $services->getUserOptionsLookup();
return static::isFeatureAvailableToUser( $user, $feature ) && (
if ( $feature ) {
// Check for a specific feature
( $feature && $optionsLookup->getOption( $user, 'discussiontools-' . $feature ) ) ||
return $optionsLookup->getOption( $user, 'discussiontools-' . $feature );
} else {
// Check for any feature
( !$feature && (
$optionsLookup->getOption( $user, 'discussiontools-newtopictool' ) ||
$optionsLookup->getOption( $user, 'discussiontools-replytool' )
) )
);
foreach ( static::FEATURES as $feat ) {
if ( $optionsLookup->getOption( $user, 'discussiontools-' . $feat ) ) {
return true;
}
}
return false;
}
}
/**
@ -103,7 +119,7 @@ class HookUtils {
* in it; if they're eligible and not enrolled, it will enroll them.
*
* @param User $user
* @param string|null $feature Feature to check for: 'replytool' or 'newtopictool'.
* @param string|null $feature Feature to check for (one of static::FEATURES)
* Null will check for any DT feature.
* @return string 'test' if in the test group, 'control' if in the control group, or '' if they've
* never been in the test
@ -166,7 +182,7 @@ class HookUtils {
* Check if the tool is available on a given page
*
* @param OutputPage $output
* @param string|null $feature Feature to check for: 'replytool' or 'newtopictool'.
* @param string|null $feature Feature to check for (one of static::FEATURES)
* Null will check for any DT feature.
* @return bool
*/

View file

@ -41,13 +41,11 @@ class PageHooks implements
'ext.discussionTools.init'
] );
$output->addJsConfigVars(
'wgDiscussionToolsFeaturesEnabled',
[
'replytool' => HookUtils::isFeatureEnabledForOutput( $output, 'replytool' ),
'newtopictool' => HookUtils::isFeatureEnabledForOutput( $output, 'newtopictool' ),
]
);
$enabledVars = [];
foreach ( HookUtils::FEATURES as $feature ) {
$enabledVars[$feature] = HookUtils::isFeatureEnabledForOutput( $output, $feature );
}
$output->addJsConfigVars( 'wgDiscussionToolsFeaturesEnabled', $enabledVars );
$services = MediaWikiServices::getInstance();
$optionsLookup = $services->getUserOptionsLookup();
@ -93,13 +91,20 @@ class PageHooks implements
// non-cacheable reasons i.e. query string or cookie
// The addReplyLinks method is responsible for ensuring that
// reply links aren't added twice.
$replyToolEnabled = HookUtils::isFeatureEnabledForOutput( $output, 'replytool' );
if ( $replyToolEnabled ) {
foreach ( CommentFormatter::USE_WITH_FEATURES as $feature ) {
if ( HookUtils::isFeatureEnabledForOutput( $output, $feature ) ) {
CommentFormatter::addReplyLinks( $text, $lang );
// Add CSS classes to selectively enable HTML enhancements
$output->addBodyClasses( 'dt-replytool-enabled' );
break;
}
}
foreach ( HookUtils::FEATURES as $feature ) {
// Add a CSS class for each enabled feature
if ( HookUtils::isFeatureEnabledForOutput( $output, $feature ) ) {
$output->addBodyClasses( "dt-$feature-enabled" );
}
}
return true;
}
}

View file

@ -24,21 +24,15 @@ class PreferenceHooks implements
* @param array &$preferences
*/
public function onGetPreferences( $user, &$preferences ) {
if ( HookUtils::isFeatureAvailableToUser( $user, 'replytool' ) ) {
$preferences['discussiontools-replytool'] = [
foreach ( HookUtils::FEATURES as $feature ) {
if ( HookUtils::isFeatureAvailableToUser( $user, $feature ) ) {
$preferences["discussiontools-$feature"] = [
'type' => 'toggle',
'label-message' => 'discussiontools-preference-replytool',
'help-message' => 'discussiontools-preference-replytool-help',
'label-message' => "discussiontools-preference-$feature",
'help-message' => "discussiontools-preference-$feature-help",
'section' => 'editing/discussion',
];
}
if ( HookUtils::isFeatureAvailableToUser( $user, 'newtopictool' ) ) {
$preferences['discussiontools-newtopictool'] = [
'type' => 'toggle',
'label-message' => 'discussiontools-preference-newtopictool',
'help-message' => 'discussiontools-preference-newtopictool-help',
'section' => 'editing/discussion',
];
}
$preferences['discussiontools-showadvanced'] = [