Skip to content

Commit 7c79b10

Browse files
committed
Imports: Fixed drawing IDs not being updated in content
Would leave imported content with inaccessible images in many cases (or wrong references) although the drawing was still being uploaded & related to the page. Added test to cover. For #5761
1 parent 5c481b4 commit 7c79b10

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

app/Exports/ZipExports/ZipImportReferences.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ class ZipImportReferences
2929
/** @var Image[] */
3030
protected array $images = [];
3131

32-
/** @var array<string, Model> */
32+
/**
33+
* Mapping keyed by "type:old-reference-id" with values being the new imported equivalent model.
34+
* @var array<string, Model>
35+
*/
3336
protected array $referenceMap = [];
3437

3538
/** @var array<int, ZipExportPage> */
@@ -108,6 +111,22 @@ protected function handleReference(string $type, int $id): ?string
108111
return null;
109112
}
110113

114+
protected function replaceDrawingIdReferences(string $content): string
115+
{
116+
$referenceRegex = '/\sdrawio-diagram=[\'"](\d+)[\'"]/';
117+
118+
$result = preg_replace_callback($referenceRegex, function ($matches) {
119+
$key = 'image:' . $matches[1];
120+
$model = $this->referenceMap[$key] ?? null;
121+
if ($model instanceof Image && $model->type === 'drawio') {
122+
return ' drawio-diagram="' . $model->id . '"';
123+
}
124+
return $matches[0];
125+
}, $content);
126+
127+
return $result ?: $content;
128+
}
129+
111130
public function replaceReferences(): void
112131
{
113132
foreach ($this->books as $book) {
@@ -134,7 +153,9 @@ public function replaceReferences(): void
134153
$exportPage = $this->zipExportPageMap[$page->id];
135154
$contentType = $exportPage->markdown ? 'markdown' : 'html';
136155
$content = $exportPage->markdown ?: ($exportPage->html ?: '');
156+
137157
$parsed = $this->parser->parseReferences($content, $this->handleReference(...));
158+
$parsed = $this->replaceDrawingIdReferences($parsed);
138159

139160
$this->pageRepo->setContentFromInput($page, [
140161
$contentType => $parsed,

tests/Exports/ZipImportRunnerTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,4 +393,42 @@ public function test_imported_images_have_their_detected_extension_added()
393393

394394
ZipTestHelper::deleteZipForImport($import);
395395
}
396+
397+
public function test_drawing_references_are_updated_within_content()
398+
{
399+
$testImagePath = $this->files->testFilePath('test-image.png');
400+
$parent = $this->entities->chapter();
401+
402+
$import = ZipTestHelper::importFromData([], [
403+
'page' => [
404+
'name' => 'Page A',
405+
'html' => '<div drawio-diagram="1125"><img src="[[bsexport:image:1125]]"></div>',
406+
'images' => [
407+
[
408+
'id' => 1125,
409+
'name' => 'Cat',
410+
'type' => 'drawio',
411+
'file' => 'my_drawing'
412+
]
413+
],
414+
],
415+
], [
416+
'my_drawing' => $testImagePath,
417+
]);
418+
419+
$this->asAdmin();
420+
/** @var Page $page */
421+
$page = $this->runner->run($import, $parent);
422+
423+
$pageImages = Image::where('uploaded_to', '=', $page->id)->whereIn('type', ['gallery', 'drawio'])->get();
424+
$this->assertCount(1, $pageImages);
425+
$this->assertEquals('drawio', $pageImages[0]->type);
426+
427+
$drawingId = $pageImages[0]->id;
428+
$this->assertStringContainsString("drawio-diagram=\"{$drawingId}\"", $page->html);
429+
$this->assertStringNotContainsString('[[bsexport:image:1125]]', $page->html);
430+
$this->assertStringNotContainsString('drawio-diagram="1125"', $page->html);
431+
432+
ZipTestHelper::deleteZipForImport($import);
433+
}
396434
}

0 commit comments

Comments
 (0)