Module, Programme und Lizenzartikel verwenden wie Medien und Seiten die Tabelle pillars; die Kopie „Fachliche Säule“ in den redaktionellen Dimensionen wird nirgends mehr angeboten, geändert oder gefiltert und bleibt nur als Verlauf erhalten. - Migration (nur Ergänzungen): composition_pillar und license_article_pillar mit restrictOnDelete, pillar_id an den alten Dimensionswerten als dokumentierte Abbildung, einmalige wiederholbare Übernahme (LegacyPillarAdoption). - Übernahme nur über die gespeicherte Kennung pfeiler-<Nummer> und nur direkte Zuordnungen; mehrdeutige, abgeleitete und verwaiste Fälle werden gemeldet, nicht geraten. Medien bleiben bei ihrer Zuordnung. Alte Zeilen bleiben unverändert. Befehl: php artisan luf:saeulen-uebernehmen [--dry-run]. - Editoren von Modul, Programm und Lizenzartikel wählen Säulen aus der Säulenverwaltung; PillarService::assign gilt für alle Objektarten, Löschschutz zählt alle Objektarten. - Filter: Module und Programme, Aufbau-Picker und Mitgliederbibliothek nach der Säulenzuordnung; alte Adressen mit einem Säulenwert der Einordnung folgen der Säule. - Abgeleitete Säulen (aus enthaltenen Medien) bleiben getrennt von direkten. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
271 lines
11 KiB
PHP
271 lines
11 KiB
PHP
<?php
|
||
|
||
namespace App\Domain\Catalog;
|
||
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
/**
|
||
* Übernimmt die frühere Kopie „Fachliche Säule“ (Dimension `saeule`) in die zentrale Säulenverwaltung (Tabelle
|
||
* `pillars`). Verlustfrei, wiederholbar und ohne Raten:
|
||
*
|
||
* - **Zuordnung Wert → Säule** nur über die gespeicherte Kennung `pfeiler-{Säulenkennung}`, die die Migration
|
||
* vom 16.11.2026 beim Spiegeln geschrieben hat, und nur, wenn die Säule existiert und nicht jünger als der Wert ist
|
||
* (sonst kann die Kennung nicht aus ihr entstanden sein). Eine abweichende Bezeichnung ist nach einem Umbenennen
|
||
* erwartet und wird nur berichtet. Die Verknüpfung steht in `editorial_dimension_values.pillar_id`.
|
||
* - **Übernommen** werden nur direkte Zuordnungen von Modulen, Programmen und Lizenzartikeln in `composition_pillar`
|
||
* und `license_article_pillar` (insert-or-ignore, daher beliebig oft ausführbar).
|
||
* - **Nicht übernommen, sondern gemeldet:** Werte ohne belastbare Kennung, Zuordnungen zu fehlenden Objekten,
|
||
* abgeleitete Zuordnungen und unbekannte Objektarten. Abgeleitete Säulen eines Programms oder Moduls entstehen zur
|
||
* Laufzeit aus den enthaltenen Medien und werden nie als direkte Zuordnung gespeichert.
|
||
* - **Medien:** Die gespiegelten Zeilen sind eine veraltete Kopie; maßgeblich ist `content_element_pillar`. Sie werden
|
||
* nicht zurückgemischt, nur gezählt.
|
||
* - Alte Zeilen werden weder geändert noch gelöscht.
|
||
*
|
||
* Nur die Datenbankfassade, damit spätere Änderungen an Modellen die Migration nicht berühren.
|
||
*/
|
||
final class LegacyPillarAdoption
|
||
{
|
||
/** @var array<string, array{table: string, column: string, subjects: string}> */
|
||
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<array<string, mixed>>,
|
||
* unresolved: list<array<string, mixed>>,
|
||
* copied: array<string, int>,
|
||
* already: array<string, int>,
|
||
* before: array<string, int>,
|
||
* after: array<string, int>,
|
||
* mirror: array<string, int>
|
||
* }
|
||
*/
|
||
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<string, mixed> $report
|
||
* @return list<string>
|
||
*/
|
||
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-<Nummer>“, 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<string, string> 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<string, int>
|
||
*/
|
||
private function counts(): array
|
||
{
|
||
return [
|
||
'composition' => DB::table('composition_pillar')->count(),
|
||
'license_article' => DB::table('license_article_pillar')->count(),
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @param array<int, int> $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)),
|
||
];
|
||
}
|
||
}
|