*/ private const TARGETS = [ 'composition' => ['table' => 'composition_pillar', 'column' => 'composition_id', 'subjects' => 'compositions'], 'license_article' => ['table' => 'license_article_pillar', 'column' => 'license_article_id', 'subjects' => 'license_articles'], ]; /** * @return array{ * mapping: list>, * unresolved: list>, * copied: array, * already: array, * before: array, * after: array, * mirror: array * } */ public function run(bool $dryRun = false): array { $report = [ 'mapping' => [], 'unresolved' => [], 'copied' => ['composition' => 0, 'license_article' => 0], 'already' => ['composition' => 0, 'license_article' => 0], 'before' => $this->counts(), 'after' => [], 'mirror' => ['element_rows' => 0, 'missing_in_pivot' => 0, 'only_in_pivot' => 0], ]; $dimension = DB::table('editorial_dimensions')->where('slug', 'saeule')->first(); if ($dimension === null) { $report['after'] = $report['before']; return $report; } $resolved = []; foreach (DB::table('editorial_dimension_values')->where('editorial_dimension_id', $dimension->id)->orderBy('id')->get() as $value) { $pillar = $this->pillarFor($value); if (is_string($pillar)) { $report['unresolved'][] = ['art' => 'Wert', 'wert_id' => (int) $value->id, 'wert' => $value->label, 'kennung' => $value->slug, 'grund' => $pillar]; $report['mapping'][] = ['wert_id' => (int) $value->id, 'wert' => $value->label, 'saeule_id' => null, 'status' => 'offen']; continue; } $resolved[(int) $value->id] = (int) $pillar->id; $status = $value->pillar_id === null ? 'zugeordnet' : 'bereits zugeordnet'; $hint = $value->label !== $pillar->name ? 'über Kennung zugeordnet, Bezeichnung weicht ab („'.$pillar->name.'“)' : null; $report['mapping'][] = ['wert_id' => (int) $value->id, 'wert' => $value->label, 'saeule_id' => (int) $pillar->id, 'saeule' => $pillar->name, 'status' => $status, 'hinweis' => $hint]; if ($value->pillar_id === null && ! $dryRun) { DB::table('editorial_dimension_values')->where('id', $value->id)->update(['pillar_id' => $pillar->id]); } } $subjectTypes = $this->subjectTypes(); foreach (DB::table('identity_dimension_values') ->whereIn('editorial_dimension_value_id', DB::table('editorial_dimension_values')->where('editorial_dimension_id', $dimension->id)->select('id')) ->orderBy('id')->get() as $row) { $valueId = (int) $row->editorial_dimension_value_id; $label = DB::table('editorial_dimension_values')->where('id', $valueId)->value('label'); $target = $subjectTypes[$row->subject_type] ?? null; if ($target === 'content_element') { $report['mirror']['element_rows']++; continue; } $where = ['art' => 'Zuordnung', 'zuordnung_id' => (int) $row->id, 'wert_id' => $valueId, 'wert' => $label, 'objektart' => $row->subject_type, 'objekt_id' => (int) $row->subject_id, 'herkunft' => $row->origin]; if ($target === null) { $report['unresolved'][] = $where + ['grund' => 'unbekannte Objektart']; continue; } if ($row->origin !== 'direkt') { $report['unresolved'][] = $where + ['grund' => 'abgeleitete Zuordnung (wird nicht als direkte übernommen)']; continue; } if (! isset($resolved[$valueId])) { $report['unresolved'][] = $where + ['grund' => 'Wert ohne belastbare Säule']; continue; } $config = self::TARGETS[$target]; if (! DB::table($config['subjects'])->where('id', $row->subject_id)->exists()) { $report['unresolved'][] = $where + ['grund' => 'Objekt nicht mehr vorhanden']; continue; } $present = DB::table($config['table'])->where($config['column'], $row->subject_id)->where('pillar_id', $resolved[$valueId])->exists(); if ($present) { $report['already'][$target]++; continue; } if (! $dryRun) { DB::table($config['table'])->insertOrIgnore([$config['column'] => $row->subject_id, 'pillar_id' => $resolved[$valueId]]); } $report['copied'][$target]++; } $report['mirror'] = array_merge($report['mirror'], $this->mirrorDivergence($dimension->id, $resolved)); $report['after'] = $dryRun ? $report['before'] : $this->counts(); return $report; } /** * @param array $report * @return list */ public static function lines(array $report, bool $dryRun = false): array { $names = ['composition' => 'Module und Programme', 'license_article' => 'Lizenzartikel']; $lines = [$dryRun ? 'Probelauf, es wird nichts geschrieben.' : 'Übernahme abgeschlossen.', '']; foreach ($report['mapping'] as $row) { $lines[] = sprintf('Wert %d „%s“ → %s%s', $row['wert_id'], $row['wert'], $row['saeule_id'] === null ? 'keine Säule (offen)' : 'Säule '.$row['saeule_id'].' „'.($row['saeule'] ?? '').'“ ('.$row['status'].')', ! empty($row['hinweis']) ? ' – '.$row['hinweis'] : ''); } $lines[] = ''; foreach ($names as $key => $label) { $lines[] = sprintf('%s: %d übernommen, %d bereits vorhanden, Zuordnungen vorher %d, nachher %d', $label, $report['copied'][$key], $report['already'][$key], $report['before'][$key], $report['after'][$key]); } $lines[] = sprintf('Medien (nur Information): %d gespiegelte Zeilen, %d nicht mehr in der Medienzuordnung, %d nur dort. Maßgeblich bleibt die Medienzuordnung.', $report['mirror']['element_rows'], $report['mirror']['missing_in_pivot'], $report['mirror']['only_in_pivot']); if ($report['unresolved'] === []) { $lines[] = 'Keine ungeklärten Fälle.'; } else { $lines[] = ''; $lines[] = 'Ungeklärt, bitte entscheiden (nichts wurde geändert):'; foreach ($report['unresolved'] as $row) { $lines[] = '- '.json_encode($row, JSON_UNESCAPED_UNICODE); } } return $lines; } /** * Die Säule zu einem alten Wert, oder der Grund, warum es keine belastbare gibt. */ private function pillarFor(object $value): object|string { if (preg_match('/^pfeiler-(\d+)$/', (string) $value->slug, $match) !== 1) { return 'Kennung ist nicht „pfeiler-“, keine belastbare Verknüpfung'; } $pillar = DB::table('pillars')->where('id', (int) $match[1])->first(); if ($pillar === null) { return 'Die Säule zur Kennung existiert nicht mehr'; } if ($value->pillar_id !== null && (int) $value->pillar_id !== (int) $pillar->id) { return 'Gespeicherte Verknüpfung (Säule '.$value->pillar_id.') und Kennung (Säule '.$pillar->id.') widersprechen sich'; } if ($pillar->created_at !== null && $value->created_at !== null && $pillar->created_at > $value->created_at) { return 'Die Säule ist jünger als der Wert; die Kennung kann nicht aus ihr entstanden sein'; } return $pillar; } /** * @return array Objektart wie gespeichert => Ziel */ private function subjectTypes(): array { $types = []; foreach ([ 'composition' => [Composition::class], 'license_article' => [LicenseArticle::class], 'content_element' => [ContentElement::class], ] as $target => $classes) { foreach ($classes as $class) { $types[$class] = $target; $types[(new $class)->getMorphClass()] = $target; } } return $types; } /** * @return array */ private function counts(): array { return [ 'composition' => DB::table('composition_pillar')->count(), 'license_article' => DB::table('license_article_pillar')->count(), ]; } /** * @param array $resolved * @return array{missing_in_pivot: int, only_in_pivot: int} */ private function mirrorDivergence(int $dimensionId, array $resolved): array { $mirrored = []; foreach (DB::table('identity_dimension_values') ->whereIn('subject_type', array_unique([ContentElement::class, (new ContentElement)->getMorphClass()])) ->whereIn('editorial_dimension_value_id', array_keys($resolved) ?: [0]) ->get() as $row) { $mirrored[$row->subject_id.':'.$resolved[(int) $row->editorial_dimension_value_id]] = true; } $pivot = []; foreach (DB::table('content_element_pillar')->get() as $row) { $pivot[$row->content_element_id.':'.$row->pillar_id] = true; } return [ 'missing_in_pivot' => count(array_diff_key($mirrored, $pivot)), 'only_in_pivot' => count(array_diff_key($pivot, $mirrored)), ]; } }