2021-01-29 17:09:52 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* DiscussionTools preference hooks
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Hooks;
|
|
|
|
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\Config\Config;
|
|
|
|
use MediaWiki\Config\ConfigFactory;
|
|
|
|
use MediaWiki\Html\Html;
|
2021-08-30 19:38:22 +00:00
|
|
|
use MediaWiki\Linker\LinkRenderer;
|
2021-01-29 17:09:52 +00:00
|
|
|
use MediaWiki\Preferences\Hook\GetPreferencesHook;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\SpecialPage\SpecialPage;
|
|
|
|
use MediaWiki\User\User;
|
2021-01-29 17:09:52 +00:00
|
|
|
|
|
|
|
class PreferenceHooks implements
|
|
|
|
GetPreferencesHook
|
|
|
|
{
|
2021-09-07 20:51:35 +00:00
|
|
|
|
2022-10-21 19:34:18 +00:00
|
|
|
private Config $config;
|
|
|
|
private LinkRenderer $linkRenderer;
|
2021-08-30 19:38:22 +00:00
|
|
|
|
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
|
|
|
) {
|
2022-09-02 22:24:20 +00:00
|
|
|
$this->config = $configFactory->makeConfig( 'discussiontools' );
|
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',
|
2023-11-02 16:29:54 +00:00
|
|
|
// The following messages are used here:
|
|
|
|
// * discussiontools-preference-autotopicsub
|
|
|
|
// * discussiontools-preference-newtopictool
|
|
|
|
// * discussiontools-preference-replytool
|
|
|
|
// * discussiontools-preference-sourcemodetoolbar
|
|
|
|
// * discussiontools-preference-topicsubscription
|
|
|
|
// * discussiontools-preference-visualenhancements
|
2021-02-17 17:16:17 +00:00
|
|
|
'label-message' => "discussiontools-preference-$feature",
|
2023-11-02 16:29:54 +00:00
|
|
|
// The following messages are used here:
|
|
|
|
// * discussiontools-preference-autotopicsub-help
|
|
|
|
// * discussiontools-preference-newtopictool-help
|
|
|
|
// * discussiontools-preference-replytool-help
|
|
|
|
// * discussiontools-preference-sourcemodetoolbar-help
|
|
|
|
// * discussiontools-preference-topicsubscription-help
|
|
|
|
// * discussiontools-preference-visualenhancements-help
|
2021-02-17 17:16:17 +00:00
|
|
|
'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
|
|
|
|
2022-09-12 12:37:57 +00:00
|
|
|
if ( isset( $preferences['discussiontools-' . HookUtils::REPLYTOOL] ) &&
|
|
|
|
// GlobalPreferences extension would delete disabled fields, avoid referring to it.
|
|
|
|
!( $preferences['discussiontools-' . HookUtils::REPLYTOOL]['disabled'] ?? false )
|
|
|
|
) {
|
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-08-23 20:23:37 +00:00
|
|
|
$preferences['discussiontools-seenautotopicsubpopup'] = [
|
|
|
|
'type' => 'api',
|
|
|
|
];
|
2021-01-29 17:09:52 +00:00
|
|
|
|
2023-06-06 12:08:35 +00:00
|
|
|
if ( !$this->config->get( 'DiscussionToolsBeta' ) ) {
|
2021-01-29 17:09:52 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
}
|