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-02-20 20:39:35 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
2019-10-10 19:11:07 +00:00
|
|
|
class DiscussionToolsHooks {
|
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',
|
|
|
|
];
|
|
|
|
|
2019-10-28 16:52:37 +00:00
|
|
|
public static function onRegistration() {
|
2020-02-20 20:39:35 +00:00
|
|
|
$coreConfig = RequestContext::getMain()->getConfig();
|
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.
|
|
|
|
//
|
|
|
|
// HACK: Do not run this test on CI as $wgLocaltimezone is not configured.
|
2020-02-20 20:39:35 +00:00
|
|
|
if ( !$coreConfig->get( 'Localtimezone' ) && !getenv( 'ZUUL_PROJECT' ) ) {
|
2019-10-28 16:52:37 +00:00
|
|
|
throw new \ConfigException( 'DiscussionTools requires $wgLocaltimezone to be set' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 19:11:07 +00:00
|
|
|
/**
|
|
|
|
* 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 ) {
|
2020-02-20 20:39:35 +00:00
|
|
|
$dtConfig = MediaWikiServices::getInstance()->getConfigFactory()
|
|
|
|
->makeConfig( 'discussiontools' );
|
2019-10-10 19:11:07 +00:00
|
|
|
$title = $output->getTitle();
|
2019-12-09 14:43:39 +00:00
|
|
|
$actionName = Action::getActionName( $output->getContext() );
|
2020-02-04 21:06:13 +00:00
|
|
|
$req = $output->getRequest();
|
2020-02-18 18:28:17 +00:00
|
|
|
$user = $skin->getUser();
|
|
|
|
$enabled = $dtConfig->get( 'DiscussionToolsEnable' ) && (
|
|
|
|
!$dtConfig->get( 'DiscussionToolsBeta' ) ||
|
|
|
|
$user->getOption( 'discussiontools-betaenable' )
|
|
|
|
);
|
2020-02-04 21:06:13 +00:00
|
|
|
|
2019-10-10 19:11:07 +00:00
|
|
|
if (
|
2020-04-01 20:48:51 +00:00
|
|
|
// Don't show on edit pages, history, etc.
|
|
|
|
$actionName === 'view' &&
|
2019-10-10 19:11:07 +00:00
|
|
|
// Only wikitext pages (e.g. not Flow boards)
|
|
|
|
$title->getContentModel() === CONTENT_MODEL_WIKITEXT &&
|
2020-02-04 21:06:13 +00:00
|
|
|
(
|
|
|
|
// Query parameter to load on any wikitext page for testing
|
|
|
|
$req->getVal( 'dtenable' ) ||
|
|
|
|
// If configured, load on all talk pages
|
2020-02-18 18:28:17 +00:00
|
|
|
( $enabled && $title->isTalkPage() )
|
2020-02-04 21:06:13 +00:00
|
|
|
// TODO: Allow non talk pages to be treated as talk pages
|
|
|
|
// using a magic word.
|
|
|
|
)
|
2019-10-10 19:11:07 +00:00
|
|
|
) {
|
2019-09-26 07:06:56 +00:00
|
|
|
$output->addModules( [
|
|
|
|
'ext.discussionTools.init'
|
|
|
|
] );
|
2019-10-10 19:11:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-24 19:22:29 +00:00
|
|
|
|
2020-02-18 18:28:17 +00:00
|
|
|
/**
|
|
|
|
* 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 ) {
|
|
|
|
$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.
|
|
|
|
*/
|
|
|
|
public static function onListDefinedTags( &$tags ) {
|
2020-02-06 20:54:58 +00:00
|
|
|
$tags = array_merge( $tags, static::$tags );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements the RecentChange_save hook, to add a whitelisted set of changetags
|
|
|
|
* to edits.
|
|
|
|
*
|
|
|
|
* @param RecentChange $recentChange
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onRecentChangeSave( RecentChange $recentChange ) {
|
|
|
|
// 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
|
|
|
}
|