0
0
Fork 0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-05-01 06:59:52 +00:00

Fixed entities created with blank slugs.

Fixes .
This commit is contained in:
Dan Brown 2016-08-13 13:53:04 +01:00
parent 07c7d5af17
commit 2d958e88bf
4 changed files with 19 additions and 17 deletions

View file

@ -216,12 +216,10 @@ class BookRepo extends EntityRepo
*/ */
public function findSuitableSlug($name, $currentId = false) public function findSuitableSlug($name, $currentId = false)
{ {
$originalSlug = Str::slug($name); $slug = Str::slug($name);
$slug = $originalSlug; if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
$count = 2;
while ($this->doesSlugExist($slug, $currentId)) { while ($this->doesSlugExist($slug, $currentId)) {
$slug = $originalSlug . '-' . $count; $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
$count++;
} }
return $slug; return $slug;
} }
@ -229,7 +227,7 @@ class BookRepo extends EntityRepo
/** /**
* Get all child objects of a book. * Get all child objects of a book.
* Returns a sorted collection of Pages and Chapters. * Returns a sorted collection of Pages and Chapters.
* Loads the bookslug onto child elements to prevent access database access for getting the slug. * Loads the book slug onto child elements to prevent access database access for getting the slug.
* @param Book $book * @param Book $book
* @param bool $filterDrafts * @param bool $filterDrafts
* @return mixed * @return mixed
@ -245,7 +243,7 @@ class BookRepo extends EntityRepo
$pages = $pageQuery->get(); $pages = $pageQuery->get();
$chapterQuery = $book->chapters()->with(['pages' => function($query) use ($filterDrafts) { $chapterQuery = $book->chapters()->with(['pages' => function ($query) use ($filterDrafts) {
$this->permissionService->enforcePageRestrictions($query, 'view'); $this->permissionService->enforcePageRestrictions($query, 'view');
if ($filterDrafts) $query->where('draft', '=', false); if ($filterDrafts) $query->where('draft', '=', false);
}]); }]);
@ -263,7 +261,7 @@ class BookRepo extends EntityRepo
$child->pages->each(function ($page) use ($bookSlug) { $child->pages->each(function ($page) use ($bookSlug) {
$page->setAttribute('bookSlug', $bookSlug); $page->setAttribute('bookSlug', $bookSlug);
}); });
$child->pages = $child->pages->sortBy(function($child, $key) { $child->pages = $child->pages->sortBy(function ($child, $key) {
$score = $child->priority; $score = $child->priority;
if ($child->draft) $score -= 100; if ($child->draft) $score -= 100;
return $score; return $score;
@ -272,7 +270,7 @@ class BookRepo extends EntityRepo
}); });
// Sort items with drafts first then by priority. // Sort items with drafts first then by priority.
return $children->sortBy(function($child, $key) { return $children->sortBy(function ($child, $key) {
$score = $child->priority; $score = $child->priority;
if ($child->isA('page') && $child->draft) $score -= 100; if ($child->isA('page') && $child->draft) $score -= 100;
return $score; return $score;

View file

@ -81,7 +81,7 @@ class ChapterRepo extends EntityRepo
{ {
$pages = $this->permissionService->enforcePageRestrictions($chapter->pages())->get(); $pages = $this->permissionService->enforcePageRestrictions($chapter->pages())->get();
// Sort items with drafts first then by priority. // Sort items with drafts first then by priority.
return $pages->sortBy(function($child, $key) { return $pages->sortBy(function ($child, $key) {
$score = $child->priority; $score = $child->priority;
if ($child->draft) $score -= 100; if ($child->draft) $score -= 100;
return $score; return $score;
@ -151,6 +151,7 @@ class ChapterRepo extends EntityRepo
public function findSuitableSlug($name, $bookId, $currentId = false) public function findSuitableSlug($name, $bookId, $currentId = false)
{ {
$slug = Str::slug($name); $slug = Str::slug($name);
if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
while ($this->doesSlugExist($slug, $bookId, $currentId)) { while ($this->doesSlugExist($slug, $bookId, $currentId)) {
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3); $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
} }

View file

@ -147,7 +147,7 @@ class PageRepo extends EntityRepo
$draftPage->fill($input); $draftPage->fill($input);
// Save page tags if present // Save page tags if present
if(isset($input['tags'])) { if (isset($input['tags'])) {
$this->tagRepo->saveTagsToEntity($draftPage, $input['tags']); $this->tagRepo->saveTagsToEntity($draftPage, $input['tags']);
} }
@ -319,7 +319,7 @@ class PageRepo extends EntityRepo
} }
// Save page tags if present // Save page tags if present
if(isset($input['tags'])) { if (isset($input['tags'])) {
$this->tagRepo->saveTagsToEntity($page, $input['tags']); $this->tagRepo->saveTagsToEntity($page, $input['tags']);
} }
@ -405,7 +405,7 @@ class PageRepo extends EntityRepo
$draft->fill($data); $draft->fill($data);
if (setting('app-editor') !== 'markdown') $draft->markdown = ''; if (setting('app-editor') !== 'markdown') $draft->markdown = '';
$draft->save(); $draft->save();
return $draft; return $draft;
} }
@ -591,14 +591,15 @@ class PageRepo extends EntityRepo
/** /**
* Gets a suitable slug for the resource * Gets a suitable slug for the resource
* @param $name * @param string $name
* @param $bookId * @param int $bookId
* @param bool|false $currentId * @param bool|false $currentId
* @return string * @return string
*/ */
public function findSuitableSlug($name, $bookId, $currentId = false) public function findSuitableSlug($name, $bookId, $currentId = false)
{ {
$slug = Str::slug($name); $slug = Str::slug($name);
if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
while ($this->doesSlugExist($slug, $bookId, $currentId)) { while ($this->doesSlugExist($slug, $bookId, $currentId)) {
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3); $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
} }

View file

@ -151,8 +151,10 @@ class EntityTest extends TestCase
->visit('/books/create') ->visit('/books/create')
->type($book->name, '#name') ->type($book->name, '#name')
->type($book->description, '#description') ->type($book->description, '#description')
->press('Save Book') ->press('Save Book');
->seePageIs('/books/my-first-book-2');
$expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
$this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
$book = \BookStack\Book::where('slug', '=', 'my-first-book')->first(); $book = \BookStack\Book::where('slug', '=', 'my-first-book')->first();
return $book; return $book;