2019-10-10 13:25:11 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* DiscussionTools extension hooks
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
* @license MIT
|
|
|
|
*/
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2021-01-29 18:36:04 +00:00
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Hooks;
|
2020-05-14 22:44:49 +00:00
|
|
|
|
|
|
|
use Action;
|
2020-12-14 18:52:47 +00:00
|
|
|
use ExtensionRegistry;
|
2020-02-20 20:39:35 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-05-14 22:44:49 +00:00
|
|
|
use OutputPage;
|
2020-12-16 16:08:56 +00:00
|
|
|
use PageProps;
|
2020-05-14 22:44:49 +00:00
|
|
|
use RequestContext;
|
2020-12-16 16:08:56 +00:00
|
|
|
use Title;
|
2020-05-15 20:18:06 +00:00
|
|
|
use User;
|
2020-02-20 20:39:35 +00:00
|
|
|
|
2021-01-29 18:36:04 +00:00
|
|
|
class HookUtils {
|
2021-04-08 12:30:28 +00:00
|
|
|
public const REPLYTOOL = 'replytool';
|
|
|
|
public const NEWTOPICTOOL = 'newtopictool';
|
|
|
|
public const SOURCEMODETOOLBAR = 'sourcemodetoolbar';
|
|
|
|
public const TOPICSUBSCRIPTION = 'topicsubscription';
|
2021-02-17 17:16:17 +00:00
|
|
|
/**
|
|
|
|
* @var string[] List of all sub-features. Will be used to generate:
|
|
|
|
* - Feature override global: $wgDiscussionTools_FEATURE
|
2021-03-13 14:39:39 +00:00
|
|
|
* - Body class: ext-discussiontools-FEATURE-enabled
|
2021-02-17 17:16:17 +00:00
|
|
|
* - User option: discussiontools-FEATURE
|
|
|
|
*/
|
|
|
|
public const FEATURES = [
|
2021-04-08 12:30:28 +00:00
|
|
|
self::REPLYTOOL,
|
|
|
|
self::NEWTOPICTOOL,
|
|
|
|
self::SOURCEMODETOOLBAR,
|
|
|
|
self::TOPICSUBSCRIPTION,
|
2021-02-17 17:16:17 +00:00
|
|
|
];
|
|
|
|
|
2019-10-10 19:11:07 +00:00
|
|
|
/**
|
2020-12-16 16:07:32 +00:00
|
|
|
* Check if a DiscussionTools feature is available to this user
|
2019-10-10 19:11:07 +00:00
|
|
|
*
|
2020-12-16 16:07:32 +00:00
|
|
|
* @param User $user
|
2021-02-17 17:16:17 +00:00
|
|
|
* @param string|null $feature Feature to check for (one of static::FEATURES)
|
2020-12-16 16:07:32 +00:00
|
|
|
* Null will check for any DT feature.
|
2020-09-10 13:43:13 +00:00
|
|
|
* @return bool
|
2019-10-10 19:11:07 +00:00
|
|
|
*/
|
2021-01-29 17:09:52 +00:00
|
|
|
public static function isFeatureAvailableToUser( User $user, ?string $feature = null ) : bool {
|
2020-12-16 16:07:32 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$dtConfig = $services->getConfigFactory()->makeConfig( 'discussiontools' );
|
2020-09-10 13:43:13 +00:00
|
|
|
|
2020-12-16 16:07:32 +00:00
|
|
|
if ( !$dtConfig->get( 'DiscussionToolsEnable' ) ) {
|
2020-12-16 16:08:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-08 12:30:28 +00:00
|
|
|
if ( $feature === self::TOPICSUBSCRIPTION && $user->isAnon() ) {
|
2021-02-17 22:34:02 +00:00
|
|
|
// Users must be logged in to use topic subscription
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-16 16:07:32 +00:00
|
|
|
$optionsLookup = $services->getUserOptionsLookup();
|
|
|
|
|
2021-01-13 19:30:59 +00:00
|
|
|
if ( $feature ) {
|
2021-02-17 17:16:17 +00:00
|
|
|
// Feature-specific override
|
2021-01-13 19:30:59 +00:00
|
|
|
if ( $dtConfig->get( 'DiscussionTools_' . $feature ) !== 'default' ) {
|
|
|
|
// Feature setting can be 'available' or 'unavailable', overriding any BetaFeatures settings
|
|
|
|
return $dtConfig->get( 'DiscussionTools_' . $feature ) === 'available';
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-17 17:16:17 +00:00
|
|
|
// Non-feature-specific override, check for any feature
|
|
|
|
foreach ( static::FEATURES as $feat ) {
|
|
|
|
if ( $dtConfig->get( 'DiscussionTools_' . $feat ) === 'available' ) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-01-13 19:30:59 +00:00
|
|
|
}
|
2020-12-14 18:52:47 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 16:07:32 +00:00
|
|
|
// No feature-specific override found.
|
|
|
|
|
2021-01-13 06:38:11 +00:00
|
|
|
if ( $dtConfig->get( 'DiscussionToolsBeta' ) ) {
|
|
|
|
$betaenabled = $optionsLookup->getOption( $user, 'discussiontools-betaenable', -1 );
|
|
|
|
if ( $betaenabled !== -1 ) {
|
|
|
|
// betaenable doesn't have a default value, so we can check
|
|
|
|
// for it being unset like this. If the user has explicitly
|
2021-03-09 21:20:03 +00:00
|
|
|
// enabled or disabled it, we should immediately return that.
|
2021-01-13 06:38:11 +00:00
|
|
|
return $betaenabled;
|
|
|
|
}
|
|
|
|
// Otherwise, being in the "test" group for this feature means
|
|
|
|
// it's effectively beta-enabled.
|
2021-02-17 17:16:17 +00:00
|
|
|
return static::determineUserABTestBucket( $user, $feature ) === 'test';
|
2021-01-13 06:38:11 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 16:07:32 +00:00
|
|
|
// Assume that if BetaFeature is turned off, or user has it enabled, that
|
|
|
|
// some features are available.
|
|
|
|
// If this isn't the case, then DiscussionToolsEnable should have been set to false.
|
2021-01-13 06:38:11 +00:00
|
|
|
return true;
|
2020-12-16 16:07:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a DiscussionTools feature is enabled by this user
|
|
|
|
*
|
|
|
|
* @param User $user
|
2021-02-17 17:16:17 +00:00
|
|
|
* @param string|null $feature Feature to check for (one of static::FEATURES)
|
2020-12-16 16:07:32 +00:00
|
|
|
* Null will check for any DT feature.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-01-29 17:09:52 +00:00
|
|
|
public static function isFeatureEnabledForUser( User $user, ?string $feature = null ) : bool {
|
2021-02-17 17:16:17 +00:00
|
|
|
if ( !static::isFeatureAvailableToUser( $user, $feature ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-16 16:07:32 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$optionsLookup = $services->getUserOptionsLookup();
|
2021-02-17 17:16:17 +00:00
|
|
|
if ( $feature ) {
|
2020-12-16 16:07:32 +00:00
|
|
|
// Check for a specific feature
|
2021-02-17 17:16:17 +00:00
|
|
|
return $optionsLookup->getOption( $user, 'discussiontools-' . $feature );
|
|
|
|
} else {
|
2020-12-16 16:07:32 +00:00
|
|
|
// Check for any feature
|
2021-02-17 17:16:17 +00:00
|
|
|
foreach ( static::FEATURES as $feat ) {
|
|
|
|
if ( $optionsLookup->getOption( $user, 'discussiontools-' . $feat ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-16 16:08:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 06:38:11 +00:00
|
|
|
/**
|
|
|
|
* Work out the A/B test bucket for the current user
|
|
|
|
*
|
2021-03-09 21:20:03 +00:00
|
|
|
* Checks whether the user has been enrolled in the last A/B test, if any was enabled.
|
|
|
|
*
|
|
|
|
* If the A/B test is enabled, and the user is eligible and not enrolled, it will enroll them.
|
2021-01-13 06:38:11 +00:00
|
|
|
*
|
|
|
|
* @param User $user
|
2021-02-17 17:16:17 +00:00
|
|
|
* @param string|null $feature Feature to check for (one of static::FEATURES)
|
2021-01-13 06:38:11 +00:00
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
private static function determineUserABTestBucket( $user, $feature = null ) : string {
|
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$optionsManager = $services->getUserOptionsManager();
|
|
|
|
$dtConfig = $services->getConfigFactory()->makeConfig( 'discussiontools' );
|
|
|
|
|
|
|
|
$abtest = $dtConfig->get( 'DiscussionToolsABTest' );
|
2021-03-09 21:20:03 +00:00
|
|
|
$abstate = $optionsManager->getOption( $user, 'discussiontools-abtest' );
|
|
|
|
|
2021-01-13 06:38:11 +00:00
|
|
|
if (
|
|
|
|
!$user->isAnon() &&
|
2021-01-21 21:46:05 +00:00
|
|
|
( $abtest == 'all' || ( !$feature && $abtest ) || ( $feature && $abtest == $feature ) )
|
2021-01-13 06:38:11 +00:00
|
|
|
) {
|
|
|
|
// The A/B test is enabled, and the user is qualified to be in the
|
|
|
|
// test by being logged in.
|
|
|
|
if ( !$abstate && $optionsManager->getOption( $user, 'discussiontools-editmode' ) === '' ) {
|
|
|
|
// Assign the user to a group. This is only being done to
|
|
|
|
// users who have never used the tool before, for which we're
|
|
|
|
// using the presence of discussiontools-editmode as a proxy,
|
|
|
|
// as it should be set as soon as the user interacts with the tool.
|
|
|
|
$abstate = $user->getId() % 2 == 0 ? 'test' : 'control';
|
|
|
|
$optionsManager->setOption( $user, 'discussiontools-abtest', $abstate );
|
|
|
|
$optionsManager->saveOptions( $user );
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 21:20:03 +00:00
|
|
|
return $abstate;
|
2021-01-13 06:38:11 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 16:08:56 +00:00
|
|
|
/**
|
2020-12-16 16:07:32 +00:00
|
|
|
* Check if the tools are available for a given title
|
2020-12-16 16:08:56 +00:00
|
|
|
*
|
|
|
|
* @param Title $title
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-01-29 17:09:52 +00:00
|
|
|
public static function isAvailableForTitle( Title $title ) : bool {
|
2020-09-10 13:43:13 +00:00
|
|
|
// Only wikitext pages (e.g. not Flow boards)
|
|
|
|
if ( $title->getContentModel() !== CONTENT_MODEL_WIKITEXT ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-02 18:16:54 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
2020-09-10 13:43:13 +00:00
|
|
|
|
|
|
|
$dtConfig = $services->getConfigFactory()->makeConfig( 'discussiontools' );
|
2020-02-04 21:06:13 +00:00
|
|
|
|
2020-12-16 16:08:56 +00:00
|
|
|
$props = PageProps::getInstance()->getProperties( $title, 'newsectionlink' );
|
|
|
|
$hasNewSectionLink = isset( $props[ $title->getArticleId() ] );
|
|
|
|
|
2020-12-16 16:07:32 +00:00
|
|
|
// Check that the page supports discussions.
|
|
|
|
// Treat pages with __NEWSECTIONLINK__ as talk pages (T245890)
|
|
|
|
return $hasNewSectionLink ||
|
2020-09-10 13:43:13 +00:00
|
|
|
// `wantSignatures` includes talk pages
|
2020-12-16 16:07:32 +00:00
|
|
|
$services->getNamespaceInfo()->wantSignatures( $title->getNamespace() );
|
2020-09-10 13:43:13 +00:00
|
|
|
// TODO: Consider not loading if forceHideNewSectionLink is true.
|
2020-12-16 16:07:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the tool is available on a given page
|
|
|
|
*
|
|
|
|
* @param OutputPage $output
|
2021-02-17 17:16:17 +00:00
|
|
|
* @param string|null $feature Feature to check for (one of static::FEATURES)
|
2020-12-16 16:07:32 +00:00
|
|
|
* Null will check for any DT feature.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-01-29 17:09:52 +00:00
|
|
|
public static function isFeatureEnabledForOutput( OutputPage $output, ?string $feature = null ) : bool {
|
2020-12-16 16:07:32 +00:00
|
|
|
// Don't show on edit pages, history, etc.
|
|
|
|
if ( Action::getActionName( $output->getContext() ) !== 'view' ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$title = $output->getTitle();
|
|
|
|
// Don't show on pages without a Title
|
|
|
|
if ( !$title ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't show on mobile
|
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) ) {
|
|
|
|
$mobFrontContext = MediaWikiServices::getInstance()->getService( 'MobileFrontend.Context' );
|
|
|
|
if ( $mobFrontContext->shouldDisplayMobileView() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 22:34:02 +00:00
|
|
|
// Topic subscription is not available on your own talk page, as you will
|
|
|
|
// get 'mention-user-talk' notifications already. (T276996)
|
2021-04-07 23:22:23 +00:00
|
|
|
if ( $feature === self::TOPICSUBSCRIPTION && $title->getNamespace() === NS_USER_TALK && !$title->isSubpage() ) {
|
2021-02-17 22:34:02 +00:00
|
|
|
$user = User::newFromName( $title->getText() );
|
2021-04-07 23:22:23 +00:00
|
|
|
if ( $user && $user->equals( $output->getUser() ) ) {
|
2021-02-17 22:34:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 16:07:32 +00:00
|
|
|
// ?dtenable=1 overrides all user and title checks
|
2021-04-13 22:08:52 +00:00
|
|
|
$queryEnable = $output->getRequest()->getVal( 'dtenable' ) ||
|
2020-12-16 16:07:32 +00:00
|
|
|
// Extra hack for parses from API, where this parameter isn't passed to derivative requests
|
2021-04-13 22:08:52 +00:00
|
|
|
RequestContext::getMain()->getRequest()->getVal( 'dtenable' );
|
|
|
|
// The cookie hack allows users to enable all features when they are not
|
|
|
|
// yet available on the wiki
|
|
|
|
$cookieEnable = $output->getRequest()->getCookie( 'discussiontools-tempenable' ) ?: false;
|
|
|
|
|
|
|
|
if ( $feature === self::TOPICSUBSCRIPTION ) {
|
|
|
|
// Can't be enabled via query/cookie, because the tables may not exist yet (T280082)
|
|
|
|
$queryEnable = false;
|
|
|
|
$cookieEnable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $queryEnable ) {
|
2020-12-16 16:07:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return static::isAvailableForTitle( $title ) && (
|
|
|
|
static::isFeatureEnabledForUser( $output->getUser(), $feature ) ||
|
2021-04-13 22:08:52 +00:00
|
|
|
$cookieEnable
|
2020-09-10 13:43:13 +00:00
|
|
|
);
|
|
|
|
}
|
2019-10-10 19:11:07 +00:00
|
|
|
}
|