]> BookStack Code Mirror - bookstack/blob - app/Repos/EntityRepo.php
28f4c0ae8466a805b456cb8ba2eb08e5e7b98038
[bookstack] / app / Repos / EntityRepo.php
1 <?php namespace BookStack\Repos;
2
3 use BookStack\Book;
4 use BookStack\Chapter;
5 use BookStack\Entity;
6 use BookStack\Page;
7 use BookStack\Services\PermissionService;
8 use BookStack\User;
9 use Illuminate\Support\Facades\Log;
10
11 class EntityRepo
12 {
13
14     /**
15      * @var Book $book
16      */
17     public $book;
18
19     /**
20      * @var Chapter
21      */
22     public $chapter;
23
24     /**
25      * @var Page
26      */
27     public $page;
28
29     /**
30      * @var PermissionService
31      */
32     protected $permissionService;
33
34     /**
35      * Acceptable operators to be used in a query
36      * @var array
37      */
38     protected $queryOperators = ['<=', '>=', '=', '<', '>', 'like', '!='];
39
40     /**
41      * EntityService constructor.
42      */
43     public function __construct()
44     {
45         $this->book = app(Book::class);
46         $this->chapter = app(Chapter::class);
47         $this->page = app(Page::class);
48         $this->permissionService = app(PermissionService::class);
49     }
50
51     /**
52      * Get the latest books added to the system.
53      * @param int $count
54      * @param int $page
55      * @param bool $additionalQuery
56      * @return
57      */
58     public function getRecentlyCreatedBooks($count = 20, $page = 0, $additionalQuery = false)
59     {
60         $query = $this->permissionService->enforceBookRestrictions($this->book)
61             ->orderBy('created_at', 'desc');
62         if ($additionalQuery !== false && is_callable($additionalQuery)) {
63             $additionalQuery($query);
64         }
65         return $query->skip($page * $count)->take($count)->get();
66     }
67
68     /**
69      * Get the most recently updated books.
70      * @param $count
71      * @param int $page
72      * @return mixed
73      */
74     public function getRecentlyUpdatedBooks($count = 20, $page = 0)
75     {
76         return $this->permissionService->enforceBookRestrictions($this->book)
77             ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get();
78     }
79
80     /**
81      * Get the latest pages added to the system.
82      * @param int $count
83      * @param int $page
84      * @param bool $additionalQuery
85      * @return
86      */
87     public function getRecentlyCreatedPages($count = 20, $page = 0, $additionalQuery = false)
88     {
89         $query = $this->permissionService->enforcePageRestrictions($this->page)
90             ->orderBy('created_at', 'desc')->where('draft', '=', false);
91         if ($additionalQuery !== false && is_callable($additionalQuery)) {
92             $additionalQuery($query);
93         }
94         return $query->with('book')->skip($page * $count)->take($count)->get();
95     }
96
97     /**
98      * Get the latest chapters added to the system.
99      * @param int $count
100      * @param int $page
101      * @param bool $additionalQuery
102      * @return
103      */
104     public function getRecentlyCreatedChapters($count = 20, $page = 0, $additionalQuery = false)
105     {
106         $query = $this->permissionService->enforceChapterRestrictions($this->chapter)
107             ->orderBy('created_at', 'desc');
108         if ($additionalQuery !== false && is_callable($additionalQuery)) {
109             $additionalQuery($query);
110         }
111         return $query->skip($page * $count)->take($count)->get();
112     }
113
114     /**
115      * Get the most recently updated pages.
116      * @param $count
117      * @param int $page
118      * @return mixed
119      */
120     public function getRecentlyUpdatedPages($count = 20, $page = 0)
121     {
122         return $this->permissionService->enforcePageRestrictions($this->page)
123             ->where('draft', '=', false)
124             ->orderBy('updated_at', 'desc')->with('book')->skip($page * $count)->take($count)->get();
125     }
126
127     /**
128      * Get draft pages owned by the current user.
129      * @param int $count
130      * @param int $page
131      */
132     public function getUserDraftPages($count = 20, $page = 0)
133     {
134         $user = auth()->user();
135         return $this->page->where('draft', '=', true)
136             ->where('created_by', '=', $user->id)
137             ->orderBy('updated_at', 'desc')
138             ->skip($count * $page)->take($count)->get();
139     }
140
141     /**
142      * Updates entity restrictions from a request
143      * @param $request
144      * @param Entity $entity
145      */
146     public function updateEntityPermissionsFromRequest($request, Entity $entity)
147     {
148         $entity->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
149         $entity->permissions()->delete();
150         if ($request->has('restrictions')) {
151             foreach ($request->get('restrictions') as $roleId => $restrictions) {
152                 foreach ($restrictions as $action => $value) {
153                     $entity->permissions()->create([
154                         'role_id' => $roleId,
155                         'action'  => strtolower($action)
156                     ]);
157                 }
158             }
159         }
160         $entity->save();
161         $this->permissionService->buildJointPermissionsForEntity($entity);
162     }
163
164     /**
165      * Prepare a string of search terms by turning
166      * it into an array of terms.
167      * Keeps quoted terms together.
168      * @param $termString
169      * @return array
170      */
171     public function prepareSearchTerms($termString)
172     {
173         $termString = $this->cleanSearchTermString($termString);
174         preg_match_all('/(".*?")/', $termString, $matches);
175         $terms = [];
176         if (count($matches[1]) > 0) {
177             foreach ($matches[1] as $match) {
178                 $terms[] = $match;
179             }
180             $termString = trim(preg_replace('/"(.*?)"/', '', $termString));
181         }
182         if (!empty($termString)) $terms = array_merge($terms, explode(' ', $termString));
183         return $terms;
184     }
185
186     /**
187      * Removes any special search notation that should not
188      * be used in a full-text search.
189      * @param $termString
190      * @return mixed
191      */
192     protected function cleanSearchTermString($termString)
193     {
194         // Strip tag searches
195         $termString = preg_replace('/\[.*?\]/', '', $termString);
196         // Reduced multiple spacing into single spacing
197         $termString = preg_replace("/\s{2,}/", " ", $termString);
198         return $termString;
199     }
200
201     /**
202      * Get the available query operators as a regex escaped list.
203      * @return mixed
204      */
205     protected function getRegexEscapedOperators()
206     {
207         $escapedOperators = [];
208         foreach ($this->queryOperators as $operator) {
209             $escapedOperators[] = preg_quote($operator);
210         }
211         return join('|', $escapedOperators);
212     }
213
214     /**
215      * Parses advanced search notations and adds them to the db query.
216      * @param $query
217      * @param $termString
218      * @return mixed
219      */
220     protected function addAdvancedSearchQueries($query, $termString)
221     {
222         $escapedOperators = $this->getRegexEscapedOperators();
223         // Look for tag searches
224         preg_match_all("/\[(.*?)((${escapedOperators})(.*?))?\]/", $termString, $tags);
225         if (count($tags[0]) > 0) {
226             $this->applyTagSearches($query, $tags);
227         }
228
229         return $query;
230     }
231
232     /**
233      * Apply extracted tag search terms onto a entity query.
234      * @param $query
235      * @param $tags
236      * @return mixed
237      */
238     protected function applyTagSearches($query, $tags) {
239         $query->where(function($query) use ($tags) {
240             foreach ($tags[1] as $index => $tagName) {
241                 $query->whereHas('tags', function($query) use ($tags, $index, $tagName) {
242                     $tagOperator = $tags[3][$index];
243                     $tagValue = $tags[4][$index];
244                     if (!empty($tagOperator) && !empty($tagValue) && in_array($tagOperator, $this->queryOperators)) {
245                         if (is_numeric($tagValue) && $tagOperator !== 'like') {
246                             // We have to do a raw sql query for this since otherwise PDO will quote the value and MySQL will
247                             // search the value as a string which prevents being able to do number-based operations
248                             // on the tag values. We ensure it has a numeric value and then cast it just to be sure.
249                             $tagValue = (float) trim($query->getConnection()->getPdo()->quote($tagValue), "'");
250                             $query->where('name', '=', $tagName)->whereRaw("value ${tagOperator} ${tagValue}");
251                         } else {
252                             $query->where('name', '=', $tagName)->where('value', $tagOperator, $tagValue);
253                         }
254                     } else {
255                         $query->where('name', '=', $tagName);
256                     }
257                 });
258             }
259         });
260         return $query;
261     }
262
263 }
264
265
266
267
268
269
270
271
272
273
274
275