[Mediawiki-l] How best to set up hooks in an extension function ?

Tim Starling tstarling at wikimedia.org
Wed Jul 1 00:11:20 UTC 2009


Richard Alan wrote:
> I thought I had read somewhere that it was not a good idea to set
> function hooks inside of a class in this way.
> what follows is just one of many examples out there:
> 
> $wgExtensionFunctions[]        = 'wfSetupCSS'
> 
> function wfSetupCSS() {
>     global $wgCSS;
>     $wgCSS = new CSS();
> }
> 
> class CSS {
> 
>     function CSS() {
>         global $wgParser, $wgCSSMagic;
>         $wgParser->setFunctionHook($wgCSSMagic, array($this, 'magicCss'));
>         }
> 
> 
> Any other suggestions on how to implement this type of functionality.
> Would it be better not to instantiate the CSS
> class and just use a static class with a function to set the hook like this:
> 
> $wgExtensionFunctions[]        = 'CSS::SetupCSS'
> 
> Where SetupCSS was a static function to set the hook.

For recent versions of MediaWiki use the ParserFirstCallInit hook:

$wgHooks['ParserFirstCallInit'][] = 'wfCSSFirstCallInit';
function wfCSSFirstCallInit( &$parser ) {
    global $wgCSSMagic, $wgCSS;
    $parser->setFunctionHook($wgCSSMagic, array($wgCSS, 'magicCss'))
}

But don't call your class name "CSS" and don't name the global $wgCSS,
use something less likely to be broken by future conflicting changes
in the MediaWiki core or in PHP. And don't use a configurable magic
word name, just use a string literal.

-- Tim Starling




More information about the MediaWiki-l mailing list