Page MenuHomePhabricator

Pass context to GetPreferences hook
Open, Needs TriagePublic

Description

The core preferences sections called in the same context have a context passed. Extension using RequestContext instead include

  • UniversalLanguageSelector
  • GlobalCssJs (which could technically work around using closuers over the HTMLField params... )
  • BetaFeatures
  • CheckUser
  • Gadgets
  • MultimediaViewer
  • OATHAuth
  • WP25EasterEggs (somewhat)

Is it just possible to add another optional param to a hook?

Event Timeline

Is it just possible to add another optional param to a hook?

Unfortunately, not – adding a parameter to the interface makes signatures of implementing classes incompatible. So there needs to be a new hook. To make future migrations less painful, that hook could specify the current by-ref array parameter, and an object parameter whose type is a read-only class containing both the user and the context, i.e.

<?php
class GetPreferencesData {
	public function __construct(
		public readonly UserIdentity $user, // since we add a new hook anyway, this can be a narrower UserIdentity rather than a full User
		public readonly IContextSource $context,
		// Since this constructor is not @stable to call, further parameters can be added here in the future without breaking compatibility
	) { }
}

interface GetPreferences2Hook {
	public function onGetPreferences2( GetPreferencesData $data, array &$preferences );
}

However, we should be mindful about what the context gets used for. For example, I don’t think presence or absence of preferences should depend on the context (which is what UniversalLanguageSelector and WP25EasterEggs do) – this means that if I set a desktop-only preference on Vector, and later change an unrelated preference on Minerva, the desktop-only preference may get cleared or otherwise behave unexpectedly. On the other hand, using it for localizing messages (e.g. GlobalCssJs does this) should be fine.

Also, GlobalCssJs uses the context to get the main config from it – this wouldn’t need a context at all.

Also, GlobalCssJs uses the context to get the main config from it – this wouldn’t need a context at all.

It uses the context language.

I was aware of that, I wanted to point out that at least two out of three usages can be migrated to not use the context at all – reading my comment again, I see how easy it was to understand it differently, though. (I intentionally wrote “to get the main config” and not “only to get the main config”, but this intention probably didn’t come through.)