getContext() ) !== 'view' ) { return false; } $title = $output->getTitle(); // Only wikitext pages (e.g. not Flow boards) if ( $title->getContentModel() !== CONTENT_MODEL_WIKITEXT ) { return false; } // Query parameter override to load on any wikitext page for testing if ( $output->getRequest()->getVal( 'dtenable' ) ) { return true; } $user = $output->getUser(); $services = MediaWikiServices::getInstance(); $optionsLookup = $services->getUserOptionsLookup(); $dtConfig = $services->getConfigFactory()->makeConfig( 'discussiontools' ); $isBeta = $dtConfig->get( 'DiscussionToolsBeta' ); $userEnabled = $dtConfig->get( 'DiscussionToolsEnable' ) && ( ( $isBeta && $optionsLookup->getOption( $user, 'discussiontools-betaenable' ) ) || ( !$isBeta && $optionsLookup->getOption( $user, 'discussiontools-replytool' ) ) ); // Finally check the user has the tool enabled and that the page // supports discussions. return $userEnabled && ( // `wantSignatures` includes talk pages $services->getNamespaceInfo()->wantSignatures( $title->getNamespace() ) || // Treat pages with __NEWSECTIONLINK__ as talk pages (T245890) $output->showNewSectionLink() // TODO: Consider not loading if forceHideNewSectionLink is true. ); } /** * Adds DiscussionTools JS to the output. * * This is attached to the MediaWiki 'BeforePageDisplay' hook. * * @param OutputPage $output The page view. * @param Skin $skin The skin that's going to build the UI. */ public static function onBeforePageDisplay( OutputPage $output, Skin $skin ) : void { if ( self::isAvailable( $output ) ) { $output->addModuleStyles( [ 'ext.discussionTools.init.styles' ] ); $output->addModules( [ 'ext.discussionTools.init' ] ); $services = MediaWikiServices::getInstance(); $optionsLookup = $services->getUserOptionsLookup(); $req = $output->getRequest(); $user = $output->getUser(); $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 ); } } } /** * Set static (not request-specific) JS configuration variables * * @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars * @param array &$vars Array of variables to be added into the output of the startup module * @param string $skinName Current skin name to restrict config variables to a certain skin */ public static function onResourceLoaderGetConfigVars( array &$vars, string $skinName ) : void { $dtConfig = MediaWikiServices::getInstance()->getConfigFactory() ->makeConfig( 'discussiontools' ); $vars['wgDTSchemaEditAttemptStepSamplingRate'] = $dtConfig->get( 'DTSchemaEditAttemptStepSamplingRate' ); $vars['wgDTSchemaEditAttemptStepOversample'] = $dtConfig->get( 'DTSchemaEditAttemptStepOversample' ); } /** * OutputPageBeforeHTML hook handler * @see https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageBeforeHTML * * @param OutputPage $output The OutputPage object to which wikitext is added * @param string &$text The HTML to be wrapped inside the #mw-content-text element * @return bool */ public static function onOutputPageBeforeHTML( OutputPage $output, string &$text ) : bool { // TODO: This is based on the current user, is there an issue with caching? if ( !self::isAvailable( $output ) ) { return true; } $formatter = new CommentFormatter( $text ); $start = microtime( true ); $formatter->addReplyLinks(); $text = $formatter->getText(); $duration = microtime( true ) - $start; $stats = MediaWikiServices::getInstance()->getStatsdDataFactory(); $stats->timing( 'discussiontools.addReplyLinks', $duration * 1000 ); return true; } /** * Handler for the GetPreferences hook, to add and hide user preferences as configured * * @param User $user The user object * @param array &$preferences Their preferences object */ public static function onGetPreferences( User $user, array &$preferences ) { $services = MediaWikiServices::getInstance(); $dtConfig = $services->getConfigFactory()->makeConfig( 'discussiontools' ); $optionsLookup = $services->getUserOptionsLookup(); if ( $dtConfig->get( 'DiscussionToolsEnable' ) && !$dtConfig->get( 'DiscussionToolsBeta' ) ) { $preferences['discussiontools-replytool'] = [ 'type' => 'toggle', 'label-message' => 'discussiontools-preference-replytool', 'help-message' => 'discussiontools-preference-replytool-help', 'section' => 'editing/discussion', ]; } if ( $dtConfig->get( 'DiscussionToolsEnable' ) && ( // The 'hide-if' mechanism can't hide the empty section name, so instead hide the option // here if we're in beta mode and the user has not enabled the beta feature !$dtConfig->get( 'DiscussionToolsBeta' ) || $optionsLookup->getOption( $user, 'discussiontools-betaenable' ) ) ) { $basePrefName = $dtConfig->get( 'DiscussionToolsBeta' ) ? 'discussiontools-betaenable' : 'discussiontools-replytool'; $preferences['discussiontools-showadvanced'] = [ 'type' => 'toggle', 'label-message' => 'discussiontools-preference-showadvanced', 'section' => 'editing/discussion', 'hide-if' => [ '!==', $basePrefName, '1' ], ]; } $preferences['discussiontools-editmode'] = [ 'type' => 'api', 'validation-callback' => function ( $value ) { return in_array( $value, [ '', 'source', 'visual' ], true ); }, ]; } /** * Handler for the GetBetaPreferences hook, to add and hide user beta preferences as configured * * @param User $user The user object * @param array &$preferences Their preferences object */ public static function onGetBetaPreferences( User $user, array &$preferences ) : void { $coreConfig = RequestContext::getMain()->getConfig(); $iconpath = $coreConfig->get( 'ExtensionAssetsPath' ) . '/DiscussionTools/images'; $dtConfig = MediaWikiServices::getInstance()->getConfigFactory() ->makeConfig( 'discussiontools' ); if ( $dtConfig->get( 'DiscussionToolsEnable' ) && $dtConfig->get( 'DiscussionToolsBeta' ) ) { $preferences['discussiontools-betaenable'] = [ 'version' => '1.0', 'label-message' => 'discussiontools-preference-label', 'desc-message' => 'discussiontools-preference-description', 'screenshot' => [ 'ltr' => "$iconpath/betafeatures-icon-DiscussionTools-ltr.svg", 'rtl' => "$iconpath/betafeatures-icon-DiscussionTools-rtl.svg", ], 'info-message' => 'discussiontools-preference-info-link', 'discussion-message' => 'discussiontools-preference-discussion-link', 'requirements' => [ 'javascript' => true ] ]; } } /** * Implements the ListDefinedTags and ChangeTagsListActive hooks, to * populate core Special:Tags with the change tags in use by DiscussionTools. * * @param array &$tags Available change tags. */ public static function onListDefinedTags( array &$tags ) : void { $tags = array_merge( $tags, static::$tags ); } /** * Implements the RecentChange_save hook, to add an allowed set of changetags * to edits. * * @param RecentChange $recentChange * @return bool */ public static function onRecentChangeSave( RecentChange $recentChange ) : bool { // only apply to api edits, since there's no case where discussiontools // should be using the form-submit method. if ( !defined( 'MW_API' ) ) { return true; } $request = RequestContext::getMain()->getRequest(); $tags = explode( ',', $request->getVal( 'dttags' ) ); $tags = array_values( array_intersect( $tags, static::$tags ) ); if ( $tags ) { $recentChange->addTags( $tags ); } return true; } }