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
|
|
|
|
2020-12-14 18:52:47 +00:00
|
|
|
use ExtensionRegistry;
|
2021-07-28 10:36:58 +00:00
|
|
|
use IContextSource;
|
2022-02-16 23:29:10 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentUtils;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ContentThreadItemSet;
|
2022-01-19 20:39:54 +00:00
|
|
|
use MediaWiki\Extension\Gadgets\GadgetRepo;
|
2022-02-16 23:29:10 +00:00
|
|
|
use MediaWiki\Extension\VisualEditor\ParsoidHelper;
|
2022-08-05 19:50:42 +00:00
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2020-02-20 20:39:35 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-02-16 23:29:10 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2021-08-02 16:25:23 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2022-02-16 23:29:10 +00:00
|
|
|
use MWException;
|
2020-05-14 22:44:49 +00:00
|
|
|
use OutputPage;
|
2022-02-16 23:29:10 +00:00
|
|
|
use Psr\Log\NullLogger;
|
2020-05-14 22:44:49 +00:00
|
|
|
use RequestContext;
|
2020-12-16 16:08:56 +00:00
|
|
|
use Title;
|
2022-02-16 23:29:10 +00:00
|
|
|
use TitleValue;
|
2022-08-05 19:50:42 +00:00
|
|
|
use Wikimedia\Assert\Assert;
|
2022-02-16 23:29:10 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
|
|
|
use Wikimedia\Parsoid\Utils\DOMUtils;
|
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-08-17 20:23:27 +00:00
|
|
|
public const AUTOTOPICSUB = 'autotopicsub';
|
2021-01-28 17:19:52 +00:00
|
|
|
public const VISUALENHANCEMENTS = 'visualenhancements';
|
2022-04-21 13:08:59 +00:00
|
|
|
public const VISUALENHANCEMENTS_REPLY = 'visualenhancements_reply';
|
2021-08-17 20:23:27 +00:00
|
|
|
|
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 = [
|
2022-06-09 13:51:33 +00:00
|
|
|
// Can't use static:: in compile-time constants
|
2021-04-08 12:30:28 +00:00
|
|
|
self::REPLYTOOL,
|
|
|
|
self::NEWTOPICTOOL,
|
|
|
|
self::SOURCEMODETOOLBAR,
|
|
|
|
self::TOPICSUBSCRIPTION,
|
2021-08-17 20:23:27 +00:00
|
|
|
self::AUTOTOPICSUB,
|
2021-01-28 17:19:52 +00:00
|
|
|
self::VISUALENHANCEMENTS,
|
2022-04-21 13:08:59 +00:00
|
|
|
self::VISUALENHANCEMENTS_REPLY,
|
2021-02-17 17:16:17 +00:00
|
|
|
];
|
|
|
|
|
2022-01-19 20:39:54 +00:00
|
|
|
public const FEATURES_CONFLICT_WITH_GADGET = [
|
|
|
|
self::REPLYTOOL,
|
|
|
|
];
|
|
|
|
|
2021-12-07 22:32:06 +00:00
|
|
|
protected static $propCache = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a title has a page prop, and use an in-memory cache to avoid extra queries
|
|
|
|
*
|
|
|
|
* @param Title $title Title
|
|
|
|
* @param string $prop Page property
|
|
|
|
* @return bool Title has page property
|
|
|
|
*/
|
|
|
|
public static function hasPagePropCached( Title $title, string $prop ): bool {
|
|
|
|
$id = $title->getArticleId();
|
2022-06-09 13:51:33 +00:00
|
|
|
if ( !isset( static::$propCache[ $id ] ) ) {
|
|
|
|
static::$propCache[ $id ] = [];
|
2021-12-07 22:32:06 +00:00
|
|
|
}
|
2022-06-09 13:51:33 +00:00
|
|
|
if ( !isset( static::$propCache[ $id ][ $prop ] ) ) {
|
2021-12-07 22:32:06 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$props = $services->getPageProps()->getProperties( $title, $prop );
|
2022-06-09 13:51:33 +00:00
|
|
|
static::$propCache[ $id ][ $prop ] = isset( $props[ $id ] );
|
2021-12-07 22:32:06 +00:00
|
|
|
}
|
2022-06-09 13:51:33 +00:00
|
|
|
return static::$propCache[ $id ][ $prop ];
|
2021-12-07 22:32:06 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 23:29:10 +00:00
|
|
|
/**
|
|
|
|
* Parse a revision by using the discussion parser on the HTML provided by Parsoid.
|
|
|
|
*
|
|
|
|
* @param RevisionRecord $revRecord
|
|
|
|
* @return ContentThreadItemSet
|
|
|
|
*/
|
|
|
|
public static function parseRevisionParsoidHtml( RevisionRecord $revRecord ): ContentThreadItemSet {
|
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$parsoidHelper = new ParsoidHelper(
|
|
|
|
$services->getMainConfig(),
|
|
|
|
new NullLogger(),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get HTML for the revision
|
|
|
|
$status = $parsoidHelper->requestRestbasePageHtml( $revRecord );
|
|
|
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
[ 'message' => $msg, 'params' => $params ] = $status->getErrors()[0];
|
|
|
|
throw new MWException( wfMessage( $msg, ...$params )->text() );
|
|
|
|
}
|
|
|
|
|
|
|
|
$title = TitleValue::newFromPage( $revRecord->getPage() );
|
|
|
|
|
|
|
|
$response = $status->getValue();
|
|
|
|
$html = $response['body'];
|
|
|
|
|
|
|
|
// Run the discussion parser on it
|
|
|
|
$doc = DOMUtils::parseHTML( $html );
|
|
|
|
$container = DOMCompat::getBody( $doc );
|
|
|
|
|
|
|
|
CommentUtils::unwrapParsoidSections( $container );
|
|
|
|
|
|
|
|
$parser = $services->getService( 'DiscussionTools.CommentParser' );
|
|
|
|
return $parser->parse( $container, $title );
|
|
|
|
}
|
|
|
|
|
2022-01-19 20:39:54 +00:00
|
|
|
/**
|
|
|
|
* @param UserIdentity $user
|
|
|
|
* @param string $feature Feature to check for
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function featureConflictsWithGadget( UserIdentity $user, string $feature ) {
|
|
|
|
$dtConfig = MediaWikiServices::getInstance()->getConfigFactory()
|
|
|
|
->makeConfig( 'discussiontools' );
|
|
|
|
$gadgetName = $dtConfig->get( 'DiscussionToolsConflictingGadgetName' );
|
|
|
|
if ( !$gadgetName ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !in_array( $feature, static::FEATURES_CONFLICT_WITH_GADGET ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$extensionRegistry = ExtensionRegistry::getInstance();
|
|
|
|
if ( $extensionRegistry->isLoaded( 'Gadgets' ) ) {
|
|
|
|
$gadgetsRepo = GadgetRepo::singleton();
|
|
|
|
$match = array_search( $gadgetName, $gadgetsRepo->getGadgetIds() );
|
|
|
|
if ( $match !== false ) {
|
|
|
|
try {
|
|
|
|
return $gadgetsRepo->getGadget( $gadgetName )
|
|
|
|
->isEnabled( $user );
|
|
|
|
} catch ( \InvalidArgumentException $e ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2021-08-02 16:25:23 +00:00
|
|
|
* @param UserIdentity $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-08-02 16:25:23 +00:00
|
|
|
public static function isFeatureAvailableToUser( UserIdentity $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;
|
|
|
|
}
|
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
if (
|
|
|
|
( $feature === static::TOPICSUBSCRIPTION || $feature === static::AUTOTOPICSUB ) &&
|
|
|
|
!$user->isRegistered()
|
|
|
|
) {
|
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
|
|
|
}
|
|
|
|
|
2021-12-17 17:30:09 +00:00
|
|
|
// Being in the "test" group for this feature means it's enabled. This
|
|
|
|
// overrules the wiki's beta feature setting. (However, a user who's
|
|
|
|
// in the control group can still bypass this and enable the feature
|
|
|
|
// normally.)
|
|
|
|
$abtest = static::determineUserABTestBucket( $user, $feature );
|
|
|
|
if ( $abtest === 'test' ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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' ) ) {
|
2021-12-17 17:30:09 +00:00
|
|
|
$betaenabled = $optionsLookup->getOption( $user, 'discussiontools-betaenable', 0 );
|
|
|
|
return (bool)$betaenabled;
|
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
|
|
|
|
*
|
2021-08-02 16:25:23 +00:00
|
|
|
* @param UserIdentity $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-08-02 16:25:23 +00:00
|
|
|
public static function isFeatureEnabledForUser( UserIdentity $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 ) {
|
2022-01-19 20:39:54 +00:00
|
|
|
if ( static::featureConflictsWithGadget( $user, $feature ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-16 16:07:32 +00:00
|
|
|
// Check for a specific feature
|
2022-04-21 13:08:59 +00:00
|
|
|
$enabled = $optionsLookup->getOption( $user, 'discussiontools-' . $feature );
|
|
|
|
// `null` means there is no user option for this feature, so it must be enabled
|
|
|
|
return $enabled === null ? true : $enabled;
|
2021-02-17 17:16:17 +00:00
|
|
|
} 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
|
|
|
|
*
|
2022-05-19 06:07:19 +00:00
|
|
|
* Currently this just checks whether the user is logged in, and assigns
|
|
|
|
* them to a consistent bucket based on their ID.
|
2021-01-13 06:38:11 +00:00
|
|
|
*
|
2021-08-02 16:25:23 +00:00
|
|
|
* @param UserIdentity $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.
|
2022-05-19 06:07:19 +00:00
|
|
|
* @return string 'test' if in the test group, 'control' if in the control group, or '' if
|
|
|
|
* they're not in the test
|
2021-01-13 06:38:11 +00:00
|
|
|
*/
|
2022-05-19 06:07:19 +00:00
|
|
|
public static function determineUserABTestBucket( UserIdentity $user, ?string $feature = null ): string {
|
2021-01-13 06:38:11 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$optionsManager = $services->getUserOptionsManager();
|
|
|
|
$dtConfig = $services->getConfigFactory()->makeConfig( 'discussiontools' );
|
|
|
|
|
|
|
|
$abtest = $dtConfig->get( 'DiscussionToolsABTest' );
|
2021-03-09 21:20:03 +00:00
|
|
|
|
2021-01-13 06:38:11 +00:00
|
|
|
if (
|
2021-08-02 16:25:23 +00:00
|
|
|
$user->isRegistered() &&
|
2022-05-19 06:07:19 +00:00
|
|
|
( $feature ? ( $abtest == $feature ) : (bool)$abtest )
|
2021-01-13 06:38:11 +00:00
|
|
|
) {
|
2022-05-19 06:07:19 +00:00
|
|
|
return $user->getId() % 2 == 0 ? 'test' : 'control';
|
2021-01-13 06:38:11 +00:00
|
|
|
}
|
2021-12-10 07:21:33 +00:00
|
|
|
return '';
|
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
|
2022-04-19 15:03:29 +00:00
|
|
|
* @param string|null $feature Feature to check for (one of static::FEATURES)
|
|
|
|
* Null will check for any DT feature.
|
2020-12-16 16:08:56 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2022-04-19 15:03:29 +00:00
|
|
|
public static function isAvailableForTitle( Title $title, ?string $feature = null ): bool {
|
2021-04-26 17:39:01 +00:00
|
|
|
// Only wikitext pages (e.g. not Flow boards, special pages)
|
2020-09-10 13:43:13 +00:00
|
|
|
if ( $title->getContentModel() !== CONTENT_MODEL_WIKITEXT ) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-26 17:39:01 +00:00
|
|
|
if ( !$title->canExist() ) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-10 13:43:13 +00:00
|
|
|
|
2022-04-19 15:03:29 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
|
|
|
if ( $feature === static::VISUALENHANCEMENTS ) {
|
|
|
|
$dtConfig = $services->getConfigFactory()->makeConfig( 'discussiontools' );
|
|
|
|
$namespaces = $dtConfig->get( 'DiscussionTools_visualenhancements_namespaces' );
|
|
|
|
if ( is_array( $namespaces ) ) {
|
|
|
|
// Only allow visual enhancements in specified namespaces
|
|
|
|
return in_array( $title->getNamespace(), $namespaces, true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
$hasNewSectionLink = static::hasPagePropCached( $title, 'newsectionlink' );
|
2020-12-16 16:08:56 +00:00
|
|
|
|
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
|
2021-09-26 00:37:46 +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-07-22 07:25:13 +00:00
|
|
|
public static function isFeatureEnabledForOutput( OutputPage $output, ?string $feature = null ): bool {
|
2022-06-01 21:43:42 +00:00
|
|
|
// Only show on normal page views (not history etc.), and in edit mode for previews
|
2022-07-09 11:48:17 +00:00
|
|
|
if (
|
|
|
|
// Don't try to call $output->getActionName if testing for NEWTOPICTOOL as we use
|
|
|
|
// the hook onGetActionName to override the action for the tool on empty pages.
|
|
|
|
// If we tried to call it here it would set up infinite recursion (T312689)
|
|
|
|
$feature !== static::NEWTOPICTOOL &&
|
|
|
|
!in_array( $output->getActionName(), [ 'view', 'edit', 'submit' ] )
|
|
|
|
) {
|
2020-12-16 16:07:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$title = $output->getTitle();
|
|
|
|
// Don't show on pages without a Title
|
|
|
|
if ( !$title ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-17 22:34:02 +00:00
|
|
|
// Topic subscription is not available on your own talk page, as you will
|
2021-08-17 18:33:46 +00:00
|
|
|
// get 'edit-user-talk' notifications already. (T276996)
|
2021-08-17 20:23:27 +00:00
|
|
|
if (
|
2022-06-09 13:51:33 +00:00
|
|
|
( $feature === static::TOPICSUBSCRIPTION || $feature === static::AUTOTOPICSUB ) &&
|
2021-08-17 20:23:27 +00:00
|
|
|
$title->equals( $output->getUser()->getTalkPage() )
|
|
|
|
) {
|
2021-08-17 18:33:46 +00:00
|
|
|
return false;
|
2021-02-17 22:34:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 16:07:32 +00:00
|
|
|
// ?dtenable=1 overrides all user and title checks
|
2021-06-26 15:46:07 +00:00
|
|
|
$queryEnable = $output->getRequest()->getRawVal( '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-15 00:21:53 +00:00
|
|
|
RequestContext::getMain()->getRequest()->getRawVal( 'dtenable' );
|
2021-04-13 22:08:52 +00:00
|
|
|
|
2022-07-27 16:20:02 +00:00
|
|
|
if ( $queryEnable ) {
|
2020-12-16 16:07:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-04 18:22:10 +00:00
|
|
|
if ( $queryEnable === '0' ) {
|
2021-06-26 15:46:07 +00:00
|
|
|
// ?dtenable=0 forcibly disables the feature regardless of any other checks (T285578)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-19 15:03:29 +00:00
|
|
|
if ( !static::isAvailableForTitle( $title, $feature ) ) {
|
2022-02-23 12:01:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:42:13 +00:00
|
|
|
$isMobile = false;
|
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) ) {
|
|
|
|
$mobFrontContext = MediaWikiServices::getInstance()->getService( 'MobileFrontend.Context' );
|
|
|
|
$isMobile = $mobFrontContext->shouldDisplayMobileView();
|
|
|
|
}
|
|
|
|
$dtConfig = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'discussiontools' );
|
|
|
|
|
2022-02-23 12:01:45 +00:00
|
|
|
if ( $isMobile ) {
|
2022-02-22 18:42:13 +00:00
|
|
|
// Enabling mobile removes MobileFrontend's reply and new topic tools, so always
|
|
|
|
// enable these tools as a replacement.
|
2022-02-23 12:01:45 +00:00
|
|
|
return $dtConfig->get( 'DiscussionToolsEnableMobile' ) && (
|
|
|
|
$feature === null ||
|
2022-06-09 13:51:33 +00:00
|
|
|
$feature === static::REPLYTOOL ||
|
|
|
|
$feature === static::NEWTOPICTOOL ||
|
2021-01-28 17:19:52 +00:00
|
|
|
$feature === static::SOURCEMODETOOLBAR ||
|
|
|
|
// Even though mobile ignores user preferences, TOPICSUBSCRIPTION must
|
|
|
|
// still be disabled if the user isn't registered.
|
|
|
|
( $feature === static::TOPICSUBSCRIPTION && $output->getUser()->isRegistered() ) ||
|
2022-07-08 21:40:41 +00:00
|
|
|
// Even though mobile ignores user preferences, VISUALENHANCEMENTS must
|
|
|
|
// still be disabled if is unavailable on the wiki.
|
|
|
|
(
|
2022-08-01 21:29:01 +00:00
|
|
|
( $feature === static::VISUALENHANCEMENTS || $feature === static::VISUALENHANCEMENTS_REPLY ) &&
|
2022-07-08 21:40:41 +00:00
|
|
|
$dtConfig->get( 'DiscussionTools_' . $feature ) !== 'unavailable'
|
|
|
|
)
|
2022-02-23 12:01:45 +00:00
|
|
|
);
|
2022-02-22 18:42:13 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 12:01:45 +00:00
|
|
|
return static::isFeatureEnabledForUser( $output->getUser(), $feature );
|
2020-09-10 13:43:13 +00:00
|
|
|
}
|
2021-07-28 10:36:58 +00:00
|
|
|
|
2022-07-06 23:51:53 +00:00
|
|
|
/**
|
|
|
|
* Check if the "New section" tab would be shown in a normal skin.
|
|
|
|
*
|
|
|
|
* @param IContextSource $context
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function shouldShowNewSectionTab( IContextSource $context ): bool {
|
|
|
|
$title = $context->getTitle();
|
|
|
|
$output = $context->getOutput();
|
|
|
|
|
|
|
|
// Match the logic in MediaWiki core (as defined in SkinTemplate::buildContentNavigationUrlsInternal):
|
|
|
|
// https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/add6d0a0e38167a710fb47fac97ff3004451494c/includes/skins/SkinTemplate.php#1317
|
|
|
|
// * __NONEWSECTIONLINK__ is not present (OutputPage::forceHideNewSectionLink) and...
|
2022-07-28 13:30:54 +00:00
|
|
|
// - This is the current revision of a non-redirect in a talk namespace or...
|
2022-07-06 23:51:53 +00:00
|
|
|
// - __NEWSECTIONLINK__ is present (OutputPage::showNewSectionLink)
|
|
|
|
return (
|
|
|
|
!static::hasPagePropCached( $title, 'nonewsectionlink' ) &&
|
2022-07-28 13:30:54 +00:00
|
|
|
( ( $title->isTalkPage() && !$title->isRedirect() && $output->isRevisionCurrent() ) ||
|
2022-07-06 23:51:53 +00:00
|
|
|
static::hasPagePropCached( $title, 'newsectionlink' ) )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-28 10:36:58 +00:00
|
|
|
/**
|
|
|
|
* Check if this page view should open the new topic tool on page load.
|
|
|
|
*
|
|
|
|
* @param IContextSource $context
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-09-20 16:20:06 +00:00
|
|
|
public static function shouldOpenNewTopicTool( IContextSource $context ): bool {
|
2021-07-28 10:36:58 +00:00
|
|
|
$req = $context->getRequest();
|
|
|
|
$out = $context->getOutput();
|
|
|
|
|
|
|
|
return (
|
|
|
|
// ?title=...&action=edit§ion=new
|
|
|
|
// ?title=...&veaction=editsource§ion=new
|
|
|
|
( $req->getVal( 'action' ) === 'edit' || $req->getVal( 'veaction' ) === 'editsource' ) &&
|
2021-09-20 16:20:06 +00:00
|
|
|
$req->getVal( 'section' ) === 'new' &&
|
2021-07-28 10:36:58 +00:00
|
|
|
// Adding a new topic with preloaded text is not supported yet (T269310)
|
|
|
|
!(
|
|
|
|
$req->getVal( 'editintro' ) || $req->getVal( 'preload' ) ||
|
|
|
|
$req->getVal( 'preloadparams' ) || $req->getVal( 'preloadtitle' )
|
|
|
|
) &&
|
|
|
|
// User has new topic tool enabled (and not using &dtenable=0)
|
2022-06-09 13:51:33 +00:00
|
|
|
static::isFeatureEnabledForOutput( $out, static::NEWTOPICTOOL )
|
2021-07-28 10:36:58 +00:00
|
|
|
);
|
|
|
|
}
|
2021-09-20 16:20:06 +00:00
|
|
|
|
|
|
|
/**
|
2022-08-05 19:50:42 +00:00
|
|
|
* Check if this page view should display the "empty state" message for empty talk pages.
|
2021-09-20 16:20:06 +00:00
|
|
|
*
|
|
|
|
* @param IContextSource $context
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function shouldDisplayEmptyState( IContextSource $context ): bool {
|
|
|
|
$req = $context->getRequest();
|
|
|
|
$out = $context->getOutput();
|
2022-01-07 21:33:46 +00:00
|
|
|
$user = $context->getUser();
|
2021-09-20 16:20:06 +00:00
|
|
|
$title = $context->getTitle();
|
|
|
|
|
2022-01-07 21:33:46 +00:00
|
|
|
$optionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
|
|
|
|
|
2021-09-20 16:20:06 +00:00
|
|
|
return (
|
|
|
|
(
|
|
|
|
// When following a red link from another page (but not when clicking the 'Edit' tab)
|
2022-01-07 21:33:46 +00:00
|
|
|
(
|
|
|
|
$req->getVal( 'action' ) === 'edit' && $req->getVal( 'redlink' ) === '1' &&
|
|
|
|
// …if not disabled by the user
|
|
|
|
$optionsLookup->getOption( $user, 'discussiontools-newtopictool-createpage' )
|
|
|
|
) ||
|
2021-09-20 16:20:06 +00:00
|
|
|
// When the new topic tool will be opened (usually when clicking the 'Add topic' tab)
|
2022-06-09 13:51:33 +00:00
|
|
|
static::shouldOpenNewTopicTool( $context ) ||
|
2021-09-20 16:20:06 +00:00
|
|
|
// In read mode (accessible for non-existent pages by clicking 'Cancel' in editor)
|
|
|
|
$req->getVal( 'action', 'view' ) === 'view'
|
|
|
|
) &&
|
|
|
|
// Only in talk namespaces, not including other namespaces that isAvailableForTitle() allows
|
|
|
|
$title->isTalkPage() &&
|
2022-08-05 19:50:42 +00:00
|
|
|
// Only if the subject page or the user exists (T288319, T312560)
|
|
|
|
static::pageSubjectExists( $title ) &&
|
2022-07-06 23:51:53 +00:00
|
|
|
// The default display will probably be more useful for links to old revisions of deleted
|
|
|
|
// pages (existing pages are already excluded in shouldShowNewSectionTab())
|
2021-09-20 16:20:06 +00:00
|
|
|
$req->getIntOrNull( 'oldid' ) === null &&
|
2022-07-06 23:51:53 +00:00
|
|
|
// Only if "New section" tab would be shown by the skin.
|
|
|
|
// If the page doesn't exist, this only happens in talk namespaces.
|
|
|
|
// If the page exists, it also considers magic words on the page.
|
|
|
|
static::shouldShowNewSectionTab( $context ) &&
|
2021-09-20 16:20:06 +00:00
|
|
|
// User has new topic tool enabled (and not using &dtenable=0)
|
2022-06-09 13:51:33 +00:00
|
|
|
static::isFeatureEnabledForOutput( $out, static::NEWTOPICTOOL )
|
2021-09-20 16:20:06 +00:00
|
|
|
);
|
|
|
|
}
|
2021-08-17 20:23:27 +00:00
|
|
|
|
2022-08-05 19:50:42 +00:00
|
|
|
/**
|
|
|
|
* Return whether the corresponding subject page exists, or (if the page is a user talk page,
|
|
|
|
* excluding subpages) whether the user is registered or a valid IP address.
|
|
|
|
*
|
|
|
|
* @param LinkTarget $talkPage
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function pageSubjectExists( LinkTarget $talkPage ): bool {
|
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$namespaceInfo = $services->getNamespaceInfo();
|
|
|
|
Assert::precondition( $namespaceInfo->isTalk( $talkPage->getNamespace() ), "Page is a talk page" );
|
|
|
|
|
|
|
|
if ( $talkPage->getNamespace() === NS_USER_TALK && strpos( $talkPage->getText(), '/' ) === false ) {
|
|
|
|
if ( $services->getUserNameUtils()->isIP( $talkPage->getText() ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$subjectUser = $services->getUserFactory()->newFromName( $talkPage->getText() );
|
|
|
|
if ( $subjectUser && $subjectUser->isRegistered() ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$subjectPage = $namespaceInfo->getSubjectPage( $talkPage );
|
|
|
|
return $services->getPageStore()->getPageForLink( $subjectPage )->exists();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 20:23:27 +00:00
|
|
|
/**
|
|
|
|
* Check if we should be adding automatic topic subscriptions for this user on this page.
|
|
|
|
*
|
|
|
|
* @param UserIdentity $user
|
|
|
|
* @param Title $title
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function shouldAddAutoSubscription( UserIdentity $user, Title $title ): bool {
|
|
|
|
// This duplicates the logic from isFeatureEnabledForOutput(),
|
|
|
|
// because we don't have access to the request or the output here.
|
|
|
|
|
|
|
|
// Topic subscription is not available on your own talk page, as you will
|
|
|
|
// get 'edit-user-talk' notifications already. (T276996)
|
|
|
|
// (can't use User::getTalkPage() to check because this is a UserIdentity)
|
|
|
|
if ( $title->inNamespace( NS_USER_TALK ) && $title->getText() === $user->getName() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:52:10 +00:00
|
|
|
// Users flagged as bots shouldn't be autosubscribed. They can
|
|
|
|
// manually subscribe if it becomes relevant. (T301933)
|
|
|
|
$user = MediaWikiServices::getInstance()
|
|
|
|
->getUserFactory()
|
|
|
|
->newFromUserIdentity( $user );
|
|
|
|
if ( $user->isBot() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-17 20:23:27 +00:00
|
|
|
// Check if the user has automatic subscriptions enabled, and the tools are enabled on the page.
|
|
|
|
return static::isAvailableForTitle( $title ) &&
|
2022-06-09 13:51:33 +00:00
|
|
|
static::isFeatureEnabledForUser( $user, static::AUTOTOPICSUB );
|
2021-08-17 20:23:27 +00:00
|
|
|
}
|
2019-10-10 19:11:07 +00:00
|
|
|
}
|