2019-10-10 13:25:11 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* DiscussionTools extension hooks
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
* @license MIT
|
|
|
|
*/
|
2019-09-26 07:06:56 +00:00
|
|
|
|
2020-05-14 22:44:49 +00:00
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
|
|
|
use Action;
|
|
|
|
use ConfigException;
|
2020-12-14 18:52:47 +00:00
|
|
|
use ExtensionRegistry;
|
2020-02-20 20:39:35 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-11-02 19:12:22 +00:00
|
|
|
use MWExceptionHandler;
|
2020-05-14 22:44:49 +00:00
|
|
|
use OutputPage;
|
2020-12-16 16:08:56 +00:00
|
|
|
use PageProps;
|
2020-05-14 22:44:49 +00:00
|
|
|
use RecentChange;
|
|
|
|
use RequestContext;
|
|
|
|
use Skin;
|
2020-11-02 20:31:37 +00:00
|
|
|
use Throwable;
|
2020-12-16 16:08:56 +00:00
|
|
|
use Title;
|
2020-05-15 20:18:06 +00:00
|
|
|
use User;
|
2020-05-07 19:16:14 +00:00
|
|
|
use VisualEditorHooks;
|
2020-11-02 19:12:22 +00:00
|
|
|
use WebRequest;
|
2020-02-20 20:39:35 +00:00
|
|
|
|
2020-05-14 22:44:49 +00:00
|
|
|
class Hooks {
|
2019-10-28 16:52:37 +00:00
|
|
|
|
2020-02-06 20:54:58 +00:00
|
|
|
private static $tags = [
|
|
|
|
'discussiontools',
|
|
|
|
// Features:
|
|
|
|
'discussiontools-reply',
|
|
|
|
'discussiontools-edit',
|
|
|
|
'discussiontools-newtopic',
|
|
|
|
// Input methods:
|
|
|
|
'discussiontools-source',
|
|
|
|
'discussiontools-visual',
|
|
|
|
];
|
|
|
|
|
2020-05-14 23:09:20 +00:00
|
|
|
public static function onRegistration() : void {
|
2020-06-17 20:45:17 +00:00
|
|
|
// Use globals instead of Config. Accessing it so early blows up unrelated extensions (T255704).
|
2020-09-22 23:05:25 +00:00
|
|
|
global $wgLocaltimezone, $wgFragmentMode;
|
|
|
|
// HACK: Do not run these tests on CI as the globals are not configured.
|
|
|
|
if ( getenv( 'ZUUL_PROJECT' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-28 16:52:37 +00:00
|
|
|
// If $wgLocaltimezone isn't hard-coded, it is evaluated from the system
|
|
|
|
// timezone. On some systems this isn't guaranteed to be static, for example
|
|
|
|
// on Debian, GMT can get converted to UTC, instead of Europe/London.
|
|
|
|
// Timestamp parsing assumes that the timezone never changes.
|
2020-09-22 23:05:25 +00:00
|
|
|
if ( !$wgLocaltimezone ) {
|
2020-05-14 22:44:49 +00:00
|
|
|
throw new ConfigException( 'DiscussionTools requires $wgLocaltimezone to be set' );
|
2019-10-28 16:52:37 +00:00
|
|
|
}
|
2020-09-22 23:05:25 +00:00
|
|
|
// If $wgFragmentMode is set to use 'legacy' encoding, determining the IDs of our thread
|
|
|
|
// headings is harder, especially since the implementation is different in Parsoid.
|
|
|
|
if ( !isset( $wgFragmentMode[0] ) || $wgFragmentMode[0] !== 'html5' ) {
|
|
|
|
throw new ConfigException( 'DiscussionTools requires $wgFragmentMode to be set to ' .
|
|
|
|
"[ 'html5', 'legacy' ] or [ 'html5' ]" );
|
|
|
|
}
|
2019-10-28 16:52:37 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 19:11:07 +00:00
|
|
|
/**
|
2020-09-10 13:43:13 +00:00
|
|
|
* Check if the tool is available on a given page
|
2019-10-10 19:11:07 +00:00
|
|
|
*
|
2020-09-10 13:43:13 +00:00
|
|
|
* @param OutputPage $output
|
|
|
|
* @return bool
|
2019-10-10 19:11:07 +00:00
|
|
|
*/
|
2020-09-10 13:43:13 +00:00
|
|
|
private static function isAvailable( OutputPage $output ) : bool {
|
|
|
|
// Don't show on edit pages, history, etc.
|
|
|
|
if ( Action::getActionName( $output->getContext() ) !== 'view' ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-16 16:08:56 +00:00
|
|
|
$title = $output->getTitle();
|
|
|
|
// Don't show on pages without a Title
|
|
|
|
if ( !$title ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-14 18:52:47 +00:00
|
|
|
// Don't show on mobile
|
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) ) {
|
|
|
|
$mobFrontContext = MediaWikiServices::getInstance()->getService( 'MobileFrontend.Context' );
|
|
|
|
if ( $mobFrontContext->shouldDisplayMobileView() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 16:08:56 +00:00
|
|
|
return self::isAvailableForTitleAndUser(
|
|
|
|
$title,
|
|
|
|
$output->getUser(),
|
|
|
|
// overrideAllChecks
|
|
|
|
// Query parameter override to load on any wikitext page for testing
|
|
|
|
$output->getRequest()->getVal( 'dtenable' ) ||
|
|
|
|
// Extra hack for parses from API, where this parameter isn't passed to derivative requests
|
|
|
|
RequestContext::getMain()->getRequest()->getVal( 'dtenable' ),
|
|
|
|
// overrideUserEnabled
|
|
|
|
$output->getRequest()->getCookie( 'discussiontools-tempenable' ) ?: false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the tool should be available for a given title and user
|
|
|
|
*
|
|
|
|
* @param Title $title
|
|
|
|
* @param User $user
|
|
|
|
* @param bool $overrideAllChecks Override all checks, excluding those which make
|
|
|
|
* it technically impossible to load reply links (content model check).
|
|
|
|
* @param bool $overrideUserPrefs Override user preference check
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function isAvailableForTitleAndUser(
|
|
|
|
Title $title,
|
|
|
|
User $user,
|
|
|
|
bool $overrideAllChecks = false,
|
|
|
|
bool $overrideUserPrefs = false
|
|
|
|
) {
|
2020-09-10 13:43:13 +00:00
|
|
|
// Only wikitext pages (e.g. not Flow boards)
|
|
|
|
if ( $title->getContentModel() !== CONTENT_MODEL_WIKITEXT ) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-16 16:08:56 +00:00
|
|
|
if ( $overrideAllChecks ) {
|
2020-11-18 18:19:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-09-10 13:43:13 +00:00
|
|
|
|
2020-04-02 18:16:54 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
2020-05-20 18:40:15 +00:00
|
|
|
$optionsLookup = $services->getUserOptionsLookup();
|
2020-09-10 13:43:13 +00:00
|
|
|
|
|
|
|
$dtConfig = $services->getConfigFactory()->makeConfig( 'discussiontools' );
|
2020-09-10 13:52:33 +00:00
|
|
|
$isBeta = $dtConfig->get( 'DiscussionToolsBeta' );
|
2020-12-16 16:08:56 +00:00
|
|
|
$userEnabled = $overrideUserPrefs || (
|
2020-10-18 11:52:02 +00:00
|
|
|
$dtConfig->get( 'DiscussionToolsEnable' ) && (
|
|
|
|
( $isBeta && $optionsLookup->getOption( $user, 'discussiontools-betaenable' ) ) ||
|
|
|
|
( !$isBeta && $optionsLookup->getOption( $user, 'discussiontools-replytool' ) )
|
|
|
|
)
|
2020-02-18 18:28:17 +00:00
|
|
|
);
|
2020-02-04 21:06:13 +00:00
|
|
|
|
2020-12-16 16:08:56 +00:00
|
|
|
$props = PageProps::getInstance()->getProperties( $title, 'newsectionlink' );
|
|
|
|
$hasNewSectionLink = isset( $props[ $title->getArticleId() ] );
|
|
|
|
|
2020-09-10 13:43:13 +00:00
|
|
|
// 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)
|
2020-12-16 16:08:56 +00:00
|
|
|
$hasNewSectionLink
|
2020-09-10 13:43:13 +00:00
|
|
|
// TODO: Consider not loading if forceHideNewSectionLink is true.
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds DiscussionTools JS to the output.
|
|
|
|
*
|
|
|
|
* This is attached to the MediaWiki 'BeforePageDisplay' hook.
|
|
|
|
*
|
2020-11-11 08:31:59 +00:00
|
|
|
* @param OutputPage $output
|
2020-09-10 13:43:13 +00:00
|
|
|
* @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 ) ) {
|
2020-09-16 12:07:27 +00:00
|
|
|
$output->addModuleStyles( [
|
|
|
|
'ext.discussionTools.init.styles'
|
|
|
|
] );
|
2019-09-26 07:06:56 +00:00
|
|
|
$output->addModules( [
|
|
|
|
'ext.discussionTools.init'
|
|
|
|
] );
|
2020-05-07 19:16:14 +00:00
|
|
|
|
2020-09-10 13:43:13 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$optionsLookup = $services->getUserOptionsLookup();
|
|
|
|
$req = $output->getRequest();
|
|
|
|
$user = $output->getUser();
|
2020-05-20 18:40:15 +00:00
|
|
|
$editor = $optionsLookup->getOption( $user, 'discussiontools-editmode' );
|
2020-05-07 19:16:14 +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
|
|
|
|
);
|
|
|
|
}
|
2019-10-10 19:11:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-24 19:22:29 +00:00
|
|
|
|
2020-04-13 15:30:20 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-05-14 23:09:20 +00:00
|
|
|
public static function onResourceLoaderGetConfigVars( array &$vars, string $skinName ) : void {
|
2020-04-13 15:30:20 +00:00
|
|
|
$dtConfig = MediaWikiServices::getInstance()->getConfigFactory()
|
|
|
|
->makeConfig( 'discussiontools' );
|
|
|
|
|
|
|
|
$vars['wgDTSchemaEditAttemptStepSamplingRate'] =
|
|
|
|
$dtConfig->get( 'DTSchemaEditAttemptStepSamplingRate' );
|
|
|
|
$vars['wgDTSchemaEditAttemptStepOversample'] =
|
|
|
|
$dtConfig->get( 'DTSchemaEditAttemptStepOversample' );
|
|
|
|
}
|
|
|
|
|
2020-09-16 12:07:27 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2020-11-16 15:50:46 +00:00
|
|
|
$start = microtime( true );
|
2020-11-02 19:12:22 +00:00
|
|
|
try {
|
|
|
|
// Add reply links and hidden data about comment ranges.
|
2020-11-16 15:50:46 +00:00
|
|
|
$newText = CommentFormatter::addReplyLinks( $text );
|
2020-11-02 20:31:37 +00:00
|
|
|
} catch ( Throwable $e ) {
|
|
|
|
// Catch errors, so that they don't cause the entire page to not display.
|
2020-11-02 19:12:22 +00:00
|
|
|
// Log it and add the request ID in a comment to make it easier to find in the logs.
|
|
|
|
MWExceptionHandler::logException( $e );
|
|
|
|
|
|
|
|
$requestId = htmlspecialchars( WebRequest::getRequestId() );
|
|
|
|
$info = "<!-- [$requestId] DiscussionTools could not add reply links on this page -->";
|
|
|
|
$text .= "\n" . $info;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$text = $newText;
|
2020-09-16 12:07:27 +00:00
|
|
|
|
|
|
|
$duration = microtime( true ) - $start;
|
|
|
|
|
|
|
|
$stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
|
|
|
|
$stats->timing( 'discussiontools.addReplyLinks', $duration * 1000 );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-07 19:16:14 +00:00
|
|
|
/**
|
|
|
|
* Handler for the GetPreferences hook, to add and hide user preferences as configured
|
|
|
|
*
|
2020-11-11 08:31:59 +00:00
|
|
|
* @param User $user
|
|
|
|
* @param array &$preferences
|
2020-05-07 19:16:14 +00:00
|
|
|
*/
|
|
|
|
public static function onGetPreferences( User $user, array &$preferences ) {
|
2020-10-15 15:04:36 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$dtConfig = $services->getConfigFactory()->makeConfig( 'discussiontools' );
|
2020-09-10 13:52:33 +00:00
|
|
|
|
|
|
|
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',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-10-28 18:03:22 +00:00
|
|
|
$preferences['discussiontools-showadvanced'] = [
|
|
|
|
'type' => 'api',
|
|
|
|
];
|
2020-10-15 15:04:36 +00:00
|
|
|
|
2020-05-07 19:16:14 +00:00
|
|
|
$preferences['discussiontools-editmode'] = [
|
|
|
|
'type' => 'api',
|
|
|
|
'validation-callback' => function ( $value ) {
|
|
|
|
return in_array( $value, [ '', 'source', 'visual' ], true );
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-02-18 18:28:17 +00:00
|
|
|
/**
|
|
|
|
* Handler for the GetBetaPreferences hook, to add and hide user beta preferences as configured
|
|
|
|
*
|
2020-11-11 08:31:59 +00:00
|
|
|
* @param User $user
|
|
|
|
* @param array &$preferences
|
2020-02-18 18:28:17 +00:00
|
|
|
*/
|
2020-05-14 23:09:20 +00:00
|
|
|
public static function onGetBetaPreferences( User $user, array &$preferences ) : void {
|
2020-02-18 18:28:17 +00:00
|
|
|
$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
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 19:22:29 +00:00
|
|
|
/**
|
2020-02-06 20:54:58 +00:00
|
|
|
* Implements the ListDefinedTags and ChangeTagsListActive hooks, to
|
|
|
|
* populate core Special:Tags with the change tags in use by DiscussionTools.
|
2020-01-24 19:22:29 +00:00
|
|
|
*
|
|
|
|
* @param array &$tags Available change tags.
|
|
|
|
*/
|
2020-05-14 23:09:20 +00:00
|
|
|
public static function onListDefinedTags( array &$tags ) : void {
|
2020-02-06 20:54:58 +00:00
|
|
|
$tags = array_merge( $tags, static::$tags );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-10 15:18:31 +00:00
|
|
|
* Implements the RecentChange_save hook, to add an allowed set of changetags
|
2020-02-06 20:54:58 +00:00
|
|
|
* to edits.
|
|
|
|
*
|
|
|
|
* @param RecentChange $recentChange
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-05-14 23:09:20 +00:00
|
|
|
public static function onRecentChangeSave( RecentChange $recentChange ) : bool {
|
2020-02-06 20:54:58 +00:00
|
|
|
// 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;
|
2020-01-24 19:22:29 +00:00
|
|
|
}
|
2019-10-10 19:11:07 +00:00
|
|
|
}
|