extensions/ExtensionName/ExtensionName.php:
$wgHooks['LanguageGetMagic'][] = 'ExtensionName::LanguageGetMagic'; $wgExtensionFunctions[] = array( 'ExtensionName', 'ExtensionFunction' ); $wgAutoloadClasses['ExtensionName'] = dirname(__FILE__).'/ExtensionName.class.php'; $wgAutoloadClasses['ExtensionName_Functions'] = dirname(__FILE__).'/ExtensionName.functions.php';
extensions/ExtensionName/ExtensionName.class.php:
class ExtensionName { static public function LoadParserFunctions() { global $wgParser; $wgParser->setFunctionHook( 'foo', array( 'ExtensionName_Functions', 'foo' ) ); return true; } static public function SetupLanguageMagic( &$magicWords, $langCode ) { /* ... Initilization for your ParserFunction magic words ... */ return true; } }
extension/ExtensionName/ExtensionName.functions.php:
class ExtensionName_Functions { static public function foo( &$parser, $a = '', $b = '', $c = '' ) { return "$a foo $b foo $c"; } }
Though my thoughts may diverge from how other people do it. I particularly like in this setup how only the things like initialization of parser functions, and their magic words go in the base class. And that's autoloaded after MediaWiki is well and ready to load it. And the actual heavy parserfunctions that make up the extension is off in a completely separate file which never ends up loaded unless we're doing parsing and someone called that parser function. This is basically how I set up most of my extensions, namely WikiCode. However, in that case with how many varying functions I have, I went the road of splitting them up into groups, and doing the autoloading inside of the ExtensionFunction. As well as registration of the parserfunctions.
~Daniel Friesen(Dantman) of: -The Gaiapedia (http://gaia.wikia.com) -Wikia ACG on Wikia.com (http://wikia.com/wiki/Wikia_ACG) -and Wiki-Tools.com (http://wiki-tools.com)
Jim Hu wrote:
On May 22, 2008, at 9:38 AM, MinuteElectron wrote:
DanTMan wrote:
Tim Starling wrote:
Loading large amounts of code is slow and memory hungry, especially on installations without an opcode cache. So lazy-load your code, by putting everything into autoloaded classes. Use static member functions for hooks.
^_^ I've been creating classes with the name of my extension for awhile and putting all the hooks and extension functions into them and autoloading the class for awhile now. Heh, I just did it because it looked cleaner than using ugly efExtName functions, didn't know it was a design basic.
Of course, this loads the entire class -- even anything you might not need at that time; any extension functions you want in $wgExtensionFunctions could just be put in the global scope anyway since they are always going to be loaded regardless of what code path is chosen.
Being a self-taught amateur, I need an explanation of how to do this...is this what you mean?
Bad: one file: myExtension.php: $wgHooks[the hook][] = efMyExtension; function efMyExtension(){ lots of code ... }
Good: two or more files:
- myExtension/includes/myExtension.php
$wgHooks[the hook][] = efMyExtension; $wgAutoloadClasses['myExtensionClass'] = dirname(__FILE__).myExtensionClass.php;
function efMyExtension(params){ $foo = new myExtensionClass; $foo->execute(params) }
- myExtension/class.myExtensionClass.php
class myExtensionClass{ function __construct(){ } function execute(param){ lots of code } }
Jim
MinuteElectron.
Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
===================================== Jim Hu Associate Professor Dept. of Biochemistry and Biophysics 2128 TAMU Texas A&M Univ. College Station, TX 77843-2128 979-862-4054
Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l