Übernimm Texte aus Shinkan als bereinigtes HTML

Der Abruf wertet text_format aus. Bei html gelten Kurzbeschreibung, Lernziel,
Durchführung, Vorbereitung, Trainerhinweise und die Abweichung der Durchführung
als HTML; Verweise auf Quellmedien entfallen. Vergleich und Dreiwegeabgleich
arbeiten in kanonischer Form, die Vorschau zeigt Klartext. Ohne Angabe bleibt
alles Klartext wie bisher.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-23 23:45:54 +01:00
1 parent 4584dcd013
commit be24f3bf64
4 files changed
+224 -31

No files matched your search

+80 -26
View File
@@ -33,6 +33,9 @@ class ShinkanImport
'trainer_notes' => 'Trainerhinweise',
];
/** Felder, die Shinkan bei `text_format: html` als HTML liefert (ADR-50). Ohne diese Angabe sind alle Texte Klartext. */
private const HTML_FIELDS = ['summary', 'goal', 'execution', 'preparation', 'trainer_notes'];
/** Änderungshinweis der Textfassungen, die die Übernahme angelegt hat. Alles andere ist eine lokale Bearbeitung. */
private const IMPORT_NOTE = 'Übernahme aus Shinkan';
@@ -212,8 +215,8 @@ public function preview(ExerciseSource $source): array
$sections = [];
foreach (self::SECTIONS as $field => $label) {
if (trim((string) ($document[$field] ?? '')) !== '') {
$sections[] = ['label' => $label, 'text' => mb_strimwidth(trim((string) $document[$field]), 0, 280, '…')];
if ($this->value($document, $field) !== '') {
$sections[] = ['label' => $label, 'text' => FormattedText::excerpt($this->value($document, $field), 280)];
}
}
@@ -223,16 +226,16 @@ public function preview(ExerciseSource $source): array
'hash' => $snapshot?->content_hash,
'state' => $state,
'title' => (string) ($document['title'] ?? $source->title),
'summary' => (string) ($document['summary'] ?? ''),
'goal' => (string) ($document['goal'] ?? ''),
'summary' => FormattedText::plain($this->value($document, 'summary')),
'goal' => FormattedText::plain($this->value($document, 'goal')),
'duration' => $this->duration($document),
'equipment' => (string) ($document['equipment'] ?? ''),
'equipment' => FormattedText::plain($this->value($document, 'equipment')),
'sections' => $sections,
'variants' => array_map(fn (array $variant): array => [
'key' => (string) ($variant['id'] ?? ''),
'name' => (string) ($variant['name'] ?? ''),
'description' => (string) ($variant['description'] ?? ''),
'changes' => trim((string) ($variant['execution_changes'] ?? '')) !== '',
'changes' => $this->variantChanges($document, $variant) !== '',
], array_values(array_filter((array) ($document['variants'] ?? []), 'is_array'))),
'media' => array_map(fn (array $media): string => ($media['title'] ?? 'Medium').' ('.($media['media_type'] ?? '').')', array_values(array_filter((array) ($document['media'] ?? []), 'is_array'))),
'changes' => $previous === null ? [] : $this->changes($previous->payload, $document, $source),
@@ -260,7 +263,7 @@ public function changes(array $before, array $after, ?ExerciseSource $source = n
] + self::SECTIONS;
foreach ($labels as $field => $label) {
if (trim((string) ($before[$field] ?? '')) !== trim((string) ($after[$field] ?? ''))) {
if (! $this->sameValue($field, $before, $after)) {
$lines[] = $label.' geändert';
}
}
@@ -282,7 +285,7 @@ public function changes(array $before, array $after, ?ExerciseSource $source = n
}
if (trim((string) ($variant['description'] ?? '')) !== trim((string) ($old[$key]['description'] ?? ''))
|| trim((string) ($variant['execution_changes'] ?? '')) !== trim((string) ($old[$key]['execution_changes'] ?? ''))) {
|| ! FormattedText::same($this->variantChanges($after, $variant), $this->variantChanges($before, (array) $old[$key]))) {
$lines[] = 'Variante „'.($variant['name'] ?? '').'“ geändert';
}
}
@@ -298,7 +301,7 @@ public function changes(array $before, array $after, ?ExerciseSource $source = n
$lines[] = 'Der Titel wurde lokal geändert und bleibt unverändert';
}
if (($before['summary'] ?? '') !== ($after['summary'] ?? '') && filled($source->composition->summary) && $source->composition->summary !== ($before['summary'] ?? '')) {
if (! $this->sameValue('summary', $before, $after) && filled($source->composition->summary) && ! FormattedText::same($source->composition->summary, $this->value($before, 'summary'))) {
$lines[] = 'Die Kurzbeschreibung wurde lokal geändert und bleibt unverändert';
}
}
@@ -367,7 +370,11 @@ public function textDecisions(ExerciseSource $source, array $document, string $w
$latest = $source->composition->versions()->orderByDesc('number')->first();
foreach (self::FIELDS as $field => [$column, $label]) {
if ($latest !== null && $this->classifyField($latest->{$column}, $previousSource[$field] ?? null, $document[$field] ?? null) === $wanted) {
$isText = in_array($field, ['goal', 'equipment'], true);
$old = $isText ? ($this->value($previousSource, $field) ?: null) : ($previousSource[$field] ?? null);
$new = $isText ? ($this->value($document, $field) ?: null) : ($document[$field] ?? null);
if ($latest !== null && $this->classifyField($latest->{$column}, $old, $new, $isText) === $wanted) {
$result[] = ['key' => $field, 'label' => $label];
}
}
@@ -436,7 +443,7 @@ public function adopt(ExerciseSourceSnapshot $snapshot, ?User $actor = null, arr
$title = trim((string) ($document['title'] ?? '')) ?: 'Übung aus Shinkan';
$previousSnapshot = $this->lastAdopted($source);
$previousSource = $previousSnapshot?->payload ?? [];
$composition = $source->composition ?? $this->createComposition($title, (string) ($document['summary'] ?? ''));
$composition = $source->composition ?? $this->createComposition($title, $this->value($document, 'summary'));
if ($source->composition !== null && $previousSnapshot !== null) {
$this->carryOverTexts($composition, $previousSource, $document);
@@ -479,10 +486,11 @@ public function adopt(ExerciseSourceSnapshot $snapshot, ?User $actor = null, arr
$fields = [];
foreach (self::FIELDS as $field => [$column, $label]) {
$value = $document[$field] ?? null;
$isText = in_array($field, ['goal', 'equipment'], true);
$value = $isText ? ($this->value($document, $field) ?: null) : ($document[$field] ?? null);
if ($previous !== null && $previousSnapshot !== null) {
$decision = $this->classifyField($previous->{$column}, $previousSource[$field] ?? null, $value);
$decision = $this->classifyField($previous->{$column}, $isText ? ($this->value($previousSource, $field) ?: null) : ($previousSource[$field] ?? null), $value, $isText);
if ($decision === 'conflict') {
$decision = ($choices[$field] ?? null) === 'lokal' ? 'keep_local' : (($choices[$field] ?? null) === 'quelle' ? 'source' : 'conflict');
@@ -535,10 +543,10 @@ private function plan(ExerciseSource $source, array $document, array $previousSo
$title = trim((string) ($document['title'] ?? '')) ?: 'Übung aus Shinkan';
foreach (self::SECTIONS as $field => $label) {
$text = trim((string) ($document[$field] ?? ''));
$text = $this->value($document, $field);
if ($text !== '') {
$plan[] = $this->textItem($source, $field, $label, $title.' · '.$label, $label, $text, null, $previousSource[$field] ?? null);
$plan[] = $this->textItem($source, $field, $label, $title.' · '.$label, $label, $text, null, $this->value($previousSource, $field) ?: null);
}
}
@@ -554,7 +562,7 @@ private function plan(ExerciseSource $source, array $document, array $previousSo
'kind' => 'variant',
'variant' => ['key' => $key, 'name' => $name, 'description' => (string) ($variant['description'] ?? ''), 'origin' => 'quelle'],
];
$changes = trim((string) ($variant['execution_changes'] ?? ''));
$changes = $this->variantChanges($document, $variant);
if ($changes !== '') {
$before = collect((array) ($previousSource['variants'] ?? []))->firstWhere('id', $key);
@@ -566,7 +574,7 @@ private function plan(ExerciseSource $source, array $document, array $previousSo
'Abweichung der Durchführung',
$changes,
$key,
is_array($before) ? ($before['execution_changes'] ?? null) : null,
is_array($before) ? ($this->variantChanges($previousSource, $before) ?: null) : null,
'variant_'.$key,
);
}
@@ -599,22 +607,68 @@ private function textItem(ExerciseSource $source, string $field, string $label,
private function classifyText(?ContentElementVersion $latest, string $text, ?string $previousText): string
{
if ($latest === null || trim((string) $latest->body) === $text || $latest->change_note === self::IMPORT_NOTE) {
if ($latest === null || FormattedText::same($latest->body, $text) || $latest->change_note === self::IMPORT_NOTE) {
return 'source';
}
return $previousText === null || trim($previousText) !== $text ? 'conflict' : 'keep_local';
return $previousText === null || ! FormattedText::same($previousText, $text) ? 'conflict' : 'keep_local';
}
private function classifyField(mixed $local, mixed $previousSource, mixed $new): string
private function classifyField(mixed $local, mixed $previousSource, mixed $new, bool $text = false): string
{
$plain = fn (mixed $value): string => $value === null ? '' : trim((string) $value);
$same = $text
? fn (mixed $a, mixed $b): bool => FormattedText::same($a === null ? null : (string) $a, $b === null ? null : (string) $b)
: fn (mixed $a, mixed $b): bool => ($a === null ? '' : trim((string) $a)) === ($b === null ? '' : trim((string) $b));
if ($plain($local) === $plain($new) || $plain($local) === $plain($previousSource)) {
if ($same($local, $new) || $same($local, $previousSource)) {
return 'source';
}
return $plain($previousSource) !== $plain($new) ? 'conflict' : 'keep_local';
return ! $same($previousSource, $new) ? 'conflict' : 'keep_local';
}
/**
* Text eines Felds der Quelle in der Form, in der er übernommen wird: Bei `text_format: html` wird das HTML bereinigt
* und kanonisch, sonst bleibt der Klartext, wie er geliefert wird.
*
* @param array<string, mixed> $document
*/
private function value(array $document, string $field): string
{
$raw = trim((string) ($document[$field] ?? ''));
return ($document['text_format'] ?? null) === 'html' && in_array($field, self::HTML_FIELDS, true)
? FormattedText::canonicalHtml($raw)
: $raw;
}
/**
* Abweichung der Durchführung einer Variante, im Format des Dokuments.
*
* @param array<string, mixed> $document
* @param array<string, mixed> $variant
*/
private function variantChanges(array $document, array $variant): string
{
$raw = trim((string) ($variant['execution_changes'] ?? ''));
return ($document['text_format'] ?? null) === 'html' ? FormattedText::canonicalHtml($raw) : $raw;
}
/**
* Ist das Feld in beiden Quellständen inhaltlich gleich? Texte werden als kanonischer Text verglichen, alles andere
* ohne führenden und folgenden Leerraum.
*
* @param array<string, mixed> $before
* @param array<string, mixed> $after
*/
private function sameValue(string $field, array $before, array $after): bool
{
if (in_array($field, [...self::HTML_FIELDS, 'equipment'], true)) {
return FormattedText::same($this->value($before, $field), $this->value($after, $field));
}
return trim((string) ($before[$field] ?? '')) === trim((string) ($after[$field] ?? ''));
}
/**
@@ -630,8 +684,8 @@ private function carryOverTexts(Composition $composition, array $before, array $
$composition->designation = trim((string) $after['title']);
}
if (array_key_exists('summary', $after) && ((string) $composition->summary === '' || $composition->summary === ($before['summary'] ?? null))) {
$composition->summary = trim((string) $after['summary']) ?: null;
if (array_key_exists('summary', $after) && ((string) $composition->summary === '' || FormattedText::same($composition->summary, $this->value($before, 'summary')))) {
$composition->summary = $this->value($after, 'summary') ?: null;
}
$composition->save();
@@ -708,7 +762,7 @@ private function sectionVersion(ExerciseSource $source, string $field, string $t
$element->save();
}
return $latest !== null && trim((string) $latest->body) === $text
return $latest !== null && FormattedText::same($latest->body, $text)
? $latest
: $content->publishElementVersion($element, $text, [], self::IMPORT_NOTE);
}