NOW MANAGED ON CODEBERG
Optimized loading of page/chapter URLs to be a little more efficient
Dan Brown1a6293ce245f
diff --git a/app/Entities/Models/BookChild.php b/app/Entities/Models/BookChild.phpindex f61878208..263eceb58 100644--- a/app/Entities/Models/BookChild.php+++ b/app/Entities/Models/BookChild.php@@ -10,14 +10,29 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;** @property int $book_id* @property int $priority+ * @property string $book_slug* @property Book $book** @method Builder whereSlugs(string $bookSlug, string $childSlug)*/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.*/public function scopeWhereSlugs(Builder $query, string $bookSlug, string $childSlug)diff --git a/app/Entities/Models/Chapter.php b/app/Entities/Models/Chapter.phpindex fd818b703..f6f8427a3 100644--- a/app/Entities/Models/Chapter.php+++ b/app/Entities/Models/Chapter.php@@ -36,7 +36,7 @@ class Chapter extends BookChild{$parts = ['books',- urlencode($this->getAttribute('bookSlug') ?? $this->book->slug),+ urlencode($this->book_slug ?? $this->book->slug),'chapter',urlencode($this->slug),trim($path, '/'),diff --git a/app/Entities/Models/Page.php b/app/Entities/Models/Page.phpindex 123600539..a3a04f403 100644--- a/app/Entities/Models/Page.php+++ b/app/Entities/Models/Page.php@@ -25,9 +25,10 @@ use Permissions;*/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';@@ -48,19 +49,6 @@ class Page extends BookChildreturn 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.*@@ -119,7 +107,7 @@ class Page extends BookChild{$parts = ['books',- urlencode($this->getAttribute('bookSlug') ?? $this->book->slug),+ urlencode($this->book_slug ?? $this->book->slug),$this->draft ? 'draft' : 'page',$this->draft ? $this->id : urlencode($this->slug),trim($path, '/'),diff --git a/app/Entities/Tools/BookContents.php b/app/Entities/Tools/BookContents.phpindex af7746e56..0b8e714fc 100644--- a/app/Entities/Tools/BookContents.php+++ b/app/Entities/Tools/BookContents.php@@ -93,9 +93,11 @@ class BookContents/*** 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) {$query->where('draft', '=', false);diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.phpindex 4a71b56b9..c2ee826c3 100644--- a/app/Http/Controllers/HomeController.php+++ b/app/Http/Controllers/HomeController.php@@ -10,7 +10,6 @@ use BookStack\Entities\Queries\TopFavourites;use BookStack\Entities\Repos\BookRepo;use BookStack\Entities\Repos\BookshelfRepo;use BookStack\Entities\Tools\PageContent;-use Views;class HomeController extends Controller{@@ -41,6 +40,7 @@ class HomeController extends Controller->where('draft', false)->orderBy('updated_at', 'desc')->take($favourites->count() > 0 ? 6 : 12)+ ->select(Page::$listAttributes)->get();$homepageOptions = ['default', 'books', 'bookshelves', 'page'];