Page MenuHomePhabricator

Allow conditional registration of RestRoutes
Open, Needs TriagePublic

Description

As seen in T362027: Using rest.php seems to show Math has a hard dependancy on WikibaseClient due to use of 'Math.WikibaseConnector' service, there are potentially extensions defining RestRoutes that depend on (injecting) a service which requires another extension (in this case, WikibaseClient), but don't need the extension for the rest (or even, most) of the functionality to work. So we do not want to unnecessarily introduce a hard dependancy in the extension on the 3rd party extension if it is not necessary, and other features work/fallback gracefully.

There is not currently a dedicated hook for allowing conditional registration of a Rest Route.

As always, we could use ExtensionFunctions or extension.json callback.

One, or both of those might actually work in this case, but it feels more like a hack.

Plus, with how Rest Routes are accessed as ExtensionRegistry::getInstance()->getAttribute( 'RestRoutes' ) in EntryPoint.php, we can't inject config into there (in the callback)... easily via the SettingsBuilder and the fact that RestRoutes is an attribute? Similar presumably applies for ExtensionFunctions too.

Wikibase does have some stuff around using $wgRestAPIAdditionalRouteFiles, but that is for CI, and/or suggestions to add to LocalSettings.php. It also doesn't seem to have any dynamic way to abort adding them, for example if a dependancy isn't met as in this situation. However, it does look like this could be used (abused?) in this case...

https://www.mediawiki.org/wiki/Manual:$wgRestAPIAdditionalRouteFiles

A common usage is to enable development/experimental endpoints only on test wikis.

And similarly, in MW itself:

RestAPIAdditionalRouteFiles:
    default: []
    type: array
    description: |-
        Additional REST API Route files.
        A common usage is to enable development/experimental endpoints only on test wikis.

This feels like something a dedicated hook may be better placed to solve. Else, if conditional registration using something like ExtensionFunctions and $wgRestAPIAdditionalRouteFiles is deemed acceptable, it probably should be documented somewhere (including on https://www.mediawiki.org/wiki/Manual:$wgRestAPIAdditionalRouteFiles which doesn't suggest it is for this purpose)...

See also: T286515: Create a hook to manipulate configuration on startup.

Event Timeline

It looks like Parsoid is doing this with extension registration's callback, as seen in extension/src/RegistrationHookHandler.php.

If either option (ExtensionFunctions and/or callback) are deemed acceptable, maybe this is only a documentation issue.