diff --git a/app/Console/Commands/MigrateMediaOrdersCommand.php b/app/Console/Commands/MigrateMediaOrdersCommand.php index a107feb..ae60ded 100644 --- a/app/Console/Commands/MigrateMediaOrdersCommand.php +++ b/app/Console/Commands/MigrateMediaOrdersCommand.php @@ -381,6 +381,16 @@ private function processReport(object $legacyReport, int $cooperationId, int $ta [$cleanTitle, $cleanLink] = $this->extractLinkData($legacyReport); + $exists = DB::table('reports') + ->where('media_task_assignment_id', $mediaTaskAssignment->id) + ->where('publication_date', $legacyReport->broadcast) + ->where('link', $cleanLink) + ->exists(); + + if ($exists) { + return; + } + DB::table('reports')->insert([ 'media_task_assignment_id' => $mediaTaskAssignment->id, 'title' => $cleanTitle, diff --git a/app/Console/Commands/MigrateReportsCommand.php b/app/Console/Commands/MigrateReportsCommand.php index 281b6fc..6445dba 100644 --- a/app/Console/Commands/MigrateReportsCommand.php +++ b/app/Console/Commands/MigrateReportsCommand.php @@ -134,12 +134,32 @@ private function processReport(object $legacy): void return; } - // Insert the report + [$cleanTitle, $cleanLink] = $this->extractLinkData($legacy); + + $existing = DB::table('reports') + ->where('media_task_assignment_id', $mediaTaskAssignment->id) + ->where('publication_date', $legacy->broadcast) + ->where('link', $cleanLink) + ->first(); + + if ($existing) { + if (! isset($this->migratedIds[$existing->id])) { + $report = Report::find($existing->id); + if ($report) { + $this->migrateProof($report, $legacy); + } + } + + $this->output->writeln('SKIP'); + + return; + } + DB::table('reports')->insert([ 'media_task_assignment_id' => $mediaTaskAssignment->id, - 'title' => $legacy->title ?? '', + 'title' => $cleanTitle, 'publication_date' => $legacy->broadcast, - 'link' => $legacy->link ?? '', + 'link' => $cleanLink, 'description' => '', 'status' => $this->mapApprovalStatus((string) $legacy->status), 'created_at' => $legacy->created_at ?? now(), @@ -148,7 +168,6 @@ private function processReport(object $legacy): void $newReportId = (int) DB::getPdo()->lastInsertId(); - // Migrate proof image if not already done if (! isset($this->migratedIds[$newReportId])) { $report = Report::find($newReportId); if ($report) { @@ -222,6 +241,45 @@ private function guessMime(string $fileName): string }; } + /** + * Ekstrak title & link bersih dari legacy report. + * + * @return array{0: string, 1: string} [title, link] + */ + private function extractLinkData(object $legacyReport): array + { + $rawTitle = trim($legacyReport->title ?? ''); + $rawLink = trim($legacyReport->link ?? ''); + + $titleIsUrl = (bool) preg_match('#^https?://#i', $rawTitle); + + preg_match('#https?://\S+#', $rawLink, $urlMatches); + $extractedUrl = rtrim($urlMatches[0] ?? '', '.,;)'); + + if ($titleIsUrl) { + $cleanLink = $rawTitle; + + $lines = array_values(array_filter( + array_map('trim', preg_split('/[\r\n]+/', $rawLink)) + )); + $candidateTitle = ''; + foreach ($lines as $line) { + if (! preg_match('#^https?://#i', $line)) { + $candidateTitle = $line; + break; + } + } + $cleanTitle = $candidateTitle !== '' + ? mb_substr($candidateTitle, 0, 255) + : mb_substr($rawTitle, 0, 255); + } else { + $cleanLink = $extractedUrl ?: mb_substr($rawLink, 0, 2048); + $cleanTitle = mb_substr($rawTitle, 0, 255); + } + + return [$cleanTitle, $cleanLink]; + } + /** * Map legacy reports.status integer to ApprovalStatus enum value. * Legacy: 0 = menunggu, 1 = menerima, 2 = menolak