Page MenuHomePhabricator

There should be a way to set a global preference programmatically
Closed, ResolvedPublic

Description

Currently the only way to set a global preference from the code of another extension is GlobalPreferencesFactory::setGlobalPreferences() which sucks in so many ways:

  • it's big and heavy and does a bunch of things only remotely related to writing a global preference value, like generating HtmlForm elements for the global preferences form
  • it expects a full list of the user's global preferences, ie. you need to pass it all existing preferences of the user, otherwise you have just deleted all the user's preferences
    • this also allows for all kinds of exciting race conditions
  • it will break when the current request does not have a title, and behaves differently depending on what that title is
  • it calls the GlobalPreferencesSetGlobalPreferences hook, which probably shouldn't be called for programmatic changes

You can directly create a Storage object and use Storage::save(), which is a little saner, although it still requires the full list of the user's preferences; but that's not exposed via the service container and it's fragile to instantiate another extension's internal classes directly.

Or you can do a faux API call to ApiGlobalPreferences (which uses GlobalPreferencesFactory internally), but API calls are quite costly, even faux ones.

There really should be a better way to do this.

(One can update an existing global preference via UserOptionsManager, but that has no way to globally set a preference that wasn't global before.)

Details

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald Transcript

Any parent task? An interface needs a caller.

My use case is T384549: Create a per-user flag for enabling SUL3. I think there are a couple others too:

  • ApiGlobalPreferences itself could use a simpler method.
  • Some extensions (e.g. GrowthExperiments) use "seen" flags for onboarding, guided tours and such, which don't really make sense to repeat for each wiki; I think historically this has been solved by having a local preference and setting it to "seen" on autocreate , but that's both imprecise (there's no guarantee the user wants to watch onboarding on the wiki where they signed up) and a waste of DB space. Or just ignored, which leads to an annyoing user experience (e.g. T135767: Have to close Welcome message on every single project separately).
  • Also any preference that has its own UI (which is not that rare for beginner-facing features since Special:Preferences is a fairly complex and confusing UI) would need this, for example T233802: Add an option in the global preferences to enable Advanced Contributions mode globally,

I can add a $global=GLOBAL_CREATE mode in UserOptionsManager::setOption(), there are just a few tricky questions:

  • What if there are no global stores? Should it save the option locally?
  • What if there is a local override already? Should it act like GLOBAL_UPDATE, which updates the local override?
  • What if there is more than one global store? Which one do you save it in? OK, never going to happen, but the data model requires us to ask this question. Stores have names but the names are not exposed right now.

If there are no global stores, that means the wiki is not part of a farm and using the local store is as global as it gets, so I think storing locally is the expected behavior there.
The trickier question is, what should happen if the global store fails? For example, you use CentralAuth with GlobalPreferences, the wiki is partially migrated, and the user in question does not have a central account yet. Should the entire operation fail? Should it try to store locally instead? I think storing locally is still the best option, but it's less obvious.

(Except, can a store fail for transient reasons, like failing to obtain a DB lock? GlobalPreferences can't, but the UserOptionsStore interface doesn't really specify what a false return means. Also, it says the implementation should return false if no change was made; the GlobalPreferences implementation doesn't actually do that, but if some store did return false when the value to be stored matched the already stored value, and that resulted in a failover to the next store, that would be a problem. So maybe the interface should be changed to say, only return false if the store cannot handle the given user?)

By the same logic, if there are multiple stores, just trying them in order and using the first one that doesn't fail seems like reasonable behavior, even if it's not something we expect to happen in practice.

For local overrides, not sure... behave like GLOBAL_UPDATE, or maybe just delete the local override? (Also, what if there is no local override, but there's a local value? AIUI writing a value in the GlobalPreferences store that previously had nothing for that user/option combo is equivalent to checking "I want to manage this option globally", if you want to preserve the local value having an effect locally, you need to add an override.)
In practice, I think this would mostly be used for internal preferences that are not exposed to the user via Special:Preferences, so there would never be a local override and the exact behavior doesn't matter that much. But conceptually, "update the local value if there's a local override", "keep the local value and add a local override for it" and "delete the local value and its override" all seem like sensible options.

The trickier question is, what should happen if the global store fails? For example, you use CentralAuth with GlobalPreferences, the wiki is partially migrated, and the user in question does not have a central account yet. Should the entire operation fail? Should it try to store locally instead? I think storing locally is still the best option, but it's less obvious.

For your purposes, the caller can check if the user is attached, and if not, pass $global=GLOBAL_IGNORE.

(Except, can a store fail for transient reasons, like failing to obtain a DB lock?

No, that would be an exception.

GlobalPreferences can't, but the UserOptionsStore interface doesn't really specify what a false return means. Also, it says the implementation should return false if no change was made; the GlobalPreferences implementation doesn't actually do that

I think it's clear enough. Nothing is actually using the return value that I know of, but Storage::replaceAndDelete() shows why the convention exists:

		if ( $rows || $deletions ) {
			$key = $this->getCacheKey();
			$this->getCache()->delete( $key );
		}

This could be improved by checking affectedRows() and passing it back down the stack. Interpreted in the context of cache invalidation, true can mean "maybe", and false means there were definitely no changes. Failure is supposed to be indicated by an exception, not a false return.

but if some store did return false when the value to be stored matched the already stored value, and that resulted in a failover to the next store, that would be a problem. So maybe the interface should be changed to say, only return false if the store cannot handle the given user?)

I think it's good enough as it is for now.

Change #1121797 had a related patch set uploaded (by Tim Starling; author: Tim Starling):

[mediawiki/core@master] user: Add UserOptionsManager::GLOBAL_CREATE

https://gerrit.wikimedia.org/r/1121797

For local overrides, not sure... behave like GLOBAL_UPDATE, or maybe just delete the local override?

I initially implemented deleting the local override, but it turned out to have some complicated edge cases, so I backed that out in favour of just ignoring local overrides, leaving them active.

(Also, what if there is no local override, but there's a local value?

The local value is ignored on load and will not be updated on save.

Change #1121797 merged by jenkins-bot:

[mediawiki/core@master] user: Add UserOptionsManager::GLOBAL_CREATE

https://gerrit.wikimedia.org/r/1121797

Tgr assigned this task to tstarling.

Thank you!