mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-06-04 08:54:33 +08:00
Optimized loading of page/chapter URLs to be a little more efficient
- Loaded book_slug as part of chapter/page queries instead of books being loaded in afterwards. - Removed unused page method. - Updated some page queries to load specific attributes.
This commit is contained in:
@ -10,14 +10,29 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|||||||
*
|
*
|
||||||
* @property int $book_id
|
* @property int $book_id
|
||||||
* @property int $priority
|
* @property int $priority
|
||||||
|
* @property string $book_slug
|
||||||
* @property Book $book
|
* @property Book $book
|
||||||
*
|
*
|
||||||
* @method Builder whereSlugs(string $bookSlug, string $childSlug)
|
* @method Builder whereSlugs(string $bookSlug, string $childSlug)
|
||||||
*/
|
*/
|
||||||
abstract class BookChild extends Entity
|
abstract class BookChild extends Entity
|
||||||
{
|
{
|
||||||
|
|
||||||
|
protected static function boot()
|
||||||
|
{
|
||||||
|
parent::boot();
|
||||||
|
|
||||||
|
// Load book slugs onto these models by default during query-time
|
||||||
|
static::addGlobalScope('book_slug', function(Builder $builder) {
|
||||||
|
$builder->addSelect(['book_slug' => function($builder) {
|
||||||
|
$builder->select('slug')
|
||||||
|
->from('books')
|
||||||
|
->whereColumn('books.id', '=', 'book_id');
|
||||||
|
}]);
|
||||||
|
});
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Scope a query to find items where the the child has the given childSlug
|
* Scope a query to find items where the child has the given childSlug
|
||||||
* where its parent has the bookSlug.
|
* where its parent has the bookSlug.
|
||||||
*/
|
*/
|
||||||
public function scopeWhereSlugs(Builder $query, string $bookSlug, string $childSlug)
|
public function scopeWhereSlugs(Builder $query, string $bookSlug, string $childSlug)
|
||||||
|
@ -36,7 +36,7 @@ class Chapter extends BookChild
|
|||||||
{
|
{
|
||||||
$parts = [
|
$parts = [
|
||||||
'books',
|
'books',
|
||||||
urlencode($this->getAttribute('bookSlug') ?? $this->book->slug),
|
urlencode($this->book_slug ?? $this->book->slug),
|
||||||
'chapter',
|
'chapter',
|
||||||
urlencode($this->slug),
|
urlencode($this->slug),
|
||||||
trim($path, '/'),
|
trim($path, '/'),
|
||||||
|
@ -25,9 +25,10 @@ use Permissions;
|
|||||||
*/
|
*/
|
||||||
class Page extends BookChild
|
class Page extends BookChild
|
||||||
{
|
{
|
||||||
protected $fillable = ['name', 'priority', 'markdown'];
|
public static $listAttributes = ['name', 'id', 'slug', 'book_id', 'text', 'created_at', 'updated_at'];
|
||||||
|
public static $contentAttributes = ['name', 'id', 'slug', 'book_id', 'html', 'text', 'created_at', 'updated_at'];
|
||||||
|
|
||||||
protected $simpleAttributes = ['name', 'id', 'slug'];
|
protected $fillable = ['name', 'priority', 'markdown'];
|
||||||
|
|
||||||
public $textField = 'text';
|
public $textField = 'text';
|
||||||
|
|
||||||
@ -48,19 +49,6 @@ class Page extends BookChild
|
|||||||
return parent::scopeVisible($query);
|
return parent::scopeVisible($query);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts this page into a simplified array.
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function toSimpleArray()
|
|
||||||
{
|
|
||||||
$array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
|
|
||||||
$array['url'] = $this->getUrl();
|
|
||||||
|
|
||||||
return $array;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the chapter that this page is in, If applicable.
|
* Get the chapter that this page is in, If applicable.
|
||||||
*
|
*
|
||||||
@ -119,7 +107,7 @@ class Page extends BookChild
|
|||||||
{
|
{
|
||||||
$parts = [
|
$parts = [
|
||||||
'books',
|
'books',
|
||||||
urlencode($this->getAttribute('bookSlug') ?? $this->book->slug),
|
urlencode($this->book_slug ?? $this->book->slug),
|
||||||
$this->draft ? 'draft' : 'page',
|
$this->draft ? 'draft' : 'page',
|
||||||
$this->draft ? $this->id : urlencode($this->slug),
|
$this->draft ? $this->id : urlencode($this->slug),
|
||||||
trim($path, '/'),
|
trim($path, '/'),
|
||||||
|
@ -93,9 +93,11 @@ class BookContents
|
|||||||
/**
|
/**
|
||||||
* Get the visible pages within this book.
|
* Get the visible pages within this book.
|
||||||
*/
|
*/
|
||||||
protected function getPages(bool $showDrafts = false): Collection
|
protected function getPages(bool $showDrafts = false, bool $getPageContent = false): Collection
|
||||||
{
|
{
|
||||||
$query = Page::visible()->where('book_id', '=', $this->book->id);
|
$query = Page::visible()
|
||||||
|
->select($getPageContent ? Page::$contentAttributes : Page::$listAttributes)
|
||||||
|
->where('book_id', '=', $this->book->id);
|
||||||
|
|
||||||
if (!$showDrafts) {
|
if (!$showDrafts) {
|
||||||
$query->where('draft', '=', false);
|
$query->where('draft', '=', false);
|
||||||
|
@ -10,7 +10,6 @@ use BookStack\Entities\Queries\TopFavourites;
|
|||||||
use BookStack\Entities\Repos\BookRepo;
|
use BookStack\Entities\Repos\BookRepo;
|
||||||
use BookStack\Entities\Repos\BookshelfRepo;
|
use BookStack\Entities\Repos\BookshelfRepo;
|
||||||
use BookStack\Entities\Tools\PageContent;
|
use BookStack\Entities\Tools\PageContent;
|
||||||
use Views;
|
|
||||||
|
|
||||||
class HomeController extends Controller
|
class HomeController extends Controller
|
||||||
{
|
{
|
||||||
@ -41,6 +40,7 @@ class HomeController extends Controller
|
|||||||
->where('draft', false)
|
->where('draft', false)
|
||||||
->orderBy('updated_at', 'desc')
|
->orderBy('updated_at', 'desc')
|
||||||
->take($favourites->count() > 0 ? 6 : 12)
|
->take($favourites->count() > 0 ? 6 : 12)
|
||||||
|
->select(Page::$listAttributes)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
$homepageOptions = ['default', 'books', 'bookshelves', 'page'];
|
$homepageOptions = ['default', 'books', 'bookshelves', 'page'];
|
||||||
|
Reference in New Issue
Block a user