2021-01-29 17:09:52 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* DiscussionTools page hooks
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Hooks;
|
|
|
|
|
2021-02-17 22:34:02 +00:00
|
|
|
use DOMDocument;
|
2021-01-29 18:31:27 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentFormatter;
|
2021-02-17 22:34:02 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\SubscriptionStore;
|
2021-01-29 17:09:52 +00:00
|
|
|
use MediaWiki\Hook\BeforePageDisplayHook;
|
|
|
|
use MediaWiki\Hook\OutputPageBeforeHTMLHook;
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
use OutputPage;
|
|
|
|
use Skin;
|
|
|
|
use VisualEditorHooks;
|
2021-02-17 22:34:02 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
2021-01-29 17:09:52 +00:00
|
|
|
|
|
|
|
class PageHooks implements
|
|
|
|
BeforePageDisplayHook,
|
|
|
|
OutputPageBeforeHTMLHook
|
|
|
|
{
|
2021-02-17 22:34:02 +00:00
|
|
|
/** @var SubscriptionStore */
|
|
|
|
protected $subscriptionStore;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param SubscriptionStore $subscriptionStore
|
|
|
|
*/
|
|
|
|
public function __construct( SubscriptionStore $subscriptionStore ) {
|
|
|
|
$this->subscriptionStore = $subscriptionStore;
|
|
|
|
}
|
|
|
|
|
2021-01-29 17:09:52 +00:00
|
|
|
/**
|
|
|
|
* 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();
|
2021-03-06 12:08:50 +00:00
|
|
|
// 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() ) ) {
|
2021-01-29 17:09:52 +00:00
|
|
|
$output->addModuleStyles( [
|
2021-02-17 22:34:02 +00:00
|
|
|
'ext.discussionTools.init.styles',
|
|
|
|
// Topic subscription star
|
|
|
|
'oojs-ui.styles.icons-moderation',
|
2021-01-29 17:09:52 +00:00
|
|
|
] );
|
2021-03-06 12:08:50 +00:00
|
|
|
}
|
|
|
|
// Load modules if any DT feature is enabled for this user
|
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output ) ) {
|
2021-01-29 17:09:52 +00:00
|
|
|
$output->addModules( [
|
|
|
|
'ext.discussionTools.init'
|
|
|
|
] );
|
|
|
|
|
2021-02-17 17:16:17 +00:00
|
|
|
$enabledVars = [];
|
|
|
|
foreach ( HookUtils::FEATURES as $feature ) {
|
|
|
|
$enabledVars[$feature] = HookUtils::isFeatureEnabledForOutput( $output, $feature );
|
|
|
|
}
|
|
|
|
$output->addJsConfigVars( 'wgDiscussionToolsFeaturesEnabled', $enabledVars );
|
2021-01-29 17:09:52 +00:00
|
|
|
|
|
|
|
$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 ) {
|
2021-01-28 17:14:20 +00:00
|
|
|
$lang = $output->getLanguage();
|
2021-01-29 17:09:52 +00:00
|
|
|
// Check after the parser cache if reply links need to be added for
|
|
|
|
// non-cacheable reasons i.e. query string or cookie
|
|
|
|
// The addReplyLinks method is responsible for ensuring that
|
|
|
|
// reply links aren't added twice.
|
2021-02-17 17:16:17 +00:00
|
|
|
foreach ( CommentFormatter::USE_WITH_FEATURES as $feature ) {
|
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, $feature ) ) {
|
|
|
|
CommentFormatter::addReplyLinks( $text, $lang );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-01-28 17:14:20 +00:00
|
|
|
|
2021-02-17 17:16:17 +00:00
|
|
|
foreach ( HookUtils::FEATURES as $feature ) {
|
|
|
|
// Add a CSS class for each enabled feature
|
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, $feature ) ) {
|
2021-03-13 14:39:39 +00:00
|
|
|
$output->addBodyClasses( "ext-discussiontools-$feature-enabled" );
|
2021-02-17 17:16:17 +00:00
|
|
|
}
|
2021-01-29 17:09:52 +00:00
|
|
|
}
|
2021-02-17 17:16:17 +00:00
|
|
|
|
2021-04-08 12:30:28 +00:00
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, HookUtils::TOPICSUBSCRIPTION ) ) {
|
2021-02-17 22:34:02 +00:00
|
|
|
$subscriptionStore = $this->subscriptionStore;
|
|
|
|
$doc = new DOMDocument();
|
|
|
|
$user = $output->getUser();
|
|
|
|
$title = $output->getTitle();
|
|
|
|
$text = preg_replace_callback(
|
|
|
|
'/<!--__DTSUBSCRIBE__(.*)-->/',
|
|
|
|
function ( $matches ) use ( $doc, $subscriptionStore, $user ) {
|
|
|
|
$itemName = $matches[1];
|
|
|
|
$items = $subscriptionStore->getSubscriptionItemsForUser(
|
|
|
|
$user,
|
|
|
|
$itemName
|
|
|
|
);
|
|
|
|
$isSubscribed = count( $items ) && !$items[0]->isMuted();
|
|
|
|
$subscribe = $doc->createElement( 'span' );
|
|
|
|
$subscribe->setAttribute(
|
|
|
|
'class',
|
|
|
|
'ext-discussiontools-section-subscribe ' .
|
|
|
|
( $isSubscribed ? 'oo-ui-icon-unStar oo-ui-image-progressive' : 'oo-ui-icon-star' )
|
|
|
|
);
|
|
|
|
$subscribe->setAttribute( 'data-mw-comment-name', $itemName );
|
|
|
|
return DOMCompat::getOuterHTML( $subscribe );
|
|
|
|
},
|
|
|
|
$text
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-29 17:09:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|