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.
Sorry if the question appears a bit dumb but I'm interested to hear what the more experienced guys think.
Thanks,
Richard
Date: Wed, 1 Jul 2009 02:25:09 +0800 From: richard.philippines@gmail.com To: mediawiki-l@lists.wikimedia.org Subject: [Mediawiki-l] How best to set up hooks in an extension function ?
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.
Sorry if the question appears a bit dumb but I'm interested to hear what the more experienced guys think.
Thanks,
Richard _______________________________________________ MediaWiki-l mailing list MediaWiki-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-l
_________________________________________________________________ Vous voulez savoir ce que vous pouvez faire avec le nouveau Windows Live ? Lancez-vous ! http://www.microsoft.com/windows/windowslive/default.aspx
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
Thanks Tim,
Your advice was exactly what I needed. I'm setting up my code like below. ABC's are replaced with real names and I've removed most of the code to just show the important parts. I took out the line: $wgExtensionFunctions[] = 'wfSetupCSS' and instead create a new class and then call a function in the class to register the hooks.
-----------------------------------------------------------------------------------------------------------------------------------
File: ABC_setup.php
$oABC = new ABC(); $oABC->registerHooks();
-----------------------------------------------------------------------------------------------------------------------------------
File: ABC.php
class ABC {
private $abcd;
public function registerHooks() { global $wgHooks, $wgExtensionFunctions, $wgVersion; $wgHooks['SkinTemplateOutputPageBeforeExec'][] = array($this, 'onSkinTemplateOutputPageBeforeExec'); $wgHooks['OutputPageBeforeHTML'][] = array($this, 'onOutputPageBeforeHTML'); $wgHooks['LanguageGetMagic'][] = array($this, 'onLanguageGetMagic'); $wgHooks['ParserFirstCallInit'][] = array($this, 'onParserFirstCallInit'); }
function onParserFirstCallInit( ) {
global $wgParser; $wgParser->setFunctionHook etc ...
-----------------------------------------------------------------------------------------------------------------------------------
Can you let me know if this is what you are suggesting assuming the code will run on say 1.14 MW where the parserfirstcallinit is implemented.
Also any suggestions you might have? I'd like to be sure the framework meets with the latest standards.
One last question. Is this the best place for me to be posting this kind of question?
Thanks,
Richard
Date: Wed, 1 Jul 2009 18:21:29 +0800 From: richard.philippines@gmail.com To: mediawiki-l@lists.wikimedia.org Subject: Re: [Mediawiki-l] How best to set up hooks in an extension function ?
Thanks Tim,
Your advice was exactly what I needed. I'm setting up my code like below. ABC's are replaced with real names and I've removed most of the code to just show the important parts. I took out the line: $wgExtensionFunctions[] = 'wfSetupCSS' and instead create a new class and then call a function in the class to register the hooks.
File: ABC_setup.php
$oABC = new ABC(); $oABC->registerHooks();
File: ABC.php
class ABC {
private $abcd;
public function registerHooks() { global $wgHooks, $wgExtensionFunctions, $wgVersion; $wgHooks['SkinTemplateOutputPageBeforeExec'][] = array($this, 'onSkinTemplateOutputPageBeforeExec'); $wgHooks['OutputPageBeforeHTML'][] = array($this, 'onOutputPageBeforeHTML'); $wgHooks['LanguageGetMagic'][] = array($this, 'onLanguageGetMagic'); $wgHooks['ParserFirstCallInit'][] = array($this, 'onParserFirstCallInit'); }
function onParserFirstCallInit( ) {
global $wgParser; $wgParser->setFunctionHook etc ...
Can you let me know if this is what you are suggesting assuming the code will run on say 1.14 MW where the parserfirstcallinit is implemented.
Also any suggestions you might have? I'd like to be sure the framework meets with the latest standards.
One last question. Is this the best place for me to be posting this kind of question?
Thanks,
Richard _______________________________________________ MediaWiki-l mailing list MediaWiki-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-l
_________________________________________________________________ Découvrez toutes les possibilités de communication avec vos proches http://www.microsoft.com/windows/windowslive/default.aspx
Sebastien,Both of your replies to my post are empty. Can you please try again if you have a comment to make.
Richard
mediawiki-l@lists.wikimedia.org