Skip to content

Commit

Permalink
Create/update schema with update.php script
Browse files Browse the repository at this point in the history
Add handler for 'LoadExtensionSchemaUpdates' hook and create/update
database schema when maintenance script update.php from core is run.
Without this, extension users needed to manually run the SQL scripts
from sql/ folder in order for the extension to be functional after
downloading and registering in LocalSettings.php
Documentation page on mediawiki.org does not list these as necessary
steps towards installing the extension and only mentiones
sql/contenttranslation.sql file.

Also, split SQL files per table definition and remove
unnecessary (and potentially dangerous) DROP TABLE statements
at the beginning of files.

Change-Id: I449817e3038bfc4c7a139acc73905ca19c19f72c
  • Loading branch information
petarpetkovic committed Apr 23, 2019
1 parent caae07b commit bbd5328
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 41 deletions.
1 change: 1 addition & 0 deletions extension.json
Expand Up @@ -28,6 +28,7 @@
"ContentTranslationAliases": "ContentTranslation.alias.php"
},
"Hooks": {
"LoadExtensionSchemaUpdates": "ContentTranslation\\Hooks::onLoadExtensionSchemaUpdates",
"BeforePageDisplay": "ContentTranslation\\Hooks::addModules",
"GetBetaFeaturePreferences": "ContentTranslation\\Hooks::onGetBetaFeaturePreferences",
"ResourceLoaderGetConfigVars": "ContentTranslation\\Hooks::addConfig",
Expand Down
39 changes: 39 additions & 0 deletions includes/Hooks.php
Expand Up @@ -54,6 +54,45 @@ public static function isEnabledForUser( User $user ) {
return self::isBetaFeatureEnabled( $user );
}

/**
* Hook: LoadExtensionSchemaUpdates
* @param DatabaseUpdater $updater
*/
public static function onLoadExtensionSchemaUpdates( $updater ) {
global $wgContentTranslationCluster, $wgContentTranslationDatabase;

// Following tables should only be created if both cluster and database are false.
// Otherwise they are not created in the place they are accesses, because
// DatabaseUpdater does not support other databases other than main wiki schema.
if ( $wgContentTranslationCluster !== false || $wgContentTranslationDatabase !== false ) {
return;
}

$dir = dirname( __DIR__ );

$updater->addExtensionTable( 'cx_translations', "$dir/sql/translations.sql" );
$updater->addExtensionTable( 'cx_translators', "$dir/sql/translators.sql" );
$updater->addExtensionTable( 'cx_lists', "$dir/sql/lists.sql" );
$updater->addExtensionTable( 'cx_suggestions', "$dir/sql/suggestions.sql" );
$updater->addExtensionTable( 'cx_corpora', "$dir/sql/parallel-corpora.sql" );

$updater->addExtensionField(
'cx_translations',
'translation_cx_version',
"$dir/sql/patch-2018-03-contenttranslation-add-version.sql"
);
$updater->dropExtensionIndex(
'cx_translations',
'cx_translation_pair',
"$dir/sql/patch-update-cx-unique-index.sql"
);
$updater->addExtensionIndex(
'cx_translations',
'cx_translation_ref',
"$dir/sql/patch-update-cx-unique-index.sql"
);
}

/**
* Hook: BeforePageDisplay
* @param OutputPage $out
Expand Down
18 changes: 0 additions & 18 deletions sql/lists.sql
@@ -1,6 +1,5 @@
-- Content translation suggestion related tables

DROP TABLE IF EXISTS /*_*/cx_lists;
CREATE TABLE /*_*/cx_lists (
-- List id
cxl_id int NOT NULL PRIMARY KEY auto_increment,
Expand All @@ -27,20 +26,3 @@ CREATE UNIQUE INDEX /*_*/cx_lists_relevant ON /*_*/cx_lists (
cxl_end_time
);

DROP TABLE IF EXISTS /*_*/cx_suggestions;
CREATE TABLE /*_*/cx_suggestions (
-- Foreign key to cxl_id
cxs_list_id int NOT NULL,
-- Source language code
cxs_source_language varbinary(36) NOT NULL,
-- Target language code
cxs_target_language varbinary(36),
-- Title of the suggestion
cxs_title varbinary(512) NOT NULL
) /*$wgDBTableOptions*/;

CREATE INDEX /*i*/cx_suggestions_by_lang ON /*_*/cx_suggestions (
cxs_list_id,
cxs_source_language,
cxs_target_language
);
1 change: 0 additions & 1 deletion sql/parallel-corpora.sql
@@ -1,7 +1,6 @@
-- Table to store parallel corpora data
-- Special cxc_section_id of CX_CATEGORY_METADATA is used to store pairs of source and target categories

DROP TABLE IF EXISTS /*_*/cx_corpora;
CREATE TABLE /*_*/cx_corpora (
cxc_id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
-- Translation to join with cx_translations
Expand Down
16 changes: 16 additions & 0 deletions sql/suggestions.sql
@@ -0,0 +1,16 @@
CREATE TABLE /*_*/cx_suggestions (
-- Foreign key to cxl_id
cxs_list_id int NOT NULL,
-- Source language code
cxs_source_language varbinary(36) NOT NULL,
-- Target language code
cxs_target_language varbinary(36),
-- Title of the suggestion
cxs_title varbinary(512) NOT NULL
) /*$wgDBTableOptions*/;

CREATE INDEX /*i*/cx_suggestions_by_lang ON /*_*/cx_suggestions (
cxs_list_id,
cxs_source_language,
cxs_target_language
);
22 changes: 0 additions & 22 deletions sql/contenttranslation.sql → sql/translations.sql
@@ -1,12 +1,3 @@
-- Example steps for creating a new database for testing this:
-- mysql> CREATE DATABASE contenttranslation;
-- mysql> USE contenttranslation;
-- In the next line replace "USER" with the value of $wgDBuser from your LocalSettings.php,
-- and replace localhost with your hostname if needed.
-- mysql> GRANT ALL ON contenttranslation.* to 'USER'@'localhost';
-- mysql> SOURCE contenttranslation.sql

DROP TABLE IF EXISTS /*_*/cx_translations;
CREATE TABLE /*_*/cx_translations (
-- translation id. Autogenerated.
translation_id int primary key auto_increment,
Expand Down Expand Up @@ -55,16 +46,3 @@ CREATE INDEX /*i*/cx_translation_languages ON /*_*/cx_translations (
translation_target_language,
translation_status
);

DROP TABLE IF EXISTS /*_*/cx_translators;
CREATE TABLE /*_*/cx_translators (
-- Translators id - global user id
translator_user_id int not null,
-- Translation id - foreign key to translations.translation_id
translator_translation_id int not null
) /*$wgDBTableOptions*/;

CREATE UNIQUE INDEX /*i*/cx_translation_translators ON /*_*/cx_translators (
translator_user_id,
translator_translation_id
);
11 changes: 11 additions & 0 deletions sql/translators.sql
@@ -0,0 +1,11 @@
CREATE TABLE /*_*/cx_translators (
-- Translators id - global user id
translator_user_id int not null,
-- Translation id - foreign key to translations.translation_id
translator_translation_id int not null
) /*$wgDBTableOptions*/;

CREATE UNIQUE INDEX /*i*/cx_translation_translators ON /*_*/cx_translators (
translator_user_id,
translator_translation_id
);

0 comments on commit bbd5328

Please sign in to comment.