Skip to content

Commit

Permalink
Added indexes, Reduced queries on pages
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddanbrown committed Nov 26, 2015
1 parent 3825ea8 commit 22f8a40
Show file tree
Hide file tree
Showing 19 changed files with 319 additions and 48 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ Homestead.yaml
/public/bower
/public/build
/storage/images
_ide_helper.php
_ide_helper.php
/storage/debugbar
10 changes: 0 additions & 10 deletions app/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ public function chapters()
return $this->hasMany('BookStack\Chapter');
}

public function children()
{
$pages = $this->pages()->where('chapter_id', '=', 0)->get();
$chapters = $this->chapters()->get();
foreach($chapters as $chapter) {
$pages->push($chapter);
}
return $pages->sortBy('priority');
}

public function getExcerpt($length = 100)
{
return strlen($this->description) > $length ? substr($this->description, 0, $length-3) . '...' : $this->description;
Expand Down
3 changes: 2 additions & 1 deletion app/Chapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function pages()

public function getUrl()
{
return '/books/' . $this->book->slug . '/chapter/' . $this->slug;
$bookSlug = isset($this->bookSlug) ? $this->bookSlug : $this->book->slug;
return '/books/' . $bookSlug. '/chapter/' . $this->slug;
}

public function getExcerpt($length = 100)
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/BookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public function show($slug)
{
$book = $this->bookRepo->getBySlug($slug);
Views::add($book);
return view('books/show', ['book' => $book, 'current' => $book]);
$bookChildren = $this->bookRepo->getChildren($book);
return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/ChapterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ public function show($bookSlug, $chapterSlug)
{
$book = $this->bookRepo->getBySlug($bookSlug);
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
$sidebarTree = $this->bookRepo->getChildren($book);
Views::add($chapter);
return view('chapters/show', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
return view('chapters/show', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter, 'sidebarTree' => $sidebarTree]);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ public function show($bookSlug, $pageSlug)
{
$book = $this->bookRepo->getBySlug($bookSlug);
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
$sidebarTree = $this->bookRepo->getChildren($book);
Views::add($page);
return view('pages/show', ['page' => $page, 'book' => $book, 'current' => $page]);
return view('pages/show', ['page' => $page, 'book' => $book, 'current' => $page, 'sidebarTree' => $sidebarTree]);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion app/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public function revisions()

public function getUrl()
{
return '/books/' . $this->book->slug . '/page/' . $this->slug;
// TODO - Extract this and share with chapters
$bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
return '/books/' . $bookSlug . '/page/' . $this->slug;
}

public function getExcerpt($length = 100)
Expand Down
24 changes: 24 additions & 0 deletions app/Repos/BookRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,30 @@ public function findSuitableSlug($name, $currentId = false)
return $slug;
}

/**
* Get all child objects of a book.
* Returns a sorted collection of Pages and Chapters.
* Loads the bookslug onto child elements to prevent access database access for getting the slug.
* @param Book $book
* @return mixed
*/
public function getChildren(Book $book)
{
$pages = $book->pages()->where('chapter_id', '=', 0)->get();
$chapters = $book->chapters()->with('pages')->get();
$children = $pages->merge($chapters);
$bookSlug = $book->slug;
$children->each(function ($child) use ($bookSlug) {
$child->setAttribute('bookSlug', $bookSlug);
if ($child->isA('chapter')) {
$child->pages->each(function ($page) use ($bookSlug) {
$page->setAttribute('bookSlug', $bookSlug);
});
}
});
return $children->sortBy('priority');
}

/**
* Get books by search term.
* @param $term
Expand Down
33 changes: 24 additions & 9 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,20 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
*/
protected $hidden = ['password', 'remember_token'];

/**
* This holds the user's permissions when loaded.
* @var array
*/
protected $permissions;

/**
* Returns a default guest user.
*/
public static function getDefault()
{
return new static([
'email' => 'guest',
'name' => 'Guest'
'name' => 'Guest'
]);
}

Expand All @@ -58,7 +64,19 @@ public function roles()

public function getRoleAttribute()
{
return $this->roles()->first();
return $this->roles()->with('permissions')->first();
}

/**
* Loads the user's permissions from thier role.
*/
private function loadPermissions()
{
if (isset($this->permissions)) return;
$this->load('roles.permissions');
$permissions = $this->roles[0]->permissions;
$permissionsArray = $permissions->pluck('name')->all();
$this->permissions = $permissionsArray;
}

/**
Expand All @@ -68,14 +86,11 @@ public function getRoleAttribute()
*/
public function can($permissionName)
{
if($this->email == 'guest') {
if ($this->email == 'guest') {
return false;
}
$permissions = $this->role->permissions()->get();
$permissionSearch = $permissions->search(function ($item, $key) use ($permissionName) {
return $item->name == $permissionName;
});
return $permissionSearch !== false;
$this->loadPermissions();
return array_search($permissionName, $this->permissions) !== false;
}

/**
Expand Down Expand Up @@ -114,7 +129,7 @@ public function socialAccounts()
*/
public function hasSocialAccount($socialDriver = false)
{
if($socialDriver === false) {
if ($socialDriver === false) {
return $this->socialAccounts()->count() > 0;
}

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"laravel/framework": "5.1.*",
"intervention/image": "^2.3",
"barryvdh/laravel-ide-helper": "^2.1",
"laravel/socialite": "^2.0"
"laravel/socialite": "^2.0",
"barryvdh/laravel-debugbar": "^2.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand Down
Loading

0 comments on commit 22f8a40

Please sign in to comment.