diff options
Diffstat (limited to 'includes/user/Options/UserOptionsManager.php')
-rw-r--r-- | includes/user/Options/UserOptionsManager.php | 107 |
1 files changed, 38 insertions, 69 deletions
diff --git a/includes/user/Options/UserOptionsManager.php b/includes/user/Options/UserOptionsManager.php index 7219fcf5959a..b3372374ad7a 100644 --- a/includes/user/Options/UserOptionsManager.php +++ b/includes/user/Options/UserOptionsManager.php @@ -22,14 +22,12 @@ namespace MediaWiki\User\Options; use InvalidArgumentException; use MediaWiki\Config\ServiceOptions; -use MediaWiki\Context\IContextSource; use MediaWiki\HookContainer\HookContainer; use MediaWiki\HookContainer\HookRunner; use MediaWiki\Language\LanguageCode; use MediaWiki\Language\LanguageConverter; use MediaWiki\Languages\LanguageConverterFactory; use MediaWiki\MainConfigNames; -use MediaWiki\MediaWikiServices; use MediaWiki\User\UserFactory; use MediaWiki\User\UserIdentity; use MediaWiki\User\UserNameUtils; @@ -77,6 +75,14 @@ class UserOptionsManager extends UserOptionsLookup { */ public const GLOBAL_UPDATE = 'update'; + /** + * Create a new global preference in the first available global store. + * If there are no global stores, update the local value. If there was + * already a global preference, update it. + * @since 1.44 + */ + public const GLOBAL_CREATE = 'create'; + private const LOCAL_STORE_KEY = 'local'; private ServiceOptions $serviceOptions; @@ -246,12 +252,13 @@ class UserOptionsManager extends UserOptionsLookup { * @param UserIdentity $user * @param string $oname The option to set * @param mixed $val New value to set. - * @param string $global Since 1.43. What to do if the option was set - * globally using the GlobalPreferences extension. One of the - * self::GLOBAL_* constants: - * - GLOBAL_IGNORE: Do nothing. The option remains with its previous value. - * - GLOBAL_OVERRIDE: Add a local override. - * - GLOBAL_UPDATE: Update the option globally. + * @param string $global Since 1.43. The global update behaviour, used if + * GlobalPreferences is installed: + * - GLOBAL_IGNORE: If there is a global preference, do nothing. The option remains with + * its previous value. + * - GLOBAL_OVERRIDE: If there is a global preference, add a local override. + * - GLOBAL_UPDATE: If there is a global preference, update it. + * - GLOBAL_CREATE: Create a new global preference, overriding any local value. * The UI should typically ask for the user's consent before setting a global * option. */ @@ -267,34 +274,6 @@ class UserOptionsManager extends UserOptionsLookup { } /** - * Reset certain (or all) options to the site defaults - * - * The optional parameter determines which kinds of preferences will be reset. - * Supported values are everything that can be reported by getOptionKinds() - * and 'all', which forces a reset of *all* preferences and overrides everything else. - * - * @note You need to call saveOptions() to actually write to the database. - * - * @deprecated since 1.43 use resetOptionsByName() with PreferencesFactory::getOptionNamesForReset() - * - * @param UserIdentity $user - * @param IContextSource $context Context source used when $resetKinds does not contain 'all'. - * @param array|string $resetKinds Which kinds of preferences to reset. - * Defaults to [ 'registered', 'registered-multiselect', 'registered-checkmatrix', 'unused' ] - */ - public function resetOptions( - UserIdentity $user, - IContextSource $context, - $resetKinds = [ 'registered', 'registered-multiselect', 'registered-checkmatrix', 'unused' ] - ) { - wfDeprecated( __METHOD__, '1.43' ); - $preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory(); - $optionsToReset = $preferencesFactory->getOptionNamesForReset( - $this->userFactory->newFromUserIdentity( $user ), $context, $resetKinds ); - $this->resetOptionsByName( $user, $optionsToReset ); - } - - /** * Reset a list of options to the site defaults * * @note You need to call saveOptions() to actually write to the database. @@ -325,36 +304,6 @@ class UserOptionsManager extends UserOptionsLookup { } /** - * @deprecated since 1.43 use PreferencesFactory::listResetKinds() - * - * @return string[] Option kinds - */ - public function listOptionKinds(): array { - wfDeprecated( __METHOD__, '1.43' ); - $preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory(); - return $preferencesFactory->listResetKinds(); - } - - /** - * @deprecated since 1.43 use PreferencesFactory::getResetKinds - * - * @param UserIdentity $userIdentity - * @param IContextSource $context - * @param array|null $options - * @return string[] - */ - public function getOptionKinds( - UserIdentity $userIdentity, - IContextSource $context, - $options = null - ): array { - wfDeprecated( __METHOD__, '1.43' ); - $user = $this->userFactory->newFromUserIdentity( $userIdentity ); - $preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory(); - return $preferencesFactory->getResetKinds( $user, $context, $options ); - } - - /** * Saves the non-default options for this user, as previously set e.g. via * setOption(), in the database's "user_properties" (preferences) table. * @@ -412,11 +361,16 @@ class UserOptionsManager extends UserOptionsLookup { $valOrNull = (string)$value; } $source = $cache->sources[$key] ?? self::LOCAL_STORE_KEY; + $updateAction = $cache->globalUpdateActions[$key] ?? self::GLOBAL_IGNORE; + if ( $source === self::LOCAL_STORE_KEY ) { - $updatesByStore[self::LOCAL_STORE_KEY][$key] = $valOrNull; + if ( $updateAction === self::GLOBAL_CREATE ) { + $updatesByStore[$this->getStoreNameForGlobalCreate()][$key] = $valOrNull; + } else { + $updatesByStore[self::LOCAL_STORE_KEY][$key] = $valOrNull; + } } else { - $updateAction = $cache->globalUpdateActions[$key] ?? self::GLOBAL_IGNORE; - if ( $updateAction === self::GLOBAL_UPDATE ) { + if ( $updateAction === self::GLOBAL_UPDATE || $updateAction === self::GLOBAL_CREATE ) { $updatesByStore[$source][$key] = $valOrNull; } elseif ( $updateAction === self::GLOBAL_OVERRIDE ) { $updatesByStore[self::LOCAL_STORE_KEY][$key] = $valOrNull; @@ -667,6 +621,21 @@ class UserOptionsManager extends UserOptionsLookup { } return $this->stores; } + + /** + * Get the name of the store to be used when setOption() is called with + * GLOBAL_CREATE and there is no existing global preference value. + * + * @return string + */ + private function getStoreNameForGlobalCreate() { + foreach ( $this->getStores() as $name => $store ) { + if ( $name !== self::LOCAL_STORE_KEY ) { + return $name; + } + } + return self::LOCAL_STORE_KEY; + } } /** @deprecated class alias since 1.42 */ |