2021-01-29 17:09:52 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* DiscussionTools page hooks
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Hooks;
|
|
|
|
|
2021-07-29 06:12:10 +00:00
|
|
|
use Article;
|
2021-07-28 10:36:58 +00:00
|
|
|
use Html;
|
|
|
|
use IContextSource;
|
|
|
|
use MediaWiki\Actions\Hook\GetActionNameHook;
|
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;
|
2021-07-29 06:12:10 +00:00
|
|
|
use MediaWiki\Page\Hook\BeforeDisplayNoArticleTextHook;
|
|
|
|
use OOUI\ButtonWidget;
|
2021-01-29 17:09:52 +00:00
|
|
|
use OutputPage;
|
2021-07-29 06:12:10 +00:00
|
|
|
use RequestContext;
|
2021-01-29 17:09:52 +00:00
|
|
|
use Skin;
|
|
|
|
use VisualEditorHooks;
|
|
|
|
|
|
|
|
class PageHooks implements
|
2021-07-29 06:12:10 +00:00
|
|
|
BeforeDisplayNoArticleTextHook,
|
2021-01-29 17:09:52 +00:00
|
|
|
BeforePageDisplayHook,
|
2021-07-28 10:36:58 +00:00
|
|
|
GetActionNameHook,
|
2021-01-29 17:09:52 +00:00
|
|
|
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
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function onBeforePageDisplay( $output, $skin ): void {
|
2021-01-29 17:09:52 +00:00
|
|
|
$user = $output->getUser();
|
2021-07-29 06:12:10 +00:00
|
|
|
$req = $output->getRequest();
|
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',
|
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();
|
|
|
|
$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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-07-28 10:36:58 +00:00
|
|
|
|
|
|
|
// Replace the action=edit§ion=new form with the new topic tool.
|
2021-07-29 06:12:10 +00:00
|
|
|
if (
|
|
|
|
HookUtils::shouldUseNewTopicTool( $output->getContext() ) &&
|
|
|
|
// unless we got here via a redlink, in which case we want to allow the empty
|
|
|
|
// state to be displayed:
|
|
|
|
$req->getVal( 'redlink' ) !== '1'
|
|
|
|
) {
|
2021-07-28 10:36:58 +00:00
|
|
|
$output->addJsConfigVars( 'wgDiscussionToolsStartNewTopicTool', true );
|
|
|
|
|
|
|
|
// For no-JS compatibility, redirect to the old new section editor if JS is unavailable.
|
|
|
|
// This isn't great, because the user has to load the page twice. But making a page that is
|
|
|
|
// both a view mode and an edit mode seems difficult, so I'm cutting some corners here.
|
|
|
|
// (Code below adapted from VisualEditor.)
|
|
|
|
$params = $output->getRequest()->getValues();
|
|
|
|
$params['dtenable'] = '0';
|
|
|
|
$url = wfScript() . '?' . wfArrayToCgi( $params );
|
|
|
|
$escapedUrl = htmlspecialchars( $url );
|
|
|
|
|
|
|
|
// Redirect if the user has no JS (<noscript>)
|
|
|
|
$output->addHeadItem(
|
|
|
|
'dt-noscript-fallback',
|
|
|
|
"<noscript><meta http-equiv=\"refresh\" content=\"0; url=$escapedUrl\"></noscript>"
|
|
|
|
);
|
|
|
|
// Redirect if the user has no ResourceLoader
|
|
|
|
$output->addScript( Html::inlineScript(
|
|
|
|
"(window.NORLQ=window.NORLQ||[]).push(" .
|
|
|
|
"function(){" .
|
|
|
|
"location.href=\"$url\";" .
|
|
|
|
"}" .
|
|
|
|
");"
|
|
|
|
) );
|
|
|
|
}
|
2021-01-29 17:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-04-19 18:34:55 +00:00
|
|
|
// Check after the parser cache if tools need to be added for
|
2021-01-29 17:09:52 +00:00
|
|
|
// non-cacheable reasons i.e. query string or cookie
|
2021-04-19 18:34:55 +00:00
|
|
|
// The addDiscussionTools method is responsible for ensuring that
|
|
|
|
// tools aren't added twice.
|
2021-02-17 17:16:17 +00:00
|
|
|
foreach ( CommentFormatter::USE_WITH_FEATURES as $feature ) {
|
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, $feature ) ) {
|
2021-04-15 21:09:55 +00:00
|
|
|
CommentFormatter::addDiscussionTools( $text );
|
2021-02-17 17:16:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-01-28 17:14:20 +00:00
|
|
|
|
2021-07-29 06:12:10 +00:00
|
|
|
$this->addFeatureBodyClasses( $output );
|
2021-02-17 17:16:17 +00:00
|
|
|
|
2021-04-08 12:30:28 +00:00
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, HookUtils::TOPICSUBSCRIPTION ) ) {
|
2021-04-19 19:14:07 +00:00
|
|
|
$text = CommentFormatter::postprocessTopicSubscription(
|
|
|
|
$text, $lang, $this->subscriptionStore, $output->getUser()
|
2021-02-17 22:34:02 +00:00
|
|
|
);
|
|
|
|
}
|
2021-04-15 21:09:55 +00:00
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, HookUtils::REPLYTOOL ) ) {
|
|
|
|
$text = CommentFormatter::postprocessReplyTool(
|
|
|
|
$text, $lang
|
|
|
|
);
|
|
|
|
}
|
2021-02-17 22:34:02 +00:00
|
|
|
|
2021-01-29 17:09:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-07-28 10:36:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GetActionName hook handler
|
|
|
|
*
|
|
|
|
* @param IContextSource $context Request context
|
|
|
|
* @param string &$action Default action name, reassign to change it
|
|
|
|
* @return void This hook must not abort, it must return no value
|
|
|
|
*/
|
|
|
|
public function onGetActionName( IContextSource $context, string &$action ): void {
|
|
|
|
if ( $action === 'edit' && HookUtils::shouldUseNewTopicTool( $context ) ) {
|
|
|
|
$action = 'view';
|
|
|
|
}
|
|
|
|
}
|
2021-07-29 06:12:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* BeforeDisplayNoArticleText hook handler
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforeDisplayNoArticleText
|
|
|
|
*
|
|
|
|
* @param Article $article The (empty) article
|
|
|
|
* @return bool|void This hook can abort
|
|
|
|
*/
|
|
|
|
public function onBeforeDisplayNoArticleText( $article ) {
|
|
|
|
// We want to override the empty state for articles on which we would be enabled
|
|
|
|
$title = $article->getTitle();
|
|
|
|
$oldid = $article->getOldID();
|
|
|
|
if ( $oldid || $title->hasSourceText() ) {
|
|
|
|
// The default display will probably be useful here, so leave it.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$context = $article->getContext();
|
|
|
|
$output = $context->getOutput();
|
|
|
|
if ( !HookUtils::isFeatureEnabledForOutput( $output, HookUtils::NEWTOPICTOOL ) ) {
|
|
|
|
// Our empty states are all about using the new topic tool
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$output->enableOOUI();
|
|
|
|
$output->enableClientCache( false );
|
|
|
|
|
|
|
|
// OutputPageBeforeHTML won't have run, since there's no parsed text
|
|
|
|
// to display, but we need these classes or reply links won't show
|
|
|
|
// after a topic is posted.
|
|
|
|
$this->addFeatureBodyClasses( $output );
|
|
|
|
|
|
|
|
$coreConfig = RequestContext::getMain()->getConfig();
|
|
|
|
$iconpath = $coreConfig->get( 'ExtensionAssetsPath' ) . '/DiscussionTools/images';
|
|
|
|
|
|
|
|
$dir = $context->getLanguage()->getDir();
|
|
|
|
$lang = $context->getLanguage()->getHtmlCode();
|
|
|
|
|
|
|
|
$output->addHTML(
|
|
|
|
// This being mw-parser-output is a lie, but makes the reply controller cope much better with everything
|
|
|
|
Html::openElement( 'div', [ 'class' => "ext-discussiontools-emptystate mw-parser-output noarticletext" ] ) .
|
|
|
|
Html::openElement( 'div', [ 'class' => "ext-discussiontools-emptystate-text" ] )
|
|
|
|
);
|
|
|
|
if ( $title->equals( $output->getUser()->getTalkPage() ) ) {
|
|
|
|
$output->addHTML(
|
|
|
|
Html::rawElement( 'h3', [], $context->msg( 'discussiontools-emptystate-title-self' )->parse() ) .
|
|
|
|
Html::rawElement( 'p', [], $context->msg( 'discussiontools-emptystate-desc-self' )->parse() )
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$titleMsg = $title->getNamespace() == NS_USER_TALK ?
|
|
|
|
'discussiontools-emptystate-title-user' :
|
|
|
|
'discussiontools-emptystate-title';
|
|
|
|
$output->addHTML(
|
|
|
|
Html::rawElement( 'h3', [], $context->msg( $titleMsg )->parse() ) .
|
|
|
|
Html::rawElement( 'p', [],
|
|
|
|
$context->msg(
|
|
|
|
$title->getNamespace() == NS_USER_TALK ?
|
|
|
|
'discussiontools-emptystate-desc-user' :
|
|
|
|
'discussiontools-emptystate-desc'
|
|
|
|
)->parse()
|
|
|
|
) .
|
|
|
|
new ButtonWidget( [
|
|
|
|
'label' => $context->msg( 'discussiontools-emptystate-button' )->text(),
|
|
|
|
'href' => $title->getLocalURL( 'action=edit§ion=new' ),
|
|
|
|
'flags' => [ 'primary', 'progressive' ]
|
|
|
|
] )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$output->addHTML(
|
|
|
|
Html::closeElement( 'div' ) .
|
|
|
|
Html::element( 'img', [
|
|
|
|
'src' => $iconpath . '/emptystate.svg',
|
|
|
|
'class' => "ext-discussiontools-emptystate-logo",
|
|
|
|
// This is a purely decorative element
|
|
|
|
'alt' => "",
|
|
|
|
] ) .
|
|
|
|
Html::closeElement( 'div' )
|
|
|
|
);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to add feature-toggle classes to the output's body
|
|
|
|
*
|
|
|
|
* @param OutputPage $output
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function addFeatureBodyClasses( OutputPage $output ): void {
|
|
|
|
foreach ( HookUtils::FEATURES as $feature ) {
|
|
|
|
// Add a CSS class for each enabled feature
|
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, $feature ) ) {
|
|
|
|
$output->addBodyClasses( "ext-discussiontools-$feature-enabled" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 17:09:52 +00:00
|
|
|
}
|