1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-12-23 23:02:08 +03:00

Slugs: Added slug recording at points of generation

Also moved some model-level helpers, which used app container
resolution, to be injected services instead.
This commit is contained in:
Dan Brown
2025-11-23 23:29:30 +00:00
parent e64fc60bdf
commit 291a807d98
13 changed files with 141 additions and 64 deletions

View File

@@ -8,6 +8,8 @@ use BookStack\Entities\Models\HasCoverInterface;
use BookStack\Entities\Models\HasDescriptionInterface;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Queries\PageQueries;
use BookStack\Entities\Tools\SlugGenerator;
use BookStack\Entities\Tools\SlugHistory;
use BookStack\Exceptions\ImageUploadException;
use BookStack\References\ReferenceStore;
use BookStack\References\ReferenceUpdater;
@@ -25,6 +27,8 @@ class BaseRepo
protected ReferenceStore $referenceStore,
protected PageQueries $pageQueries,
protected BookSorter $bookSorter,
protected SlugGenerator $slugGenerator,
protected SlugHistory $slugHistory,
) {
}
@@ -43,7 +47,7 @@ class BaseRepo
'updated_by' => user()->id,
'owned_by' => user()->id,
]);
$entity->refreshSlug();
$this->refreshSlug($entity);
if ($entity instanceof HasDescriptionInterface) {
$this->updateDescription($entity, $input);
@@ -78,7 +82,7 @@ class BaseRepo
$entity->updated_by = user()->id;
if ($entity->isDirty('name') || empty($entity->slug)) {
$entity->refreshSlug();
$this->refreshSlug($entity);
}
if ($entity instanceof HasDescriptionInterface) {
@@ -155,4 +159,16 @@ class BaseRepo
$entity->descriptionInfo()->set('', $input['description']);
}
}
/**
* Refresh the slug for the given entity.
*/
public function refreshSlug(Entity $entity): void
{
if ($entity->id) {
$this->slugHistory->recordForEntity($entity);
}
$this->slugGenerator->regenerateForEntity($entity);
}
}

View File

@@ -7,6 +7,7 @@ use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Queries\EntityQueries;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Tools\ParentChanger;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Exceptions\MoveOperationException;
use BookStack\Exceptions\PermissionsException;
@@ -21,6 +22,7 @@ class ChapterRepo
protected BaseRepo $baseRepo,
protected EntityQueries $entityQueries,
protected TrashCan $trashCan,
protected ParentChanger $parentChanger,
) {
}
@@ -97,7 +99,7 @@ class ChapterRepo
}
return (new DatabaseTransaction(function () use ($chapter, $parent) {
$chapter = $chapter->changeBook($parent->id);
$this->parentChanger->changeBook($chapter, $parent->id);
$chapter->rebuildPermissions();
Activity::add(ActivityType::CHAPTER_MOVE, $chapter);

View File

@@ -12,6 +12,7 @@ use BookStack\Entities\Queries\EntityQueries;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Tools\PageEditorType;
use BookStack\Entities\Tools\ParentChanger;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Exceptions\MoveOperationException;
use BookStack\Exceptions\PermissionsException;
@@ -31,6 +32,7 @@ class PageRepo
protected ReferenceStore $referenceStore,
protected ReferenceUpdater $referenceUpdater,
protected TrashCan $trashCan,
protected ParentChanger $parentChanger,
) {
}
@@ -242,7 +244,7 @@ class PageRepo
}
$page->updated_by = user()->id;
$page->refreshSlug();
$this->baseRepo->refreshSlug($page);
$page->save();
$page->indexForSearch();
$this->referenceStore->updateForEntity($page);
@@ -284,7 +286,7 @@ class PageRepo
return (new DatabaseTransaction(function () use ($page, $parent) {
$page->chapter_id = ($parent instanceof Chapter) ? $parent->id : null;
$newBookId = ($parent instanceof Chapter) ? $parent->book->id : $parent->id;
$page = $page->changeBook($newBookId);
$this->parentChanger->changeBook($page, $newBookId);
$page->rebuildPermissions();
Activity::add(ActivityType::PAGE_MOVE, $page);