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-09-03 02:24:03 +00:00
|
|
|
use IDBAccessObject;
|
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-08-05 19:50:42 +00:00
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2020-02-20 20:39:35 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-12-13 15:50:02 +00:00
|
|
|
use MediaWiki\Parser\Parsoid\ParsoidOutputAccess;
|
2022-02-16 23:29:10 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2021-08-02 16:25:23 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2020-05-14 22:44:49 +00:00
|
|
|
use OutputPage;
|
2022-09-03 02:24:03 +00:00
|
|
|
use ParserOptions;
|
2020-05-14 22:44:49 +00:00
|
|
|
use RequestContext;
|
2022-10-28 18:24:02 +00:00
|
|
|
use RuntimeException;
|
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 {
|
2022-10-21 19:34:18 +00:00
|
|
|
|
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';
|
2022-04-19 17:43:49 +00:00
|
|
|
public const VISUALENHANCEMENTS_PAGEFRAME = 'visualenhancements_pageframe';
|
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,
|
2022-04-19 17:43:49 +00:00
|
|
|
self::VISUALENHANCEMENTS_PAGEFRAME,
|
2021-02-17 17:16:17 +00:00
|
|
|
];
|
|
|
|
|
2022-01-19 20:39:54 +00:00
|
|
|
public const FEATURES_CONFLICT_WITH_GADGET = [
|
|
|
|
self::REPLYTOOL,
|
|
|
|
];
|
|
|
|
|
2022-10-21 19:34:18 +00:00
|
|
|
protected static array $propCache = [];
|
2021-12-07 22:32:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2023-01-11 14:45:20 +00:00
|
|
|
* @param string|false $updateParserCacheFor Whether the parser cache should be updated on cache miss.
|
2022-12-13 15:50:02 +00:00
|
|
|
* May be set to false for batch operations to avoid flooding the cache.
|
2023-01-11 14:45:20 +00:00
|
|
|
* Otherwise, it should be set to the name of the calling method (__METHOD__),
|
|
|
|
* so we can track what is causing parser cache writes.
|
2022-12-13 15:50:02 +00:00
|
|
|
*
|
2022-02-16 23:29:10 +00:00
|
|
|
* @return ContentThreadItemSet
|
|
|
|
*/
|
2022-12-13 15:50:02 +00:00
|
|
|
public static function parseRevisionParsoidHtml(
|
|
|
|
RevisionRecord $revRecord,
|
2023-01-11 14:45:20 +00:00
|
|
|
$updateParserCacheFor
|
2022-12-13 15:50:02 +00:00
|
|
|
): ContentThreadItemSet {
|
2022-09-06 20:14:40 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
2022-09-03 02:24:03 +00:00
|
|
|
$mainConfig = $services->getMainConfig();
|
2022-11-21 12:37:16 +00:00
|
|
|
$parsoidOutputAccess = $services->getParsoidOutputAccess();
|
|
|
|
|
2022-11-21 13:49:21 +00:00
|
|
|
// Look up the page by ID in master. If we just used $revRecord->getPage(),
|
|
|
|
// ParsoidOutputAccess would look it up by namespace+title in replica.
|
2022-11-21 12:37:16 +00:00
|
|
|
$pageRecord = $services->getPageStore()->getPageById( $revRecord->getPageId() ) ?:
|
|
|
|
$services->getPageStore()->getPageById( $revRecord->getPageId(), IDBAccessObject::READ_LATEST );
|
|
|
|
Assert::postcondition( $pageRecord !== null, 'Revision had no page' );
|
|
|
|
|
2022-12-05 19:54:14 +00:00
|
|
|
$parserOptions = ParserOptions::newFromAnon();
|
2023-01-11 14:45:20 +00:00
|
|
|
|
2023-01-13 13:33:02 +00:00
|
|
|
// HACK: remove before the release of MW 1.40 / early 2023.
|
|
|
|
if ( $mainConfig->has( 'TemporaryParsoidHandlerParserCacheWriteRatio' ) ) {
|
|
|
|
// We need to be careful about ramping up the cache writes,
|
|
|
|
// so we don't run out of disk space.
|
|
|
|
if ( wfRandom() >= $mainConfig->get( 'TemporaryParsoidHandlerParserCacheWriteRatio' ) ) {
|
|
|
|
$updateParserCacheFor = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:45:20 +00:00
|
|
|
if ( $updateParserCacheFor ) {
|
|
|
|
// $updateParserCache contains the name of the calling method
|
|
|
|
$parserOptions->setRenderReason( $updateParserCacheFor );
|
|
|
|
}
|
2022-12-05 19:54:14 +00:00
|
|
|
|
2022-11-21 12:37:16 +00:00
|
|
|
$status = $parsoidOutputAccess->getParserOutput(
|
|
|
|
$pageRecord,
|
2022-12-05 19:54:14 +00:00
|
|
|
$parserOptions,
|
2022-12-13 15:50:02 +00:00
|
|
|
$revRecord,
|
|
|
|
// Don't flood the parser cache
|
2023-01-11 14:45:20 +00:00
|
|
|
$updateParserCacheFor ? 0 : ParsoidOutputAccess::OPT_NO_UPDATE_CACHE
|
2022-11-21 12:37:16 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
[ 'message' => $key, 'params' => $params ] = $status->getErrors()[0];
|
|
|
|
$message = wfMessage( $key, ...$params );
|
2022-10-28 18:24:02 +00:00
|
|
|
throw new RuntimeException( $message->inLanguage( 'en' )->useDatabase( false )->text() );
|
2022-10-20 03:21:57 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 12:37:16 +00:00
|
|
|
$parserOutput = $status->getValue();
|
|
|
|
$html = $parserOutput->getText();
|
2022-02-16 23:29:10 +00:00
|
|
|
|
|
|
|
// Run the discussion parser on it
|
|
|
|
$doc = DOMUtils::parseHTML( $html );
|
|
|
|
$container = DOMCompat::getBody( $doc );
|
|
|
|
|
|
|
|
CommentUtils::unwrapParsoidSections( $container );
|
|
|
|
|
|
|
|
$parser = $services->getService( 'DiscussionTools.CommentParser' );
|
2022-09-03 02:24:03 +00:00
|
|
|
$title = TitleValue::newFromPage( $revRecord->getPage() );
|
2022-02-16 23:29:10 +00:00
|
|
|
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 ) &&
|
2022-11-07 12:28:03 +00:00
|
|
|
// Users must be logged in to use topic subscription, and Echo must be installed (T322498)
|
|
|
|
( !$user->isRegistered() || !ExtensionRegistry::getInstance()->isLoaded( 'Echo' ) )
|
2022-06-09 13:51:33 +00:00
|
|
|
) {
|
2021-02-17 22:34:02 +00:00
|
|
|
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
|
|
|
|
2022-10-31 21:03:49 +00:00
|
|
|
if ( $feature ? ( $abtest == $feature ) : (bool)$abtest ) {
|
|
|
|
if ( $user->isRegistered() ) {
|
|
|
|
return $user->getId() % 2 == 0 ? 'test' : 'control';
|
|
|
|
}
|
|
|
|
// logged out
|
|
|
|
$req = RequestContext::getMain()->getRequest();
|
|
|
|
$cookie = $req->getCookie( 'DTAB', '' );
|
|
|
|
if ( $cookie ) {
|
|
|
|
return $cookie;
|
|
|
|
}
|
|
|
|
// we just want to remember this across all calls in this request
|
|
|
|
static $bucket = false;
|
|
|
|
if ( !$bucket ) {
|
|
|
|
$bucket = rand( 0, 1 ) <= 0.5 ? 'test' : 'control';
|
|
|
|
}
|
|
|
|
return $bucket;
|
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 ) {
|
2023-01-11 18:35:51 +00:00
|
|
|
// Visual enhancements are only enabled on talk namespaces (T325417)
|
|
|
|
return $title->isTalkPage();
|
2022-04-19 15:03:29 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-22 18:46:31 +00:00
|
|
|
// Subfeatures are disabled if the main feature is disabled
|
|
|
|
if ( (
|
|
|
|
$feature === static::VISUALENHANCEMENTS_REPLY ||
|
|
|
|
$feature === static::VISUALENHANCEMENTS_PAGEFRAME
|
|
|
|
) && !self::isFeatureEnabledForOutput( $output, static::VISUALENHANCEMENTS ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-10-31 19:02:32 +00:00
|
|
|
return (
|
|
|
|
$dtConfig->get( 'DiscussionToolsEnableMobile' ) ||
|
|
|
|
static::determineUserABTestBucket( $output->getUser(), 'mobile' ) === 'test'
|
|
|
|
) && (
|
2022-02-23 12:01:45 +00:00
|
|
|
$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-11-01 21:09:37 +00:00
|
|
|
$feature === static::VISUALENHANCEMENTS ||
|
|
|
|
$feature === static::VISUALENHANCEMENTS_REPLY ||
|
|
|
|
$feature === static::VISUALENHANCEMENTS_PAGEFRAME
|
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
|
2022-08-26 10:07:15 +00:00
|
|
|
( $req->getRawVal( 'action' ) === 'edit' || $req->getRawVal( 'veaction' ) === 'editsource' ) &&
|
|
|
|
$req->getRawVal( 'section' ) === 'new' &&
|
2021-07-28 10:36:58 +00:00
|
|
|
// Adding a new topic with preloaded text is not supported yet (T269310)
|
|
|
|
!(
|
2022-08-26 10:07:15 +00:00
|
|
|
$req->getCheck( 'editintro' ) || $req->getCheck( 'preload' ) ||
|
2022-10-08 18:49:34 +00:00
|
|
|
$req->getCheck( 'preloadparams' ) || $req->getCheck( 'preloadtitle' ) ||
|
|
|
|
// Switching or previewing from an external tool (T316333)
|
|
|
|
$req->getCheck( 'wpTextbox1' )
|
2021-07-28 10:36:58 +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-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
|
|
|
(
|
2022-08-26 10:07:15 +00:00
|
|
|
$req->getRawVal( 'action' ) === 'edit' && $req->getRawVal( 'redlink' ) === '1' &&
|
2022-01-07 21:33:46 +00:00
|
|
|
// …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)
|
2022-08-26 10:07:15 +00:00
|
|
|
$req->getRawVal( 'action', 'view' ) === 'view'
|
2021-09-20 16:20:06 +00:00
|
|
|
) &&
|
|
|
|
// 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
|
|
|
}
|