Ü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);
}
@@ -35,8 +35,12 @@ ## 1. Felder, die der Katalog liefert und die Lara & Felix übernimmt
| Variante `sequence_order` | `variants[].sequence_order` | Position der Variante (Reihenfolge der Antwort) | Kundenansicht |
| Medien `public_id`, `media_type`, `title`, `sort_order` | `media[]` | **Nur Quellenangabe**: gespeichert im Stand der Quelle, gezeigt in der Vorschau. Nie Medienausgabe, nie Teil eines Artikel-Stands | Übernahme (Vorschau) |
Klartext: Der Katalog liefert Texte als Klartext (`plaintext`). Absätze und Listen entstehen aus den Konventionen der
Textformat: Der Katalog nennt das Format mit `text_format`. Bei `html` (Stand Shinkan vom 23.11.2026) sind Kurzbeschreibung,
Lernziel, Durchführung, Vorbereitung, Trainerhinweise und die Abweichung der Durchführung einer Variante bereinigtes HTML
(Absatz, Zwischenüberschrift, fett, kursiv, Listen, Link, Zeilenumbruch; ADR-50). Verweise auf Quellmedien im Text entfallen.
Titel, Variantenbeschreibung und Ausstattung bleiben Text. Fehlt die Angabe, sind alle Texte Klartext mit den Konventionen der
Anleitung (Leerzeilen, „- “, „1. “). Zeichen außerhalb von Windows-1252 erscheinen im PDF lesbar ersetzt, sonst als „?“.
Fixtures: `katalog-detail.json` (Klartext) und `katalog-detail-html.json` (HTML).
## 2. Verhalten beim Fassungswechsel
+101 -4
View File
@@ -134,6 +134,81 @@ public function test_the_representative_catalog_answer_lands_in_the_documented_p
$this->assertSame(['Quellvideo Sicherer Stand (video)'], $preview['media'], 'Medien bleiben Quellenangabe ohne Datei.');
}
public function test_the_representative_html_answer_lands_formatted_in_the_documented_places(): void
{
$document = json_decode((string) file_get_contents(base_path('tests/Fixtures/shinkan/katalog-detail-html.json')), true, 512, JSON_THROW_ON_ERROR);
$import = app(ShinkanImport::class);
$version = $import->adopt($import->record($document));
$body = fn (string $role): string => (string) $version->members->firstWhere('role', $role)->contentElementVersion->body;
$this->assertSame('<p>Die Kinder üben einen <strong>sicheren Stand</strong> und ein deutliches Stopp.</p>', $version->composition->summary);
$this->assertSame('<p>Die Kinder können mit fester Stimme und sicherem Stand Stopp sagen.</p>', $version->learning_goal);
$this->assertSame('Matten, Pratzen', $version->material_note);
$this->assertStringContainsString('<ol><li><p>Sicheren Stand einnehmen</p></li>', $body('Durchführung'));
$this->assertStringContainsString('<em>wie sich der Stand angefühlt hat</em>', $body('Durchführung'));
$this->assertStringNotContainsString('data-shinkan', $body('Durchführung'), 'Der Verweis auf ein Quellmedium entfällt.');
$this->assertSame('<p>Matten im Kreis auslegen.<br>Genug Platz für Abstand zwischen den Kindern lassen.</p>', str_replace('<br />', '<br>', $body('Vorbereitung')));
$this->assertStringContainsString('<ul><li><p>Leise Kinder ermutigen</p></li>', $body('Trainerhinweise'));
$this->assertSame('<p>Die Kinder bilden Paare und üben <em>abwechselnd</em>.</p>', $body('Abweichung der Durchführung'));
}
public function test_html_from_the_source_is_cleaned_and_lands_as_formatted_text(): void
{
$import = app(ShinkanImport::class);
$document = $this->htmlDocument('h1');
$version = $import->adopt($import->record($document));
$body = fn (string $role): string => (string) $version->members->firstWhere('role', $role)->contentElementVersion->body;
$this->assertSame('<p>Kurz <strong>erklärt</strong>.</p>', $version->composition->summary);
$this->assertSame('<p>Ziel:</p><ul><li><p>Stand</p></li><li><p>Stimme</p></li></ul>', $version->learning_goal);
$this->assertSame('Matten, Pratzen', $version->material_note, 'Das Material bleibt Text.');
$this->assertSame('<p>Die Kinder <strong>stehen</strong> sicher.</p><h3>Ablauf</h3><ul><li><p>Stimme einsetzen</p></li></ul>', $body('Durchführung'), 'Skript und Medienverweis fallen weg, Absatz, Hervorhebung und Liste bleiben.');
$this->assertStringNotContainsString('script', $body('Durchführung'));
$this->assertStringNotContainsString('data-shinkan', $body('Durchführung'));
$this->assertSame('<p>Mit <em>Partner</em> üben.</p>', $body('Abweichung der Durchführung'));
$preview = $import->preview(ExerciseSource::query()->firstOrFail());
$this->assertSame('Kurz erklärt.', $preview['summary'], 'Die Vorschau zeigt Klartext.');
$this->assertSame('Ziel:
- Stand
- Stimme', $preview['goal']);
$this->assertStringNotContainsString('<', json_encode($preview['sections']));
}
public function test_the_same_html_again_creates_no_new_text_version_and_a_formatting_only_change_is_noticed(): void
{
$import = app(ShinkanImport::class);
$first = $import->adopt($import->record($this->htmlDocument('h1')));
$again = $this->htmlDocument('h2');
$again['execution'] = '<div>Die Kinder <b>stehen</b> sicher.<span data-shinkan-exercise-media="9"></span></div>
<h2>Ablauf</h2><ul><li>Stimme einsetzen</li></ul>';
$second = $import->adopt($import->record($again));
$this->assertSame(
$first->members->firstWhere('role', 'Durchführung')->content_element_version_id,
$second->members->firstWhere('role', 'Durchführung')->content_element_version_id,
'Andere Schreibweise desselben Inhalts ist keine neue Textfassung.',
);
$changed = $this->htmlDocument('h3');
$changed['execution'] = '<p>Die Kinder stehen sicher.</p><h3>Ablauf</h3><ul><li>Stimme einsetzen</li></ul>';
$preview = $import->preview(ExerciseSource::query()->firstOrFail());
$import->record($changed);
$this->assertContains('Durchführung geändert', $import->preview(ExerciseSource::query()->firstOrFail())['changes'], 'Fett entfernt ist eine Änderung.');
}
public function test_a_plain_text_document_is_still_taken_as_plain_text(): void
{
$import = app(ShinkanImport::class);
$version = $import->adopt($import->record($this->document('h1')));
$this->assertSame('Kurz erklärt.', $version->composition->summary);
$this->assertSame('Nein sagen üben', $version->learning_goal);
}
public function test_a_changed_variant_is_recognised_and_a_new_version_keeps_the_local_addition(): void
{
$import = app(ShinkanImport::class);
@@ -685,8 +760,8 @@ public function test_an_imported_exercise_is_edited_like_any_other_and_the_local
$second = $composition->versions()->orderByDesc('number')->firstOrFail();
$this->assertSame(2, $second->number);
$this->assertSame('Unsere eigene Durchführung.', $second->members->firstWhere('role', 'Durchführung')->contentElementVersion->body);
$this->assertSame('Unser Lernziel', $second->learning_goal);
$this->assertSame('<p>Unsere eigene Durchführung.</p>', $second->members->firstWhere('role', 'Durchführung')->contentElementVersion->body);
$this->assertSame('<p>Unser Lernziel</p>', $second->learning_goal);
$this->assertSame('Nein sagen üben', $first->fresh()->learning_goal, 'Die frühere Fassung bleibt unverändert.');
// Die Quelle ändert nur die Trainerhinweise.
@@ -701,11 +776,11 @@ public function test_an_imported_exercise_is_edited_like_any_other_and_the_local
$third = $import->adopt($snapshot);
$this->assertSame('Unsere eigene Durchführung.', $third->members->firstWhere('role', 'Durchführung')->contentElementVersion->body);
$this->assertSame('<p>Unsere eigene Durchführung.</p>', $third->members->firstWhere('role', 'Durchführung')->contentElementVersion->body);
$this->assertSame('lokal', $third->members->firstWhere('role', 'Durchführung')->origin);
$this->assertSame('Neue Hinweise der Quelle.', $third->members->firstWhere('role', 'Trainerhinweise')->contentElementVersion->body);
$this->assertSame('quelle', $third->members->firstWhere('role', 'Trainerhinweise')->origin);
$this->assertSame('Unser Lernziel', $third->learning_goal);
$this->assertSame('<p>Unser Lernziel</p>', $third->learning_goal);
}
public function test_a_text_changed_locally_and_in_the_source_needs_a_deliberate_choice(): void
@@ -806,6 +881,28 @@ private function fake(array $details, array $changes, string $cursor, ?array $li
Http::fake($fakes);
}
/**
* Antwort der Quelle mit `text_format: html`, wie sie Shinkan liefert.
*
* @return array<string, mixed>
*/
private function htmlDocument(string $hash): array
{
return [
...$this->document($hash),
'text_format' => 'html',
'summary' => '<p>Kurz <b>erklärt</b>.</p>',
'goal' => '<p>Ziel:</p><ul><li>Stand</li><li>Stimme</li></ul>',
'execution' => '<p>Die Kinder <strong>stehen</strong> sicher.<span data-shinkan-exercise-media="7" class="shinkan-inline-media"></span></p><h3>Ablauf</h3><ul><li>Stimme einsetzen</li></ul><script>alert(1)</script>',
'preparation' => '<p>Matten auslegen.</p>',
'trainer_notes' => '<p>Auf Abstand achten.</p>',
'variants' => [
['id' => self::VARIANT_A, 'name' => 'Mit Partner', 'description' => 'Zu zweit', 'execution_changes' => '<p>Mit <i>Partner</i> üben.</p>', 'sequence_order' => 1],
['id' => self::VARIANT_B, 'name' => 'Allein', 'description' => 'Einzeln', 'execution_changes' => '', 'sequence_order' => 2],
],
];
}
/**
* @return array<string, mixed>
*/
@@ -0,0 +1,38 @@
{
"source_id": "6f1c1c52-0a53-4f6b-8e0e-3f0c2f6b9a11",
"title": "Sicher stehen und Stopp sagen",
"summary": "<p>Die Kinder üben einen <strong>sicheren Stand</strong> und ein deutliches Stopp.</p>",
"goal": "<p>Die Kinder können mit fester Stimme und sicherem Stand Stopp sagen.</p>",
"execution": "<p>Die Kinder stellen sich im Kreis auf.</p><ol><li>Sicheren Stand einnehmen</li><li>Arme nach vorn, Handflächen zeigen nach vorn</li><li>Laut und deutlich „Stopp!“ sagen</li></ol><p>Zum Abschluss besprechen alle, <em>wie sich der Stand angefühlt hat</em>.</p><p><span data-shinkan-exercise-media=\"1\" data-shinkan-exercise-media-size=\"medium\" class=\"shinkan-inline-media\"></span></p>",
"preparation": "<p>Matten im Kreis auslegen.<br>Genug Platz für Abstand zwischen den Kindern lassen.</p>",
"trainer_notes": "<p>Auf <strong>Abstand</strong> achten.</p><ul><li>Leise Kinder ermutigen</li><li>Laute Kinder zum Zuhören anhalten</li></ul>",
"equipment": "Matten, Pratzen",
"duration_min": 10,
"duration_max": 15,
"variants": [
{
"id": "11111111-1111-4111-8111-111111111111",
"name": "Mit Partner",
"description": "Zu zweit üben, ein Kind spielt die Rolle des Gegenübers.",
"execution_changes": "<p>Die Kinder bilden Paare und üben <em>abwechselnd</em>.</p>",
"sequence_order": 1
},
{
"id": "22222222-2222-4222-8222-222222222222",
"name": "Allein vor dem Spiegel",
"description": "Einzelübung ohne Partner.",
"execution_changes": "",
"sequence_order": 2
}
],
"media": [
{
"id": "33333333-3333-4333-8333-333333333333",
"media_type": "video",
"title": "Quellvideo Sicherer Stand",
"sort_order": 1
}
],
"content_hash": "6c1b0e1c9f6a4d7e8f21c3b4a5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f9",
"text_format": "html"
}