Skip to content

enterprisemediawiki/ParserFunctionHelper

Repository files navigation

Parser Function Helper

Helper class for creating MediaWiki parser functions.

Installation

  1. Download this extension and put the "ParserFunctionHelper" directory into your MediaWiki "extensions" directory.
  2. 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).

Generate a new extension with script

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');.

Creating your own extension (old instructions)

Below are old instructions. I haven't had time to figure out what's still applicable.

  1. Copy the BasicParserFunction extension code from https://github.com/enterprisemediawiki/BasicParserFunction and put the files in your wiki's extension directory.
  2. 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".
  3. View the 6 files in your extension (this tutorial assumes you kept it called "BasicParserFunction"):
  4. BasicParserFunction.php: The "entry point" for your extension, where everything is setup for MediaWiki
  5. Magic.php: Defines the name of your parser function(s)
  6. README.md: Documenation
  7. SubstrCount.php: This is the meat of your parser function, where the logic is handled.
  8. i18n/en.json: Internationalization file, specifying what text should be displayed if you're using the English version of a wiki.
  9. i18n/es.json: Just like en.json, but Spanish instead of English
  10. Edit three lines in BasicParserFunction.php:
  11. 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".
  12. 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.
  13. (optional, for now) Edit the "credits" line. See https://www.mediawiki.org/wiki/Manual:$wgExtensionCredits for more info.
  14. Edit Magic.php:
  15. Change the name of your parser function!
  16. Change the name of SubstrCount.php to whatever you want your parser function to be called.
  17. Edit your newly-named file. The SubstrCount.php file includes inline annotations for its function, but the gist is this:
  18. The __construct function defines the name, parameters and defaults for the parser function
  19. The render function defines what the function does