Manual:Hooks/LoadExtensionSchemaUpdates

From MediaWiki.org
Jump to navigation Jump to search
LoadExtensionSchemaUpdates
Available from version 1.10.1
Fired when MediaWiki is updated to allow extensions to update the database
Define function:
public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) { ... }
Attach hook:

In extension.json:

{
	"Hooks": {
		"LoadExtensionSchemaUpdates": "MyExtensionHooks::onLoadExtensionSchemaUpdates"
	}
}

For MediaWiki ≤1.25:

$wgHooks['LoadExtensionSchemaUpdates'][] = 'MyExtensionHooks::onLoadExtensionSchemaUpdates';
Called from:File(s): installer/DatabaseUpdater.php
Function(s): __construct

For more information about attaching hooks, see Manual:Hooks.
For examples of extensions using this hook, see Category:LoadExtensionSchemaUpdates extensions.

Usage[edit]

If your extension requires changes to the database when MediaWiki is updated, do it with this hook.
Warning Warning: If your extension is used on any production WMF-hosted wiki please follow the Schema change guide.
Warning Warning: Your extension should not modify any MW core database. Instead, the extension should create new tables with foreign key to the relevant MW table.

Summary[edit]

  1. Create your hook as indicated below. Each examples shows how to setup the hook function. However, if you have more than one schema update, you can put them in the same function (and it will use multiple SQL files).
  2. Make sure the hook has access to any necessary SQL files, which are usually located in an sql/ directory.
  3. Format the SQL files correctly. See the ArticleFeedbackv5 SQL, and the corresponding hooks file, for some examples.
  4. From the command line run the php maintenance/update.php script to update your wiki’s database with your extension’s LoadExtensionSchemaUpdates hook. See the update.php manual for more information.

>= 1.25[edit]

Extension registration was introduced in 1.25, and so the Hooks section of extension.json should be used instead of $wgHooks. For example:

"Hooks": {
    "LoadExtensionSchemaUpdates": "MediaWiki\\Extension\\ExtensionName\\Hooks::onLoadExtensionSchemaUpdates"
}

And in ExtensionName/includes/Hooks.php:

namespace MediaWiki\Extension\ExtensionName;

use DatabaseUpdater;

class Hooks {
	public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) {
		$updater->modifyExtensionField(
			'tablename',
			'name_of_field',
			 __DIR__ . '/sql/patch_file_changing_field.sql'
		);
	}
}

The code of the hook callback is the same as for earlier versions (see below).

See also[edit]