Fixed errors that occured when merging & refactored entity repositories
Also deleted the git '.orig' files that got added in last merge.
Showing
8 changed files
with
80 additions
and
680 deletions
| 1 | <?php namespace BookStack\Repos; | 1 | <?php namespace BookStack\Repos; |
| 2 | 2 | ||
| 3 | -use Activity; | 3 | +use BookStack\Exceptions\NotFoundException; |
| 4 | use Illuminate\Support\Str; | 4 | use Illuminate\Support\Str; |
| 5 | use BookStack\Book; | 5 | use BookStack\Book; |
| 6 | use Views; | 6 | use Views; |
| 7 | 7 | ||
| 8 | -class BookRepo | 8 | +class BookRepo extends EntityRepo |
| 9 | { | 9 | { |
| 10 | - | ||
| 11 | - protected $book; | ||
| 12 | protected $pageRepo; | 10 | protected $pageRepo; |
| 13 | protected $chapterRepo; | 11 | protected $chapterRepo; |
| 14 | - protected $restrictionService; | ||
| 15 | 12 | ||
| 16 | /** | 13 | /** |
| 17 | * BookRepo constructor. | 14 | * BookRepo constructor. |
| 18 | - * @param Book $book | ||
| 19 | * @param PageRepo $pageRepo | 15 | * @param PageRepo $pageRepo |
| 20 | * @param ChapterRepo $chapterRepo | 16 | * @param ChapterRepo $chapterRepo |
| 21 | - * @param RestrictionService $restrictionService | ||
| 22 | */ | 17 | */ |
| 23 | - public function __construct(Book $book, PageRepo $pageRepo, ChapterRepo $chapterRepo, RestrictionService $restrictionService) | 18 | + public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo) |
| 24 | { | 19 | { |
| 25 | - $this->book = $book; | ||
| 26 | $this->pageRepo = $pageRepo; | 20 | $this->pageRepo = $pageRepo; |
| 27 | $this->chapterRepo = $chapterRepo; | 21 | $this->chapterRepo = $chapterRepo; |
| 28 | - $this->restrictionService = $restrictionService; | 22 | + parent::__construct(); |
| 29 | } | 23 | } |
| 30 | 24 | ||
| 31 | /** | 25 | /** |
| ... | @@ -90,7 +84,6 @@ class BookRepo | ... | @@ -90,7 +84,6 @@ class BookRepo |
| 90 | */ | 84 | */ |
| 91 | public function getRecentlyViewed($count = 10, $page = 0) | 85 | public function getRecentlyViewed($count = 10, $page = 0) |
| 92 | { | 86 | { |
| 93 | - // TODO restrict | ||
| 94 | return Views::getUserRecentlyViewed($count, $page, $this->book); | 87 | return Views::getUserRecentlyViewed($count, $page, $this->book); |
| 95 | } | 88 | } |
| 96 | 89 | ||
| ... | @@ -102,7 +95,6 @@ class BookRepo | ... | @@ -102,7 +95,6 @@ class BookRepo |
| 102 | */ | 95 | */ |
| 103 | public function getPopular($count = 10, $page = 0) | 96 | public function getPopular($count = 10, $page = 0) |
| 104 | { | 97 | { |
| 105 | - // TODO - Restrict | ||
| 106 | return Views::getPopular($count, $page, $this->book); | 98 | return Views::getPopular($count, $page, $this->book); |
| 107 | } | 99 | } |
| 108 | 100 | ||
| ... | @@ -241,16 +233,7 @@ class BookRepo | ... | @@ -241,16 +233,7 @@ class BookRepo |
| 241 | */ | 233 | */ |
| 242 | public function getBySearch($term, $count = 20, $paginationAppends = []) | 234 | public function getBySearch($term, $count = 20, $paginationAppends = []) |
| 243 | { | 235 | { |
| 244 | - preg_match_all('/"(.*?)"/', $term, $matches); | 236 | + $terms = $this->prepareSearchTerms($term); |
| 245 | - if (count($matches[1]) > 0) { | ||
| 246 | - $terms = $matches[1]; | ||
| 247 | - $term = trim(preg_replace('/"(.*?)"/', '', $term)); | ||
| 248 | - } else { | ||
| 249 | - $terms = []; | ||
| 250 | - } | ||
| 251 | - if (!empty($term)) { | ||
| 252 | - $terms = array_merge($terms, explode(' ', $term)); | ||
| 253 | - } | ||
| 254 | $books = $this->restrictionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms)) | 237 | $books = $this->restrictionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms)) |
| 255 | ->paginate($count)->appends($paginationAppends); | 238 | ->paginate($count)->appends($paginationAppends); |
| 256 | $words = join('|', explode(' ', preg_quote(trim($term), '/'))); | 239 | $words = join('|', explode(' ', preg_quote(trim($term), '/'))); |
| ... | @@ -262,27 +245,4 @@ class BookRepo | ... | @@ -262,27 +245,4 @@ class BookRepo |
| 262 | return $books; | 245 | return $books; |
| 263 | } | 246 | } |
| 264 | 247 | ||
| 265 | - /** | ||
| 266 | - * Updates books restrictions from a request | ||
| 267 | - * @param $request | ||
| 268 | - * @param $book | ||
| 269 | - */ | ||
| 270 | - public function updateRestrictionsFromRequest($request, $book) | ||
| 271 | - { | ||
| 272 | - // TODO - extract into shared repo | ||
| 273 | - $book->restricted = $request->has('restricted') && $request->get('restricted') === 'true'; | ||
| 274 | - $book->restrictions()->delete(); | ||
| 275 | - if ($request->has('restrictions')) { | ||
| 276 | - foreach ($request->get('restrictions') as $roleId => $restrictions) { | ||
| 277 | - foreach ($restrictions as $action => $value) { | ||
| 278 | - $book->restrictions()->create([ | ||
| 279 | - 'role_id' => $roleId, | ||
| 280 | - 'action' => strtolower($action) | ||
| 281 | - ]); | ||
| 282 | - } | ||
| 283 | - } | ||
| 284 | - } | ||
| 285 | - $book->save(); | ||
| 286 | - } | ||
| 287 | - | ||
| 288 | } | 248 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
app/Repos/BookRepo.php.orig
deleted
100644 → 0
| 1 | -<?php namespace BookStack\Repos; | ||
| 2 | - | ||
| 3 | -use Activity; | ||
| 4 | -use BookStack\Exceptions\NotFoundException; | ||
| 5 | -use BookStack\Services\RestrictionService; | ||
| 6 | -use Illuminate\Support\Str; | ||
| 7 | -use BookStack\Book; | ||
| 8 | -use Views; | ||
| 9 | - | ||
| 10 | -class BookRepo | ||
| 11 | -{ | ||
| 12 | - | ||
| 13 | - protected $book; | ||
| 14 | - protected $pageRepo; | ||
| 15 | - protected $chapterRepo; | ||
| 16 | - protected $restrictionService; | ||
| 17 | - | ||
| 18 | - /** | ||
| 19 | - * BookRepo constructor. | ||
| 20 | - * @param Book $book | ||
| 21 | - * @param PageRepo $pageRepo | ||
| 22 | - * @param ChapterRepo $chapterRepo | ||
| 23 | - * @param RestrictionService $restrictionService | ||
| 24 | - */ | ||
| 25 | - public function __construct(Book $book, PageRepo $pageRepo, ChapterRepo $chapterRepo, RestrictionService $restrictionService) | ||
| 26 | - { | ||
| 27 | - $this->book = $book; | ||
| 28 | - $this->pageRepo = $pageRepo; | ||
| 29 | - $this->chapterRepo = $chapterRepo; | ||
| 30 | - $this->restrictionService = $restrictionService; | ||
| 31 | - } | ||
| 32 | - | ||
| 33 | - /** | ||
| 34 | - * Base query for getting books. | ||
| 35 | - * Takes into account any restrictions. | ||
| 36 | - * @return mixed | ||
| 37 | - */ | ||
| 38 | - private function bookQuery() | ||
| 39 | - { | ||
| 40 | - return $this->restrictionService->enforceBookRestrictions($this->book, 'view'); | ||
| 41 | - } | ||
| 42 | - | ||
| 43 | - /** | ||
| 44 | - * Get the book that has the given id. | ||
| 45 | - * @param $id | ||
| 46 | - * @return mixed | ||
| 47 | - */ | ||
| 48 | - public function getById($id) | ||
| 49 | - { | ||
| 50 | - return $this->bookQuery()->findOrFail($id); | ||
| 51 | - } | ||
| 52 | - | ||
| 53 | - /** | ||
| 54 | - * Get all books, Limited by count. | ||
| 55 | - * @param int $count | ||
| 56 | - * @return mixed | ||
| 57 | - */ | ||
| 58 | - public function getAll($count = 10) | ||
| 59 | - { | ||
| 60 | - $bookQuery = $this->bookQuery()->orderBy('name', 'asc'); | ||
| 61 | - if (!$count) return $bookQuery->get(); | ||
| 62 | - return $bookQuery->take($count)->get(); | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - /** | ||
| 66 | - * Get all books paginated. | ||
| 67 | - * @param int $count | ||
| 68 | - * @return mixed | ||
| 69 | - */ | ||
| 70 | - public function getAllPaginated($count = 10) | ||
| 71 | - { | ||
| 72 | - return $this->bookQuery() | ||
| 73 | - ->orderBy('name', 'asc')->paginate($count); | ||
| 74 | - } | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - /** | ||
| 78 | - * Get the latest books. | ||
| 79 | - * @param int $count | ||
| 80 | - * @return mixed | ||
| 81 | - */ | ||
| 82 | - public function getLatest($count = 10) | ||
| 83 | - { | ||
| 84 | - return $this->bookQuery()->orderBy('created_at', 'desc')->take($count)->get(); | ||
| 85 | - } | ||
| 86 | - | ||
| 87 | - /** | ||
| 88 | - * Gets the most recently viewed for a user. | ||
| 89 | - * @param int $count | ||
| 90 | - * @param int $page | ||
| 91 | - * @return mixed | ||
| 92 | - */ | ||
| 93 | - public function getRecentlyViewed($count = 10, $page = 0) | ||
| 94 | - { | ||
| 95 | - // TODO restrict | ||
| 96 | - return Views::getUserRecentlyViewed($count, $page, $this->book); | ||
| 97 | - } | ||
| 98 | - | ||
| 99 | - /** | ||
| 100 | - * Gets the most viewed books. | ||
| 101 | - * @param int $count | ||
| 102 | - * @param int $page | ||
| 103 | - * @return mixed | ||
| 104 | - */ | ||
| 105 | - public function getPopular($count = 10, $page = 0) | ||
| 106 | - { | ||
| 107 | - // TODO - Restrict | ||
| 108 | - return Views::getPopular($count, $page, $this->book); | ||
| 109 | - } | ||
| 110 | - | ||
| 111 | - /** | ||
| 112 | - * Get a book by slug | ||
| 113 | - * @param $slug | ||
| 114 | - * @return mixed | ||
| 115 | - * @throws NotFoundException | ||
| 116 | - */ | ||
| 117 | - public function getBySlug($slug) | ||
| 118 | - { | ||
| 119 | - $book = $this->bookQuery()->where('slug', '=', $slug)->first(); | ||
| 120 | - if ($book === null) throw new NotFoundException('Book not found'); | ||
| 121 | - return $book; | ||
| 122 | - } | ||
| 123 | - | ||
| 124 | - /** | ||
| 125 | - * Checks if a book exists. | ||
| 126 | - * @param $id | ||
| 127 | - * @return bool | ||
| 128 | - */ | ||
| 129 | - public function exists($id) | ||
| 130 | - { | ||
| 131 | - return $this->bookQuery()->where('id', '=', $id)->exists(); | ||
| 132 | - } | ||
| 133 | - | ||
| 134 | - /** | ||
| 135 | - * Get a new book instance from request input. | ||
| 136 | - * @param $input | ||
| 137 | - * @return Book | ||
| 138 | - */ | ||
| 139 | - public function newFromInput($input) | ||
| 140 | - { | ||
| 141 | - return $this->book->newInstance($input); | ||
| 142 | - } | ||
| 143 | - | ||
| 144 | - /** | ||
| 145 | - * Destroy a book identified by the given slug. | ||
| 146 | - * @param $bookSlug | ||
| 147 | - */ | ||
| 148 | - public function destroyBySlug($bookSlug) | ||
| 149 | - { | ||
| 150 | - $book = $this->getBySlug($bookSlug); | ||
| 151 | - foreach ($book->pages as $page) { | ||
| 152 | - $this->pageRepo->destroy($page); | ||
| 153 | - } | ||
| 154 | - foreach ($book->chapters as $chapter) { | ||
| 155 | - $this->chapterRepo->destroy($chapter); | ||
| 156 | - } | ||
| 157 | - $book->views()->delete(); | ||
| 158 | - $book->restrictions()->delete(); | ||
| 159 | - $book->delete(); | ||
| 160 | - } | ||
| 161 | - | ||
| 162 | - /** | ||
| 163 | - * Get the next child element priority. | ||
| 164 | - * @param Book $book | ||
| 165 | - * @return int | ||
| 166 | - */ | ||
| 167 | - public function getNewPriority($book) | ||
| 168 | - { | ||
| 169 | - $lastElem = $this->getChildren($book)->pop(); | ||
| 170 | - return $lastElem ? $lastElem->priority + 1 : 0; | ||
| 171 | - } | ||
| 172 | - | ||
| 173 | - /** | ||
| 174 | - * @param string $slug | ||
| 175 | - * @param bool|false $currentId | ||
| 176 | - * @return bool | ||
| 177 | - */ | ||
| 178 | - public function doesSlugExist($slug, $currentId = false) | ||
| 179 | - { | ||
| 180 | - $query = $this->book->where('slug', '=', $slug); | ||
| 181 | - if ($currentId) { | ||
| 182 | - $query = $query->where('id', '!=', $currentId); | ||
| 183 | - } | ||
| 184 | - return $query->count() > 0; | ||
| 185 | - } | ||
| 186 | - | ||
| 187 | - /** | ||
| 188 | - * Provides a suitable slug for the given book name. | ||
| 189 | - * Ensures the returned slug is unique in the system. | ||
| 190 | - * @param string $name | ||
| 191 | - * @param bool|false $currentId | ||
| 192 | - * @return string | ||
| 193 | - */ | ||
| 194 | - public function findSuitableSlug($name, $currentId = false) | ||
| 195 | - { | ||
| 196 | - $originalSlug = Str::slug($name); | ||
| 197 | - $slug = $originalSlug; | ||
| 198 | - $count = 2; | ||
| 199 | - while ($this->doesSlugExist($slug, $currentId)) { | ||
| 200 | - $slug = $originalSlug . '-' . $count; | ||
| 201 | - $count++; | ||
| 202 | - } | ||
| 203 | - return $slug; | ||
| 204 | - } | ||
| 205 | - | ||
| 206 | - /** | ||
| 207 | - * Get all child objects of a book. | ||
| 208 | - * Returns a sorted collection of Pages and Chapters. | ||
| 209 | - * Loads the bookslug onto child elements to prevent access database access for getting the slug. | ||
| 210 | - * @param Book $book | ||
| 211 | - * @return mixed | ||
| 212 | - */ | ||
| 213 | - public function getChildren(Book $book) | ||
| 214 | - { | ||
| 215 | - $pageQuery = $book->pages()->where('chapter_id', '=', 0); | ||
| 216 | - $pageQuery = $this->restrictionService->enforcePageRestrictions($pageQuery, 'view'); | ||
| 217 | - $pages = $pageQuery->get(); | ||
| 218 | - | ||
| 219 | - $chapterQuery = $book->chapters()->with(['pages' => function($query) { | ||
| 220 | - $this->restrictionService->enforcePageRestrictions($query, 'view'); | ||
| 221 | - }]); | ||
| 222 | - $chapterQuery = $this->restrictionService->enforceChapterRestrictions($chapterQuery, 'view'); | ||
| 223 | - $chapters = $chapterQuery->get(); | ||
| 224 | - $children = $pages->merge($chapters); | ||
| 225 | - $bookSlug = $book->slug; | ||
| 226 | - $children->each(function ($child) use ($bookSlug) { | ||
| 227 | - $child->setAttribute('bookSlug', $bookSlug); | ||
| 228 | - if ($child->isA('chapter')) { | ||
| 229 | - $child->pages->each(function ($page) use ($bookSlug) { | ||
| 230 | - $page->setAttribute('bookSlug', $bookSlug); | ||
| 231 | - }); | ||
| 232 | - } | ||
| 233 | - }); | ||
| 234 | - return $children->sortBy('priority'); | ||
| 235 | - } | ||
| 236 | - | ||
| 237 | - /** | ||
| 238 | - * Get books by search term. | ||
| 239 | - * @param $term | ||
| 240 | - * @param int $count | ||
| 241 | - * @param array $paginationAppends | ||
| 242 | - * @return mixed | ||
| 243 | - */ | ||
| 244 | - public function getBySearch($term, $count = 20, $paginationAppends = []) | ||
| 245 | - { | ||
| 246 | -<<<<<<< HEAD | ||
| 247 | - preg_match_all('/"(.*?)"/', $term, $matches); | ||
| 248 | - if (count($matches[1]) > 0) { | ||
| 249 | - $terms = $matches[1]; | ||
| 250 | - $term = trim(preg_replace('/"(.*?)"/', '', $term)); | ||
| 251 | - } else { | ||
| 252 | - $terms = []; | ||
| 253 | - } | ||
| 254 | - if (!empty($term)) { | ||
| 255 | - $terms = array_merge($terms, explode(' ', $term)); | ||
| 256 | - } | ||
| 257 | - $books = $this->book->fullTextSearchQuery(['name', 'description'], $terms) | ||
| 258 | -======= | ||
| 259 | - $terms = explode(' ', $term); | ||
| 260 | - $books = $this->restrictionService->enforceBookRestrictions($this->book->fullTextSearchQuery(['name', 'description'], $terms)) | ||
| 261 | ->>>>>>> custom_role_system | ||
| 262 | - ->paginate($count)->appends($paginationAppends); | ||
| 263 | - $words = join('|', explode(' ', preg_quote(trim($term), '/'))); | ||
| 264 | - foreach ($books as $book) { | ||
| 265 | - //highlight | ||
| 266 | - $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100)); | ||
| 267 | - $book->searchSnippet = $result; | ||
| 268 | - } | ||
| 269 | - return $books; | ||
| 270 | - } | ||
| 271 | - | ||
| 272 | - /** | ||
| 273 | - * Updates books restrictions from a request | ||
| 274 | - * @param $request | ||
| 275 | - * @param $book | ||
| 276 | - */ | ||
| 277 | - public function updateRestrictionsFromRequest($request, $book) | ||
| 278 | - { | ||
| 279 | - // TODO - extract into shared repo | ||
| 280 | - $book->restricted = $request->has('restricted') && $request->get('restricted') === 'true'; | ||
| 281 | - $book->restrictions()->delete(); | ||
| 282 | - if ($request->has('restrictions')) { | ||
| 283 | - foreach ($request->get('restrictions') as $roleId => $restrictions) { | ||
| 284 | - foreach ($restrictions as $action => $value) { | ||
| 285 | - $book->restrictions()->create([ | ||
| 286 | - 'role_id' => $roleId, | ||
| 287 | - 'action' => strtolower($action) | ||
| 288 | - ]); | ||
| 289 | - } | ||
| 290 | - } | ||
| 291 | - } | ||
| 292 | - $book->save(); | ||
| 293 | - } | ||
| 294 | - | ||
| 295 | -} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| ... | @@ -3,27 +3,11 @@ | ... | @@ -3,27 +3,11 @@ |
| 3 | 3 | ||
| 4 | use Activity; | 4 | use Activity; |
| 5 | use BookStack\Exceptions\NotFoundException; | 5 | use BookStack\Exceptions\NotFoundException; |
| 6 | -use BookStack\Services\RestrictionService; | ||
| 7 | use Illuminate\Support\Str; | 6 | use Illuminate\Support\Str; |
| 8 | use BookStack\Chapter; | 7 | use BookStack\Chapter; |
| 9 | 8 | ||
| 10 | -class ChapterRepo | 9 | +class ChapterRepo extends EntityRepo |
| 11 | { | 10 | { |
| 12 | - | ||
| 13 | - protected $chapter; | ||
| 14 | - protected $restrictionService; | ||
| 15 | - | ||
| 16 | - /** | ||
| 17 | - * ChapterRepo constructor. | ||
| 18 | - * @param Chapter $chapter | ||
| 19 | - * @param RestrictionService $restrictionService | ||
| 20 | - */ | ||
| 21 | - public function __construct(Chapter $chapter, RestrictionService $restrictionService) | ||
| 22 | - { | ||
| 23 | - $this->chapter = $chapter; | ||
| 24 | - $this->restrictionService = $restrictionService; | ||
| 25 | - } | ||
| 26 | - | ||
| 27 | /** | 11 | /** |
| 28 | * Base query for getting chapters, Takes restrictions into account. | 12 | * Base query for getting chapters, Takes restrictions into account. |
| 29 | * @return mixed | 13 | * @return mixed |
| ... | @@ -148,7 +132,7 @@ class ChapterRepo | ... | @@ -148,7 +132,7 @@ class ChapterRepo |
| 148 | 132 | ||
| 149 | /** | 133 | /** |
| 150 | * Get chapters by the given search term. | 134 | * Get chapters by the given search term. |
| 151 | - * @param $term | 135 | + * @param string $term |
| 152 | * @param array $whereTerms | 136 | * @param array $whereTerms |
| 153 | * @param int $count | 137 | * @param int $count |
| 154 | * @param array $paginationAppends | 138 | * @param array $paginationAppends |
| ... | @@ -156,16 +140,7 @@ class ChapterRepo | ... | @@ -156,16 +140,7 @@ class ChapterRepo |
| 156 | */ | 140 | */ |
| 157 | public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = []) | 141 | public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = []) |
| 158 | { | 142 | { |
| 159 | - preg_match_all('/"(.*?)"/', $term, $matches); | 143 | + $terms = $this->prepareSearchTerms($term); |
| 160 | - if (count($matches[1]) > 0) { | ||
| 161 | - $terms = $matches[1]; | ||
| 162 | - $term = trim(preg_replace('/"(.*?)"/', '', $term)); | ||
| 163 | - } else { | ||
| 164 | - $terms = []; | ||
| 165 | - } | ||
| 166 | - if (!empty($term)) { | ||
| 167 | - $terms = array_merge($terms, explode(' ', $term)); | ||
| 168 | - } | ||
| 169 | $chapters = $this->restrictionService->enforceChapterRestrictions($this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms)) | 144 | $chapters = $this->restrictionService->enforceChapterRestrictions($this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms)) |
| 170 | ->paginate($count)->appends($paginationAppends); | 145 | ->paginate($count)->appends($paginationAppends); |
| 171 | $words = join('|', explode(' ', preg_quote(trim($term), '/'))); | 146 | $words = join('|', explode(' ', preg_quote(trim($term), '/'))); |
| ... | @@ -195,27 +170,4 @@ class ChapterRepo | ... | @@ -195,27 +170,4 @@ class ChapterRepo |
| 195 | return $chapter; | 170 | return $chapter; |
| 196 | } | 171 | } |
| 197 | 172 | ||
| 198 | - /** | ||
| 199 | - * Updates pages restrictions from a request | ||
| 200 | - * @param $request | ||
| 201 | - * @param $chapter | ||
| 202 | - */ | ||
| 203 | - public function updateRestrictionsFromRequest($request, $chapter) | ||
| 204 | - { | ||
| 205 | - // TODO - extract into shared repo | ||
| 206 | - $chapter->restricted = $request->has('restricted') && $request->get('restricted') === 'true'; | ||
| 207 | - $chapter->restrictions()->delete(); | ||
| 208 | - if ($request->has('restrictions')) { | ||
| 209 | - foreach($request->get('restrictions') as $roleId => $restrictions) { | ||
| 210 | - foreach ($restrictions as $action => $value) { | ||
| 211 | - $chapter->restrictions()->create([ | ||
| 212 | - 'role_id' => $roleId, | ||
| 213 | - 'action' => strtolower($action) | ||
| 214 | - ]); | ||
| 215 | - } | ||
| 216 | - } | ||
| 217 | - } | ||
| 218 | - $chapter->save(); | ||
| 219 | - } | ||
| 220 | - | ||
| 221 | } | 173 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
app/Repos/ChapterRepo.php.orig
deleted
100644 → 0
| 1 | -<?php namespace BookStack\Repos; | ||
| 2 | - | ||
| 3 | - | ||
| 4 | -use Activity; | ||
| 5 | -use BookStack\Exceptions\NotFoundException; | ||
| 6 | -use BookStack\Services\RestrictionService; | ||
| 7 | -use Illuminate\Support\Str; | ||
| 8 | -use BookStack\Chapter; | ||
| 9 | - | ||
| 10 | -class ChapterRepo | ||
| 11 | -{ | ||
| 12 | - | ||
| 13 | - protected $chapter; | ||
| 14 | - protected $restrictionService; | ||
| 15 | - | ||
| 16 | - /** | ||
| 17 | - * ChapterRepo constructor. | ||
| 18 | - * @param Chapter $chapter | ||
| 19 | - * @param RestrictionService $restrictionService | ||
| 20 | - */ | ||
| 21 | - public function __construct(Chapter $chapter, RestrictionService $restrictionService) | ||
| 22 | - { | ||
| 23 | - $this->chapter = $chapter; | ||
| 24 | - $this->restrictionService = $restrictionService; | ||
| 25 | - } | ||
| 26 | - | ||
| 27 | - /** | ||
| 28 | - * Base query for getting chapters, Takes restrictions into account. | ||
| 29 | - * @return mixed | ||
| 30 | - */ | ||
| 31 | - private function chapterQuery() | ||
| 32 | - { | ||
| 33 | - return $this->restrictionService->enforceChapterRestrictions($this->chapter, 'view'); | ||
| 34 | - } | ||
| 35 | - | ||
| 36 | - /** | ||
| 37 | - * Check if an id exists. | ||
| 38 | - * @param $id | ||
| 39 | - * @return bool | ||
| 40 | - */ | ||
| 41 | - public function idExists($id) | ||
| 42 | - { | ||
| 43 | - return $this->chapterQuery()->where('id', '=', $id)->count() > 0; | ||
| 44 | - } | ||
| 45 | - | ||
| 46 | - /** | ||
| 47 | - * Get a chapter by a specific id. | ||
| 48 | - * @param $id | ||
| 49 | - * @return mixed | ||
| 50 | - */ | ||
| 51 | - public function getById($id) | ||
| 52 | - { | ||
| 53 | - return $this->chapterQuery()->findOrFail($id); | ||
| 54 | - } | ||
| 55 | - | ||
| 56 | - /** | ||
| 57 | - * Get all chapters. | ||
| 58 | - * @return \Illuminate\Database\Eloquent\Collection|static[] | ||
| 59 | - */ | ||
| 60 | - public function getAll() | ||
| 61 | - { | ||
| 62 | - return $this->chapterQuery()->all(); | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - /** | ||
| 66 | - * Get a chapter that has the given slug within the given book. | ||
| 67 | - * @param $slug | ||
| 68 | - * @param $bookId | ||
| 69 | - * @return mixed | ||
| 70 | - * @throws NotFoundException | ||
| 71 | - */ | ||
| 72 | - public function getBySlug($slug, $bookId) | ||
| 73 | - { | ||
| 74 | - $chapter = $this->chapterQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first(); | ||
| 75 | - if ($chapter === null) throw new NotFoundException('Chapter not found'); | ||
| 76 | - return $chapter; | ||
| 77 | - } | ||
| 78 | - | ||
| 79 | - /** | ||
| 80 | - * Get the child items for a chapter | ||
| 81 | - * @param Chapter $chapter | ||
| 82 | - */ | ||
| 83 | - public function getChildren(Chapter $chapter) | ||
| 84 | - { | ||
| 85 | - return $this->restrictionService->enforcePageRestrictions($chapter->pages())->get(); | ||
| 86 | - } | ||
| 87 | - | ||
| 88 | - /** | ||
| 89 | - * Create a new chapter from request input. | ||
| 90 | - * @param $input | ||
| 91 | - * @return $this | ||
| 92 | - */ | ||
| 93 | - public function newFromInput($input) | ||
| 94 | - { | ||
| 95 | - return $this->chapter->fill($input); | ||
| 96 | - } | ||
| 97 | - | ||
| 98 | - /** | ||
| 99 | - * Destroy a chapter and its relations by providing its slug. | ||
| 100 | - * @param Chapter $chapter | ||
| 101 | - */ | ||
| 102 | - public function destroy(Chapter $chapter) | ||
| 103 | - { | ||
| 104 | - if (count($chapter->pages) > 0) { | ||
| 105 | - foreach ($chapter->pages as $page) { | ||
| 106 | - $page->chapter_id = 0; | ||
| 107 | - $page->save(); | ||
| 108 | - } | ||
| 109 | - } | ||
| 110 | - Activity::removeEntity($chapter); | ||
| 111 | - $chapter->views()->delete(); | ||
| 112 | - $chapter->restrictions()->delete(); | ||
| 113 | - $chapter->delete(); | ||
| 114 | - } | ||
| 115 | - | ||
| 116 | - /** | ||
| 117 | - * Check if a chapter's slug exists. | ||
| 118 | - * @param $slug | ||
| 119 | - * @param $bookId | ||
| 120 | - * @param bool|false $currentId | ||
| 121 | - * @return bool | ||
| 122 | - */ | ||
| 123 | - public function doesSlugExist($slug, $bookId, $currentId = false) | ||
| 124 | - { | ||
| 125 | - $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId); | ||
| 126 | - if ($currentId) { | ||
| 127 | - $query = $query->where('id', '!=', $currentId); | ||
| 128 | - } | ||
| 129 | - return $query->count() > 0; | ||
| 130 | - } | ||
| 131 | - | ||
| 132 | - /** | ||
| 133 | - * Finds a suitable slug for the provided name. | ||
| 134 | - * Checks database to prevent duplicate slugs. | ||
| 135 | - * @param $name | ||
| 136 | - * @param $bookId | ||
| 137 | - * @param bool|false $currentId | ||
| 138 | - * @return string | ||
| 139 | - */ | ||
| 140 | - public function findSuitableSlug($name, $bookId, $currentId = false) | ||
| 141 | - { | ||
| 142 | - $slug = Str::slug($name); | ||
| 143 | - while ($this->doesSlugExist($slug, $bookId, $currentId)) { | ||
| 144 | - $slug .= '-' . substr(md5(rand(1, 500)), 0, 3); | ||
| 145 | - } | ||
| 146 | - return $slug; | ||
| 147 | - } | ||
| 148 | - | ||
| 149 | - /** | ||
| 150 | - * Get chapters by the given search term. | ||
| 151 | - * @param $term | ||
| 152 | - * @param array $whereTerms | ||
| 153 | - * @param int $count | ||
| 154 | - * @param array $paginationAppends | ||
| 155 | - * @return mixed | ||
| 156 | - */ | ||
| 157 | - public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = []) | ||
| 158 | - { | ||
| 159 | -<<<<<<< HEAD | ||
| 160 | - preg_match_all('/"(.*?)"/', $term, $matches); | ||
| 161 | - if (count($matches[1]) > 0) { | ||
| 162 | - $terms = $matches[1]; | ||
| 163 | - $term = trim(preg_replace('/"(.*?)"/', '', $term)); | ||
| 164 | - } else { | ||
| 165 | - $terms = []; | ||
| 166 | - } | ||
| 167 | - if (!empty($term)) { | ||
| 168 | - $terms = array_merge($terms, explode(' ', $term)); | ||
| 169 | - } | ||
| 170 | - $chapters = $this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms) | ||
| 171 | -======= | ||
| 172 | - $terms = explode(' ', $term); | ||
| 173 | - $chapters = $this->restrictionService->enforceChapterRestrictions($this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms)) | ||
| 174 | ->>>>>>> custom_role_system | ||
| 175 | - ->paginate($count)->appends($paginationAppends); | ||
| 176 | - $words = join('|', explode(' ', preg_quote(trim($term), '/'))); | ||
| 177 | - foreach ($chapters as $chapter) { | ||
| 178 | - //highlight | ||
| 179 | - $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $chapter->getExcerpt(100)); | ||
| 180 | - $chapter->searchSnippet = $result; | ||
| 181 | - } | ||
| 182 | - return $chapters; | ||
| 183 | - } | ||
| 184 | - | ||
| 185 | - /** | ||
| 186 | - * Changes the book relation of this chapter. | ||
| 187 | - * @param $bookId | ||
| 188 | - * @param Chapter $chapter | ||
| 189 | - * @return Chapter | ||
| 190 | - */ | ||
| 191 | - public function changeBook($bookId, Chapter $chapter) | ||
| 192 | - { | ||
| 193 | - $chapter->book_id = $bookId; | ||
| 194 | - foreach ($chapter->activity as $activity) { | ||
| 195 | - $activity->book_id = $bookId; | ||
| 196 | - $activity->save(); | ||
| 197 | - } | ||
| 198 | - $chapter->slug = $this->findSuitableSlug($chapter->name, $bookId, $chapter->id); | ||
| 199 | - $chapter->save(); | ||
| 200 | - return $chapter; | ||
| 201 | - } | ||
| 202 | - | ||
| 203 | - /** | ||
| 204 | - * Updates pages restrictions from a request | ||
| 205 | - * @param $request | ||
| 206 | - * @param $chapter | ||
| 207 | - */ | ||
| 208 | - public function updateRestrictionsFromRequest($request, $chapter) | ||
| 209 | - { | ||
| 210 | - // TODO - extract into shared repo | ||
| 211 | - $chapter->restricted = $request->has('restricted') && $request->get('restricted') === 'true'; | ||
| 212 | - $chapter->restrictions()->delete(); | ||
| 213 | - if ($request->has('restrictions')) { | ||
| 214 | - foreach($request->get('restrictions') as $roleId => $restrictions) { | ||
| 215 | - foreach ($restrictions as $action => $value) { | ||
| 216 | - $chapter->restrictions()->create([ | ||
| 217 | - 'role_id' => $roleId, | ||
| 218 | - 'action' => strtolower($action) | ||
| 219 | - ]); | ||
| 220 | - } | ||
| 221 | - } | ||
| 222 | - } | ||
| 223 | - $chapter->save(); | ||
| 224 | - } | ||
| 225 | - | ||
| 226 | -} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1 | <?php namespace BookStack\Repos; | 1 | <?php namespace BookStack\Repos; |
| 2 | 2 | ||
| 3 | - | ||
| 4 | use BookStack\Book; | 3 | use BookStack\Book; |
| 5 | use BookStack\Chapter; | 4 | use BookStack\Chapter; |
| 5 | +use BookStack\Entity; | ||
| 6 | use BookStack\Page; | 6 | use BookStack\Page; |
| 7 | use BookStack\Services\RestrictionService; | 7 | use BookStack\Services\RestrictionService; |
| 8 | 8 | ||
| 9 | class EntityRepo | 9 | class EntityRepo |
| 10 | { | 10 | { |
| 11 | 11 | ||
| 12 | + /** | ||
| 13 | + * @var Book $book | ||
| 14 | + */ | ||
| 12 | public $book; | 15 | public $book; |
| 16 | + | ||
| 17 | + /** | ||
| 18 | + * @var Chapter | ||
| 19 | + */ | ||
| 13 | public $chapter; | 20 | public $chapter; |
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * @var Page | ||
| 24 | + */ | ||
| 14 | public $page; | 25 | public $page; |
| 15 | - private $restrictionService; | 26 | + |
| 27 | + /** | ||
| 28 | + * @var RestrictionService | ||
| 29 | + */ | ||
| 30 | + protected $restrictionService; | ||
| 16 | 31 | ||
| 17 | /** | 32 | /** |
| 18 | * EntityService constructor. | 33 | * EntityService constructor. |
| 19 | - * @param Book $book | ||
| 20 | - * @param Chapter $chapter | ||
| 21 | - * @param Page $page | ||
| 22 | - * @param RestrictionService $restrictionService | ||
| 23 | */ | 34 | */ |
| 24 | - public function __construct(Book $book, Chapter $chapter, Page $page, RestrictionService $restrictionService) | 35 | + public function __construct() |
| 25 | { | 36 | { |
| 26 | - $this->book = $book; | 37 | + $this->book = app(Book::class); |
| 27 | - $this->chapter = $chapter; | 38 | + $this->chapter = app(Chapter::class); |
| 28 | - $this->page = $page; | 39 | + $this->page = app(Page::class); |
| 29 | - $this->restrictionService = $restrictionService; | 40 | + $this->restrictionService = app(RestrictionService::class); |
| 30 | } | 41 | } |
| 31 | 42 | ||
| 32 | /** | 43 | /** |
| ... | @@ -37,7 +48,7 @@ class EntityRepo | ... | @@ -37,7 +48,7 @@ class EntityRepo |
| 37 | public function getRecentlyCreatedBooks($count = 20, $page = 0) | 48 | public function getRecentlyCreatedBooks($count = 20, $page = 0) |
| 38 | { | 49 | { |
| 39 | return $this->restrictionService->enforceBookRestrictions($this->book) | 50 | return $this->restrictionService->enforceBookRestrictions($this->book) |
| 40 | - ->orderBy('created_at', 'desc')->skip($page*$count)->take($count)->get(); | 51 | + ->orderBy('created_at', 'desc')->skip($page * $count)->take($count)->get(); |
| 41 | } | 52 | } |
| 42 | 53 | ||
| 43 | /** | 54 | /** |
| ... | @@ -49,7 +60,7 @@ class EntityRepo | ... | @@ -49,7 +60,7 @@ class EntityRepo |
| 49 | public function getRecentlyUpdatedBooks($count = 20, $page = 0) | 60 | public function getRecentlyUpdatedBooks($count = 20, $page = 0) |
| 50 | { | 61 | { |
| 51 | return $this->restrictionService->enforceBookRestrictions($this->book) | 62 | return $this->restrictionService->enforceBookRestrictions($this->book) |
| 52 | - ->orderBy('updated_at', 'desc')->skip($page*$count)->take($count)->get(); | 63 | + ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get(); |
| 53 | } | 64 | } |
| 54 | 65 | ||
| 55 | /** | 66 | /** |
| ... | @@ -60,7 +71,7 @@ class EntityRepo | ... | @@ -60,7 +71,7 @@ class EntityRepo |
| 60 | public function getRecentlyCreatedPages($count = 20, $page = 0) | 71 | public function getRecentlyCreatedPages($count = 20, $page = 0) |
| 61 | { | 72 | { |
| 62 | return $this->restrictionService->enforcePageRestrictions($this->page) | 73 | return $this->restrictionService->enforcePageRestrictions($this->page) |
| 63 | - ->orderBy('created_at', 'desc')->skip($page*$count)->take($count)->get(); | 74 | + ->orderBy('created_at', 'desc')->skip($page * $count)->take($count)->get(); |
| 64 | } | 75 | } |
| 65 | 76 | ||
| 66 | /** | 77 | /** |
| ... | @@ -72,7 +83,49 @@ class EntityRepo | ... | @@ -72,7 +83,49 @@ class EntityRepo |
| 72 | public function getRecentlyUpdatedPages($count = 20, $page = 0) | 83 | public function getRecentlyUpdatedPages($count = 20, $page = 0) |
| 73 | { | 84 | { |
| 74 | return $this->restrictionService->enforcePageRestrictions($this->page) | 85 | return $this->restrictionService->enforcePageRestrictions($this->page) |
| 75 | - ->orderBy('updated_at', 'desc')->skip($page*$count)->take($count)->get(); | 86 | + ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get(); |
| 87 | + } | ||
| 88 | + | ||
| 89 | + /** | ||
| 90 | + * Updates entity restrictions from a request | ||
| 91 | + * @param $request | ||
| 92 | + * @param Entity $entity | ||
| 93 | + */ | ||
| 94 | + public function updateRestrictionsFromRequest($request, Entity $entity) | ||
| 95 | + { | ||
| 96 | + $entity->restricted = $request->has('restricted') && $request->get('restricted') === 'true'; | ||
| 97 | + $entity->restrictions()->delete(); | ||
| 98 | + if ($request->has('restrictions')) { | ||
| 99 | + foreach ($request->get('restrictions') as $roleId => $restrictions) { | ||
| 100 | + foreach ($restrictions as $action => $value) { | ||
| 101 | + $entity->restrictions()->create([ | ||
| 102 | + 'role_id' => $roleId, | ||
| 103 | + 'action' => strtolower($action) | ||
| 104 | + ]); | ||
| 105 | + } | ||
| 106 | + } | ||
| 107 | + } | ||
| 108 | + $entity->save(); | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + /** | ||
| 112 | + * Prepare a string of search terms by turning | ||
| 113 | + * it into an array of terms. | ||
| 114 | + * Keeps quoted terms together. | ||
| 115 | + * @param $termString | ||
| 116 | + * @return array | ||
| 117 | + */ | ||
| 118 | + protected function prepareSearchTerms($termString) | ||
| 119 | + { | ||
| 120 | + preg_match_all('/"(.*?)"/', $termString, $matches); | ||
| 121 | + if (count($matches[1]) > 0) { | ||
| 122 | + $terms = $matches[1]; | ||
| 123 | + $termString = trim(preg_replace('/"(.*?)"/', '', $termString)); | ||
| 124 | + } else { | ||
| 125 | + $terms = []; | ||
| 126 | + } | ||
| 127 | + if (!empty($termString)) $terms = array_merge($terms, explode(' ', $termString)); | ||
| 128 | + return $terms; | ||
| 76 | } | 129 | } |
| 77 | 130 | ||
| 78 | 131 | ... | ... |
| ... | @@ -3,34 +3,23 @@ | ... | @@ -3,34 +3,23 @@ |
| 3 | 3 | ||
| 4 | use Activity; | 4 | use Activity; |
| 5 | use BookStack\Book; | 5 | use BookStack\Book; |
| 6 | -use BookStack\Chapter; | ||
| 7 | use BookStack\Exceptions\NotFoundException; | 6 | use BookStack\Exceptions\NotFoundException; |
| 8 | -use BookStack\Services\RestrictionService; | ||
| 9 | -use Illuminate\Http\Request; | ||
| 10 | -use Illuminate\Support\Facades\Auth; | ||
| 11 | -use Illuminate\Support\Facades\Log; | ||
| 12 | use Illuminate\Support\Str; | 7 | use Illuminate\Support\Str; |
| 13 | use BookStack\Page; | 8 | use BookStack\Page; |
| 14 | use BookStack\PageRevision; | 9 | use BookStack\PageRevision; |
| 15 | -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
| 16 | 10 | ||
| 17 | -class PageRepo | 11 | +class PageRepo extends EntityRepo |
| 18 | { | 12 | { |
| 19 | - protected $page; | ||
| 20 | protected $pageRevision; | 13 | protected $pageRevision; |
| 21 | - protected $restrictionService; | ||
| 22 | 14 | ||
| 23 | /** | 15 | /** |
| 24 | * PageRepo constructor. | 16 | * PageRepo constructor. |
| 25 | - * @param Page $page | ||
| 26 | * @param PageRevision $pageRevision | 17 | * @param PageRevision $pageRevision |
| 27 | - * @param RestrictionService $restrictionService | ||
| 28 | */ | 18 | */ |
| 29 | - public function __construct(Page $page, PageRevision $pageRevision, RestrictionService $restrictionService) | 19 | + public function __construct(PageRevision $pageRevision) |
| 30 | { | 20 | { |
| 31 | - $this->page = $page; | ||
| 32 | $this->pageRevision = $pageRevision; | 21 | $this->pageRevision = $pageRevision; |
| 33 | - $this->restrictionService = $restrictionService; | 22 | + parent::__construct(); |
| 34 | } | 23 | } |
| 35 | 24 | ||
| 36 | /** | 25 | /** |
| ... | @@ -200,16 +189,7 @@ class PageRepo | ... | @@ -200,16 +189,7 @@ class PageRepo |
| 200 | */ | 189 | */ |
| 201 | public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = []) | 190 | public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = []) |
| 202 | { | 191 | { |
| 203 | - preg_match_all('/"(.*?)"/', $term, $matches); | 192 | + $terms = $this->prepareSearchTerms($term); |
| 204 | - if (count($matches[1]) > 0) { | ||
| 205 | - $terms = $matches[1]; | ||
| 206 | - $term = trim(preg_replace('/"(.*?)"/', '', $term)); | ||
| 207 | - } else { | ||
| 208 | - $terms = []; | ||
| 209 | - } | ||
| 210 | - if (!empty($term)) { | ||
| 211 | - $terms = array_merge($terms, explode(' ', $term)); | ||
| 212 | - } | ||
| 213 | $pages = $this->restrictionService->enforcePageRestrictions($this->page->fullTextSearchQuery(['name', 'text'], $terms, $whereTerms)) | 193 | $pages = $this->restrictionService->enforcePageRestrictions($this->page->fullTextSearchQuery(['name', 'text'], $terms, $whereTerms)) |
| 214 | ->paginate($count)->appends($paginationAppends); | 194 | ->paginate($count)->appends($paginationAppends); |
| 215 | 195 | ||
| ... | @@ -416,27 +396,4 @@ class PageRepo | ... | @@ -416,27 +396,4 @@ class PageRepo |
| 416 | return $this->pageQuery()->orderBy('updated_at', 'desc')->paginate($count); | 396 | return $this->pageQuery()->orderBy('updated_at', 'desc')->paginate($count); |
| 417 | } | 397 | } |
| 418 | 398 | ||
| 419 | - /** | ||
| 420 | - * Updates pages restrictions from a request | ||
| 421 | - * @param $request | ||
| 422 | - * @param $page | ||
| 423 | - */ | ||
| 424 | - public function updateRestrictionsFromRequest($request, $page) | ||
| 425 | - { | ||
| 426 | - // TODO - extract into shared repo | ||
| 427 | - $page->restricted = $request->has('restricted') && $request->get('restricted') === 'true'; | ||
| 428 | - $page->restrictions()->delete(); | ||
| 429 | - if ($request->has('restrictions')) { | ||
| 430 | - foreach($request->get('restrictions') as $roleId => $restrictions) { | ||
| 431 | - foreach ($restrictions as $action => $value) { | ||
| 432 | - $page->restrictions()->create([ | ||
| 433 | - 'role_id' => $roleId, | ||
| 434 | - 'action' => strtolower($action) | ||
| 435 | - ]); | ||
| 436 | - } | ||
| 437 | - } | ||
| 438 | - } | ||
| 439 | - $page->save(); | ||
| 440 | - } | ||
| 441 | - | ||
| 442 | } | 399 | } | ... | ... |
app/Repos/PageRepo.php.orig
deleted
100644 → 0
This diff is collapsed.
Click to expand it.
-
Please register or sign in to post a comment