]> BookStack Code Mirror - bookstack/blob - app/Repos/BookRepo.php
Updated fulltext search with custom escaped query
[bookstack] / app / Repos / BookRepo.php
1 <?php namespace BookStack\Repos;
2
3 use Activity;
4 use Illuminate\Support\Str;
5 use BookStack\Book;
6 use Views;
7
8 class BookRepo
9 {
10
11     protected $book;
12     protected $pageRepo;
13     protected $chapterRepo;
14
15     /**
16      * BookRepo constructor.
17      * @param Book $book
18      * @param PageRepo $pageRepo
19      * @param ChapterRepo $chapterRepo
20      */
21     public function __construct(Book $book, PageRepo $pageRepo, ChapterRepo $chapterRepo)
22     {
23         $this->book = $book;
24         $this->pageRepo = $pageRepo;
25         $this->chapterRepo = $chapterRepo;
26     }
27
28     /**
29      * Get the book that has the given id.
30      * @param $id
31      * @return mixed
32      */
33     public function getById($id)
34     {
35         return $this->book->findOrFail($id);
36     }
37
38     /**
39      * Get all books, Limited by count.
40      * @param int $count
41      * @return mixed
42      */
43     public function getAll($count = 10)
44     {
45         $bookQuery = $this->book->orderBy('name', 'asc');
46         if (!$count) return $bookQuery->get();
47         return $bookQuery->take($count)->get();
48     }
49
50     /**
51      * Get all books paginated.
52      * @param int $count
53      * @return mixed
54      */
55     public function getAllPaginated($count = 10)
56     {
57         return $this->book->orderBy('name', 'asc')->paginate($count);
58     }
59
60
61     /**
62      * Get the latest books.
63      * @param int $count
64      * @return mixed
65      */
66     public function getLatest($count = 10)
67     {
68         return $this->book->orderBy('created_at', 'desc')->take($count)->get();
69     }
70
71     /**
72      * Gets the most recently viewed for a user.
73      * @param int $count
74      * @param int $page
75      * @return mixed
76      */
77     public function getRecentlyViewed($count = 10, $page = 0)
78     {
79         return Views::getUserRecentlyViewed($count, $page, $this->book);
80     }
81
82     /**
83      * Gets the most viewed books.
84      * @param int $count
85      * @param int $page
86      * @return mixed
87      */
88     public function getPopular($count = 10, $page = 0)
89     {
90         return Views::getPopular($count, $page, $this->book);
91     }
92
93     /**
94      * Get a book by slug
95      * @param $slug
96      * @return mixed
97      */
98     public function getBySlug($slug)
99     {
100         $book = $this->book->where('slug', '=', $slug)->first();
101         if ($book === null) abort(404);
102         return $book;
103     }
104
105     /**
106      * Checks if a book exists.
107      * @param $id
108      * @return bool
109      */
110     public function exists($id)
111     {
112         return $this->book->where('id', '=', $id)->exists();
113     }
114
115     /**
116      * Get a new book instance from request input.
117      * @param $input
118      * @return Book
119      */
120     public function newFromInput($input)
121     {
122         return $this->book->fill($input);
123     }
124
125     /**
126      * Count the amount of books that have a specific slug.
127      * @param $slug
128      * @return mixed
129      */
130     public function countBySlug($slug)
131     {
132         return $this->book->where('slug', '=', $slug)->count();
133     }
134
135     /**
136      * Destroy a book identified by the given slug.
137      * @param $bookSlug
138      */
139     public function destroyBySlug($bookSlug)
140     {
141         $book = $this->getBySlug($bookSlug);
142         foreach ($book->pages as $page) {
143             $this->pageRepo->destroy($page);
144         }
145         foreach ($book->chapters as $chapter) {
146             $this->chapterRepo->destroy($chapter);
147         }
148         $book->views()->delete();
149         $book->delete();
150     }
151
152     /**
153      * Get the next child element priority.
154      * @param Book $book
155      * @return int
156      */
157     public function getNewPriority($book)
158     {
159         $lastElem = $this->getChildren($book)->pop();
160         return $lastElem ? $lastElem->priority + 1 : 0;
161     }
162
163     /**
164      * @param string $slug
165      * @param bool|false $currentId
166      * @return bool
167      */
168     public function doesSlugExist($slug, $currentId = false)
169     {
170         $query = $this->book->where('slug', '=', $slug);
171         if ($currentId) {
172             $query = $query->where('id', '!=', $currentId);
173         }
174         return $query->count() > 0;
175     }
176
177     /**
178      * Provides a suitable slug for the given book name.
179      * Ensures the returned slug is unique in the system.
180      * @param string $name
181      * @param bool|false $currentId
182      * @return string
183      */
184     public function findSuitableSlug($name, $currentId = false)
185     {
186         $originalSlug = Str::slug($name);
187         $slug = $originalSlug;
188         $count = 2;
189         while ($this->doesSlugExist($slug, $currentId)) {
190             $slug = $originalSlug . '-' . $count;
191             $count++;
192         }
193         return $slug;
194     }
195
196     /**
197      * Get all child objects of a book.
198      * Returns a sorted collection of Pages and Chapters.
199      * Loads the bookslug onto child elements to prevent access database access for getting the slug.
200      * @param Book $book
201      * @return mixed
202      */
203     public function getChildren(Book $book)
204     {
205         $pages = $book->pages()->where('chapter_id', '=', 0)->get();
206         $chapters = $book->chapters()->with('pages')->get();
207         $children = $pages->merge($chapters);
208         $bookSlug = $book->slug;
209         $children->each(function ($child) use ($bookSlug) {
210             $child->setAttribute('bookSlug', $bookSlug);
211             if ($child->isA('chapter')) {
212                 $child->pages->each(function ($page) use ($bookSlug) {
213                     $page->setAttribute('bookSlug', $bookSlug);
214                 });
215             }
216         });
217         return $children->sortBy('priority');
218     }
219
220     /**
221      * Get books by search term.
222      * @param $term
223      * @param int $count
224      * @param array $paginationAppends
225      * @return mixed
226      */
227     public function getBySearch($term, $count = 20, $paginationAppends = [])
228     {
229         preg_match_all('/"(.*?)"/', $term, $matches);
230         if (count($matches[1]) > 0) {
231             $terms = $matches[1];
232             $term = trim(preg_replace('/"(.*?)"/', '', $term));
233         } else {
234             $terms = [];
235         }
236         if (!empty($term)) {
237             $terms = array_merge($terms, explode(' ', $term));
238         }
239         $books = $this->book->fullTextSearchQuery(['name', 'description'], $terms)
240             ->paginate($count)->appends($paginationAppends);
241         $words = join('|', explode(' ', preg_quote(trim($term), '/')));
242         foreach ($books as $book) {
243             //highlight
244             $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $book->getExcerpt(100));
245             $book->searchSnippet = $result;
246         }
247         return $books;
248     }
249
250 }