]> BookStack Code Mirror - bookstack/commitdiff
Optimized loading of page/chapter URLs to be a little more efficient
authorDan Brown <redacted>
Sat, 21 Aug 2021 18:58:19 +0000 (19:58 +0100)
committerDan Brown <redacted>
Sat, 21 Aug 2021 18:59:55 +0000 (19:59 +0100)
- 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.

app/Entities/Models/BookChild.php
app/Entities/Models/Chapter.php
app/Entities/Models/Page.php
app/Entities/Tools/BookContents.php
app/Http/Controllers/HomeController.php

index f61878208eac80fbdb84d0dfa2e2810bf4e63910..263eceb58a4b15ee8a0fb767cbb66ac98d31386f 100644 (file)
@@ -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)
index fd818b703736b9fe9e7bacd0f67fc5848e94a47c..f6f8427a3a0050e6908250cab0be9fdf9491878d 100644 (file)
@@ -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, '/'),
index 123600539b053f71b7a428d408e75e80cbfe95a6..a3a04f403b9242456c4235b506332c720e78557f 100644 (file)
@@ -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 BookChild
         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.
      *
@@ -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, '/'),
index af7746e561c135bb85c7d6d1c15892d4864aa420..0b8e714fcf441048afee2a2c1f89dc85595dc4fa 100644 (file)
@@ -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);
index 4a71b56b9c1d44b41820ccd66242406181a3ac2d..c2ee826c3bd04dcdd640859b66f5e3d794b31cff 100644 (file)
@@ -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'];
Morty Proxy This is a proxified and sanitized view of the page, visit original site.