Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkp/pkp-lib#10729 fix I9707_WeblateUILocales, consider locales in arr… #10734

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use APP\core\Application;
use PKP\install\DowngradeNotSupportedException;
use PKP\migration\Migration;

Expand Down Expand Up @@ -50,9 +49,9 @@ public function up(): void

$update = function (object $localeId, string $settingName, string $settingValue): void {
DB::table($this->getContextSettingsTable())
->where($this->getContextIdColumn(), '=', $localeId->{$this->getContextIdColumn()})
->where('setting_name', '=', $settingName)
->update(['setting_value' => $settingValue]);
->where($this->getContextIdColumn(), '=', $localeId->{$this->getContextIdColumn()})
->where('setting_name', '=', $settingName)
->update(['setting_value' => $settingValue]);
};

$pluck = fn (object $localeId, string $settingName): array => json_decode(
Expand Down Expand Up @@ -94,65 +93,27 @@ public function up(): void
: 'TABLE_SCHEMA';

$updateLength = fn (string $l) => collect(
DB::select("
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = ? AND $schemaLocName = ?",
DB::select(
"
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = ? AND {$schemaLocName} = ?",
Comment on lines +96 to +100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the database has few tables/columns to loop through, then the Schema::getAllTables() (or the DB::getDoctrineSchemaManager()->listXXX()) together with the Schema::getColumns() will provide a better compatibility and simplify the code.

It's also possible to cache the return of getAllTables()/getColumns() to have less database requests.

[$l, DB::connection()->getDatabaseName()]
)
)->each(fn (\stdClass $sc) => Schema::table(
)->each(
fn (\stdClass $sc) => Schema::table(
$sc->TABLE_NAME ?? $sc->table_name,
fn (Blueprint $table) => collect(Schema::getColumns($sc->TABLE_NAME ?? $sc->table_name))
->where('name', $l)
->first(default:[])['nullable'] ?? false
? $table->string($l, 28)->nullable()->change()
: $table->string($l, 28)->change()
? $table->string($l, 28)->nullable()->change()
: $table->string($l, 28)->change()
)
);

$updateLength('primary_locale');
$updateLength('locale');

/**
* Convert locales
*/

$localesTable = [
"be@cyrillic" => "be",
"bs" => "bs_Latn",
"fr_FR" => "fr",
"nb" => "nb_NO",
"sr@cyrillic" => "sr_Cyrl",
"sr@latin" => "sr_Latn",
"uz@cyrillic" => "uz",
"uz@latin" => "uz_Latn",
"zh_CN" => "zh_Hans",
];

$tableNames = array_merge([
'author_settings',
'controlled_vocab_entry_settings',
'publication_settings',
'submission_file_settings',
'submission_settings',
'submissions',
], Application::get()->getName() === 'omp'
? ['publication_format_settings', 'submission_chapter_settings',]
: ['publication_galley_settings', 'publication_galleys',]
);

collect($tableNames)
->each(fn (string $tn) => collect(DB::table($tn)->select('locale')->distinct()->get())
->each(function (\stdClass $sc) use ($tn, $localesTable) {
if (isset($localesTable[$sc->locale])) {
DB::table($tn)
->where('locale', '=', $sc->locale)
->update([
'locale' => $localesTable[$sc->locale]
]);
}
})
);
}

/**
Expand Down
203 changes: 190 additions & 13 deletions classes/migration/upgrade/v3_5_0/I9707_WeblateUILocales.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,206 @@
namespace PKP\migration\upgrade\v3_5_0;

use Illuminate\Database\PostgresConnection;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
use PKP\install\DowngradeNotSupportedException;
use PKP\migration\Migration;

class I9707_WeblateUILocales extends Migration
abstract class I9707_WeblateUILocales extends Migration
{
protected string $CONTEXT_TABLE = '';
protected string $CONTEXT_SETTINGS_TABLE = '';
protected string $CONTEXT_COLUMN = '';
Comment on lines +25 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the pattern at the other migration of this PR is better (i.e. you're forced to provide the values):

Suggested change
protected string $CONTEXT_TABLE = '';
protected string $CONTEXT_SETTINGS_TABLE = '';
protected string $CONTEXT_COLUMN = '';
abstract protected function getContextTable(): string;
abstract protected function getContextSettingsTable(): string;
abstract protected function getContextIdColumn(): string;


/**
* Run the migrations.
*/
public function up(): void
{
$localesTable = [
$affectedLocales = $this->getAffectedLocales();

// update all locale and primary_locale columns
$schemaLocName = (DB::connection() instanceof PostgresConnection) ? 'TABLE_CATALOG' : 'TABLE_SCHEMA';
$renameLocale = fn (string $l) => collect(DB::select("SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = ? AND {$schemaLocName} = ?", [$l, DB::connection()->getDatabaseName()]))
Comment on lines +37 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the database has few tables/columns to loop through, then the Schema::getAllTables() (or the DB::getDoctrineSchemaManager()->listXXX()) together with the Schema::getColumns() will provide a better compatibility and simplify the code.

It's also possible to cache the return of getAllTables()/getColumns() to have less database requests.

->each(function (\stdClass $sc) use ($l, $affectedLocales) {
foreach ($affectedLocales as $uiLocale => $weblateLocale) {
DB::table($sc->TABLE_NAME ?? $sc->table_name)->where($l, '=', $uiLocale)->update([$l => $weblateLocale]);
}
Comment on lines +40 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop can be turned into a single query:
...->whereIn($l, $uiLocales)->update([$l => DB::raw("CASE {$l} WHEN X THEN A WHEN Y THEN B END")]);.

});
$renameLocale('primary_locale');
$renameLocale('locale');

// site supported and installed locales
$site = DB::table('site')
->select(['supported_locales', 'installed_locales'])
->first();
$this->updateArrayLocale($site->supported_locales, 'site', 'supported_locales');
$this->updateArrayLocale($site->installed_locales, 'site', 'installed_locales');

// users locales
$migration = $this;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this isn't needed, you can just use $this inside the function.

DB::table('users')->chunkById(1000, function ($users) use ($migration) {
foreach ($users as $user) {
$migration->updateArrayLocale($user->locales, 'users', 'locales', null, 'user_id', $user->user_id);
}
}, 'user_id');
Comment on lines +56 to +60
Copy link
Contributor

@jonasraoni jonasraoni Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As these "array" columns are using JSON, then the updateArrayLocale() method could be updated to issue a single query, basically a sequence of self-enclosed replaces (e.g. locales = REPLACE(REPLACE(locales, '"X"', "A"), '"Y"', "B")).

Some installations have thousands of users, due to spammers, so anything that avoids looping through the users is worth in my opinion.


// context supported locales
$supportedDefaultSubmissionLocale = DB::table($this->CONTEXT_SETTINGS_TABLE)
->where('setting_name', '=', 'supportedDefaultSubmissionLocale')
->get()
->pluck('setting_value')
->first();
if (in_array($supportedDefaultSubmissionLocale, array_keys($affectedLocales))) {
DB::table($this->CONTEXT_SETTINGS_TABLE)
->where('setting_name', '=', 'supportedDefaultSubmissionLocale')
->update(['setting_value' => $affectedLocales[$supportedDefaultSubmissionLocale]]);
}
$contextLocaleSettingNames = [
'supportedFormLocales',
'supportedLocales',
'supportedSubmissionLocales',
'supportedAddedSubmissionLocales',
'supportedSubmissionMetadataLocales',
];
foreach ($contextLocaleSettingNames as $contextLocaleSettingName) {
$contextSettingLocales = DB::table($this->CONTEXT_SETTINGS_TABLE)
->where('setting_name', '=', $contextLocaleSettingName)
->get();
foreach ($contextSettingLocales as $contextSettingLocale) {
$this->updateArrayLocale($contextSettingLocale->setting_value, $this->CONTEXT_SETTINGS_TABLE, 'setting_value', $contextLocaleSettingName, $this->CONTEXT_COLUMN, $contextSettingLocale->{$this->CONTEXT_COLUMN});
}
}


// plugin_settings
// customBlockManager
$blockPluginName = 'customblockmanagerplugin';
$blockLocalizedSettingNames = ['blockTitle', 'blockContent'];

$contextIds = DB::table($this->CONTEXT_TABLE)
->get()
->pluck($this->CONTEXT_COLUMN);

foreach ($contextIds as $contextId) {
$blocks = DB::table('plugin_settings')
->where('plugin_name', '=', $blockPluginName)
->where('setting_name', '=', 'blocks')
->where('context_id', '=', $contextId)
->get()
->pluck('setting_value');

if (!$blocks->isEmpty()) {
$blockNames = $blocks->first();

$blocksArray = json_decode($blockNames, true);
if (is_null($blocksArray)) {
$blocksArray = unserialize($blockNames);
}

foreach ($blocksArray as $block) {
foreach ($blockLocalizedSettingNames as $blockLocalizedSettingName) {
$blockLocalizedContent = DB::table('plugin_settings')
->where('plugin_name', '=', $block)
->where('setting_name', '=', $blockLocalizedSettingName)
->where('context_id', '=', $contextId)
->first();

if (isset($blockLocalizedContent)) {
$this->updateArrayKeysLocaleSetting($blockLocalizedContent->setting_value, 'plugin_settings', 'plugin_setting_id', $blockLocalizedContent->plugin_setting_id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed fast, but it seems to me that this method is doing something similar to the updateArrayLocale().

}
}
}
}
}
}

/**
* Update array of locales
*/
public function updateArrayLocale(string $dbLocales, string $table, string $column, ?string $settingName = null, ?string $tableKeyColumn = null, ?int $id = null)
{
$locales = json_decode($dbLocales) ?: [];
$affectedLocales = $this->getAffectedLocales();
$localesToMigrate = array_intersect($locales, array_keys($affectedLocales));
if (empty($localesToMigrate)) {
return;
}

$newLocales = [];
foreach ($locales as $locale) {
$updatedLocale = $this->getUpdatedLocale($locale);
if (!is_null($updatedLocale)) {
if (!in_array($updatedLocale, $newLocales)) {
$newLocales[] = $updatedLocale;
}
} else {
$newLocales[] = $locale;
}
}

DB::table($table)
->when(
isset($tableKeyColumn) && isset($id),
fn (Builder $query) => $query->where($tableKeyColumn, '=', $id)
)
->when(
isset($settingName),
fn (Builder $query) => $query->where('setting_name', '=', $settingName)
)
->update([
$column => $newLocales
]);
}

/**
* Update locales that are array keys
*/
public function updateArrayKeysLocaleSetting(string $contentArray, string $table, string $tableKeyColumn, int $id)
{
$contentElements = json_decode($contentArray, true) ?: [];
$affectedLocales = $this->getAffectedLocales();
$localesToMigrate = array_intersect_key($contentElements, $affectedLocales);
if (empty($localesToMigrate)) {
return;
}

$newLocales = [];
foreach (array_keys($contentElements) as $locale) {
$updatedLocale = $this->getUpdatedLocale($locale);
if (!is_null($updatedLocale)) {
$newLocales[$updatedLocale] = $contentElements[$locale];
} else {
$newLocales[$locale] = $contentElements[$locale];
}
}
$jsonString = json_encode($newLocales);
DB::table($table)
->where($tableKeyColumn, '=', $id)
->update([
'setting_value' => $jsonString
]);
}

/**
* Returns null if no conversion is needed or
* the new, converted value.
*/
public function getUpdatedLocale(string $localeValue): ?string
{
$affectedLocales = $this->getAffectedLocales();
if (!in_array($localeValue, array_keys($affectedLocales))) {
return null;
}
return $affectedLocales[$localeValue];
Comment on lines +205 to +209
Copy link
Contributor

@jonasraoni jonasraoni Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$affectedLocales = $this->getAffectedLocales();
if (!in_array($localeValue, array_keys($affectedLocales))) {
return null;
}
return $affectedLocales[$localeValue];
return $this->getAffectedLocales()[$localeValue] ?? null;

Or a safer option with fallback (requires reviewing if it makes sense on the call sites)

Suggested change
$affectedLocales = $this->getAffectedLocales();
if (!in_array($localeValue, array_keys($affectedLocales))) {
return null;
}
return $affectedLocales[$localeValue];
return $this->getAffectedLocales()[$localeValue] ?? $localeValue;

}

/**
* Returns the effected locales along with the corresponding rename for each
*/
public static function getAffectedLocales(): array
{
return [
'be@cyrillic' => 'be',
'bs' => 'bs_Latn',
'fr_FR' => 'fr',
Expand All @@ -39,17 +227,6 @@ public function up(): void
'uz@latin' => 'uz_Latn',
'zh_CN' => 'zh_Hans',
];

$schemaLocName = (DB::connection() instanceof PostgresConnection) ? 'TABLE_CATALOG' : 'TABLE_SCHEMA';
$renameLocale = fn (string $l) => collect(DB::select("SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = ? AND {$schemaLocName} = ?", [$l, DB::connection()->getDatabaseName()]))
->each(function (\stdClass $sc) use ($l, $localesTable) {
foreach ($localesTable as $uiLocale => $weblateLocale) {
DB::table($sc->TABLE_NAME ?? $sc->table_name)->where($l, '=', $uiLocale)->update([$l => $weblateLocale]);
}
});

$renameLocale('primary_locale');
$renameLocale('locale');
}

/**
Expand Down