Skip to content
Permalink
Browse files

Create/update schema with update.php script

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...
Pl217 committed Apr 8, 2019
1 parent caae07b commit bbd5328b3766ffb283372df59567423e9d0b8b0f
Showing with 67 additions and 41 deletions.
  1. +1 −0 extension.json
  2. +39 −0 includes/Hooks.php
  3. +0 −18 sql/lists.sql
  4. +0 −1 sql/parallel-corpora.sql
  5. +16 −0 sql/suggestions.sql
  6. +0 −22 sql/{contenttranslation.sql → translations.sql}
  7. +11 −0 sql/translators.sql
@@ -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",
@@ -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
@@ -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,
@@ -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,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
@@ -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
);
@@ -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,
@@ -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
);
@@ -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.
You can’t perform that action at this time.