return url('/' . implode('/', $parts));
}
+ /**
+ * Get the ID-based permalink for this page.
+ */
+ public function getPermalink(): string
+ {
+ return url("/link/{$this->id}");
+ }
+
/**
* Get this page for JSON display.
*/
if (userCan(Permission::PageCreate, $copyChapter)) {
/** @var Page $page */
foreach ($original->getVisiblePages() as $page) {
- $this->clonePage($page, $copyChapter, $page->name);
+ $this->createPageClone($page, $copyChapter, $page->name);
}
}
+ $this->referenceChangeContext->add($original, $copyChapter);
+
return $copyChapter;
}
$directChildren = $original->getDirectVisibleChildren();
foreach ($directChildren as $child) {
if ($child instanceof Chapter && userCan(Permission::ChapterCreate, $copyBook)) {
- $this->cloneChapter($child, $copyBook, $child->name);
+ $this->createChapterClone($child, $copyBook, $child->name);
}
if ($child instanceof Page && !$child->draft && userCan(Permission::PageCreate, $copyBook)) {
- $this->clonePage($child, $copyBook, $child->name);
+ $this->createPageClone($child, $copyBook, $child->name);
}
}
}
}
+ $this->referenceChangeContext->add($original, $copyBook);
+
return $copyBook;
}
{
$this->changes[] = [$oldEntity, $newEntity];
}
+
+ /**
+ * Get all the change pairs.
+ * Returned array is an array of pairs, where the first item is the old entity
+ * and the second is the new entity.
+ * @return array<array{0: Entity, 1: Entity}>
+ */
+ public function getChanges(): array
+ {
+ return $this->changes;
+ }
+
+ /**
+ * Get all the new entities from the changes.
+ */
+ public function getNewEntities(): array
+ {
+ return array_column($this->changes, 1);
+ }
+
+ /**
+ * Get all the old entities from the changes.
+ */
+ public function getOldEntities(): array
+ {
+ return array_column($this->changes, 0);
+ }
+
+ public function getNewForOld(Entity $oldEntity): ?Entity
+ {
+ foreach ($this->changes as [$old, $new]) {
+ if ($old->id === $oldEntity->id && $old->type === $oldEntity->type) {
+ return $new;
+ }
+ }
+ return null;
+ }
}
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\HasDescriptionInterface;
use BookStack\Entities\Models\Entity;
-use BookStack\Entities\Models\EntityContainerData;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\RevisionRepo;
use BookStack\Util\HtmlDocument;
}
}
+ /**
+ * Change existing references for a range of entities using the given context.
+ */
public function changeReferencesUsingContext(ReferenceChangeContext $context): void
{
- // TODO
+ $bindings = [];
+ foreach ($context->getOldEntities() as $old) {
+ $bindings[] = $old->getMorphClass();
+ $bindings[] = $old->id;
+ }
- // We should probably have references by this point, so we could use those for efficient
- // discovery instead of scanning each item within the context.
+ // No targets to update within the context, so no need to continue.
+ if (count($bindings) < 2) {
+ return;
+ }
+
+ $toReferenceQuery = '(to_type, to_id) IN (' . rtrim(str_repeat('(?,?),', count($bindings) / 2), ',') . ')';
+
+ // Cycle each new entity in the context
+ foreach ($context->getNewEntities() as $new) {
+ // For each, get all references from it which lead to other items within the context of the change
+ $newReferencesInContext = $new->referencesFrom()->whereRaw($toReferenceQuery, $bindings)->get();
+ // For each reference, update the URL and the reference entry
+ foreach ($newReferencesInContext as $reference) {
+ $oldToEntity = $reference->to;
+ $newToEntity = $context->getNewForOld($oldToEntity);
+ if ($newToEntity === null) {
+ continue;
+ }
+
+ $this->updateReferencesWithinEntity($new, $oldToEntity->getUrl(), $newToEntity->getUrl());
+ if ($newToEntity instanceof Page && $oldToEntity instanceof Page) {
+ $this->updateReferencesWithinPage($newToEntity, $oldToEntity->getPermalink(), $newToEntity->getPermalink());
+ }
+ $reference->to_id = $newToEntity->id;
+ $reference->to_type = $newToEntity->getMorphClass();
+ $reference->save();
+ }
+ }
}
/**
'html' => '<p>This is a test <a href="' . $book->getUrl() . '">book link</a></p>',
]);
- $html = '<p>This is a test <a href="' . $page->getUrl() . '">page link</a></p>';
-
// Quick pre-update to get stable slug
$this->put($book->getUrl(), ['name' => 'Internal ref test']);
$book->refresh();
+ $page->refresh();
+ $html = '<p>This is a test <a href="' . $page->getUrl() . '">page link</a></p>';
$this->put($book->getUrl(), ['name' => 'Internal ref test', 'description_html' => $html]);
$this->post($book->getUrl('/copy'), ['name' => 'My copied book']);
'html' => '<p>This is a test <a href="' . $chapter->getUrl() . '">chapter link</a></p>',
]);
- $html = '<p>This is a test <a href="' . $page->getUrl() . '">page link</a></p>';
-
// Quick pre-update to get stable slug
$this->put($chapter->getUrl(), ['name' => 'Internal ref test']);
$chapter->refresh();
+ $page->refresh();
+ $html = '<p>This is a test <a href="' . $page->getUrl() . '">page link</a></p>';
$this->put($chapter->getUrl(), ['name' => 'Internal ref test', 'description_html' => $html]);
$this->post($chapter->getUrl('/copy'), ['name' => 'My copied chapter']);
$newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
$newPage = $newChapter->pages()->where('name', '=', 'reference test page')->first();
- $this->assertStringContainsString($newChapter->getUrl(), $newPage->html);
- $this->assertStringContainsString($newPage->getUrl(), $newChapter->description_html);
+ $this->assertStringContainsString($newChapter->getUrl() . '"', $newPage->html);
+ $this->assertStringContainsString($newPage->getUrl() . '"', $newChapter->description_html);
- $this->assertStringNotContainsString($chapter->getUrl(), $newPage->html);
- $this->assertStringNotContainsString($page->getUrl(), $newChapter->description_html);
+ $this->assertStringNotContainsString($chapter->getUrl() . '"', $newPage->html);
+ $this->assertStringNotContainsString($page->getUrl() . '"', $newChapter->description_html);
}
public function test_page_copy_updates_internal_self_references()