| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
namespace MediaWiki\Extension\DiscussionTools\Hooks; |
| 11 |
|
| 12 |
use MediaWiki\Config\Config; |
| 13 |
use MediaWiki\Config\ConfigFactory; |
| 14 |
use MediaWiki\Html\Html; |
| 15 |
use MediaWiki\Linker\LinkRenderer; |
| 16 |
use MediaWiki\Preferences\Hook\GetPreferencesHook; |
| 17 |
use MediaWiki\SpecialPage\SpecialPage; |
| 18 |
use MediaWiki\User\User; |
| 19 |
|
| 20 |
class PreferenceHooks implements |
| 21 |
GetPreferencesHook |
| 22 |
{ |
| 23 |
|
| 24 |
private Config $config; |
| 25 |
private LinkRenderer $linkRenderer; |
| 26 |
|
| 27 |
public function __construct( |
| 28 |
ConfigFactory $configFactory, |
| 29 |
LinkRenderer $linkRenderer |
| 30 |
) { |
| 31 |
$this->config = $configFactory->makeConfig( 'discussiontools' ); |
| 32 |
$this->linkRenderer = $linkRenderer; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private static function arrayRenameKey( array $array, string $from, string $to ): array { |
| 44 |
$out = []; |
| 45 |
foreach ( $array as $key => $value ) { |
| 46 |
if ( $key === $from ) { |
| 47 |
$key = $to; |
| 48 |
} |
| 49 |
$out[$key] = $value; |
| 50 |
} |
| 51 |
return $out; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function onGetPreferences( $user, &$preferences ) { |
| 61 |
if ( HookUtils::isFeatureAvailableToUser( $user ) ) { |
| 62 |
$preferences['discussiontools-summary'] = [ |
| 63 |
'type' => 'info', |
| 64 |
'default' => wfMessage( 'discussiontools-preference-summary' )->parse(), |
| 65 |
'raw' => true, |
| 66 |
'section' => 'editing/discussion', |
| 67 |
]; |
| 68 |
} |
| 69 |
foreach ( HookUtils::FEATURES as $feature ) { |
| 70 |
if ( |
| 71 |
$feature === HookUtils::VISUALENHANCEMENTS_REPLY || |
| 72 |
$feature === HookUtils::VISUALENHANCEMENTS_PAGEFRAME |
| 73 |
) { |
| 74 |
|
| 75 |
continue; |
| 76 |
} |
| 77 |
if ( HookUtils::isFeatureAvailableToUser( $user, $feature ) ) { |
| 78 |
$preferences["discussiontools-$feature"] = [ |
| 79 |
'type' => 'toggle', |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
'label-message' => "discussiontools-preference-$feature", |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
'help-message' => "discussiontools-preference-$feature-help", |
| 96 |
'section' => 'editing/discussion', |
| 97 |
]; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
if ( $feature === HookUtils::NEWTOPICTOOL ) { |
| 102 |
$preferences["discussiontools-newtopictool-createpage"] = [ |
| 103 |
'type' => 'radio', |
| 104 |
'cssclass' => 'mw-htmlform-checkradio-indent', |
| 105 |
'label-message' => 'discussiontools-preference-newtopictool-createpage', |
| 106 |
'options-messages' => [ |
| 107 |
'discussiontools-preference-newtopictool-createpage-newtopictool' => 1, |
| 108 |
'discussiontools-preference-newtopictool-createpage-editor' => 0, |
| 109 |
], |
| 110 |
'disable-if' => [ '===', 'discussiontools-' . HookUtils::NEWTOPICTOOL, '' ], |
| 111 |
'section' => 'editing/discussion', |
| 112 |
]; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
if ( HookUtils::featureConflictsWithGadget( $user, $feature ) ) { |
| 118 |
$preferences["discussiontools-$feature"]['disabled'] = true; |
| 119 |
$preferences["discussiontools-$feature"]['help-message'] = |
| 120 |
[ 'discussiontools-preference-gadget-conflict', 'Special:Preferences#mw-prefsection-gadgets' ]; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if ( isset( $preferences['discussiontools-' . HookUtils::SOURCEMODETOOLBAR] ) && ( |
| 126 |
isset( $preferences['discussiontools-' . HookUtils::REPLYTOOL] ) || |
| 127 |
isset( $preferences['discussiontools-' . HookUtils::NEWTOPICTOOL] ) |
| 128 |
) ) { |
| 129 |
|
| 130 |
|
| 131 |
$preferences['discussiontools-' . HookUtils::SOURCEMODETOOLBAR]['disable-if'] = [ 'AND' ]; |
| 132 |
|
| 133 |
if ( isset( $preferences['discussiontools-' . HookUtils::REPLYTOOL] ) && |
| 134 |
|
| 135 |
!( $preferences['discussiontools-' . HookUtils::REPLYTOOL]['disabled'] ?? false ) |
| 136 |
) { |
| 137 |
$preferences['discussiontools-' . HookUtils::SOURCEMODETOOLBAR]['disable-if'][] = [ |
| 138 |
'===', 'discussiontools-' . HookUtils::REPLYTOOL, '' |
| 139 |
]; |
| 140 |
} |
| 141 |
if ( isset( $preferences['discussiontools-' . HookUtils::NEWTOPICTOOL] ) ) { |
| 142 |
$preferences['discussiontools-' . HookUtils::SOURCEMODETOOLBAR]['disable-if'][] = [ |
| 143 |
'===', 'discussiontools-' . HookUtils::NEWTOPICTOOL, '' |
| 144 |
]; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if ( isset( $preferences['discussiontools-' . HookUtils::AUTOTOPICSUB] ) && |
| 149 |
isset( $preferences['discussiontools-' . HookUtils::TOPICSUBSCRIPTION] ) |
| 150 |
) { |
| 151 |
|
| 152 |
$preferences['discussiontools-' . HookUtils::AUTOTOPICSUB]['disable-if'] = [ |
| 153 |
'===', 'discussiontools-' . HookUtils::TOPICSUBSCRIPTION, '' |
| 154 |
]; |
| 155 |
} |
| 156 |
|
| 157 |
$preferences['discussiontools-showadvanced'] = [ |
| 158 |
'type' => 'api', |
| 159 |
]; |
| 160 |
$preferences['discussiontools-seenautotopicsubpopup'] = [ |
| 161 |
'type' => 'api', |
| 162 |
]; |
| 163 |
|
| 164 |
if ( !$this->config->get( 'DiscussionToolsBeta' ) ) { |
| 165 |
|
| 166 |
|
| 167 |
$preferences['discussiontools-betaenable'] = [ |
| 168 |
'type' => 'api' |
| 169 |
]; |
| 170 |
} |
| 171 |
|
| 172 |
$preferences['discussiontools-editmode'] = [ |
| 173 |
'type' => 'api', |
| 174 |
'validation-callback' => static function ( $value ) { |
| 175 |
return in_array( $value, [ '', 'source', 'visual' ], true ); |
| 176 |
}, |
| 177 |
]; |
| 178 |
|
| 179 |
|
| 180 |
$categoryMessage = wfMessage( 'echo-category-title-dt-subscription' )->numParams( 1 )->escaped(); |
| 181 |
$categoryMessageExtra = $categoryMessage . |
| 182 |
Html::element( 'br' ) . |
| 183 |
wfMessage( 'parentheses' )->rawParams( |
| 184 |
$this->linkRenderer->makeLink( |
| 185 |
SpecialPage::getTitleFor( 'TopicSubscriptions' ), |
| 186 |
wfMessage( 'discussiontools-topicsubscription-preferences-editsubscriptions' )->text() |
| 187 |
) |
| 188 |
)->escaped(); |
| 189 |
if ( isset( $preferences['echo-subscriptions']['rows'] ) ) { |
| 190 |
$preferences['echo-subscriptions']['rows'] = static::arrayRenameKey( |
| 191 |
$preferences['echo-subscriptions']['rows'], |
| 192 |
$categoryMessage, |
| 193 |
$categoryMessageExtra |
| 194 |
); |
| 195 |
} |
| 196 |
if ( isset( $preferences['echo-subscriptions']['tooltips'] ) ) { |
| 197 |
$preferences['echo-subscriptions']['tooltips'] = static::arrayRenameKey( |
| 198 |
|
| 199 |
|
| 200 |
$preferences['echo-subscriptions']['tooltips'], |
| 201 |
$categoryMessage, |
| 202 |
$categoryMessageExtra |
| 203 |
); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
} |