Helper class for creating MediaWiki parser functions.
- Download this extension and put the "ParserFunctionHelper" directory into your MediaWiki "extensions" directory.
- Add the following code at the bottom of your LocalSettings.php:
wfLoadExtension('ParserFunctionHelper');
Once installed, develop your own extension that uses ParserFunctionHelper (ParserFunctionHelper doesn't do anything on it's own).
To generate a new parser function extension, run the following:
cd /path/to/your/mediawiki/extensions/ParserFunctionHelper
php createParserFunction.php --ext-name=MyNewExtension --function=my-parser-function --your-name="Your Full Name"
A new extension will be generated at /path/to/your/mediawiki/extensions/MyNewExtension
Edit /path/to/your/mediawiki/extensions/MyNewExtension/includes/<YourExtensionName>.php
to do what you want.
Then add your new extensions to LocalSettings.php
with wfLoadExtension('YourExtensionName');
.
Below are old instructions. I haven't had time to figure out what's still applicable.
- Copy the BasicParserFunction extension code from https://github.com/enterprisemediawiki/BasicParserFunction and put the files in your wiki's extension directory.
- Rename your new BasicParserFunction directory to whatever you want your extension to be called. Also, throughout the extension the name "BasicParserFunction" is used in many places. You'll want to rename these as well. You can skip this step initially if you are just learning and don't mind leaving your extension called "BasicParserFunction".
- View the 6 files in your extension (this tutorial assumes you kept it called "BasicParserFunction"):
- BasicParserFunction.php: The "entry point" for your extension, where everything is setup for MediaWiki
- Magic.php: Defines the name of your parser function(s)
- README.md: Documenation
- SubstrCount.php: This is the meat of your parser function, where the logic is handled.
- i18n/en.json: Internationalization file, specifying what text should be displayed if you're using the English version of a wiki.
- i18n/es.json: Just like en.json, but Spanish instead of English
- Edit three lines in BasicParserFunction.php:
- The variable
$GLOBALS['egParserFunctionHelperClasses'][]
can be changed to whatever your parser function should be called. In BasicParserFunction it is "BasicParserFunction\SubstrCount", but another example could be "MyParserFunction\DoImportantFunction". - The function you define in the previous step needs to have a file associated with it, and you need to tell MediaWiki to "autoload" it. So the line that starts with $GLOBALS['wgAutoloadClasses'] should be changed to reflect your parser functions name.
- (optional, for now) Edit the "credits" line. See https://www.mediawiki.org/wiki/Manual:$wgExtensionCredits for more info.
- Edit Magic.php:
- Change the name of your parser function!
- Change the name of SubstrCount.php to whatever you want your parser function to be called.
- Edit your newly-named file. The SubstrCount.php file includes inline annotations for its function, but the gist is this:
- The __construct function defines the name, parameters and defaults for the parser function
- The render function defines what the function does