1 <?php namespace BookStack\Repos;
6 use BookStack\Exceptions\NotFoundException;
9 use Illuminate\Support\Str;
11 use BookStack\PageRevision;
13 class PageRepo extends EntityRepo
16 protected $pageRevision;
20 * PageRepo constructor.
21 * @param PageRevision $pageRevision
22 * @param TagRepo $tagRepo
24 public function __construct(PageRevision $pageRevision, TagRepo $tagRepo)
26 $this->pageRevision = $pageRevision;
27 $this->tagRepo = $tagRepo;
28 parent::__construct();
32 * Base query for getting pages, Takes restrictions into account.
33 * @param bool $allowDrafts
36 private function pageQuery($allowDrafts = false)
38 $query = $this->permissionService->enforcePageRestrictions($this->page, 'view');
40 $query = $query->where('draft', '=', false);
46 * Get a page via a specific ID.
48 * @param bool $allowDrafts
51 public function getById($id, $allowDrafts = false)
53 return $this->pageQuery($allowDrafts)->findOrFail($id);
57 * Get a page identified by the given slug.
61 * @throws NotFoundException
63 public function getBySlug($slug, $bookId)
65 $page = $this->pageQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
66 if ($page === null) throw new NotFoundException('Page not found');
71 * Search through page revisions and retrieve
72 * the last page in the current book that
73 * has a slug equal to the one given.
78 public function findPageUsingOldSlug($pageSlug, $bookSlug)
80 $revision = $this->pageRevision->where('slug', '=', $pageSlug)
81 ->whereHas('page', function ($query) {
82 $this->permissionService->enforcePageRestrictions($query);
84 ->where('type', '=', 'version')
85 ->where('book_slug', '=', $bookSlug)->orderBy('created_at', 'desc')
86 ->with('page')->first();
87 return $revision !== null ? $revision->page : null;
91 * Get a new Page instance from the given input.
95 public function newFromInput($input)
97 $page = $this->page->fill($input);
102 * Count the pages with a particular slug within a book.
107 public function countBySlug($slug, $bookId)
109 return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
113 * Save a new page into the system.
114 * Input validation must be done beforehand.
115 * @param array $input
117 * @param int $chapterId
120 public function saveNew(array $input, Book $book, $chapterId = null)
122 $page = $this->newFromInput($input);
123 $page->slug = $this->findSuitableSlug($page->name, $book->id);
125 if ($chapterId) $page->chapter_id = $chapterId;
127 $page->html = $this->formatHtml($input['html']);
128 $page->text = strip_tags($page->html);
129 $page->created_by = auth()->user()->id;
130 $page->updated_by = auth()->user()->id;
132 $book->pages()->save($page);
138 * Publish a draft page to make it a normal page.
139 * Sets the slug and updates the content.
140 * @param Page $draftPage
141 * @param array $input
144 public function publishDraft(Page $draftPage, array $input)
146 $draftPage->fill($input);
148 // Save page tags if present
149 if(isset($input['tags'])) {
150 $this->tagRepo->saveTagsToEntity($draftPage, $input['tags']);
153 $draftPage->slug = $this->findSuitableSlug($draftPage->name, $draftPage->book->id);
154 $draftPage->html = $this->formatHtml($input['html']);
155 $draftPage->text = strip_tags($draftPage->html);
156 $draftPage->draft = false;
163 * Get a new draft page instance.
165 * @param Chapter|bool $chapter
168 public function getDraftPage(Book $book, $chapter = false)
170 $page = $this->page->newInstance();
171 $page->name = 'New Page';
172 $page->created_by = auth()->user()->id;
173 $page->updated_by = auth()->user()->id;
176 if ($chapter) $page->chapter_id = $chapter->id;
178 $book->pages()->save($page);
179 $this->permissionService->buildJointPermissionsForEntity($page);
184 * Formats a page's html to be tagged correctly
186 * @param string $htmlText
189 protected function formatHtml($htmlText)
191 if ($htmlText == '') return $htmlText;
192 libxml_use_internal_errors(true);
193 $doc = new DOMDocument();
194 $doc->loadHTML(mb_convert_encoding($htmlText, 'HTML-ENTITIES', 'UTF-8'));
196 $container = $doc->documentElement;
197 $body = $container->childNodes->item(0);
198 $childNodes = $body->childNodes;
200 // Ensure no duplicate ids are used
203 foreach ($childNodes as $index => $childNode) {
204 /** @var \DOMElement $childNode */
205 if (get_class($childNode) !== 'DOMElement') continue;
207 // Overwrite id if not a BookStack custom id
208 if ($childNode->hasAttribute('id')) {
209 $id = $childNode->getAttribute('id');
210 if (strpos($id, 'bkmrk') === 0 && array_search($id, $idArray) === false) {
216 // Create an unique id for the element
217 // Uses the content as a basis to ensure output is the same every time
218 // the same content is passed through.
219 $contentId = 'bkmrk-' . substr(strtolower(preg_replace('/\s+/', '-', trim($childNode->nodeValue))), 0, 20);
220 $newId = urlencode($contentId);
222 while (in_array($newId, $idArray)) {
223 $newId = urlencode($contentId . '-' . $loopIndex);
227 $childNode->setAttribute('id', $newId);
231 // Generate inner html as a string
233 foreach ($childNodes as $childNode) {
234 $html .= $doc->saveHTML($childNode);
242 * Gets pages by a search term.
243 * Highlights page content for showing in results.
244 * @param string $term
245 * @param array $whereTerms
247 * @param array $paginationAppends
250 public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = [])
252 $terms = $this->prepareSearchTerms($term);
253 $pageQuery = $this->permissionService->enforcePageRestrictions($this->page->fullTextSearchQuery(['name', 'text'], $terms, $whereTerms));
254 $pageQuery = $this->addAdvancedSearchQueries($pageQuery, $term);
255 $pages = $pageQuery->paginate($count)->appends($paginationAppends);
257 // Add highlights to page text.
258 $words = join('|', explode(' ', preg_quote(trim($term), '/')));
259 //lookahead/behind assertions ensures cut between words
260 $s = '\s\x00-/:-@\[-`{-~'; //character set for start/end of words
262 foreach ($pages as $page) {
263 preg_match_all('#(?<=[' . $s . ']).{1,30}((' . $words . ').{1,30})+(?=[' . $s . '])#uis', $page->text, $matches, PREG_SET_ORDER);
264 //delimiter between occurrences
266 foreach ($matches as $line) {
267 $results[] = htmlspecialchars($line[0], 0, 'UTF-8');
270 if (count($results) > $matchLimit) {
271 $results = array_slice($results, 0, $matchLimit);
273 $result = join('... ', $results);
276 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $result);
277 if (strlen($result) < 5) {
278 $result = $page->getExcerpt(80);
280 $page->searchSnippet = $result;
286 * Search for image usage.
287 * @param $imageString
290 public function searchForImage($imageString)
292 $pages = $this->pageQuery()->where('html', 'like', '%' . $imageString . '%')->get();
293 foreach ($pages as $page) {
294 $page->url = $page->getUrl();
298 return count($pages) > 0 ? $pages : false;
302 * Updates a page with any fillable data and saves it into the database.
304 * @param int $book_id
305 * @param string $input
308 public function updatePage(Page $page, $book_id, $input)
310 // Save a revision before updating
311 if ($page->html !== $input['html'] || $page->name !== $input['name']) {
312 $this->saveRevision($page);
315 // Prevent slug being updated if no name change
316 if ($page->name !== $input['name']) {
317 $page->slug = $this->findSuitableSlug($input['name'], $book_id, $page->id);
320 // Save page tags if present
321 if(isset($input['tags'])) {
322 $this->tagRepo->saveTagsToEntity($page, $input['tags']);
325 // Update with new details
326 $userId = auth()->user()->id;
328 $page->html = $this->formatHtml($input['html']);
329 $page->text = strip_tags($page->html);
330 if (setting('app-editor') !== 'markdown') $page->markdown = '';
331 $page->updated_by = $userId;
334 // Remove all update drafts for this user & page.
335 $this->userUpdateDraftsQuery($page, $userId)->delete();
341 * Restores a revision's content back into a page.
344 * @param int $revisionId
347 public function restoreRevision(Page $page, Book $book, $revisionId)
349 $this->saveRevision($page);
350 $revision = $this->getRevisionById($revisionId);
351 $page->fill($revision->toArray());
352 $page->slug = $this->findSuitableSlug($page->name, $book->id, $page->id);
353 $page->text = strip_tags($page->html);
354 $page->updated_by = auth()->user()->id;
360 * Saves a page revision into the system.
364 public function saveRevision(Page $page)
366 $revision = $this->pageRevision->fill($page->toArray());
367 if (setting('app-editor') !== 'markdown') $revision->markdown = '';
368 $revision->page_id = $page->id;
369 $revision->slug = $page->slug;
370 $revision->book_slug = $page->book->slug;
371 $revision->created_by = auth()->user()->id;
372 $revision->created_at = $page->updated_at;
373 $revision->type = 'version';
375 // Clear old revisions
376 if ($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) {
377 $this->pageRevision->where('page_id', '=', $page->id)
378 ->orderBy('created_at', 'desc')->skip(50)->take(5)->delete();
384 * Save a page update draft.
387 * @return PageRevision
389 public function saveUpdateDraft(Page $page, $data = [])
391 $userId = auth()->user()->id;
392 $drafts = $this->userUpdateDraftsQuery($page, $userId)->get();
394 if ($drafts->count() > 0) {
395 $draft = $drafts->first();
397 $draft = $this->pageRevision->newInstance();
398 $draft->page_id = $page->id;
399 $draft->slug = $page->slug;
400 $draft->book_slug = $page->book->slug;
401 $draft->created_by = $userId;
402 $draft->type = 'update_draft';
406 if (setting('app-editor') !== 'markdown') $draft->markdown = '';
413 * Update a draft page.
418 public function updateDraftPage(Page $page, $data = [])
422 if (isset($data['html'])) {
423 $page->text = strip_tags($data['html']);
431 * The base query for getting user update drafts.
436 private function userUpdateDraftsQuery(Page $page, $userId)
438 return $this->pageRevision->where('created_by', '=', $userId)
439 ->where('type', 'update_draft')
440 ->where('page_id', '=', $page->id)
441 ->orderBy('created_at', 'desc');
445 * Checks whether a user has a draft version of a particular page or not.
450 public function hasUserGotPageDraft(Page $page, $userId)
452 return $this->userUpdateDraftsQuery($page, $userId)->count() > 0;
456 * Get the latest updated draft revision for a particular page and user.
461 public function getUserPageDraft(Page $page, $userId)
463 return $this->userUpdateDraftsQuery($page, $userId)->first();
467 * Get the notification message that informs the user that they are editing a draft page.
468 * @param PageRevision $draft
471 public function getUserPageDraftMessage(PageRevision $draft)
473 $message = 'You are currently editing a draft that was last saved ' . $draft->updated_at->diffForHumans() . '.';
474 if ($draft->page->updated_at->timestamp > $draft->updated_at->timestamp) {
475 $message .= "\n This page has been updated by since that time. It is recommended that you discard this draft.";
481 * Check if a page is being actively editing.
482 * Checks for edits since last page updated.
483 * Passing in a minuted range will check for edits
484 * within the last x minutes.
486 * @param null $minRange
489 public function isPageEditingActive(Page $page, $minRange = null)
491 $draftSearch = $this->activePageEditingQuery($page, $minRange);
492 return $draftSearch->count() > 0;
496 * Get a notification message concerning the editing activity on
499 * @param null $minRange
502 public function getPageEditingActiveMessage(Page $page, $minRange = null)
504 $pageDraftEdits = $this->activePageEditingQuery($page, $minRange)->get();
505 $userMessage = $pageDraftEdits->count() > 1 ? $pageDraftEdits->count() . ' users have' : $pageDraftEdits->first()->createdBy->name . ' has';
506 $timeMessage = $minRange === null ? 'since the page was last updated' : 'in the last ' . $minRange . ' minutes';
507 $message = '%s started editing this page %s. Take care not to overwrite each other\'s updates!';
508 return sprintf($message, $userMessage, $timeMessage);
512 * A query to check for active update drafts on a particular page.
514 * @param null $minRange
517 private function activePageEditingQuery(Page $page, $minRange = null)
519 $query = $this->pageRevision->where('type', '=', 'update_draft')
520 ->where('page_id', '=', $page->id)
521 ->where('updated_at', '>', $page->updated_at)
522 ->where('created_by', '!=', auth()->user()->id)
525 if ($minRange !== null) {
526 $query = $query->where('updated_at', '>=', Carbon::now()->subMinutes($minRange));
533 * Gets a single revision via it's id.
537 public function getRevisionById($id)
539 return $this->pageRevision->findOrFail($id);
543 * Checks if a slug exists within a book already.
546 * @param bool|false $currentId
549 public function doesSlugExist($slug, $bookId, $currentId = false)
551 $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId);
552 if ($currentId) $query = $query->where('id', '!=', $currentId);
553 return $query->count() > 0;
557 * Changes the related book for the specified page.
558 * Changes the book id of any relations to the page that store the book id.
563 public function changeBook($bookId, Page $page)
565 $page->book_id = $bookId;
566 foreach ($page->activity as $activity) {
567 $activity->book_id = $bookId;
570 $page->slug = $this->findSuitableSlug($page->name, $bookId, $page->id);
576 * Gets a suitable slug for the resource
579 * @param bool|false $currentId
582 public function findSuitableSlug($name, $bookId, $currentId = false)
584 $slug = Str::slug($name);
585 while ($this->doesSlugExist($slug, $bookId, $currentId)) {
586 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
592 * Destroy a given page along with its dependencies.
595 public function destroy(Page $page)
597 Activity::removeEntity($page);
598 $page->views()->delete();
599 $page->tags()->delete();
600 $page->revisions()->delete();
601 $page->permissions()->delete();
602 $this->permissionService->deleteJointPermissionsForEntity($page);
607 * Get the latest pages added to the system.
610 public function getRecentlyCreatedPaginated($count = 20)
612 return $this->pageQuery()->orderBy('created_at', 'desc')->paginate($count);
616 * Get the latest pages added to the system.
619 public function getRecentlyUpdatedPaginated($count = 20)
621 return $this->pageQuery()->orderBy('updated_at', 'desc')->paginate($count);