Adding Hooks - mooeypoo/MWStew GitHub Wiki

There are hundreds of hooks available in MediaWiki, and making them available for developers is one of MWStew's purpose. In general, working with hooks for new developers can be daunting, so MWStew allows users to choose the hooks they need in their extensions and automatically adds their function (with full signature and documentation) to the Hooks.php file. It also connects the hook method to the hook itself in extension.json and adds the hook to the list in the main MWStew generator form, according to its category.

Adding each one with a template is a bit tedious. If you are looking for ways to contribute to MWStew, adding some hooks is a great help!

You can add hooks by looking at https://www.mediawiki.org/wiki/Manual:Hooks

Hooks in MWStew

MWStew uses twig templates to add hook methods to the Hooks.php file and connect them in extension.json.

The easiest way to add hooks is to look at https://www.mediawiki.org/wiki/Manual:Hooks and then go over the hook's documentation page. The documentation page contains the hook's description and, most of the time, the hook's method definition with parameter names.

PLEASE NOTE that hooks that have :: in their name are documented wrong in MediaWiki.org and have the wrong method name! Please use the conversion method below (see the section about hooks with '::' in their name) when writing their 'onHookName' method name.

Adding a new Hook

For each hook, you need to add:

  • in includes/data/hooks.json Find the hook's category and under it, add the hook's actual name with its description.
  • in includes/twig/templates/_hooks Add the file HookName.php.twig with the method signature and documentation. The method, in general is the hook's name with a prefix 'on', as in, 'onHookName' (see exception below for names containing ::)

Adding a new category of hooks

If the category doesn't exist in the includes/data/hooks.json file, please add it.

To add a new category:

  • Add a new category object into the includes/data/hooks.json, The format should be lower-cased with dashes.
  • Please make sure the json is structure still valid. New hook definitions will go inside that new json sub-object.
  • Add the new category translation to the i18n/en.json file and its documentation to i18n/qqq.json file. Category translation keys are prefixed with form-section-hooks-[category-name] (without the brackets)

Example of adding a new category

For example, adding a category "New hooks":

In includes/data/hooks.json:

{
	[...]
	"new-hooks": {
	}
}

In i18n/en.json:

	"form-section-hooks-new-hooks": "New hooks",

In i18n/qqq.json:

	"form-section-hooks-new-hooks": "Title for the section containing hooks for new hooks category.",

Hooks with :: in their names

Some new hook names contain :: characters. These are okay to use as hook names, but are not valid as PHP methods. In that case, MWStew automatically translates the method name to remove :: and upper-case the words.

To convert the name:

  • Upper-case all characters immediately following the ::
  • Remove all ::
  • Add 'on' before the name.

Note that the filename and definition still use the actual hook name (with ::) but only the method name inside the filename.php.twig uses the converted name.

For example, the hook EditPage::attemptSave:

  • Definition in includes/data/hooks.json: "EditPage::attemptSave": "Called before an article is saved, that is at the beginning of internalAttemptSave() is called.",
  • Filename: includes/twig/templates/_hooks/EditPage::attemptSave.php.twig
  • Inside the file, the method definition is public static function onEditPageAttemptSave( $editpage ) { ... }

Important notes

  • Do not add hooks that are marked as "Deprecated"
  • Please make sure that the hook content is indented 1 tab.
  • Add @see [url for the hook definition in mediawiki.org] in the documentation
  • Make sure all parameters are properly documented with their types and definitions
  • Add /* Your code here */ in the body of the method

Examples

You can add hooks by looking at https://www.mediawiki.org/wiki/Manual:Hooks

Example #1: Adding the hook "AlternateEdit"

The AlternateEdit hook is in edit-page category.

Go to the hook's definition page https://www.mediawiki.org/wiki/Manual:Hooks/AlternateEdit

In includes/data/hooks.json:

{
	"edit-page": {
		[...],
		"AlternateEdit": "This hook gets called at the beginning of &action=edit, before any user permissions are checked or any edit checking is performed."
	}
	[...]
}

Add a new file includes/twig/templates/_hooks/AlternateEdit.php.twig with the content:

	/**
	 * This hook gets called at the beginning of &action=edit,
	 * before any user permissions are checked or any edit
	 * checking is performed.
	 *
	 * Can be used to override the standard MediaWiki edit screen
	 * with a customized edit screen.
	 *
	 * @see https://www.mediawiki.org/wiki/Manual:Hooks/AlternateEdit
	 *
	 * @param EditPage $editpage the editpage (object) being called
	 * @return {boolean} Return "true" to proceed with EditPage::edit method, or "false" to exit the EditPage::edit method.
	 */
	public static function onAlternateEdit( $editpage ) {
		/* Your code here */
	}

Example #2: Adding the hook "EditPage::showEditForm:fields"

The AlternateEdit hook is in edit-page category.

Go to the hook's definition page https://www.mediawiki.org/wiki/Manual:Hooks/EditPage::showEditForm:fields

In includes/data/hooks.json:

{
	"edit-page": {
		[...],
		"EditPage::showEditForm:fields": "This hook is called before edit box and the toolbar are published."
	}
	[...]
}

Add a new file includes/twig/templates/_hooks/EditPage::showEditForm:fields.php.twig with the content (NOTICE that you will need to adjust the method name inside the file!)

	/**
	 * This hook is called before edit box and the toolbar are published.
	 *
	 * @see https://www.mediawiki.org/wiki/Manual:Hooks/EditPage::showEditForm:fields
	 *
	 * @param EditPage $editpage The current EditPage object
	 * @param OutputPage $output The Output page
	 * @return true
	 */
	public static function onEditPageShowEditFormFields( EditPage $editpage, OutputPage $output ) {
		/* Your code here */
	}
⚠️ **GitHub.com Fallback** ⚠️