2021-01-29 17:09:52 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* DiscussionTools preference hooks
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Hooks;
|
|
|
|
|
2021-09-07 20:51:35 +00:00
|
|
|
use ConfigFactory;
|
2021-08-30 19:38:22 +00:00
|
|
|
use Html;
|
2021-09-16 04:25:30 +00:00
|
|
|
use MediaWiki\Auth\Hook\LocalUserCreatedHook;
|
2021-08-30 19:38:22 +00:00
|
|
|
use MediaWiki\Linker\LinkRenderer;
|
2021-01-29 17:09:52 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
use MediaWiki\Preferences\Hook\GetPreferencesHook;
|
|
|
|
use RequestContext;
|
2021-08-30 19:38:22 +00:00
|
|
|
use SpecialPage;
|
2021-01-29 17:09:52 +00:00
|
|
|
use User;
|
|
|
|
|
|
|
|
class PreferenceHooks implements
|
2021-09-16 04:25:30 +00:00
|
|
|
LocalUserCreatedHook,
|
2021-01-29 17:09:52 +00:00
|
|
|
GetPreferencesHook
|
|
|
|
{
|
2021-09-07 20:51:35 +00:00
|
|
|
/** @var ConfigFactory */
|
|
|
|
private $configFactory;
|
|
|
|
|
2021-08-30 19:38:22 +00:00
|
|
|
/** @var LinkRenderer */
|
|
|
|
private $linkRenderer;
|
|
|
|
|
2021-09-07 20:51:35 +00:00
|
|
|
/**
|
|
|
|
* @param ConfigFactory $configFactory
|
2021-08-30 19:38:22 +00:00
|
|
|
* @param LinkRenderer $linkRenderer
|
2021-09-07 20:51:35 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2021-08-30 19:38:22 +00:00
|
|
|
ConfigFactory $configFactory,
|
|
|
|
LinkRenderer $linkRenderer
|
2021-09-07 20:51:35 +00:00
|
|
|
) {
|
|
|
|
$this->configFactory = $configFactory;
|
2021-08-30 19:38:22 +00:00
|
|
|
$this->linkRenderer = $linkRenderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rename a key in an array while preserving the order of associative array keys.
|
|
|
|
*
|
|
|
|
* @param array $array
|
|
|
|
* @param string $from
|
|
|
|
* @param string $to
|
|
|
|
* @return array Modified array
|
|
|
|
*/
|
|
|
|
private static function arrayRenameKey( array $array, string $from, string $to ): array {
|
|
|
|
$out = [];
|
|
|
|
foreach ( $array as $key => $value ) {
|
|
|
|
if ( $key === $from ) {
|
|
|
|
$key = $to;
|
|
|
|
}
|
|
|
|
$out[$key] = $value;
|
|
|
|
}
|
|
|
|
return $out;
|
2021-09-07 20:51:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 17:09:52 +00:00
|
|
|
/**
|
|
|
|
* Handler for the GetPreferences hook, to add and hide user preferences as configured
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param array &$preferences
|
|
|
|
*/
|
|
|
|
public function onGetPreferences( $user, &$preferences ) {
|
2021-08-18 18:14:39 +00:00
|
|
|
if ( HookUtils::isFeatureAvailableToUser( $user ) ) {
|
|
|
|
$preferences['discussiontools-summary'] = [
|
|
|
|
'type' => 'info',
|
|
|
|
'default' => wfMessage( 'discussiontools-preference-summary' )->parse(),
|
|
|
|
'raw' => true,
|
|
|
|
'section' => 'editing/discussion',
|
|
|
|
];
|
|
|
|
}
|
2021-02-17 17:16:17 +00:00
|
|
|
foreach ( HookUtils::FEATURES as $feature ) {
|
2022-04-19 17:43:49 +00:00
|
|
|
if (
|
|
|
|
$feature === HookUtils::VISUALENHANCEMENTS_REPLY ||
|
|
|
|
$feature === HookUtils::VISUALENHANCEMENTS_PAGEFRAME
|
|
|
|
) {
|
2022-04-21 13:08:59 +00:00
|
|
|
// Feature is never user-configurable
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-17 17:16:17 +00:00
|
|
|
if ( HookUtils::isFeatureAvailableToUser( $user, $feature ) ) {
|
|
|
|
$preferences["discussiontools-$feature"] = [
|
|
|
|
'type' => 'toggle',
|
|
|
|
'label-message' => "discussiontools-preference-$feature",
|
|
|
|
'help-message' => "discussiontools-preference-$feature-help",
|
|
|
|
'section' => 'editing/discussion',
|
|
|
|
];
|
2022-01-07 21:33:46 +00:00
|
|
|
|
2022-06-07 09:59:15 +00:00
|
|
|
// Option to enable/disable new topic tool on pages that haven't been created
|
|
|
|
// (it's inside this loop to place the options in a nice order)
|
|
|
|
if ( $feature === HookUtils::NEWTOPICTOOL ) {
|
|
|
|
$preferences["discussiontools-newtopictool-createpage"] = [
|
|
|
|
'type' => 'radio',
|
|
|
|
'cssclass' => 'mw-htmlform-checkradio-indent',
|
|
|
|
'label-message' => 'discussiontools-preference-newtopictool-createpage',
|
|
|
|
'options-messages' => [
|
|
|
|
'discussiontools-preference-newtopictool-createpage-newtopictool' => 1,
|
|
|
|
'discussiontools-preference-newtopictool-createpage-editor' => 0,
|
|
|
|
],
|
|
|
|
'disable-if' => [ '===', 'discussiontools-' . HookUtils::NEWTOPICTOOL, '' ],
|
|
|
|
'section' => 'editing/discussion',
|
|
|
|
];
|
|
|
|
}
|
2022-01-19 20:39:54 +00:00
|
|
|
|
|
|
|
// Make this option unavailable when a conflicting Convenient Discussions gadget exists
|
|
|
|
// (we can't use 'disable-if' or 'hide-if', because they don't let us change the labels).
|
|
|
|
if ( HookUtils::featureConflictsWithGadget( $user, $feature ) ) {
|
|
|
|
$preferences["discussiontools-$feature"]['disabled'] = true;
|
|
|
|
$preferences["discussiontools-$feature"]['help-message'] =
|
|
|
|
[ 'discussiontools-preference-gadget-conflict', 'Special:Preferences#mw-prefsection-gadgets' ];
|
|
|
|
}
|
2022-01-07 21:33:46 +00:00
|
|
|
}
|
2021-01-29 17:09:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 14:38:13 +00:00
|
|
|
if ( isset( $preferences['discussiontools-' . HookUtils::SOURCEMODETOOLBAR] ) && (
|
|
|
|
isset( $preferences['discussiontools-' . HookUtils::REPLYTOOL] ) ||
|
|
|
|
isset( $preferences['discussiontools-' . HookUtils::NEWTOPICTOOL] )
|
|
|
|
) ) {
|
2022-06-01 19:36:30 +00:00
|
|
|
// Disable this option when it would have no effect
|
2021-03-08 13:25:59 +00:00
|
|
|
// (both reply tool and new topic tool are disabled)
|
2022-06-01 19:36:30 +00:00
|
|
|
$preferences['discussiontools-' . HookUtils::SOURCEMODETOOLBAR]['disable-if'] = [ 'AND' ];
|
2022-02-09 14:38:13 +00:00
|
|
|
|
|
|
|
if ( isset( $preferences['discussiontools-' . HookUtils::REPLYTOOL] ) ) {
|
2022-06-01 19:36:30 +00:00
|
|
|
$preferences['discussiontools-' . HookUtils::SOURCEMODETOOLBAR]['disable-if'][] = [
|
2022-02-09 14:38:13 +00:00
|
|
|
'===', 'discussiontools-' . HookUtils::REPLYTOOL, ''
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if ( isset( $preferences['discussiontools-' . HookUtils::NEWTOPICTOOL] ) ) {
|
2022-06-01 19:36:30 +00:00
|
|
|
$preferences['discussiontools-' . HookUtils::SOURCEMODETOOLBAR]['disable-if'][] = [
|
2022-02-09 14:38:13 +00:00
|
|
|
'===', 'discussiontools-' . HookUtils::NEWTOPICTOOL, ''
|
|
|
|
];
|
|
|
|
}
|
2021-03-08 13:25:59 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 14:38:13 +00:00
|
|
|
if ( isset( $preferences['discussiontools-' . HookUtils::AUTOTOPICSUB] ) &&
|
|
|
|
isset( $preferences['discussiontools-' . HookUtils::TOPICSUBSCRIPTION] )
|
|
|
|
) {
|
2022-06-01 19:36:30 +00:00
|
|
|
// Disable automatic subscriptions when subscriptions are disabled
|
|
|
|
$preferences['discussiontools-' . HookUtils::AUTOTOPICSUB]['disable-if'] = [
|
2021-08-17 20:23:27 +00:00
|
|
|
'===', 'discussiontools-' . HookUtils::TOPICSUBSCRIPTION, ''
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-01-29 17:09:52 +00:00
|
|
|
$preferences['discussiontools-showadvanced'] = [
|
|
|
|
'type' => 'api',
|
|
|
|
];
|
2021-11-15 06:17:21 +00:00
|
|
|
$preferences['discussiontools-newtopictool-opened'] = [
|
|
|
|
'type' => 'api',
|
|
|
|
];
|
2021-12-10 17:39:34 +00:00
|
|
|
$preferences['discussiontools-newtopictool-hint-shown'] = [
|
|
|
|
'type' => 'api',
|
|
|
|
];
|
2021-08-23 20:23:37 +00:00
|
|
|
$preferences['discussiontools-seenautotopicsubpopup'] = [
|
|
|
|
'type' => 'api',
|
|
|
|
];
|
2021-01-29 17:09:52 +00:00
|
|
|
|
2021-09-07 20:51:35 +00:00
|
|
|
$dtConfig = $this->configFactory->makeConfig( 'discussiontools' );
|
2021-01-29 17:09:52 +00:00
|
|
|
if (
|
|
|
|
!$dtConfig->get( 'DiscussionToolsEnable' ) ||
|
|
|
|
!$dtConfig->get( 'DiscussionToolsBeta' )
|
|
|
|
) {
|
|
|
|
// When out of beta, preserve the user preference in case we
|
|
|
|
// bring back the beta feature for a new sub-feature. (T272071)
|
|
|
|
$preferences['discussiontools-betaenable'] = [
|
|
|
|
'type' => 'api'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$preferences['discussiontools-editmode'] = [
|
|
|
|
'type' => 'api',
|
2021-05-05 06:59:38 +00:00
|
|
|
'validation-callback' => static function ( $value ) {
|
2021-01-29 17:09:52 +00:00
|
|
|
return in_array( $value, [ '', 'source', 'visual' ], true );
|
|
|
|
},
|
|
|
|
];
|
2021-08-30 19:38:22 +00:00
|
|
|
|
|
|
|
// Add a link to Special:TopicSubscriptions to the Echo preferences matrix
|
|
|
|
$categoryMessage = wfMessage( 'echo-category-title-dt-subscription' )->numParams( 1 )->escaped();
|
|
|
|
$categoryMessageExtra = $categoryMessage .
|
|
|
|
Html::element( 'br' ) .
|
|
|
|
wfMessage( 'parentheses' )->rawParams(
|
|
|
|
$this->linkRenderer->makeLink(
|
|
|
|
SpecialPage::getTitleFor( 'TopicSubscriptions' ),
|
|
|
|
wfMessage( 'discussiontools-topicsubscription-preferences-editsubscriptions' )->text()
|
|
|
|
)
|
|
|
|
)->escaped();
|
|
|
|
if ( isset( $preferences['echo-subscriptions']['rows'] ) ) {
|
2022-06-09 13:51:33 +00:00
|
|
|
$preferences['echo-subscriptions']['rows'] = static::arrayRenameKey(
|
2021-08-30 19:38:22 +00:00
|
|
|
$preferences['echo-subscriptions']['rows'],
|
|
|
|
$categoryMessage,
|
|
|
|
$categoryMessageExtra
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( isset( $preferences['echo-subscriptions']['tooltips'] ) ) {
|
2022-06-09 13:51:33 +00:00
|
|
|
$preferences['echo-subscriptions']['tooltips'] = static::arrayRenameKey(
|
2021-08-30 19:38:22 +00:00
|
|
|
// Phan insists that this key doesn't exist, even though we just checked with isset()
|
|
|
|
// @phan-suppress-next-line PhanTypeInvalidDimOffset, PhanTypeMismatchArgument
|
|
|
|
$preferences['echo-subscriptions']['tooltips'],
|
|
|
|
$categoryMessage,
|
|
|
|
$categoryMessageExtra
|
|
|
|
);
|
|
|
|
}
|
2021-01-29 17:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the GetBetaFeaturePreferences hook, to add and hide user beta preferences as configured
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param array &$preferences
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public static function onGetBetaFeaturePreferences( User $user, array &$preferences ): void {
|
2021-01-29 17:09:52 +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
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2021-09-16 04:25:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for LocalUserCreated hook.
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/LocalUserCreated
|
|
|
|
* @param User $user User object for the created user
|
|
|
|
* @param bool $autocreated Whether this was an auto-creation
|
|
|
|
* @return bool|void True or no return value to continue or false to abort
|
|
|
|
*/
|
|
|
|
public function onLocalUserCreated( $user, $autocreated ) {
|
|
|
|
if ( !$autocreated ) {
|
|
|
|
$userOptionsManager = MediaWikiServices::getInstance()->getUserOptionsManager();
|
2021-11-09 21:33:59 +00:00
|
|
|
// We want new users to be created with email-subscriptions to our notifications enabled
|
2021-09-16 04:25:30 +00:00
|
|
|
$userOptionsManager->setOption( $user, 'echo-subscriptions-email-dt-subscription', true );
|
2021-11-09 21:33:59 +00:00
|
|
|
// The auto topic subscription feature is disabled by default for existing users, but
|
|
|
|
// we enable it for new users (T294398).
|
|
|
|
// This can only occur when the feature is available for everyone; when it's in beta,
|
|
|
|
// the new user won't have the beta enabled, so it'll never be available here.
|
|
|
|
if ( HookUtils::isFeatureAvailableToUser( $user, HookUtils::AUTOTOPICSUB ) ) {
|
|
|
|
$userOptionsManager->setOption( $user, 'discussiontools-' . HookUtils::AUTOTOPICSUB, 1 );
|
|
|
|
}
|
2021-09-16 04:25:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 17:09:52 +00:00
|
|
|
}
|