mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/DiscussionTools
synced 2024-11-25 08:50:22 +00:00
b0884b177c
composer: * mediawiki/mediawiki-codesniffer: 36.0.0 → 37.0.0 npm: * postcss: 7.0.35 → 7.0.36 * https://npmjs.com/advisories/1693 (CVE-2021-23368) * glob-parent: 5.1.1 → 5.1.2 * https://npmjs.com/advisories/1751 (CVE-2020-28469) * trim-newlines: 3.0.0 → 3.0.1 * https://npmjs.com/advisories/1753 (CVE-2021-33623) Change-Id: I7a71e23da561599da417db3b3077b78d91173bbc
137 lines
4.3 KiB
PHP
137 lines
4.3 KiB
PHP
<?php
|
|
/**
|
|
* DiscussionTools page hooks
|
|
*
|
|
* @file
|
|
* @ingroup Extensions
|
|
* @license MIT
|
|
*/
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Hooks;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentFormatter;
|
|
use MediaWiki\Extension\DiscussionTools\SubscriptionStore;
|
|
use MediaWiki\Hook\BeforePageDisplayHook;
|
|
use MediaWiki\Hook\OutputPageBeforeHTMLHook;
|
|
use MediaWiki\MediaWikiServices;
|
|
use OutputPage;
|
|
use Skin;
|
|
use VisualEditorHooks;
|
|
|
|
class PageHooks implements
|
|
BeforePageDisplayHook,
|
|
OutputPageBeforeHTMLHook
|
|
{
|
|
/** @var SubscriptionStore */
|
|
protected $subscriptionStore;
|
|
|
|
/**
|
|
* @param SubscriptionStore $subscriptionStore
|
|
*/
|
|
public function __construct( SubscriptionStore $subscriptionStore ) {
|
|
$this->subscriptionStore = $subscriptionStore;
|
|
}
|
|
|
|
/**
|
|
* Adds DiscussionTools JS to the output.
|
|
*
|
|
* This is attached to the MediaWiki 'BeforePageDisplay' hook.
|
|
*
|
|
* @param OutputPage $output
|
|
* @param Skin $skin
|
|
* @return void This hook must not abort, it must return no value
|
|
*/
|
|
public function onBeforePageDisplay( $output, $skin ): void {
|
|
$user = $output->getUser();
|
|
// Load style modules if the tools can be available for the title
|
|
// as this means the DOM may have been modified in the parser cache.
|
|
if ( HookUtils::isAvailableForTitle( $output->getTitle() ) ) {
|
|
$output->addModuleStyles( [
|
|
'ext.discussionTools.init.styles',
|
|
] );
|
|
}
|
|
// Load modules if any DT feature is enabled for this user
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output ) ) {
|
|
$output->addModules( [
|
|
'ext.discussionTools.init'
|
|
] );
|
|
|
|
$enabledVars = [];
|
|
foreach ( HookUtils::FEATURES as $feature ) {
|
|
$enabledVars[$feature] = HookUtils::isFeatureEnabledForOutput( $output, $feature );
|
|
}
|
|
$output->addJsConfigVars( 'wgDiscussionToolsFeaturesEnabled', $enabledVars );
|
|
|
|
$services = MediaWikiServices::getInstance();
|
|
$optionsLookup = $services->getUserOptionsLookup();
|
|
$req = $output->getRequest();
|
|
$editor = $optionsLookup->getOption( $user, 'discussiontools-editmode' );
|
|
// User has no preferred editor yet
|
|
// If the user has a preferred editor, this will be evaluated in the client
|
|
if ( !$editor ) {
|
|
// Check which editor we would use for articles
|
|
// VE pref is 'visualeditor'/'wikitext'. Here we describe the mode,
|
|
// not the editor, so 'visual'/'source'
|
|
$editor = VisualEditorHooks::getPreferredEditor( $user, $req ) === 'visualeditor' ?
|
|
'visual' : 'source';
|
|
$output->addJsConfigVars(
|
|
'wgDiscussionToolsFallbackEditMode',
|
|
$editor
|
|
);
|
|
}
|
|
$dtConfig = $services->getConfigFactory()->makeConfig( 'discussiontools' );
|
|
$abstate = $dtConfig->get( 'DiscussionToolsABTest' ) ?
|
|
$optionsLookup->getOption( $user, 'discussiontools-abtest' ) :
|
|
false;
|
|
if ( $abstate ) {
|
|
$output->addJsConfigVars(
|
|
'wgDiscussionToolsABTestBucket',
|
|
$abstate
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* OutputPageBeforeHTML hook handler
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageBeforeHTML
|
|
*
|
|
* @param OutputPage $output OutputPage object that corresponds to the page
|
|
* @param string &$text Text that will be displayed, in HTML
|
|
* @return bool|void This hook must not abort, it must return true or null.
|
|
*/
|
|
public function onOutputPageBeforeHTML( $output, &$text ) {
|
|
$lang = $output->getLanguage();
|
|
// Check after the parser cache if tools need to be added for
|
|
// non-cacheable reasons i.e. query string or cookie
|
|
// The addDiscussionTools method is responsible for ensuring that
|
|
// tools aren't added twice.
|
|
foreach ( CommentFormatter::USE_WITH_FEATURES as $feature ) {
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, $feature ) ) {
|
|
CommentFormatter::addDiscussionTools( $text );
|
|
break;
|
|
}
|
|
}
|
|
|
|
foreach ( HookUtils::FEATURES as $feature ) {
|
|
// Add a CSS class for each enabled feature
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, $feature ) ) {
|
|
$output->addBodyClasses( "ext-discussiontools-$feature-enabled" );
|
|
}
|
|
}
|
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, HookUtils::TOPICSUBSCRIPTION ) ) {
|
|
$text = CommentFormatter::postprocessTopicSubscription(
|
|
$text, $lang, $this->subscriptionStore, $output->getUser()
|
|
);
|
|
}
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, HookUtils::REPLYTOOL ) ) {
|
|
$text = CommentFormatter::postprocessReplyTool(
|
|
$text, $lang
|
|
);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|