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
|
|
|
|
2023-02-13 23:30:35 +00:00
|
|
|
use LqtDispatch;
|
2024-06-08 22:02:35 +00:00
|
|
|
use MediaWiki\Context\IContextSource;
|
|
|
|
use MediaWiki\Context\RequestContext;
|
2023-05-19 07:44:23 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentParser;
|
2022-02-16 23:29:10 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentUtils;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ContentThreadItemSet;
|
2022-08-05 19:50:42 +00:00
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2020-02-20 20:39:35 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\Output\OutputPage;
|
2023-10-19 19:42:55 +00:00
|
|
|
use MediaWiki\Page\ParserOutputAccess;
|
2024-10-19 21:39:18 +00:00
|
|
|
use MediaWiki\Parser\ParserOptions;
|
|
|
|
use MediaWiki\Registration\ExtensionRegistry;
|
2022-02-16 23:29:10 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2023-08-19 18:16:15 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\Title\TitleValue;
|
2021-08-02 16:25:23 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2022-10-28 18:24:02 +00:00
|
|
|
use RuntimeException;
|
2022-08-05 19:50:42 +00:00
|
|
|
use Wikimedia\Assert\Assert;
|
2023-08-08 21:16:30 +00:00
|
|
|
use Wikimedia\Parsoid\Core\ResourceLimitExceededException;
|
2022-02-16 23:29:10 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
|
|
|
use Wikimedia\Parsoid\Utils\DOMUtils;
|
2024-10-19 21:39:18 +00:00
|
|
|
use Wikimedia\Rdbms\IDBAccessObject;
|
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:
|
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
|
|
|
];
|
|
|
|
|
2023-06-06 12:12:49 +00:00
|
|
|
/**
|
|
|
|
* @var string[] List of configurable sub-features, used to generate:
|
|
|
|
* - Feature override global: $wgDiscussionTools_FEATURE
|
|
|
|
*/
|
|
|
|
public const CONFIGS = [
|
|
|
|
self::VISUALENHANCEMENTS,
|
|
|
|
self::VISUALENHANCEMENTS_REPLY,
|
|
|
|
self::VISUALENHANCEMENTS_PAGEFRAME,
|
|
|
|
];
|
|
|
|
|
2022-01-19 20:39:54 +00:00
|
|
|
public const FEATURES_CONFLICT_WITH_GADGET = [
|
|
|
|
self::REPLYTOOL,
|
|
|
|
];
|
|
|
|
|
2023-09-23 04:32:09 +00:00
|
|
|
private const CACHED_PAGE_PROPS = [
|
|
|
|
'newsectionlink',
|
|
|
|
'nonewsectionlink',
|
|
|
|
'notalk',
|
|
|
|
'archivedtalk',
|
|
|
|
];
|
|
|
|
private 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 {
|
2023-09-23 04:32:09 +00:00
|
|
|
Assert::parameter(
|
2024-05-12 16:00:43 +00:00
|
|
|
in_array( $prop, self::CACHED_PAGE_PROPS, true ),
|
2023-09-23 04:32:09 +00:00
|
|
|
'$prop',
|
|
|
|
'must be one of the cached properties'
|
|
|
|
);
|
2021-12-07 22:32:06 +00:00
|
|
|
$id = $title->getArticleId();
|
2024-05-12 16:00:43 +00:00
|
|
|
if ( !isset( self::$propCache[ $id ] ) ) {
|
2021-12-07 22:32:06 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
2023-09-23 04:32:09 +00:00
|
|
|
// Always fetch all of our properties, we need to check several of them on most requests
|
2024-05-12 16:00:43 +00:00
|
|
|
$pagePropsPerId = $services->getPageProps()->getProperties( $title, self::CACHED_PAGE_PROPS );
|
|
|
|
if ( $pagePropsPerId ) {
|
|
|
|
self::$propCache += $pagePropsPerId;
|
|
|
|
} else {
|
|
|
|
self::$propCache[ $id ] = [];
|
|
|
|
}
|
2021-12-07 22:32:06 +00:00
|
|
|
}
|
2024-05-12 16:00:43 +00:00
|
|
|
return isset( self::$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
|
2023-08-08 21:16:30 +00:00
|
|
|
* @throws ResourceLimitExceededException
|
2022-02-16 23:29:10 +00:00
|
|
|
*/
|
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();
|
2023-04-03 21:12:37 +00:00
|
|
|
$parserOutputAccess = $services->getParserOutputAccess();
|
2022-11-21 12:37:16 +00:00
|
|
|
|
2022-11-21 13:49:21 +00:00
|
|
|
// Look up the page by ID in master. If we just used $revRecord->getPage(),
|
2023-04-03 21:12:37 +00:00
|
|
|
// ParserOutputAccess 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-04-03 21:12:37 +00:00
|
|
|
$parserOptions->setUseParsoid();
|
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
|
|
|
|
2023-04-03 21:12:37 +00:00
|
|
|
$status = $parserOutputAccess->getParserOutput(
|
2022-11-21 12:37:16 +00:00
|
|
|
$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-10-19 19:42:55 +00:00
|
|
|
$updateParserCacheFor ? 0 : ParserOutputAccess::OPT_NO_UPDATE_CACHE
|
2022-11-21 12:37:16 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
[ 'message' => $key, 'params' => $params ] = $status->getErrors()[0];
|
2023-08-08 21:16:30 +00:00
|
|
|
// XXX: HtmlOutputRendererHelper checks for a resource limit exceeded message as well,
|
|
|
|
// maybe we shouldn't be catching that so low down. Re-throw for callers.
|
|
|
|
if ( $status->hasMessage( 'parsoid-resource-limit-exceeded' ) ) {
|
|
|
|
throw new ResourceLimitExceededException( $params[0] ?? '' );
|
|
|
|
}
|
2022-11-21 12:37:16 +00:00
|
|
|
$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();
|
2023-04-03 21:12:37 +00:00
|
|
|
$html = $parserOutput->getRawText();
|
2022-02-16 23:29:10 +00:00
|
|
|
|
|
|
|
// Run the discussion parser on it
|
|
|
|
$doc = DOMUtils::parseHTML( $html );
|
|
|
|
$container = DOMCompat::getBody( $doc );
|
|
|
|
|
2023-01-27 16:07:56 +00:00
|
|
|
// Unwrap sections, so that transclusions overlapping section boundaries don't cause all
|
|
|
|
// comments in the sections to be treated as transcluded from another page.
|
2022-02-16 23:29:10 +00:00
|
|
|
CommentUtils::unwrapParsoidSections( $container );
|
|
|
|
|
2023-05-19 07:44:23 +00:00
|
|
|
/** @var CommentParser $parser */
|
2022-02-16 23:29:10 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2023-06-06 12:08:00 +00:00
|
|
|
if ( !in_array( $feature, static::FEATURES_CONFLICT_WITH_GADGET, true ) ) {
|
2022-01-19 20:39:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$extensionRegistry = ExtensionRegistry::getInstance();
|
|
|
|
if ( $extensionRegistry->isLoaded( 'Gadgets' ) ) {
|
2024-06-30 09:00:43 +00:00
|
|
|
$gadgetsRepo = MediaWikiServices::getInstance()->getService( 'GadgetsRepo' );
|
2023-07-24 17:50:00 +00:00
|
|
|
$match = array_search( $gadgetName, $gadgetsRepo->getGadgetIds(), true );
|
2022-01-19 20:39:54 +00:00
|
|
|
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
|
|
|
|
2023-07-26 10:32:27 +00:00
|
|
|
$userIdentityUtils = $services->getUserIdentityUtils();
|
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)
|
2023-07-26 10:32:27 +00:00
|
|
|
( !$user->isRegistered() || $userIdentityUtils->isTemp( $user ) ||
|
2023-06-02 19:11:36 +00:00
|
|
|
!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
|
2023-06-06 12:12:49 +00:00
|
|
|
if ( !in_array( $feature, static::CONFIGS, true ) ) {
|
|
|
|
// Feature is not configurable, always available
|
|
|
|
return true;
|
|
|
|
}
|
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 {
|
2023-06-06 12:12:49 +00:00
|
|
|
// Some features are always available, so if no feature is
|
|
|
|
// specified (i.e. checking for any feature), always return true.
|
|
|
|
return true;
|
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' ) ) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
2023-02-14 13:43:39 +00:00
|
|
|
* Keep in sync with SQL conditions in persistRevisionThreadItems.php.
|
|
|
|
*
|
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;
|
|
|
|
}
|
2023-02-13 23:30:35 +00:00
|
|
|
// LiquidThreads needs a separate check, since it predates content models other than wikitext (T329423)
|
|
|
|
// @phan-suppress-next-line PhanUndeclaredClassMethod
|
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'Liquid Threads' ) && LqtDispatch::isLqtPage( $title ) ) {
|
|
|
|
return false;
|
2020-09-10 13:43:13 +00:00
|
|
|
}
|
2021-04-26 17:39:01 +00:00
|
|
|
if ( !$title->canExist() ) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-10 13:43:13 +00:00
|
|
|
|
2023-03-27 12:50:45 +00:00
|
|
|
// ARCHIVEDTALK/NOTALK magic words
|
2024-09-06 09:00:28 +00:00
|
|
|
if ( static::hasPagePropCached( $title, 'notalk' ) ) {
|
2023-03-27 12:50:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-03-24 16:45:14 +00:00
|
|
|
if (
|
|
|
|
$feature === static::REPLYTOOL &&
|
2024-09-06 09:00:28 +00:00
|
|
|
static::hasPagePropCached( $title, 'archivedtalk' )
|
2022-03-24 16:45:14 +00:00
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-19 15:03:29 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
|
|
|
if ( $feature === static::VISUALENHANCEMENTS ) {
|
2023-03-09 16:10:38 +00:00
|
|
|
$dtConfig = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'discussiontools' );
|
|
|
|
// Visual enhancements are only enabled on talk namespaces (T325417) ...
|
|
|
|
return $title->isTalkPage() || (
|
2024-09-06 09:00:28 +00:00
|
|
|
// ... or __NEWSECTIONLINK__ (T331635) or __ARCHIVEDTALK__ (T374198) pages
|
|
|
|
(
|
|
|
|
static::hasPagePropCached( $title, 'newsectionlink' ) ||
|
|
|
|
static::hasPagePropCached( $title, 'archivedtalk' )
|
|
|
|
) &&
|
2023-03-09 16:10:38 +00:00
|
|
|
// excluding the main namespace, unless it has been configured for signatures
|
|
|
|
(
|
2023-11-28 08:49:00 +00:00
|
|
|
!$title->inNamespace( NS_MAIN ) ||
|
2023-03-09 16:10:38 +00:00
|
|
|
$services->getNamespaceInfo()->wantSignatures( $title->getNamespace() )
|
|
|
|
)
|
|
|
|
);
|
2022-04-19 15:03:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 16:07:32 +00:00
|
|
|
// Check that the page supports discussions.
|
2023-02-14 14:13:17 +00:00
|
|
|
return (
|
|
|
|
// Talk namespaces, and other namespaces where the signature button is shown in wikitext
|
|
|
|
// editor using $wgExtraSignatureNamespaces (T249036)
|
|
|
|
$services->getNamespaceInfo()->wantSignatures( $title->getNamespace() ) ||
|
|
|
|
// Treat pages with __NEWSECTIONLINK__ as talk pages (T245890)
|
|
|
|
static::hasPagePropCached( $title, 'newsectionlink' )
|
|
|
|
);
|
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)
|
2023-08-28 18:53:28 +00:00
|
|
|
$feature !== static::NEWTOPICTOOL && !(
|
|
|
|
in_array( $output->getActionName(), [ 'view', 'edit', 'submit' ], true ) ||
|
|
|
|
// Subscriptions (specifically page-level subscriptions) are available on history pages (T345096)
|
|
|
|
(
|
|
|
|
$output->getActionName() === 'history' &&
|
|
|
|
$feature === static::TOPICSUBSCRIPTION
|
|
|
|
)
|
|
|
|
)
|
2022-07-09 11:48:17 +00:00
|
|
|
) {
|
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 ) {
|
2023-06-06 12:24:23 +00:00
|
|
|
return $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.
|
2023-06-02 19:11:36 +00:00
|
|
|
( $feature === static::TOPICSUBSCRIPTION && $output->getUser()->isNamed() ) ||
|
2022-11-01 21:09:37 +00:00
|
|
|
$feature === static::VISUALENHANCEMENTS ||
|
|
|
|
$feature === static::VISUALENHANCEMENTS_REPLY ||
|
2023-06-06 12:24:23 +00:00
|
|
|
$feature === static::VISUALENHANCEMENTS_PAGEFRAME;
|
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.
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*/
|
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();
|
2021-06-29 15:35:37 +00:00
|
|
|
$hasPreload = $req->getCheck( 'editintro' ) || $req->getCheck( 'preload' ) ||
|
|
|
|
$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
|
|
|
|
|
|
|
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-06-29 15:35:37 +00:00
|
|
|
// Handle new topic with preloaded text only when requested (T269310)
|
|
|
|
( $req->getCheck( 'dtpreload' ) || !$hasPreload ) &&
|
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
|
|
|
*/
|
|
|
|
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)
|
2024-10-02 10:52:51 +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.
|
|
|
|
*/
|
|
|
|
private static function pageSubjectExists( LinkTarget $talkPage ): bool {
|
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$namespaceInfo = $services->getNamespaceInfo();
|
|
|
|
Assert::precondition( $namespaceInfo->isTalk( $talkPage->getNamespace() ), "Page is a talk page" );
|
|
|
|
|
2023-11-28 08:49:00 +00:00
|
|
|
if ( $talkPage->inNamespace( NS_USER_TALK ) && !str_contains( $talkPage->getText(), '/' ) ) {
|
2022-08-05 19:50:42 +00:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|