mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-08-26 20:22:18 +00:00
Enabled utf8 slugs
Prevents slug change when using only non-ascii chars Allows use of more localised urls. Closes #233
This commit is contained in:
parent
d3c7aada89
commit
0fb1fc87c8
@ -13,9 +13,9 @@ class Book extends Entity
|
|||||||
public function getUrl($path = false)
|
public function getUrl($path = false)
|
||||||
{
|
{
|
||||||
if ($path !== false) {
|
if ($path !== false) {
|
||||||
return baseUrl('/books/' . $this->slug . '/' . trim($path, '/'));
|
return baseUrl('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
|
||||||
}
|
}
|
||||||
return baseUrl('/books/' . $this->slug);
|
return baseUrl('/books/' . urlencode($this->slug));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -32,9 +32,9 @@ class Chapter extends Entity
|
|||||||
{
|
{
|
||||||
$bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
|
$bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
|
||||||
if ($path !== false) {
|
if ($path !== false) {
|
||||||
return baseUrl('/books/' . $bookSlug. '/chapter/' . $this->slug . '/' . trim($path, '/'));
|
return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug) . '/' . trim($path, '/'));
|
||||||
}
|
}
|
||||||
return baseUrl('/books/' . $bookSlug. '/chapter/' . $this->slug);
|
return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -115,8 +115,10 @@ class ChapterController extends Controller
|
|||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$book = $this->bookRepo->getBySlug($bookSlug);
|
||||||
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
|
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
|
||||||
$this->checkOwnablePermission('chapter-update', $chapter);
|
$this->checkOwnablePermission('chapter-update', $chapter);
|
||||||
|
if ($chapter->name !== $request->get('name')) {
|
||||||
|
$chapter->slug = $this->chapterRepo->findSuitableSlug($request->get('name'), $book->id, $chapter->id);
|
||||||
|
}
|
||||||
$chapter->fill($request->all());
|
$chapter->fill($request->all());
|
||||||
$chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
|
|
||||||
$chapter->updated_by = user()->id;
|
$chapter->updated_by = user()->id;
|
||||||
$chapter->save();
|
$chapter->save();
|
||||||
Activity::add($chapter, 'chapter_update', $book->id);
|
Activity::add($chapter, 'chapter_update', $book->id);
|
||||||
|
@ -72,13 +72,13 @@ class Page extends Entity
|
|||||||
{
|
{
|
||||||
$bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
|
$bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
|
||||||
$midText = $this->draft ? '/draft/' : '/page/';
|
$midText = $this->draft ? '/draft/' : '/page/';
|
||||||
$idComponent = $this->draft ? $this->id : $this->slug;
|
$idComponent = $this->draft ? $this->id : urlencode($this->slug);
|
||||||
|
|
||||||
if ($path !== false) {
|
if ($path !== false) {
|
||||||
return baseUrl('/books/' . $bookSlug . $midText . $idComponent . '/' . trim($path, '/'));
|
return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
|
||||||
}
|
}
|
||||||
|
|
||||||
return baseUrl('/books/' . $bookSlug . $midText . $idComponent);
|
return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -147,8 +147,10 @@ class BookRepo extends EntityRepo
|
|||||||
*/
|
*/
|
||||||
public function updateFromInput(Book $book, $input)
|
public function updateFromInput(Book $book, $input)
|
||||||
{
|
{
|
||||||
|
if ($book->name !== $input['name']) {
|
||||||
|
$book->slug = $this->findSuitableSlug($input['name'], $book->id);
|
||||||
|
}
|
||||||
$book->fill($input);
|
$book->fill($input);
|
||||||
$book->slug = $this->findSuitableSlug($book->name, $book->id);
|
|
||||||
$book->updated_by = user()->id;
|
$book->updated_by = user()->id;
|
||||||
$book->save();
|
$book->save();
|
||||||
$this->permissionService->buildJointPermissionsForEntity($book);
|
$this->permissionService->buildJointPermissionsForEntity($book);
|
||||||
@ -208,8 +210,7 @@ class BookRepo extends EntityRepo
|
|||||||
*/
|
*/
|
||||||
public function findSuitableSlug($name, $currentId = false)
|
public function findSuitableSlug($name, $currentId = false)
|
||||||
{
|
{
|
||||||
$slug = Str::slug($name);
|
$slug = $this->nameToSlug($name);
|
||||||
if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
|
|
||||||
while ($this->doesSlugExist($slug, $currentId)) {
|
while ($this->doesSlugExist($slug, $currentId)) {
|
||||||
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
|
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
|
||||||
}
|
}
|
||||||
|
@ -150,8 +150,7 @@ class ChapterRepo extends EntityRepo
|
|||||||
*/
|
*/
|
||||||
public function findSuitableSlug($name, $bookId, $currentId = false)
|
public function findSuitableSlug($name, $bookId, $currentId = false)
|
||||||
{
|
{
|
||||||
$slug = Str::slug($name);
|
$slug = $this->nameToSlug($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);
|
||||||
}
|
}
|
||||||
|
@ -269,6 +269,19 @@ class EntityRepo
|
|||||||
$this->permissionService->buildJointPermissionsForEntities($collection);
|
$this->permissionService->buildJointPermissionsForEntities($collection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format a name as a url slug.
|
||||||
|
* @param $name
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function nameToSlug($name)
|
||||||
|
{
|
||||||
|
$slug = str_replace(' ', '-', strtolower($name));
|
||||||
|
$slug = preg_replace('/[\+\/\\\?\@\}\{\.\,\=\[\]\#\&\!\*\'\;\:\$\%]/', '', $slug);
|
||||||
|
if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
|
||||||
|
return $slug;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -614,8 +614,7 @@ class PageRepo extends EntityRepo
|
|||||||
*/
|
*/
|
||||||
public function findSuitableSlug($name, $bookId, $currentId = false)
|
public function findSuitableSlug($name, $bookId, $currentId = false)
|
||||||
{
|
{
|
||||||
$slug = Str::slug($name);
|
$slug = $this->nameToSlug($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);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user