Import Multiple Mod Authors

The import script has been updated to pull in additional mod authors from the hub.
This commit is contained in:
Refringe 2024-07-05 14:35:00 -04:00
parent c1a58e29dd
commit a927f10941
Signed by: Refringe
SSH Key Fingerprint: SHA256:t865XsQpfTeqPRBMN2G6+N8wlDjkgUCZF3WGW6O9N/k
3 changed files with 50 additions and 7 deletions

3
.gitignore vendored
View File

@ -7,8 +7,7 @@
/.idea /.idea
/.phpunit.cache /.phpunit.cache
/.vscode /.vscode
/caddy /data
/data/caddy
/node_modules /node_modules
/public/build /public/build
/public/hot /public/hot

View File

@ -17,6 +17,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB; 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 // 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. // tables to store the data to save on memory; we don't want this to be a hog.
$this->bringFileAuthorsLocal();
$this->bringFileOptionsLocal(); $this->bringFileOptionsLocal();
$this->bringFileContentLocal(); $this->bringFileContentLocal();
$this->bringFileVersionLabelsLocal(); $this->bringFileVersionLabelsLocal();
@ -56,6 +58,30 @@ class ImportHubData implements ShouldBeUnique, ShouldQueue
Artisan::call('scout:import', ['model' => '\App\Models\Mod']); 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. * 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) { ->chunkById(100, function (Collection $mods) use ($curl) {
foreach ($mods as $mod) { 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') $modContent = DB::table('temp_file_content')
->where('fileID', $mod->fileID) ->where('fileID', $mod->fileID)
->orderBy('fileID', 'desc') ->orderBy('fileID', 'desc')
@ -494,9 +528,9 @@ class ImportHubData implements ShouldBeUnique, ShouldQueue
continue; continue;
} }
$insertData[] = [ $modData[] = [
'hub_id' => (int) $mod->fileID, 'hub_id' => (int) $mod->fileID,
'user_id' => User::whereHubId($mod->userID)->value('id'), 'users' => $modAuthors,
'name' => $modContent?->subject ?? '', 'name' => $modContent?->subject ?? '',
'slug' => Str::slug($modContent?->subject ?? ''), 'slug' => Str::slug($modContent?->subject ?? ''),
'teaser' => Str::limit($modContent?->teaser ?? ''), 'teaser' => Str::limit($modContent?->teaser ?? ''),
@ -513,9 +547,11 @@ class ImportHubData implements ShouldBeUnique, ShouldQueue
]; ];
} }
if (! empty($insertData)) { if (! empty($modData)) {
Mod::upsert($insertData, ['hub_id'], [ // Remove the user_id from the mod data before upserting.
'user_id', $insertModData = array_map(fn ($mod) => Arr::except($mod, 'users'), $modData);
Mod::upsert($insertModData, ['hub_id'], [
'name', 'name',
'slug', 'slug',
'teaser', 'teaser',
@ -525,10 +561,15 @@ class ImportHubData implements ShouldBeUnique, ShouldQueue
'source_code_link', 'source_code_link',
'featured', 'featured',
'contains_ai_content', 'contains_ai_content',
'contains_ads',
'disabled', 'disabled',
'created_at', 'created_at',
'updated_at', 'updated_at',
]); ]);
foreach ($modData as $mod) {
Mod::whereHubId($mod['hub_id'])->first()?->users()->sync($mod['users']);
}
} }
}, 'fileID'); }, 'fileID');
@ -666,6 +707,7 @@ class ImportHubData implements ShouldBeUnique, ShouldQueue
public function failed(Exception $exception): void public function failed(Exception $exception): void
{ {
// Explicitly drop the temporary tables. // 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_option_values');
DB::unprepared('DROP TEMPORARY TABLE IF EXISTS temp_file_content'); DB::unprepared('DROP TEMPORARY TABLE IF EXISTS temp_file_content');
DB::unprepared('DROP TEMPORARY TABLE IF EXISTS temp_file_version_labels'); DB::unprepared('DROP TEMPORARY TABLE IF EXISTS temp_file_version_labels');

View File

@ -16,6 +16,8 @@ return new class extends Migration
$table->foreignId('mod_id')->constrained()->cascadeOnUpdate()->cascadeOnDelete(); $table->foreignId('mod_id')->constrained()->cascadeOnUpdate()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->cascadeOnUpdate()->cascadeOnDelete(); $table->foreignId('user_id')->constrained()->cascadeOnUpdate()->cascadeOnDelete();
$table->timestamps(); $table->timestamps();
$table->unique(['mod_id', 'user_id']);
}); });
} }