Add files migration
This commit is contained in:
110
app/Console/Commands/MigrateFilesExecute.php
Normal file
110
app/Console/Commands/MigrateFilesExecute.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Helpers\EntryHelpers;
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Spatie\SimpleExcel\SimpleExcelReader;
|
||||
|
||||
#[Signature('migrate:files:execute {--csv-file=} {--favorite-server=oedipus} ')]
|
||||
#[Description("Migrate all files tables into an EntryFile.")]
|
||||
class MigrateFilesExecute extends Command
|
||||
{
|
||||
|
||||
private function getState( array $file ): string
|
||||
{
|
||||
if( str_contains( $file['old_path'], 'private' ) )
|
||||
return 'private';
|
||||
if( str_contains( $file['old_path'], 'archive' ) )
|
||||
return 'archived';
|
||||
return 'public';
|
||||
}
|
||||
|
||||
private function getNewPath( array $file ): string
|
||||
{
|
||||
$newPath = $file['new_path'];
|
||||
$newPath = str_replace('rhpz-fs-storage//', '', $newPath);
|
||||
$newPath = str_replace( './rhpz-fs-storage/', '', $newPath);
|
||||
|
||||
return dirname($newPath);
|
||||
}
|
||||
|
||||
private function createEntryFile( array $file )
|
||||
{
|
||||
$exists = DB::table('migration_files')
|
||||
->where('filename', $file['filename'] )
|
||||
->where('old_path', $file['old_path'] )
|
||||
->where('wp_post_id', (int) $file['wp_post_id'] )
|
||||
->where('uuid', $file['uuid'] )
|
||||
->where('new_path', $this->getNewPath( $file ) )
|
||||
->exists();
|
||||
|
||||
if( $exists )
|
||||
return;
|
||||
|
||||
$entryId = DB::table('migrations_logs')
|
||||
->where('source_system', 'wp')
|
||||
->where('source_table', 'wp_posts')
|
||||
->where('target_table', 'entries' )
|
||||
->where('source_id', (int) $file['wp_post_id'] )
|
||||
->value('target_id');
|
||||
|
||||
if( !$entryId ){
|
||||
$this->info("Not copied entry for {$file['wp_post_id']}");
|
||||
return;
|
||||
}
|
||||
|
||||
$fileId = DB::table('entry_files')->insertGetId([
|
||||
'entry_id' => $entryId,
|
||||
'filename' => $file['filename'],
|
||||
'filepath' => $this->getNewPath( $file ),
|
||||
'favorite_server' => $this->option('favorite-server') ?? "oedipus",
|
||||
'favorite_at' => now(),
|
||||
'filesize' => (int) $file['filesize'],
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
'file_uuid' => $file['uuid'],
|
||||
'state' => $this->getState( $file ),
|
||||
'online_patcher' => (int) EntryHelpers::enableOnlinePatcherBasedOnExtension( $file['filename'] ),
|
||||
'secondary_online_patcher' => 0,
|
||||
'download_count' => 0
|
||||
]);
|
||||
|
||||
if( !$fileId ){
|
||||
$this->info( "Not created file for {$file['wp_post_id']}" );
|
||||
return;
|
||||
}
|
||||
|
||||
DB::table('migration_files')->insert([
|
||||
'filename' => $file['filename'],
|
||||
'old_path' => $file['old_path'],
|
||||
'wp_post_id' => (int) $file['wp_post_id'],
|
||||
'uuid' => $file['uuid'],
|
||||
'new_path' => $this->getNewPath( $file ),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
$this->info( "Created file for {$file['wp_post_id']}" );
|
||||
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$csvFile = $this->option('csv-file');
|
||||
if( !$csvFile || !is_file($csvFile) ){
|
||||
$this->error('Missing CSV file Path');
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$rows = SimpleExcelReader::create($csvFile)->useDelimiter(';')->getRows();
|
||||
$rows->each(function(array $row){
|
||||
$this->createEntryFile($row);
|
||||
});
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,8 @@
|
||||
"livewire/livewire": "^4.3",
|
||||
"predis/predis": "^3.4",
|
||||
"spatie/laravel-activitylog": "^5.0",
|
||||
"spatie/laravel-sitemap": "*"
|
||||
"spatie/laravel-sitemap": "*",
|
||||
"spatie/simple-excel": "^3.10"
|
||||
},
|
||||
"require-dev": {
|
||||
"barryvdh/laravel-debugbar": "^4.2",
|
||||
|
||||
62
composer.lock
generated
62
composer.lock
generated
@@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "6ebcacd70675cd6b17a9428bb0050a51",
|
||||
"content-hash": "7c9b7fd591cb7e152659cf998900a531",
|
||||
"packages": [
|
||||
{
|
||||
"name": "artesaos/seotools",
|
||||
@@ -5865,6 +5865,66 @@
|
||||
],
|
||||
"time": "2026-04-27T14:27:52+00:00"
|
||||
},
|
||||
{
|
||||
"name": "spatie/simple-excel",
|
||||
"version": "3.10.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/spatie/simple-excel.git",
|
||||
"reference": "2aa4cd9da6f29a23e4f7b6f4c6944dce204ea47a"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/spatie/simple-excel/zipball/2aa4cd9da6f29a23e4f7b6f4c6944dce204ea47a",
|
||||
"reference": "2aa4cd9da6f29a23e4f7b6f4c6944dce204ea47a",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"illuminate/support": "^9.0|^10.0|^11.0|^12.0|^13.0",
|
||||
"openspout/openspout": "^4.30",
|
||||
"php": "^8.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9.4|^10.5|^11.0|^12.0",
|
||||
"spatie/pest-plugin-snapshots": "^1.1|^2.1",
|
||||
"spatie/phpunit-snapshot-assertions": "^4.0|^5.1",
|
||||
"spatie/temporary-directory": "^1.2|^2.2"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Spatie\\SimpleExcel\\": "src"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Freek Van der Herten",
|
||||
"email": "freek@spatie.be",
|
||||
"homepage": "https://spatie.be",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"description": "Read and write simple Excel and CSV files",
|
||||
"homepage": "https://github.com/spatie/simple-excel",
|
||||
"keywords": [
|
||||
"simple-excel",
|
||||
"spatie"
|
||||
],
|
||||
"support": {
|
||||
"source": "https://github.com/spatie/simple-excel/tree/3.10.0"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://github.com/spatie",
|
||||
"type": "github"
|
||||
}
|
||||
],
|
||||
"time": "2026-06-15T06:21:16+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/clock",
|
||||
"version": "v8.0.8",
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('migration_files', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('filename');
|
||||
$table->string('old_path');
|
||||
$table->unsignedBigInteger('wp_post_id');
|
||||
$table->uuid('uuid');
|
||||
$table->string('new_path');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('migration_files');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user