Use wikimedia\object-factory for extension objects; hook up ExtensionRegistry

Mediawiki prefers to use an object factory pattern when creating objects.
Use ObjectFactory consistently when creating objects specified using the
extension API.  Thanks to the 'allowClassName' option to
ObjectFactory::getObjectFromSpec(), this is mostly consistent with
previous practice.  Note that the string class name is short for:
   [ 'class' => Foo::class ]
and so we've chosen to rename the 'class' property in the extension tag
configuration so that we have (in long form):
   [ 'name' => 'Cite', 'handler' => [ 'class' => Cite::class ] ]
instead of nesting two keys named 'class' in a row.  (And besides, the
content isn't really a 'class' any more, it's an "object factory
 specification".)

SiteConfig::registerExtensionModule() can now take *either* an object
factory specification for an ExtensionModule object (including a bare
class-string) *or* the contents of the configuration array that
would be returned by ExtensionModule::getConfig(), in which case it
creates an anonymous ExtensionModule object for you.  It's expected
that the latter will be preferred in extension.json, but we use the
former for our internal extension implementations at the moment.

Finally, call SiteConfig::registerExtensionModule() on the results
of ExtensionRegistery::getInstance()->getAttribute('ParsoidModules')
when running in integrated mode.  This allows you to register your
extension with a clause such as the following in your extension.json:

(simple case, naming a class which implements ExtensionModule)
{
  "name": "JsonExtension",
  "manifest_version": 2,
  ...
  "ParsoidModules": [ "Wikimedia\\Parsoid\\Ext\\JSON" ]
}

(complex case, putting the configuration array into extension.json)
{
  "name": "Cite",
  "manifest_version": 2,
  ...
  "ParsoidModules": [
    {
      "name": "Cite",
      "domProcessors": [
	"Wikimedia\\Parsoid\\Ext\\Cite\\RefProcessor",
      ],
      "tags": [
	{
	  "name": "ref",
	  "handler": "Wikimedia\\Parsoid\\Ext\\Cite\\Ref",
	  "options": {
	    "wt2html": { "sealFragment": true }
	  },
	},
	{
	  "name": "references",
	  "handler": "Wikimedia\\Parsoid\\Ext\\Cite\\References",
	  "options": {
	    "html2wt": { "format": "block" }
	  },
	}
      ],
      "styles": [
	"ext.cite.style",
	"ext.cite.styles"
      ]
    }
  ]
}

The syntax above, with `ParsoidModules` as a top-level attribute, requires
I6c74938883376ec17f3790678b435585083a440f in core.  However, with or without
that patch, the following also works:
{
  ...
  "attributes": {
    "Parsoid": {
      "Modules": [ ... ]
    }
  }
}

Bug: T133320
Change-Id: I20f641a1ff032a6da3549b01dfaf8f4cf1eb5071
This commit is contained in:
C. Scott Ananian 2020-05-04 11:19:24 -04:00 committed by Arlo Breault
parent 0cac84e6b2
commit 98d68bfa6e

View file

@ -22,14 +22,14 @@ class Cite implements ExtensionModule {
'tags' => [
[
'name' => 'ref',
'class' => Ref::class,
'handler' => Ref::class,
'options' => [
'wt2html' => [ 'sealFragment' => true ]
],
],
[
'name' => 'references',
'class' => References::class,
'handler' => References::class,
'options' => [
'html2wt' => [ 'format' => 'block' ]
],