ContentHandler instances are currently instantiated by the static method ContentHandler::getForModelID(). Instead, ContentHandlerFactory should be introduced as a service object, to act as a factory for ContentHandler instances.
Rationale:
- avoiding reliance on global state as a matter of general best practice
- avoiding reliance on global state to improve testability of code that needs to look up ContentHandler instances
Acceptance criteria:
- ContentHandlerFactory shall be managed by MediaWikiServices
- ContentHandlerFactory shall not access global variables, all necessary information shall be injected.
- ContentHandlerFactory shall have an ObjectFactory, and use it to create ContentHandler instances from "specs" that come from the ContentHandler array.
- ContentHandlerFactory shall expose the functionality of ContentHandler::getForModelId as getContentHandler( $modelName ).
- ContentHandler::getForModelID() shall be deprecated in favor of ContentHandlerFactory::getContentHandler
- ContentHandler::getForTitle() shall be deprecated without replacement
- ContentHandler::cleanupHandlersCache() shall be deprecated and converted into a noop. The single usage of this method will be made redundant, because services get reset automatically between test runs.
- The following static methods in ContentHandler shall be ported to ContentHandlerFactory and deprecated in ContentHandler:
- getContentModels()
- getAllContentFormats()
Caveat:
Copying $wgContentHandlers into a member variable in ContentHandlerFactory means that extensions that add handlers to $wgContentHandlers later on have no effect. This issue is mitigated by the fact that the global ServiceContainer is reset after initial setup is complete. This in turn may mean that handlers registered by calling the new defineContentHandler() method may be lost; to avoid this, ContentHandlerFactory may need to implement SalvageableService logic. In any case, code that manipulates $wgContentHandlers needs to be surveyed to avoid problems with the order of initialization. In particular, it may be necessary to introduce special handling into ExtensionProcessor.
Outlook:
Survey the remaining static methods in ContentHandler, and decide on a suitable replacement.