Manual:Hooks/LoadExtensionSchemaUpdates

From MediaWiki.org

< Manual:Hooks
Jump to: navigation, search
LoadExtensionSchemaUpdates
Available from version 1.10.1
Fired when MediaWiki is updated to allow extensions to update the database

*Define function:
function fnMyHook( ' ) { ... }

*Attach hook:
$wgHooks['LoadExtensionSchemaUpdates'][] = 'fnMyHook';
Called from: updaters.inc

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


Contents

[edit] Usage

If your extension requires changes to the database when MediaWiki is updated, do it with this hook. Users can then update their wiki by running update.php

[edit] To add a new table

To add a new table, in the main file for your extension:

# Schema updates for update.php
$wgHooks['LoadExtensionSchemaUpdates'][] = 'fnMyHook';
function fnMyHook() {
    global $wgExtNewTables;
    $wgExtNewTables[] = array(
        'tablename',
        dirname( __FILE__ ) . '/table.sql' );
    return true;
}

The table.sql file should contain the necessary CREATE TABLE table definition.


[edit] To add a table and/or modify a field

If your extension has already added a table, but you need to modify a field in it for a new version, make the change in the sql file for the table (for people installing the extension after the change), then make a "patch" sql file to modify the field in the table (for people updating from an older version).

# Schema updates for update.php
$wgHooks['LoadExtensionSchemaUpdates'][] = 'fnMyHook';
function fnMyHook() {
    global $wgExtNewTables, $wgExtModifiedFields;
    $wgExtNewTables[] = array(
        'tablename',
        dirname( __FILE__ ) . '/table.sql' 
    );
    $wgExtModifiedFields[] = array(
        'table',
        'field_name',
        dirname( __FILE__ ) . '/table.patch.field_name.sql'
    );
    return true;
}

The table.patch.field_name.sql file should contain the necessary ALTER TABLE statement to update old versions of the schema.

[edit] All available options

$wgExtNewTables = array(); // table, dir
$wgExtNewFields = array(); // table, column, dir
$wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
$wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
$wgExtNewIndexes = array(); // table, index, dir
$wgExtModifiedFields = array(); //table, index, dir

[edit] See also

  • Manual:Hooks/ParserTestTables - This hook may also be necessary if your extension changes the behavior of the parser (parser functions, tag hooks, etc.)