Skip to content

Commit

Permalink
Added book child reference handling on book url change
Browse files Browse the repository at this point in the history
Closes #3683
  • Loading branch information
ssddanbrown committed Aug 30, 2022
1 parent 1cc7c64 commit 9153be9
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
30 changes: 29 additions & 1 deletion app/References/ReferenceUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BookStack\References;

use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\RevisionRepo;
Expand All @@ -21,7 +22,7 @@ public function __construct(ReferenceFetcher $referenceFetcher, RevisionRepo $re

public function updateEntityPageReferences(Entity $entity, string $oldLink)
{
$references = $this->referenceFetcher->getPageReferencesToEntity($entity);
$references = $this->getReferencesToUpdate($entity);
$newLink = $entity->getUrl();

/** @var Reference $reference */
Expand All @@ -32,6 +33,33 @@ public function updateEntityPageReferences(Entity $entity, string $oldLink)
}
}

/**
* @return Reference[]
*/
protected function getReferencesToUpdate(Entity $entity): array
{
/** @var Reference[] $references */
$references = $this->referenceFetcher->getPageReferencesToEntity($entity)->values()->all();

if ($entity instanceof Book) {
$pages = $entity->pages()->get(['id']);
$chapters = $entity->chapters()->get(['id']);
$children = $pages->concat($chapters);
foreach ($children as $bookChild) {
$childRefs = $this->referenceFetcher->getPageReferencesToEntity($bookChild)->values()->all();
array_push($references, ...$childRefs);
}
}

$deduped = [];
foreach ($references as $reference) {
$key = $reference->from_id . ':' . $reference->from_type;
$deduped[$key] = $reference;
}

return array_values($deduped);
}

protected function updateReferencesWithinPage(Page $page, string $oldLink, string $newLink)
{
$page = (clone $page)->refresh();
Expand Down
51 changes: 51 additions & 0 deletions tests/References/ReferencesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\References;

use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Entities\Tools\TrashCan;
Expand Down Expand Up @@ -145,6 +146,56 @@ public function test_pages_leading_to_entity_updated_on_url_change()
}
}

public function test_pages_linking_to_other_page_updated_on_parent_book_url_change()
{
/** @var Page $bookPage */
/** @var Page $otherPage */
/** @var Book $book */
$bookPage = Page::query()->first();
$otherPage = Page::query()->where('id', '!=', $bookPage->id)->first();
$book = $bookPage->book;

$otherPage->html = '<a href="' . $bookPage->getUrl() . '">Link</a>';
$otherPage->save();
$this->createReference($otherPage, $bookPage);

$this->asEditor()->put($book->getUrl(), [
'name' => 'my updated book slugaroo',
]);

$otherPage->refresh();
$this->assertStringContainsString('href="http://localhost/books/my-updated-book-slugaroo/page/' . $bookPage->slug . '"', $otherPage->html);
$this->assertDatabaseHas('page_revisions', [
'page_id' => $otherPage->id,
'summary' => 'System auto-update of internal links',
]);
}

public function test_pages_linking_to_chapter_updated_on_parent_book_url_change()
{
/** @var Chapter $bookChapter */
/** @var Page $otherPage */
/** @var Book $book */
$bookChapter = Chapter::query()->first();
$otherPage = Page::query()->first();
$book = $bookChapter->book;

$otherPage->html = '<a href="' . $bookChapter->getUrl() . '">Link</a>';
$otherPage->save();
$this->createReference($otherPage, $bookChapter);

$this->asEditor()->put($book->getUrl(), [
'name' => 'my updated book slugaroo',
]);

$otherPage->refresh();
$this->assertStringContainsString('href="http://localhost/books/my-updated-book-slugaroo/chapter/' . $bookChapter->slug . '"', $otherPage->html);
$this->assertDatabaseHas('page_revisions', [
'page_id' => $otherPage->id,
'summary' => 'System auto-update of internal links',
]);
}

public function test_markdown_links_leading_to_entity_updated_on_url_change()
{
/** @var Page $page */
Expand Down

0 comments on commit 9153be9

Please sign in to comment.