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;
|
2022-09-02 22:24:20 +00:00
|
|
|
use Config;
|
2021-09-07 20:51:35 +00:00
|
|
|
use ConfigFactory;
|
2022-07-20 13:05:21 +00:00
|
|
|
use ExtensionRegistry;
|
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;
|
2023-02-17 23:14:44 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentUtils;
|
2021-02-17 22:34:02 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\SubscriptionStore;
|
2022-03-29 19:29:46 +00:00
|
|
|
use MediaWiki\Extension\VisualEditor\Hooks as VisualEditorHooks;
|
2021-01-29 17:09:52 +00:00
|
|
|
use MediaWiki\Hook\BeforePageDisplayHook;
|
|
|
|
use MediaWiki\Hook\OutputPageBeforeHTMLHook;
|
2023-01-27 12:29:17 +00:00
|
|
|
use MediaWiki\Hook\OutputPageParserOutputHook;
|
2023-02-17 23:14:44 +00:00
|
|
|
use MediaWiki\Hook\SkinTemplateNavigation__UniversalHook;
|
2021-12-16 11:51:04 +00:00
|
|
|
use MediaWiki\Hook\TitleGetEditNoticesHook;
|
2022-07-20 13:05:21 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-07-29 06:12:10 +00:00
|
|
|
use MediaWiki\Page\Hook\BeforeDisplayNoArticleTextHook;
|
2021-08-25 19:10:53 +00:00
|
|
|
use MediaWiki\User\UserNameUtils;
|
2021-09-07 20:51:35 +00:00
|
|
|
use MediaWiki\User\UserOptionsLookup;
|
2021-07-29 06:12:10 +00:00
|
|
|
use OOUI\ButtonWidget;
|
2021-12-16 11:51:04 +00:00
|
|
|
use OOUI\HtmlSnippet;
|
|
|
|
use OOUI\MessageWidget;
|
2021-01-29 17:09:52 +00:00
|
|
|
use OutputPage;
|
2023-01-27 12:29:17 +00:00
|
|
|
use ParserOutput;
|
2021-07-29 06:12:10 +00:00
|
|
|
use RequestContext;
|
2021-01-29 17:09:52 +00:00
|
|
|
use Skin;
|
2023-02-17 23:14:44 +00:00
|
|
|
use SkinTemplate;
|
2021-08-25 19:10:53 +00:00
|
|
|
use SpecialPage;
|
2021-12-16 11:51:04 +00:00
|
|
|
use Title;
|
2021-01-29 17:09:52 +00:00
|
|
|
|
|
|
|
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-12-16 11:51:04 +00:00
|
|
|
OutputPageBeforeHTMLHook,
|
2023-01-27 12:29:17 +00:00
|
|
|
OutputPageParserOutputHook,
|
2023-02-17 23:14:44 +00:00
|
|
|
SkinTemplateNavigation__UniversalHook,
|
2021-12-16 11:51:04 +00:00
|
|
|
TitleGetEditNoticesHook
|
2021-01-29 17:09:52 +00:00
|
|
|
{
|
2021-09-07 20:51:35 +00:00
|
|
|
|
2022-10-21 19:34:18 +00:00
|
|
|
private Config $config;
|
|
|
|
private SubscriptionStore $subscriptionStore;
|
|
|
|
private UserNameUtils $userNameUtils;
|
|
|
|
private UserOptionsLookup $userOptionsLookup;
|
2021-08-25 19:10:53 +00:00
|
|
|
|
2021-09-07 20:51:35 +00:00
|
|
|
public function __construct(
|
|
|
|
ConfigFactory $configFactory,
|
|
|
|
SubscriptionStore $subscriptionStore,
|
|
|
|
UserNameUtils $userNameUtils,
|
|
|
|
UserOptionsLookup $userOptionsLookup
|
|
|
|
) {
|
2022-09-02 22:24:20 +00:00
|
|
|
$this->config = $configFactory->makeConfig( 'discussiontools' );
|
2021-02-17 22:34:02 +00:00
|
|
|
$this->subscriptionStore = $subscriptionStore;
|
2021-08-25 19:10:53 +00:00
|
|
|
$this->userNameUtils = $userNameUtils;
|
2021-09-07 20:51:35 +00:00
|
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
2021-02-17 22:34:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 16:51:22 +00:00
|
|
|
private function isMobile(): bool {
|
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) ) {
|
|
|
|
$mobFrontContext = MediaWikiServices::getInstance()->getService( 'MobileFrontend.Context' );
|
|
|
|
return $mobFrontContext->shouldDisplayMobileView();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-10-26 14:02:30 +00:00
|
|
|
foreach ( HookUtils::FEATURES as $feature ) {
|
|
|
|
// Add a CSS class for each enabled feature
|
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, $feature ) ) {
|
|
|
|
$output->addBodyClasses( "ext-discussiontools-$feature-enabled" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 16:51:22 +00:00
|
|
|
if ( $this->isMobile() && HookUtils::isFeatureEnabledForOutput( $output, HookUtils::VISUALENHANCEMENTS ) ) {
|
|
|
|
$output->addBodyClasses( 'collapsible-headings-collapsed' );
|
|
|
|
}
|
|
|
|
|
2021-03-06 12:08:50 +00:00
|
|
|
// Load style modules if the tools can be available for the title
|
2021-10-26 14:02:30 +00:00
|
|
|
// to selectively hide DT features, depending on the body classes added above.
|
2021-12-17 16:34:23 +00:00
|
|
|
$availableForTitle = HookUtils::isAvailableForTitle( $output->getTitle() );
|
|
|
|
if ( $availableForTitle ) {
|
2021-01-28 17:19:52 +00:00
|
|
|
$output->addModuleStyles( 'ext.discussionTools.init.styles' );
|
2021-03-06 12:08:50 +00:00
|
|
|
}
|
2021-10-26 14:02:30 +00:00
|
|
|
|
2021-03-06 12:08:50 +00:00
|
|
|
// Load modules if any DT feature is enabled for this user
|
2022-10-31 21:03:49 +00:00
|
|
|
if (
|
|
|
|
HookUtils::isFeatureEnabledForOutput( $output ) ||
|
|
|
|
// If there's an a/b test we need to include the JS for unregistered users just so
|
|
|
|
// we can make sure we store the bucket
|
|
|
|
( $this->config->get( 'DiscussionToolsABTest' ) && !$user->isRegistered() )
|
|
|
|
) {
|
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
|
|
|
|
2021-09-07 20:51:35 +00:00
|
|
|
$editor = $this->userOptionsLookup->getOption( $user, 'discussiontools-editmode' );
|
2021-01-29 17:09:52 +00:00
|
|
|
// 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
|
|
|
|
);
|
|
|
|
}
|
2022-05-19 06:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This doesn't involve any DB checks, and so we can put it on every
|
|
|
|
// page to make it easy to pick for logging in WikiEditor. If this
|
|
|
|
// becomes not-cheap, move it elsewhere.
|
|
|
|
$abstate = HookUtils::determineUserABTestBucket( $user );
|
|
|
|
if ( $abstate ) {
|
|
|
|
$output->addJsConfigVars(
|
|
|
|
'wgDiscussionToolsABTestBucket',
|
|
|
|
$abstate
|
|
|
|
);
|
2021-01-29 17:09:52 +00:00
|
|
|
}
|
2021-07-28 10:36:58 +00:00
|
|
|
|
|
|
|
// Replace the action=edit§ion=new form with the new topic tool.
|
2021-09-20 16:20:06 +00:00
|
|
|
if ( HookUtils::shouldOpenNewTopicTool( $output->getContext() ) ) {
|
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\";" .
|
|
|
|
"}" .
|
|
|
|
");"
|
|
|
|
) );
|
|
|
|
}
|
2022-11-18 15:07:49 +00:00
|
|
|
|
|
|
|
if ( $output->getSkin()->getSkinName() === 'minerva' ) {
|
|
|
|
$title = $output->getTitle();
|
|
|
|
|
2022-11-29 17:34:54 +00:00
|
|
|
if (
|
|
|
|
$title->isTalkPage() &&
|
|
|
|
HookUtils::isFeatureEnabledForOutput( $output, HookUtils::REPLYTOOL ) && (
|
2023-02-07 00:31:33 +00:00
|
|
|
// 'DiscussionTools-ledeButton' property may be already set to true or false.
|
|
|
|
// Examine the other conditions only if it's unset.
|
|
|
|
$output->getProperty( 'DiscussionTools-ledeButton' ) ?? (
|
2022-11-29 17:34:54 +00:00
|
|
|
// Header shown on all talk pages, see Article::showNamespaceHeader
|
|
|
|
!$output->getContext()->msg( 'talkpageheader' )->isDisabled() &&
|
|
|
|
// Check if it isn't empty since it may use parser functions to only show itself on some pages
|
|
|
|
trim( $output->getContext()->msg( 'talkpageheader' )->text() ) !== ''
|
|
|
|
)
|
2023-02-07 00:31:33 +00:00
|
|
|
)
|
2022-11-29 17:34:54 +00:00
|
|
|
) {
|
2022-11-30 19:01:42 +00:00
|
|
|
$output->addBodyClasses( 'ext-discussiontools-init-lede-hidden' );
|
2022-11-29 17:34:54 +00:00
|
|
|
$output->enableOOUI();
|
|
|
|
$output->prependHTML(
|
|
|
|
Html::rawElement( 'div',
|
|
|
|
[ 'class' => 'ext-discussiontools-init-lede-button-container' ],
|
|
|
|
( new ButtonWidget( [
|
|
|
|
'label' => $output->getContext()->msg( 'discussiontools-ledesection-button' )->text(),
|
|
|
|
'classes' => [ 'ext-discussiontools-init-lede-button' ],
|
|
|
|
'framed' => false,
|
|
|
|
'icon' => 'info',
|
|
|
|
'infusable' => true,
|
|
|
|
] ) )
|
|
|
|
)
|
|
|
|
);
|
2022-12-08 22:42:29 +00:00
|
|
|
|
|
|
|
// Preload jquery.makeCollapsible for LedeSectionDialog.
|
|
|
|
// Using the same approach as in Skin::getDefaultModules in MediaWiki core.
|
|
|
|
if ( strpos( $output->getHTML(), 'mw-collapsible' ) !== false ) {
|
|
|
|
$output->addModules( 'jquery.makeCollapsible' );
|
|
|
|
$output->addModuleStyles( 'jquery.makeCollapsible.styles' );
|
|
|
|
}
|
2022-11-29 17:34:54 +00:00
|
|
|
}
|
2023-02-07 00:31:33 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
HookUtils::isFeatureEnabledForOutput( $output, HookUtils::NEWTOPICTOOL ) &&
|
|
|
|
// Only add the button if "New section" tab would be shown in a normal skin.
|
|
|
|
HookUtils::shouldShowNewSectionTab( $output->getContext() )
|
|
|
|
) {
|
|
|
|
$output->enableOOUI();
|
|
|
|
$output->addModuleStyles( [
|
|
|
|
// For speechBubbleAdd
|
|
|
|
'oojs-ui.styles.icons-alerts',
|
|
|
|
] );
|
|
|
|
$output->addBodyClasses( 'ext-discussiontools-init-new-topic-opened' );
|
|
|
|
|
|
|
|
// Minerva doesn't show a new topic button by default, unless the MobileFrontend
|
|
|
|
// talk page feature is enabled, but we shouldn't depend on code from there.
|
|
|
|
$output->addHTML( Html::rawElement( 'div',
|
|
|
|
[ 'class' => 'ext-discussiontools-init-new-topic' ],
|
|
|
|
( new ButtonWidget( [
|
|
|
|
'classes' => [ 'ext-discussiontools-init-new-topic-button' ],
|
|
|
|
'href' => $title->getLinkURL( [ 'action' => 'edit', 'section' => 'new' ] ),
|
|
|
|
'icon' => 'speechBubbleAdd',
|
|
|
|
'label' => $output->getContext()->msg( 'skin-action-addsection' )->text(),
|
|
|
|
'flags' => [ 'progressive', 'primary' ],
|
|
|
|
'infusable' => true,
|
|
|
|
] ) )
|
|
|
|
// For compatibility with Minerva click tracking (T295490)
|
|
|
|
->setAttributes( [ 'data-event-name' => 'talkpage.add-topic' ] )
|
|
|
|
) );
|
|
|
|
}
|
2022-11-18 15:07:49 +00:00
|
|
|
}
|
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-10-26 15:42:50 +00:00
|
|
|
// ParserOutputPostCacheTransform hook would be a better place to do this,
|
|
|
|
// so that when the ParserOutput is used directly without using this hook,
|
|
|
|
// we don't leave half-baked interface elements in it (see e.g. T292345, T294168).
|
|
|
|
// But that hook doesn't provide parameters that we need to render correctly
|
|
|
|
// (including the page title, interface language, and current user).
|
|
|
|
|
2022-11-18 15:07:49 +00:00
|
|
|
// This hook can be executed more than once per page view if the page content is composed from
|
|
|
|
// multiple sources!
|
2022-11-18 03:15:51 +00:00
|
|
|
|
2022-12-07 16:51:22 +00:00
|
|
|
$isMobile = $this->isMobile();
|
2021-01-28 17:14:20 +00:00
|
|
|
$lang = $output->getLanguage();
|
2022-07-20 13:05:21 +00:00
|
|
|
|
2021-04-08 12:30:28 +00:00
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, HookUtils::TOPICSUBSCRIPTION ) ) {
|
2021-01-28 17:19:52 +00:00
|
|
|
// Just enable OOUI PHP - the OOUI subscribe button isn't infused unless VISUALENHANCEMENTS are enabled
|
|
|
|
$output->setupOOUI();
|
2021-04-19 19:14:07 +00:00
|
|
|
$text = CommentFormatter::postprocessTopicSubscription(
|
2022-07-20 13:05:21 +00:00
|
|
|
$text, $lang, $this->subscriptionStore, $output->getUser(), $isMobile
|
2021-02-17 22:34:02 +00:00
|
|
|
);
|
|
|
|
}
|
2022-09-27 14:48:25 +00:00
|
|
|
|
2021-04-15 21:09:55 +00:00
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, HookUtils::REPLYTOOL ) ) {
|
2022-04-21 13:08:59 +00:00
|
|
|
$output->enableOOUI();
|
2021-04-15 21:09:55 +00:00
|
|
|
$text = CommentFormatter::postprocessReplyTool(
|
2022-04-21 13:08:59 +00:00
|
|
|
$text, $lang, $isMobile
|
2021-04-15 21:09:55 +00:00
|
|
|
);
|
|
|
|
}
|
2022-09-27 14:48:25 +00:00
|
|
|
|
2021-01-28 17:19:52 +00:00
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, HookUtils::VISUALENHANCEMENTS ) ) {
|
|
|
|
$output->enableOOUI();
|
2022-07-27 15:12:07 +00:00
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, HookUtils::TOPICSUBSCRIPTION ) ) {
|
|
|
|
$output->addModuleStyles( [
|
|
|
|
// Visually enhanced topic subscriptions
|
|
|
|
// bell, bellOutline
|
|
|
|
'oojs-ui.styles.icons-alerts',
|
|
|
|
] );
|
|
|
|
}
|
2022-12-13 05:49:51 +00:00
|
|
|
if (
|
|
|
|
$isMobile ||
|
|
|
|
(
|
|
|
|
HookUtils::isFeatureEnabledForOutput( $output, HookUtils::VISUALENHANCEMENTS_REPLY ) &&
|
|
|
|
CommentFormatter::isLanguageRequiringReplyIcon( $lang )
|
|
|
|
)
|
|
|
|
) {
|
2022-07-27 15:12:07 +00:00
|
|
|
$output->addModuleStyles( [
|
2022-12-13 05:49:51 +00:00
|
|
|
// Reply button:
|
2022-04-21 13:08:59 +00:00
|
|
|
// share
|
|
|
|
'oojs-ui.styles.icons-content',
|
2022-12-13 05:49:51 +00:00
|
|
|
] );
|
|
|
|
}
|
|
|
|
if ( $isMobile ) {
|
|
|
|
$output->addModuleStyles( [
|
2022-07-27 15:12:07 +00:00
|
|
|
// Mobile overflow menu:
|
|
|
|
// ellipsis
|
|
|
|
'oojs-ui.styles.icons-interactions',
|
|
|
|
// edit
|
|
|
|
'oojs-ui.styles.icons-editing-core',
|
|
|
|
] );
|
|
|
|
}
|
2021-01-28 17:19:52 +00:00
|
|
|
$text = CommentFormatter::postprocessVisualEnhancements(
|
2022-07-27 15:12:07 +00:00
|
|
|
$text, $lang, $output->getUser(), $isMobile
|
2021-01-28 17:19:52 +00:00
|
|
|
);
|
|
|
|
}
|
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
|
|
|
|
2023-01-27 12:29:17 +00:00
|
|
|
/**
|
|
|
|
* OutputPageParserOutput hook handler
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageParserOutput
|
|
|
|
*
|
|
|
|
* @param OutputPage $output
|
|
|
|
* @param ParserOutput $pout ParserOutput instance being added in $output
|
|
|
|
* @return void This hook must not abort, it must return no value
|
|
|
|
*/
|
|
|
|
public function onOutputPageParserOutput( $output, $pout ): void {
|
2023-02-07 00:31:33 +00:00
|
|
|
// ParserOutputPostCacheTransform hook would be a better place to do this,
|
|
|
|
// so that when the ParserOutput is used directly without using this hook,
|
|
|
|
// we don't leave half-baked interface elements in it (see e.g. T292345, T294168).
|
|
|
|
// But that hook doesn't provide parameters that we need to render correctly
|
|
|
|
// (including the page title, interface language, and current user).
|
|
|
|
|
|
|
|
// This hook can be executed more than once per page view if the page content is composed from
|
|
|
|
// multiple sources!
|
|
|
|
|
|
|
|
$isMobile = $this->isMobile();
|
|
|
|
$lang = $output->getLanguage();
|
|
|
|
|
|
|
|
CommentFormatter::postprocessTableOfContents( $pout, $lang );
|
|
|
|
|
|
|
|
if (
|
|
|
|
CommentFormatter::isEmptyTalkPage( $pout ) &&
|
|
|
|
HookUtils::shouldDisplayEmptyState( $output->getContext() )
|
|
|
|
) {
|
|
|
|
$output->enableOOUI();
|
|
|
|
CommentFormatter::appendToEmptyTalkPage(
|
|
|
|
$pout, $this->getEmptyStateHtml( $output->getContext() )
|
|
|
|
);
|
|
|
|
$output->addBodyClasses( 'ext-discussiontools-emptystate-shown' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, HookUtils::VISUALENHANCEMENTS ) ) {
|
|
|
|
$subtitle = CommentFormatter::postprocessVisualEnhancementsSubtitle(
|
|
|
|
$pout, $lang, $output->getUser()
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $subtitle ) {
|
|
|
|
$output->addSubtitle( $subtitle );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $output->getSkin()->getSkinName() === 'minerva' ) {
|
|
|
|
$title = $output->getTitle();
|
|
|
|
|
|
|
|
if (
|
|
|
|
$title->isTalkPage() &&
|
|
|
|
HookUtils::isFeatureEnabledForOutput( $output, HookUtils::REPLYTOOL )
|
|
|
|
) {
|
|
|
|
if (
|
|
|
|
CommentFormatter::hasCommentsInLedeContent( $pout )
|
|
|
|
) {
|
|
|
|
// If there are comments in the lede section, we can't really separate them from other lede
|
|
|
|
// content, so keep the whole section visible.
|
|
|
|
$output->setProperty( 'DiscussionTools-ledeButton', false );
|
|
|
|
|
|
|
|
} elseif (
|
|
|
|
CommentFormatter::hasLedeContent( $pout ) &&
|
|
|
|
$output->getProperty( 'DiscussionTools-ledeButton' ) === null
|
|
|
|
) {
|
|
|
|
// If there is lede content and the lede button hasn't been disabled above, enable it.
|
|
|
|
$output->setProperty( 'DiscussionTools-ledeButton', true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-27 12:29:17 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-09-20 16:20:06 +00:00
|
|
|
if ( $action === 'edit' && (
|
|
|
|
HookUtils::shouldOpenNewTopicTool( $context ) ||
|
|
|
|
HookUtils::shouldDisplayEmptyState( $context )
|
|
|
|
) ) {
|
2021-07-28 10:36:58 +00:00
|
|
|
$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
|
|
|
|
$context = $article->getContext();
|
2021-09-20 16:20:06 +00:00
|
|
|
if ( !HookUtils::shouldDisplayEmptyState( $context ) ) {
|
2021-08-06 21:04:23 +00:00
|
|
|
// Our empty states are all about using the new topic tool, but
|
|
|
|
// expect to be on a talk page, so fall back if it's not
|
|
|
|
// available or if we're in a non-talk namespace that still has
|
|
|
|
// DT features enabled
|
2021-07-29 06:12:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-09-20 16:20:06 +00:00
|
|
|
|
|
|
|
$output = $context->getOutput();
|
2021-07-29 06:12:10 +00:00
|
|
|
$output->enableOOUI();
|
2022-02-04 19:30:33 +00:00
|
|
|
$output->disableClientCache();
|
2021-07-29 06:12:10 +00:00
|
|
|
|
2022-05-21 05:09:25 +00:00
|
|
|
$html = $this->getEmptyStateHtml( $context );
|
|
|
|
|
|
|
|
$output->addHTML(
|
|
|
|
// This being mw-parser-output is a lie, but makes the reply controller cope much better with everything
|
|
|
|
Html::rawElement( 'div', [ 'class' => 'mw-parser-output noarticletext' ], $html )
|
|
|
|
);
|
|
|
|
$output->addBodyClasses( 'ext-discussiontools-emptystate-shown' );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate HTML markup for the new topic tool's empty state, shown on talk pages that don't exist
|
|
|
|
* or have no topics.
|
|
|
|
*
|
|
|
|
* @param IContextSource $context
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
|
|
|
private function getEmptyStateHtml( IContextSource $context ): string {
|
2021-07-29 06:12:10 +00:00
|
|
|
$coreConfig = RequestContext::getMain()->getConfig();
|
|
|
|
$iconpath = $coreConfig->get( 'ExtensionAssetsPath' ) . '/DiscussionTools/images';
|
|
|
|
|
|
|
|
$dir = $context->getLanguage()->getDir();
|
|
|
|
$lang = $context->getLanguage()->getHtmlCode();
|
|
|
|
|
2021-08-25 19:10:53 +00:00
|
|
|
$titleMsg = false;
|
|
|
|
$descMsg = false;
|
|
|
|
$descParams = [];
|
|
|
|
$buttonMsg = 'discussiontools-emptystate-button';
|
2021-09-20 16:20:06 +00:00
|
|
|
$title = $context->getTitle();
|
2022-01-03 15:35:24 +00:00
|
|
|
if ( $title->getNamespace() == NS_USER_TALK && !$title->isSubpage() ) {
|
2021-08-25 19:10:53 +00:00
|
|
|
// This is a user talk page
|
|
|
|
$isIP = $this->userNameUtils->isIP( $title->getText() );
|
2022-05-21 05:09:25 +00:00
|
|
|
if ( $title->equals( $context->getUser()->getTalkPage() ) ) {
|
2021-08-25 19:10:53 +00:00
|
|
|
// This is your own user talk page
|
|
|
|
if ( $isIP ) {
|
|
|
|
// You're an IP editor, so this is only *sort of* your talk page
|
|
|
|
$titleMsg = 'discussiontools-emptystate-title-self-anon';
|
|
|
|
$descMsg = 'discussiontools-emptystate-desc-self-anon';
|
|
|
|
$query = $context->getRequest()->getValues();
|
|
|
|
unset( $query['title'] );
|
|
|
|
$descParams = [
|
|
|
|
SpecialPage::getTitleFor( 'CreateAccount' )->getFullURL( [
|
|
|
|
'returnto' => $context->getTitle()->getFullText(),
|
|
|
|
'returntoquery' => wfArrayToCgi( $query ),
|
|
|
|
] ),
|
|
|
|
SpecialPage::getTitleFor( 'Userlogin' )->getFullURL( [
|
|
|
|
'returnto' => $context->getTitle()->getFullText(),
|
|
|
|
'returntoquery' => wfArrayToCgi( $query ),
|
|
|
|
] ),
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
// You're logged in, this is very much your talk page
|
|
|
|
$titleMsg = 'discussiontools-emptystate-title-self';
|
|
|
|
$descMsg = 'discussiontools-emptystate-desc-self';
|
|
|
|
}
|
|
|
|
$buttonMsg = false;
|
|
|
|
} elseif ( $isIP ) {
|
|
|
|
// This is an IP editor
|
|
|
|
$titleMsg = 'discussiontools-emptystate-title-user-anon';
|
|
|
|
$descMsg = 'discussiontools-emptystate-desc-user-anon';
|
|
|
|
} else {
|
|
|
|
// This is any other user
|
|
|
|
$titleMsg = 'discussiontools-emptystate-title-user';
|
|
|
|
$descMsg = 'discussiontools-emptystate-desc-user';
|
|
|
|
}
|
2021-07-29 06:12:10 +00:00
|
|
|
} else {
|
2021-08-25 19:10:53 +00:00
|
|
|
// This is any other page on which DT is enabled
|
|
|
|
$titleMsg = 'discussiontools-emptystate-title';
|
|
|
|
$descMsg = 'discussiontools-emptystate-desc';
|
|
|
|
}
|
2022-04-08 17:22:10 +00:00
|
|
|
|
|
|
|
$text =
|
|
|
|
Html::rawElement( 'h3', [],
|
|
|
|
$context->msg( $titleMsg )->parse()
|
|
|
|
) .
|
|
|
|
Html::rawElement( 'div', [ 'class' => 'plainlinks' ],
|
|
|
|
$context->msg( $descMsg, $descParams )->parseAsBlock()
|
|
|
|
);
|
|
|
|
|
2021-08-25 19:10:53 +00:00
|
|
|
if ( $buttonMsg ) {
|
2022-04-08 17:22:10 +00:00
|
|
|
$text .= new ButtonWidget( [
|
2021-08-25 19:10:53 +00:00
|
|
|
'label' => $context->msg( $buttonMsg )->text(),
|
|
|
|
'href' => $title->getLocalURL( 'action=edit§ion=new' ),
|
|
|
|
'flags' => [ 'primary', 'progressive' ]
|
2022-04-08 17:22:10 +00:00
|
|
|
] );
|
2021-07-29 06:12:10 +00:00
|
|
|
}
|
2022-04-08 17:22:10 +00:00
|
|
|
|
2022-05-21 05:09:25 +00:00
|
|
|
$wrapped =
|
|
|
|
Html::rawElement( 'div', [ 'class' => 'ext-discussiontools-emptystate' ],
|
|
|
|
Html::rawElement( 'div', [ 'class' => 'ext-discussiontools-emptystate-text' ], $text ) .
|
2022-04-08 17:22:10 +00:00
|
|
|
Html::element( 'img', [
|
|
|
|
'src' => $iconpath . '/emptystate.svg',
|
2022-05-21 05:09:25 +00:00
|
|
|
'class' => 'ext-discussiontools-emptystate-logo',
|
2022-04-08 17:22:10 +00:00
|
|
|
// This is a purely decorative element
|
2022-05-21 05:09:25 +00:00
|
|
|
'alt' => '',
|
2022-04-08 17:22:10 +00:00
|
|
|
] )
|
2022-05-21 05:09:25 +00:00
|
|
|
);
|
2021-07-29 06:12:10 +00:00
|
|
|
|
2022-05-21 05:09:25 +00:00
|
|
|
return $wrapped;
|
2021-07-29 06:12:10 +00:00
|
|
|
}
|
2021-04-29 14:24:49 +00:00
|
|
|
|
2023-02-17 23:14:44 +00:00
|
|
|
/**
|
|
|
|
* @param SkinTemplate $sktemplate
|
|
|
|
* @param array &$links
|
|
|
|
* @return void
|
|
|
|
* @phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
|
|
|
|
*/
|
|
|
|
public function onSkinTemplateNavigation__Universal( $sktemplate, &$links ): void {
|
|
|
|
$output = $sktemplate->getOutput();
|
|
|
|
if ( HookUtils::isFeatureEnabledForOutput( $output, HookUtils::TOPICSUBSCRIPTION ) ) {
|
|
|
|
$user = $sktemplate->getUser();
|
|
|
|
$title = $sktemplate->getTitle();
|
|
|
|
$items = $this->subscriptionStore->getSubscriptionItemsForUser(
|
|
|
|
$user,
|
|
|
|
[ CommentUtils::getNewTopicsSubscriptionId( $title ) ]
|
|
|
|
);
|
|
|
|
$subscriptionItem = count( $items ) ? $items[ 0 ] : null;
|
|
|
|
$isSubscribed = $subscriptionItem && !$subscriptionItem->isMuted();
|
|
|
|
|
|
|
|
$context = $sktemplate->getContext();
|
|
|
|
$links['actions']['dt-page-subscribe'] = [
|
|
|
|
'text' => $context->msg( $isSubscribed ?
|
|
|
|
'discussiontools-newtopicssubscription-button-unsubscribe-label' :
|
|
|
|
'discussiontools-newtopicssubscription-button-subscribe-label'
|
|
|
|
),
|
|
|
|
'title' => $context->msg( $isSubscribed ?
|
|
|
|
'discussiontools-newtopicssubscription-button-unsubscribe-tooltip' :
|
|
|
|
'discussiontools-newtopicssubscription-button-subscribe-tooltip'
|
|
|
|
),
|
|
|
|
'data-mw-subscribed' => $isSubscribed ? '1' : '0',
|
|
|
|
// TODO: Provide a no-JS action?
|
|
|
|
'href' => '#',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 11:51:04 +00:00
|
|
|
/**
|
|
|
|
* @param Title $title Title object for the page the edit notices are for
|
|
|
|
* @param int $oldid Revision ID that the edit notices are for (or 0 for latest)
|
|
|
|
* @param array &$notices Array of notices. Keys are i18n message keys, values are
|
|
|
|
* parseAsBlock()ed messages.
|
|
|
|
* @return bool|void True or no return value to continue or false to abort
|
|
|
|
*/
|
|
|
|
public function onTitleGetEditNotices( $title, $oldid, &$notices ) {
|
|
|
|
$context = RequestContext::getMain();
|
|
|
|
|
|
|
|
if (
|
|
|
|
// Hint is active
|
|
|
|
$this->userOptionsLookup->getOption( $context->getUser(), 'discussiontools-newtopictool-hint-shown' ) &&
|
|
|
|
// Turning off the new topic tool also dismisses the hint
|
|
|
|
$this->userOptionsLookup->getOption( $context->getUser(), 'discussiontools-' . HookUtils::NEWTOPICTOOL ) &&
|
|
|
|
// Only show when following the link from the new topic tool, never on normal edit attempts.
|
|
|
|
// This can be called from within ApiVisualEditor, so we can't access most request parameters
|
|
|
|
// for the main request. However, we can access 'editintro', because it's passed to the API.
|
2022-08-26 10:07:15 +00:00
|
|
|
$context->getRequest()->getRawVal( 'editintro' ) === 'mw-dt-topic-hint'
|
2021-12-16 11:51:04 +00:00
|
|
|
) {
|
|
|
|
$context->getOutput()->enableOOUI();
|
|
|
|
|
|
|
|
$returnUrl = $title->getFullURL( [
|
|
|
|
'action' => 'edit',
|
|
|
|
'section' => 'new',
|
|
|
|
'dtenable' => '1',
|
|
|
|
] );
|
|
|
|
$prefUrl = SpecialPage::getTitleFor( 'Preferences' )
|
|
|
|
->createFragmentTarget( 'mw-prefsection-editing-discussion' )->getFullURL();
|
|
|
|
|
|
|
|
$topicHint = new MessageWidget( [
|
|
|
|
'label' => new HtmlSnippet( wfMessage( 'discussiontools-newtopic-legacy-hint-return' )
|
|
|
|
->params( $returnUrl, $prefUrl )->parse() ),
|
|
|
|
'icon' => 'article',
|
|
|
|
'classes' => [ 'ext-discussiontools-ui-newTopic-hint-return' ],
|
|
|
|
] );
|
|
|
|
|
|
|
|
// Add our notice above the built-in ones
|
|
|
|
$notices = [
|
|
|
|
'discussiontools-newtopic-legacy-hint-return' => (string)$topicHint,
|
|
|
|
] + $notices;
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 17:09:52 +00:00
|
|
|
}
|