From a927f109416304b87176241f1707ca3ec52a621d Mon Sep 17 00:00:00 2001 From: Refringe Date: Fri, 5 Jul 2024 14:35:00 -0400 Subject: [PATCH] Import Multiple Mod Authors The import script has been updated to pull in additional mod authors from the hub. --- .gitignore | 3 +- app/Jobs/ImportHubData.php | 52 +++++++++++++++++-- ...24_05_15_023706_create_mod_users_table.php | 2 + 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 5118f9d..6062808 100644 --- a/.gitignore +++ b/.gitignore @@ -7,8 +7,7 @@ /.idea /.phpunit.cache /.vscode -/caddy -/data/caddy +/data /node_modules /public/build /public/hot diff --git a/app/Jobs/ImportHubData.php b/app/Jobs/ImportHubData.php index 40c27d3..7136567 100644 --- a/app/Jobs/ImportHubData.php +++ b/app/Jobs/ImportHubData.php @@ -17,6 +17,7 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\DB; @@ -35,6 +36,7 @@ class ImportHubData implements ShouldBeUnique, ShouldQueue { // Stream some data locally so that we don't have to keep accessing the Hub's database. Use MySQL temporary // tables to store the data to save on memory; we don't want this to be a hog. + $this->bringFileAuthorsLocal(); $this->bringFileOptionsLocal(); $this->bringFileContentLocal(); $this->bringFileVersionLabelsLocal(); @@ -56,6 +58,30 @@ class ImportHubData implements ShouldBeUnique, ShouldQueue Artisan::call('scout:import', ['model' => '\App\Models\Mod']); } + /** + * Bring the file authors from the Hub database to the local database temporary table. + */ + protected function bringFileAuthorsLocal(): void + { + DB::statement('DROP TEMPORARY TABLE IF EXISTS temp_file_author'); + DB::statement('CREATE TEMPORARY TABLE temp_file_author ( + fileID INT, + userID INT + )'); + + DB::connection('mysql_hub') + ->table('filebase1_file_author') + ->orderBy('fileID') + ->chunk(200, function ($relationships) { + foreach ($relationships as $relationship) { + DB::table('temp_file_author')->insert([ + 'fileID' => (int) $relationship->fileID, + 'userID' => (int) $relationship->userID, + ]); + } + }); + } + /** * Bring the file options from the Hub database to the local database temporary table. */ @@ -456,6 +482,14 @@ class ImportHubData implements ShouldBeUnique, ShouldQueue ->chunkById(100, function (Collection $mods) use ($curl) { foreach ($mods as $mod) { + // Fetch any additional authors for the mod. + $modAuthors = DB::table('temp_file_author') + ->where('fileID', $mod->fileID) + ->pluck('userID') + ->toArray(); + $modAuthors[] = $mod->userID; // Add the primary author to the list. + $modAuthors = User::whereIn('hub_id', $modAuthors)->pluck('id')->toArray(); // Replace with local IDs. + $modContent = DB::table('temp_file_content') ->where('fileID', $mod->fileID) ->orderBy('fileID', 'desc') @@ -494,9 +528,9 @@ class ImportHubData implements ShouldBeUnique, ShouldQueue continue; } - $insertData[] = [ + $modData[] = [ 'hub_id' => (int) $mod->fileID, - 'user_id' => User::whereHubId($mod->userID)->value('id'), + 'users' => $modAuthors, 'name' => $modContent?->subject ?? '', 'slug' => Str::slug($modContent?->subject ?? ''), 'teaser' => Str::limit($modContent?->teaser ?? ''), @@ -513,9 +547,11 @@ class ImportHubData implements ShouldBeUnique, ShouldQueue ]; } - if (! empty($insertData)) { - Mod::upsert($insertData, ['hub_id'], [ - 'user_id', + if (! empty($modData)) { + // Remove the user_id from the mod data before upserting. + $insertModData = array_map(fn ($mod) => Arr::except($mod, 'users'), $modData); + + Mod::upsert($insertModData, ['hub_id'], [ 'name', 'slug', 'teaser', @@ -525,10 +561,15 @@ class ImportHubData implements ShouldBeUnique, ShouldQueue 'source_code_link', 'featured', 'contains_ai_content', + 'contains_ads', 'disabled', 'created_at', 'updated_at', ]); + + foreach ($modData as $mod) { + Mod::whereHubId($mod['hub_id'])->first()?->users()->sync($mod['users']); + } } }, 'fileID'); @@ -666,6 +707,7 @@ class ImportHubData implements ShouldBeUnique, ShouldQueue public function failed(Exception $exception): void { // Explicitly drop the temporary tables. + DB::unprepared('DROP TEMPORARY TABLE IF EXISTS temp_file_author'); DB::unprepared('DROP TEMPORARY TABLE IF EXISTS temp_file_option_values'); DB::unprepared('DROP TEMPORARY TABLE IF EXISTS temp_file_content'); DB::unprepared('DROP TEMPORARY TABLE IF EXISTS temp_file_version_labels'); diff --git a/database/migrations/2024_05_15_023706_create_mod_users_table.php b/database/migrations/2024_05_15_023706_create_mod_users_table.php index ad10a91..c2ffb39 100644 --- a/database/migrations/2024_05_15_023706_create_mod_users_table.php +++ b/database/migrations/2024_05_15_023706_create_mod_users_table.php @@ -16,6 +16,8 @@ return new class extends Migration $table->foreignId('mod_id')->constrained()->cascadeOnUpdate()->cascadeOnDelete(); $table->foreignId('user_id')->constrained()->cascadeOnUpdate()->cascadeOnDelete(); $table->timestamps(); + + $table->unique(['mod_id', 'user_id']); }); }