Topic on Manual talk:Extension registration

How to instantiate a "Foo" object right after wfLoadExtension('Foo')?

6
Maiden taiwan (talkcontribs)

I have an old extension that basically does this in LocalSettings.php:

require_once('extensions/Foo.php');

Foo::DoSomething();

When I convert it to use wfLoadExtension(), the code becomes:

wfLoadExtension('Foo');

Foo::DoSomething();

The second line fails with the error:

PHP Fatal error:  Uncaught Error: Class 'Foo' not found in <path>/w/LocalSettings.php

It looks like wfLoadExtension('Foo') does not make the class Foo available in time. (The extension.js file point properly to the file containing the Foo class.)

Is there a workaround? The extension must run Foo::DoSomething() in global scope.

Jdforrester (WMF) (talkcontribs)
Maiden taiwan (talkcontribs)

Thanks so much for the quick suggestion! I see, however, that the example (ArticleRatings) uses the "global" keyword in front of the variable it sets. For various reasons, it's better if my extension can run code in global scope without using the "global" keyword.

Some context: I have written an extension that makes it super-easy to run and manage a multi-wiki farm from a single MediaWiki codebase. The extension lets you define separate "LocalSettings" config files for each site or for collections of sites. The extension must load these config files in global scope (since they set global variables). I'd rather not force authors to write "global" in the mini config files in front of every variable, so they can directly copy code from LocalSettings.php (or from mediawiki.org docs) into the mini config files. And if they ever overlook a "global", it's a subtle bug waiting to happen.

Is there a way to manage this kind of loading with wfLoadExtension/extension.json?

Legoktm (talkcontribs)

No, it's really not possible. The main issue I think you'd run into is that with extension.json, the load order of configuration has changed. All configuration is expected to be in place before extensions are loaded and initialized in Setup.php. Trying to go back and then change some configuration stuff is unlikely to work.

Additionally, the requirement of executing extension code in global scope goes directly against the design principles of extension.json, which was to eliminate that global scope.

Jdforrester (WMF) (talkcontribs)

Why can't they set configuration in LocalSettings in the normal way (which are all implicitly global), and then you write a post-registration function that takes their config and injects it into your extension's config objects?

Maiden taiwan (talkcontribs)

Hmm... may I try to unpack the second half of your sentence with a specific example?

Suppose I'm running a wiki farm with two wikis called Wiki1 and Wiki2 that share a MediaWiki tree. Wiki1 simply uses LocalSettings.php for configuration. Wiki2 has a second config file (to be loaded by my extension) that overrides values in LocalSettings.

Suppose LocalSettings.php sets $wgFoobar = true, and Wiki2's config file consists of one line:

$wgFoobar = false;

that is intended to override the value in LocalSettings.

I know how to write a "post-registration function" for my extension, as you suggest, but what you do you mean that the function "takes their config and injects it into your extensions config objects"? I am not sure what a "config object" is. What specifically would be done with the config file for Wiki2 to run its code?

Thank you very much for your help!!!

Reply to "How to instantiate a "Foo" object right after wfLoadExtension('Foo')?"