Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
65.09% |
110 / 169 |
|
22.22% |
2 / 9 |
CRAP | |
0.00% |
0 / 1 |
| Hooks | |
65.09% |
110 / 169 |
|
22.22% |
2 / 9 |
167.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getUserCounts | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| onSaveUserOptions | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
56 | |||
| onGetPreferences | |
80.34% |
94 / 117 |
|
0.00% |
0 / 1 |
41.27 | |||
| onPreferencesGetIcon | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onUserGetDefaultOptions | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
| onMakeGlobalVariablesScript | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| onSkinTemplateNavigation__Universal | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| onExtensionTypes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * This file is part of the MediaWiki extension BetaFeatures. |
| 4 | * |
| 5 | * BetaFeatures is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * BetaFeatures is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with BetaFeatures. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * BetaFeatures extension hooks |
| 19 | * |
| 20 | * @file |
| 21 | * @ingroup Extensions |
| 22 | * @copyright 2013 Mark Holmquist and others; see AUTHORS |
| 23 | * @license GPL-2.0-or-later |
| 24 | */ |
| 25 | |
| 26 | namespace MediaWiki\Extension\BetaFeatures; |
| 27 | |
| 28 | use Exception; |
| 29 | use MediaWiki\Config\Config; |
| 30 | use MediaWiki\Context\RequestContext; |
| 31 | use MediaWiki\Deferred\DeferredUpdates; |
| 32 | use MediaWiki\Extension\BetaFeatures\Hooks\HookRunner; |
| 33 | use MediaWiki\Hook\ExtensionTypesHook; |
| 34 | use MediaWiki\Hook\PreferencesGetIconHook; |
| 35 | use MediaWiki\Hook\SkinTemplateNavigation__UniversalHook; |
| 36 | use MediaWiki\HookContainer\HookContainer; |
| 37 | use MediaWiki\JobQueue\JobQueueGroupFactory; |
| 38 | use MediaWiki\Output\Hook\MakeGlobalVariablesScriptHook; |
| 39 | use MediaWiki\Output\OutputPage; |
| 40 | use MediaWiki\Preferences\Hook\GetPreferencesHook; |
| 41 | use MediaWiki\Skin\SkinFactory; |
| 42 | use MediaWiki\Skin\SkinTemplate; |
| 43 | use MediaWiki\SpecialPage\SpecialPage; |
| 44 | use MediaWiki\User\Hook\UserGetDefaultOptionsHook; |
| 45 | use MediaWiki\User\Options\Hook\SaveUserOptionsHook; |
| 46 | use MediaWiki\User\Options\UserOptionsManager; |
| 47 | use MediaWiki\User\User; |
| 48 | use MediaWiki\User\UserFactory; |
| 49 | use MediaWiki\User\UserIdentity; |
| 50 | use MediaWiki\User\UserIdentityUtils; |
| 51 | use ObjectCacheFactory; |
| 52 | use Wikimedia\Rdbms\IConnectionProvider; |
| 53 | |
| 54 | class Hooks implements |
| 55 | ExtensionTypesHook, |
| 56 | GetPreferencesHook, |
| 57 | MakeGlobalVariablesScriptHook, |
| 58 | PreferencesGetIconHook, |
| 59 | SaveUserOptionsHook, |
| 60 | SkinTemplateNavigation__UniversalHook, |
| 61 | UserGetDefaultOptionsHook |
| 62 | { |
| 63 | |
| 64 | /** |
| 65 | * @var array An array of each of the available Beta Features, with their requirements, if any. |
| 66 | * It is passed client-side for JavaScript rendering/responsiveness. |
| 67 | */ |
| 68 | private static array $features = []; |
| 69 | |
| 70 | public function __construct( |
| 71 | private readonly Config $config, |
| 72 | private readonly IConnectionProvider $dbProvider, |
| 73 | private readonly HookContainer $hookContainer, |
| 74 | private readonly JobQueueGroupFactory $jobQueueGroupFactory, |
| 75 | private readonly SkinFactory $skinFactory, |
| 76 | private readonly UserFactory $userFactory, |
| 77 | private readonly UserIdentityUtils $userIdentityUtils, |
| 78 | private readonly UserOptionsManager $userOptionsManager, |
| 79 | private readonly ObjectCacheFactory $objectCacheFactory, |
| 80 | ) { |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param string[] $prefs |
| 85 | * @param IConnectionProvider $dbProvider |
| 86 | * @return int[] |
| 87 | */ |
| 88 | public static function getUserCounts( array $prefs, IConnectionProvider $dbProvider ) { |
| 89 | $counts = []; |
| 90 | if ( !$prefs ) { |
| 91 | return $counts; |
| 92 | } |
| 93 | |
| 94 | $dbr = $dbProvider->getReplicaDatabase(); |
| 95 | $res = $dbr->newSelectQueryBuilder() |
| 96 | ->select( [ 'feature', 'number' ] ) |
| 97 | ->from( 'betafeatures_user_counts' ) |
| 98 | ->where( [ 'feature' => $prefs ] ) |
| 99 | ->caller( __METHOD__ )->fetchResultSet(); |
| 100 | |
| 101 | foreach ( $res as $row ) { |
| 102 | $counts[$row->feature] = $row->number; |
| 103 | } |
| 104 | |
| 105 | return $counts; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/SaveUserOptions |
| 110 | * |
| 111 | * @param UserIdentity $user User who's just saved their preferences |
| 112 | * @param array &$modifiedOptions List of modified options |
| 113 | * @param array $originalOptions List of original user options |
| 114 | * @throws Exception |
| 115 | */ |
| 116 | public function onSaveUserOptions( |
| 117 | UserIdentity $user, |
| 118 | array &$modifiedOptions, |
| 119 | array $originalOptions |
| 120 | ) { |
| 121 | if ( !$user->isRegistered() || $this->userIdentityUtils->isTemp( $user ) ) { |
| 122 | // Anonymous and temporary users do not have options, shorten out. |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | $betaFeatures = $this->config->get( 'BetaFeatures' ); |
| 127 | $user = $this->userFactory->newFromUserIdentity( $user ); |
| 128 | ( new HookRunner( $this->hookContainer ) )->onGetBetaFeaturePreferences( $user, $betaFeatures ); |
| 129 | |
| 130 | $jobs = []; |
| 131 | foreach ( $betaFeatures as $name => $option ) { |
| 132 | if ( !array_key_exists( $name, $modifiedOptions ) ) { |
| 133 | continue; |
| 134 | } |
| 135 | $newVal = $modifiedOptions[$name]; |
| 136 | $oldVal = $originalOptions[$name] ?? null; |
| 137 | // Check if this preference meaningfully changed |
| 138 | if ( $oldVal == $newVal ) { |
| 139 | // unchanged |
| 140 | continue; |
| 141 | } |
| 142 | // Enqueue a job to update the count for this preference |
| 143 | $jobs[] = new UpdateBetaFeatureUserCountsJob( |
| 144 | [ 'prefs' => [ $name ] ], |
| 145 | $this->dbProvider |
| 146 | ); |
| 147 | } |
| 148 | if ( $jobs !== [] ) { |
| 149 | $this->jobQueueGroupFactory->makeJobQueueGroup()->push( $jobs ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * @param User $user |
| 155 | * @param array[] &$prefs |
| 156 | * @throws BetaFeaturesMissingFieldException |
| 157 | */ |
| 158 | public function onGetPreferences( $user, &$prefs ) { |
| 159 | $betaPrefs = $this->config->get( 'BetaFeatures' ); |
| 160 | $depHooks = []; |
| 161 | |
| 162 | $hookRunner = new HookRunner( $this->hookContainer ); |
| 163 | $hookRunner->onGetBetaFeaturePreferences( $user, $betaPrefs ); |
| 164 | |
| 165 | // The following messages are generated upstream by the 'section' value |
| 166 | // * prefs-betafeatures |
| 167 | // * prefs-description-betafeatures |
| 168 | $count = count( $betaPrefs ); |
| 169 | $prefs['betafeatures-section-desc'] = [ |
| 170 | 'type' => 'info', |
| 171 | 'default' => static function () use ( $count ) { |
| 172 | return wfMessage( 'betafeatures-section-desc' ) |
| 173 | ->numParams( $count ) |
| 174 | ->parseAsBlock(); |
| 175 | }, |
| 176 | 'section' => 'betafeatures', |
| 177 | 'raw' => true, |
| 178 | ]; |
| 179 | |
| 180 | $prefs['betafeatures-auto-enroll'] = [ |
| 181 | 'type' => 'check', |
| 182 | 'label-message' => 'betafeatures-auto-enroll', |
| 183 | 'help-message' => 'betafeatures-auto-enroll-help', |
| 184 | 'section' => 'betafeatures', |
| 185 | ]; |
| 186 | |
| 187 | // Purely visual field. |
| 188 | $prefs['betafeatures-breaking-hr'] = [ |
| 189 | 'class' => HTMLHorizontalRuleField::class, |
| 190 | 'section' => 'betafeatures', |
| 191 | ]; |
| 192 | |
| 193 | $counts = self::getUserCounts( array_keys( $betaPrefs ), $this->dbProvider ); |
| 194 | |
| 195 | // Set up dependency hooks array |
| 196 | // This complex structure brought to you by Per-Wiki Configuration, |
| 197 | // coming soon to a wiki very near you. |
| 198 | $hookRunner->onGetBetaFeatureDependencyHooks( $depHooks ); |
| 199 | |
| 200 | $autoEnrollSaveSettings = []; |
| 201 | $autoEnrollAll = $this->userOptionsManager->getBoolOption( $user, 'betafeatures-auto-enroll' ); |
| 202 | |
| 203 | $autoEnroll = []; |
| 204 | |
| 205 | foreach ( $betaPrefs as $key => $info ) { |
| 206 | if ( isset( $info['auto-enrollment'] ) ) { |
| 207 | $autoEnroll[$info['auto-enrollment']] = $key; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | $hiddenPrefs = $this->config->get( 'HiddenPrefs' ); |
| 212 | $allowlist = $this->config->get( 'BetaFeaturesAllowList' ); |
| 213 | |
| 214 | foreach ( $betaPrefs as $key => $info ) { |
| 215 | // Check if feature should be skipped |
| 216 | if ( |
| 217 | // Check if feature is hidden |
| 218 | in_array( $key, $hiddenPrefs ) || |
| 219 | // Check if feature is in the allow list |
| 220 | ( is_array( $allowlist ) && !in_array( $key, $allowlist ) ) || |
| 221 | // Check if dependencies are set but not met |
| 222 | ( |
| 223 | isset( $info['dependent'] ) && |
| 224 | $info['dependent'] === true && |
| 225 | isset( $depHooks[$key] ) && |
| 226 | !$this->hookContainer->run( $depHooks[$key] ) |
| 227 | ) |
| 228 | ) { |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | $opt = [ |
| 233 | 'class' => HTMLFeatureField::class, |
| 234 | 'section' => 'betafeatures', |
| 235 | 'disable-if' => [ '===', 'betafeatures-auto-enroll', '1' ], |
| 236 | ]; |
| 237 | |
| 238 | $requiredFields = [ |
| 239 | 'label-message' => true, |
| 240 | 'desc-message' => true, |
| 241 | 'screenshot' => false, |
| 242 | 'requirements' => false, |
| 243 | 'info-link' => false, |
| 244 | 'info-message' => false, |
| 245 | 'discussion-link' => false, |
| 246 | 'discussion-message' => false, |
| 247 | 'disabled' => false, |
| 248 | ]; |
| 249 | |
| 250 | foreach ( $requiredFields as $field => $required ) { |
| 251 | if ( isset( $info[$field] ) ) { |
| 252 | $opt[$field] = $info[$field]; |
| 253 | } elseif ( $required ) { |
| 254 | // A required field isn't present in the info array |
| 255 | // we got from the GetBetaFeaturePreferences hook. |
| 256 | // Don't add this feature to the form. |
| 257 | throw new BetaFeaturesMissingFieldException( |
| 258 | "The field {$field} was missing from the beta feature {$key}." |
| 259 | ); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if ( isset( $counts[$key] ) ) { |
| 264 | $opt['user-count'] = $counts[$key]; |
| 265 | } |
| 266 | |
| 267 | // Set the beta feature in the standard preferences array |
| 268 | // Just before, unset the key to resort it in the array, in the case the key was already set |
| 269 | unset( $prefs[$key] ); |
| 270 | $prefs[$key] = $opt; |
| 271 | |
| 272 | $autoEnrollForThisPref = false; |
| 273 | |
| 274 | if ( isset( $info['group'] ) && isset( $autoEnroll[$info['group']] ) ) { |
| 275 | $autoEnrollForThisPref = $this->userOptionsManager |
| 276 | ->getBoolOption( $user, $autoEnroll[$info['group']] ); |
| 277 | } |
| 278 | |
| 279 | $exemptAutoEnroll = ( $info['exempt-from-auto-enrollment'] ?? false ) |
| 280 | || ( $info['disabled'] ?? false ); |
| 281 | $autoEnrollHere = !$exemptAutoEnroll && ( $autoEnrollAll || $autoEnrollForThisPref ); |
| 282 | |
| 283 | // Use raw value for existence test |
| 284 | $currentValue = $this->userOptionsManager->getOption( $user, $key ); |
| 285 | |
| 286 | // Keep it break now... The tests applied are against the comments below. |
| 287 | // Fixing all the tests is not worthwhile, the auto-enroll logic should be refactored later. |
| 288 | if ( $autoEnrollHere && $currentValue !== '1' ) { |
| 289 | // We haven't seen this before, and the user has auto-enroll enabled! |
| 290 | // Set the option to true and make it visible for the current user object |
| 291 | $this->userOptionsManager->setOption( $user, $key, true ); |
| 292 | // Also put it aside for saving the settings later |
| 293 | $autoEnrollSaveSettings[$key] = true; |
| 294 | } |
| 295 | |
| 296 | self::$features[$key] = []; |
| 297 | self::$features[$key]['__skip-auto-enroll'] = $exemptAutoEnroll; |
| 298 | } |
| 299 | |
| 300 | foreach ( $betaPrefs as $key => $info ) { |
| 301 | if ( isset( $prefs[$key]['requirements'] ) ) { |
| 302 | // Check which other beta features are required, and fetch their labels |
| 303 | if ( isset( $prefs[$key]['requirements']['betafeatures'] ) ) { |
| 304 | $requiredPrefs = []; |
| 305 | foreach ( $prefs[$key]['requirements']['betafeatures'] as $preference ) { |
| 306 | if ( !$this->userOptionsManager->getBoolOption( $user, $preference ) ) { |
| 307 | $requiredPrefs[] = $prefs[$preference]['label-message']; |
| 308 | } |
| 309 | } |
| 310 | if ( count( $requiredPrefs ) ) { |
| 311 | $prefs[$key]['requirements']['betafeatures-messages'] = $requiredPrefs; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // Test skin support |
| 316 | if ( isset( $prefs[$key]['requirements']['skins'] ) ) { |
| 317 | // Remove any skins that aren't installed or users can't choose |
| 318 | $prefs[$key]['requirements']['skins'] = array_intersect( |
| 319 | /** @phan-suppress-next-line PhanTypeInvalidDimOffset,PhanTypeMismatchArgumentInternal */ |
| 320 | $prefs[$key]['requirements']['skins'], |
| 321 | array_keys( $this->skinFactory->getAllowedSkins() ) |
| 322 | ); |
| 323 | |
| 324 | if ( empty( $prefs[$key]['requirements']['skins'] ) ) { |
| 325 | // If there are no valid skins, don't show the preference |
| 326 | wfDebugLog( 'BetaFeatures', "The $key BetaFeature has no valid skins installed." ); |
| 327 | continue; |
| 328 | } |
| 329 | // Also check if the user's current skin is supported |
| 330 | $prefs[$key]['requirements']['skin-not-supported'] = !in_array( |
| 331 | RequestContext::getMain()->getSkin()->getSkinName(), |
| 332 | $prefs[$key]['requirements']['skins'] |
| 333 | ); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // If a unsupported browsers list is supplied, store so it can be passed as JSON |
| 338 | self::$features[$key]['unsupportedList'] = $prefs[$key]['requirements']['unsupportedList'] ?? null; |
| 339 | } |
| 340 | |
| 341 | if ( $autoEnrollSaveSettings !== [] ) { |
| 342 | // Save the preferences to the DB post-send |
| 343 | DeferredUpdates::addCallableUpdate( |
| 344 | function () use ( $user, $autoEnrollSaveSettings ) { |
| 345 | $cache = $this->objectCacheFactory->getLocalClusterInstance(); |
| 346 | $key = $cache->makeKey( __CLASS__, 'prefs-update', $user->getId() ); |
| 347 | // T95839: If concurrent requests pile on (e.g. multiple tabs), only let one |
| 348 | // thread bother doing these updates. This avoids pointless error log spam. |
| 349 | if ( $cache->lock( $key, 0, $cache::TTL_MINUTE ) ) { |
| 350 | // Apply the settings and save |
| 351 | foreach ( $autoEnrollSaveSettings as $key => $option ) { |
| 352 | $this->userOptionsManager->setOption( $user, $key, $option ); |
| 353 | } |
| 354 | $this->userOptionsManager->saveOptions( $user ); |
| 355 | $cache->unlock( $key ); |
| 356 | } |
| 357 | } |
| 358 | ); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Add icon for Special:Preferences mobile layout |
| 364 | * |
| 365 | * @param array &$iconNames Array of icon names for their respective sections. |
| 366 | */ |
| 367 | public function onPreferencesGetIcon( &$iconNames ) { |
| 368 | $iconNames[ 'betafeatures' ] = 'labFlask'; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Add default preferences values |
| 373 | * |
| 374 | * @param array &$defaultOptions Array of preference keys and their default values. |
| 375 | */ |
| 376 | public function onUserGetDefaultOptions( &$defaultOptions ) { |
| 377 | foreach ( $this->config->get( 'BetaFeatures' ) as $key => $info ) { |
| 378 | $defaultOptions[$key] = false; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * @param array &$vars |
| 384 | * @param OutputPage $out |
| 385 | */ |
| 386 | public function onMakeGlobalVariablesScript( &$vars, $out ): void { |
| 387 | if ( self::$features ) { |
| 388 | // This is added to page view HTML on all articles. |
| 389 | // FIXME: Move this to the preferences page somehow, or |
| 390 | // bundle with the module that loads betafeatures.js. |
| 391 | $vars['wgBetaFeaturesFeatures'] = self::$features; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * @param SkinTemplate $skintemplate |
| 397 | * @param array[] &$links |
| 398 | */ |
| 399 | public function onSkinTemplateNavigation__Universal( |
| 400 | $skintemplate, |
| 401 | &$links |
| 402 | ): void { |
| 403 | $user = $skintemplate->getUser(); |
| 404 | if ( $user->isNamed() ) { |
| 405 | $personalUrls = $links['user-menu'] ?? []; |
| 406 | $personalUrls = wfArrayInsertAfter( $personalUrls, [ |
| 407 | // The following messages are generated upstream |
| 408 | // * tooltip-pt-betafeatures |
| 409 | 'betafeatures' => [ |
| 410 | 'text' => wfMessage( 'betafeatures-toplink' )->text(), |
| 411 | 'href' => SpecialPage::getTitleFor( |
| 412 | 'Preferences', false, 'mw-prefsection-betafeatures' |
| 413 | )->getLinkURL(), |
| 414 | 'active' => $skintemplate->getTitle()->isSpecial( 'Preferences' ), |
| 415 | 'icon' => 'labFlask' |
| 416 | ], |
| 417 | ], 'preferences' ); |
| 418 | $links['user-menu'] = $personalUrls; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * @param string[] &$extTypes |
| 424 | */ |
| 425 | public function onExtensionTypes( &$extTypes ) { |
| 426 | $extTypes['betafeatures'] = wfMessage( 'betafeatures-extension-type' )->text(); |
| 427 | } |
| 428 | |
| 429 | } |