1 <?php namespace BookStack\Repos;
7 use BookStack\Exceptions\NotFoundException;
11 use Illuminate\Support\Str;
13 use BookStack\PageRevision;
15 class PageRepo extends EntityRepo
18 protected $pageRevision;
22 * PageRepo constructor.
23 * @param PageRevision $pageRevision
24 * @param TagRepo $tagRepo
26 public function __construct(PageRevision $pageRevision, TagRepo $tagRepo)
28 $this->pageRevision = $pageRevision;
29 $this->tagRepo = $tagRepo;
30 parent::__construct();
34 * Base query for getting pages, Takes restrictions into account.
35 * @param bool $allowDrafts
38 private function pageQuery($allowDrafts = false)
40 $query = $this->permissionService->enforcePageRestrictions($this->page, 'view');
42 $query = $query->where('draft', '=', false);
48 * Get a page via a specific ID.
50 * @param bool $allowDrafts
53 public function getById($id, $allowDrafts = false)
55 return $this->pageQuery($allowDrafts)->findOrFail($id);
59 * Get a page identified by the given slug.
63 * @throws NotFoundException
65 public function getBySlug($slug, $bookId)
67 $page = $this->pageQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
68 if ($page === null) throw new NotFoundException('Page not found');
73 * Search through page revisions and retrieve
74 * the last page in the current book that
75 * has a slug equal to the one given.
80 public function findPageUsingOldSlug($pageSlug, $bookSlug)
82 $revision = $this->pageRevision->where('slug', '=', $pageSlug)
83 ->whereHas('page', function ($query) {
84 $this->permissionService->enforcePageRestrictions($query);
86 ->where('type', '=', 'version')
87 ->where('book_slug', '=', $bookSlug)->orderBy('created_at', 'desc')
88 ->with('page')->first();
89 return $revision !== null ? $revision->page : null;
93 * Get a new Page instance from the given input.
97 public function newFromInput($input)
99 $page = $this->page->fill($input);
104 * Count the pages with a particular slug within a book.
109 public function countBySlug($slug, $bookId)
111 return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
115 * Publish a draft page to make it a normal page.
116 * Sets the slug and updates the content.
117 * @param Page $draftPage
118 * @param array $input
121 public function publishDraft(Page $draftPage, array $input)
123 $draftPage->fill($input);
125 // Save page tags if present
126 if (isset($input['tags'])) {
127 $this->tagRepo->saveTagsToEntity($draftPage, $input['tags']);
130 $draftPage->slug = $this->findSuitableSlug($draftPage->name, $draftPage->book->id);
131 $draftPage->html = $this->formatHtml($input['html']);
132 $draftPage->text = strip_tags($draftPage->html);
133 $draftPage->draft = false;
136 $this->saveRevision($draftPage, 'Initial Publish');
142 * Get a new draft page instance.
144 * @param Chapter|bool $chapter
147 public function getDraftPage(Book $book, $chapter = false)
149 $page = $this->page->newInstance();
150 $page->name = 'New Page';
151 $page->created_by = auth()->user()->id;
152 $page->updated_by = auth()->user()->id;
155 if ($chapter) $page->chapter_id = $chapter->id;
157 $book->pages()->save($page);
158 $this->permissionService->buildJointPermissionsForEntity($page);
163 * Parse te headers on the page to get a navigation menu
167 public function getPageNav(Page $page)
169 if ($page->html == '') return null;
170 libxml_use_internal_errors(true);
171 $doc = new DOMDocument();
172 $doc->loadHTML(mb_convert_encoding($page->html, 'HTML-ENTITIES', 'UTF-8'));
173 $xPath = new DOMXPath($doc);
174 $headers = $xPath->query("//h1|//h2|//h3|//h4|//h5|//h6");
176 if (is_null($headers)) return null;
179 foreach ($headers as $header) {
180 $text = $header->nodeValue;
182 'nodeName' => strtolower($header->nodeName),
183 'level' => intval(str_replace('h', '', $header->nodeName)),
184 'link' => '#' . $header->getAttribute('id'),
185 'text' => strlen($text) > 30 ? substr($text, 0, 27) . '...' : $text
192 * Formats a page's html to be tagged correctly
194 * @param string $htmlText
197 protected function formatHtml($htmlText)
199 if ($htmlText == '') return $htmlText;
200 libxml_use_internal_errors(true);
201 $doc = new DOMDocument();
202 $doc->loadHTML(mb_convert_encoding($htmlText, 'HTML-ENTITIES', 'UTF-8'));
204 $container = $doc->documentElement;
205 $body = $container->childNodes->item(0);
206 $childNodes = $body->childNodes;
208 // Ensure no duplicate ids are used
211 foreach ($childNodes as $index => $childNode) {
212 /** @var \DOMElement $childNode */
213 if (get_class($childNode) !== 'DOMElement') continue;
215 // Overwrite id if not a BookStack custom id
216 if ($childNode->hasAttribute('id')) {
217 $id = $childNode->getAttribute('id');
218 if (strpos($id, 'bkmrk') === 0 && array_search($id, $idArray) === false) {
224 // Create an unique id for the element
225 // Uses the content as a basis to ensure output is the same every time
226 // the same content is passed through.
227 $contentId = 'bkmrk-' . substr(strtolower(preg_replace('/\s+/', '-', trim($childNode->nodeValue))), 0, 20);
228 $newId = urlencode($contentId);
230 while (in_array($newId, $idArray)) {
231 $newId = urlencode($contentId . '-' . $loopIndex);
235 $childNode->setAttribute('id', $newId);
239 // Generate inner html as a string
241 foreach ($childNodes as $childNode) {
242 $html .= $doc->saveHTML($childNode);
250 * Gets pages by a search term.
251 * Highlights page content for showing in results.
252 * @param string $term
253 * @param array $whereTerms
255 * @param array $paginationAppends
258 public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = [])
260 $terms = $this->prepareSearchTerms($term);
261 $pageQuery = $this->permissionService->enforcePageRestrictions($this->page->fullTextSearchQuery(['name', 'text'], $terms, $whereTerms));
262 $pageQuery = $this->addAdvancedSearchQueries($pageQuery, $term);
263 $pages = $pageQuery->paginate($count)->appends($paginationAppends);
265 // Add highlights to page text.
266 $words = join('|', explode(' ', preg_quote(trim($term), '/')));
267 //lookahead/behind assertions ensures cut between words
268 $s = '\s\x00-/:-@\[-`{-~'; //character set for start/end of words
270 foreach ($pages as $page) {
271 preg_match_all('#(?<=[' . $s . ']).{1,30}((' . $words . ').{1,30})+(?=[' . $s . '])#uis', $page->text, $matches, PREG_SET_ORDER);
272 //delimiter between occurrences
274 foreach ($matches as $line) {
275 $results[] = htmlspecialchars($line[0], 0, 'UTF-8');
278 if (count($results) > $matchLimit) {
279 $results = array_slice($results, 0, $matchLimit);
281 $result = join('... ', $results);
284 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $result);
285 if (strlen($result) < 5) {
286 $result = $page->getExcerpt(80);
288 $page->searchSnippet = $result;
294 * Search for image usage.
295 * @param $imageString
298 public function searchForImage($imageString)
300 $pages = $this->pageQuery()->where('html', 'like', '%' . $imageString . '%')->get();
301 foreach ($pages as $page) {
302 $page->url = $page->getUrl();
306 return count($pages) > 0 ? $pages : false;
310 * Updates a page with any fillable data and saves it into the database.
312 * @param int $book_id
313 * @param string $input
316 public function updatePage(Page $page, $book_id, $input)
318 // Hold the old details to compare later
319 $oldHtml = $page->html;
320 $oldName = $page->name;
322 // Prevent slug being updated if no name change
323 if ($page->name !== $input['name']) {
324 $page->slug = $this->findSuitableSlug($input['name'], $book_id, $page->id);
327 // Save page tags if present
328 if (isset($input['tags'])) {
329 $this->tagRepo->saveTagsToEntity($page, $input['tags']);
332 // Update with new details
333 $userId = auth()->user()->id;
335 $page->html = $this->formatHtml($input['html']);
336 $page->text = strip_tags($page->html);
337 if (setting('app-editor') !== 'markdown') $page->markdown = '';
338 $page->updated_by = $userId;
341 // Remove all update drafts for this user & page.
342 $this->userUpdateDraftsQuery($page, $userId)->delete();
344 // Save a revision after updating
345 if ($oldHtml !== $input['html'] || $oldName !== $input['name'] || $input['summary'] !== null) {
346 $this->saveRevision($page, $input['summary']);
353 * Restores a revision's content back into a page.
356 * @param int $revisionId
359 public function restoreRevision(Page $page, Book $book, $revisionId)
361 $this->saveRevision($page);
362 $revision = $this->getRevisionById($revisionId);
363 $page->fill($revision->toArray());
364 $page->slug = $this->findSuitableSlug($page->name, $book->id, $page->id);
365 $page->text = strip_tags($page->html);
366 $page->updated_by = auth()->user()->id;
372 * Saves a page revision into the system.
374 * @param null|string $summary
377 public function saveRevision(Page $page, $summary = null)
379 $revision = $this->pageRevision->newInstance($page->toArray());
380 if (setting('app-editor') !== 'markdown') $revision->markdown = '';
381 $revision->page_id = $page->id;
382 $revision->slug = $page->slug;
383 $revision->book_slug = $page->book->slug;
384 $revision->created_by = auth()->user()->id;
385 $revision->created_at = $page->updated_at;
386 $revision->type = 'version';
387 $revision->summary = $summary;
390 // Clear old revisions
391 if ($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) {
392 $this->pageRevision->where('page_id', '=', $page->id)
393 ->orderBy('created_at', 'desc')->skip(50)->take(5)->delete();
400 * Save a page update draft.
403 * @return PageRevision
405 public function saveUpdateDraft(Page $page, $data = [])
407 $userId = auth()->user()->id;
408 $drafts = $this->userUpdateDraftsQuery($page, $userId)->get();
410 if ($drafts->count() > 0) {
411 $draft = $drafts->first();
413 $draft = $this->pageRevision->newInstance();
414 $draft->page_id = $page->id;
415 $draft->slug = $page->slug;
416 $draft->book_slug = $page->book->slug;
417 $draft->created_by = $userId;
418 $draft->type = 'update_draft';
422 if (setting('app-editor') !== 'markdown') $draft->markdown = '';
429 * Update a draft page.
434 public function updateDraftPage(Page $page, $data = [])
438 if (isset($data['html'])) {
439 $page->text = strip_tags($data['html']);
447 * The base query for getting user update drafts.
452 private function userUpdateDraftsQuery(Page $page, $userId)
454 return $this->pageRevision->where('created_by', '=', $userId)
455 ->where('type', 'update_draft')
456 ->where('page_id', '=', $page->id)
457 ->orderBy('created_at', 'desc');
461 * Checks whether a user has a draft version of a particular page or not.
466 public function hasUserGotPageDraft(Page $page, $userId)
468 return $this->userUpdateDraftsQuery($page, $userId)->count() > 0;
472 * Get the latest updated draft revision for a particular page and user.
477 public function getUserPageDraft(Page $page, $userId)
479 return $this->userUpdateDraftsQuery($page, $userId)->first();
483 * Get the notification message that informs the user that they are editing a draft page.
484 * @param PageRevision $draft
487 public function getUserPageDraftMessage(PageRevision $draft)
489 $message = 'You are currently editing a draft that was last saved ' . $draft->updated_at->diffForHumans() . '.';
490 if ($draft->page->updated_at->timestamp > $draft->updated_at->timestamp) {
491 $message .= "\n This page has been updated by since that time. It is recommended that you discard this draft.";
497 * Check if a page is being actively editing.
498 * Checks for edits since last page updated.
499 * Passing in a minuted range will check for edits
500 * within the last x minutes.
502 * @param null $minRange
505 public function isPageEditingActive(Page $page, $minRange = null)
507 $draftSearch = $this->activePageEditingQuery($page, $minRange);
508 return $draftSearch->count() > 0;
512 * Get a notification message concerning the editing activity on
515 * @param null $minRange
518 public function getPageEditingActiveMessage(Page $page, $minRange = null)
520 $pageDraftEdits = $this->activePageEditingQuery($page, $minRange)->get();
521 $userMessage = $pageDraftEdits->count() > 1 ? $pageDraftEdits->count() . ' users have' : $pageDraftEdits->first()->createdBy->name . ' has';
522 $timeMessage = $minRange === null ? 'since the page was last updated' : 'in the last ' . $minRange . ' minutes';
523 $message = '%s started editing this page %s. Take care not to overwrite each other\'s updates!';
524 return sprintf($message, $userMessage, $timeMessage);
528 * A query to check for active update drafts on a particular page.
530 * @param null $minRange
533 private function activePageEditingQuery(Page $page, $minRange = null)
535 $query = $this->pageRevision->where('type', '=', 'update_draft')
536 ->where('page_id', '=', $page->id)
537 ->where('updated_at', '>', $page->updated_at)
538 ->where('created_by', '!=', auth()->user()->id)
541 if ($minRange !== null) {
542 $query = $query->where('updated_at', '>=', Carbon::now()->subMinutes($minRange));
549 * Gets a single revision via it's id.
553 public function getRevisionById($id)
555 return $this->pageRevision->findOrFail($id);
559 * Checks if a slug exists within a book already.
562 * @param bool|false $currentId
565 public function doesSlugExist($slug, $bookId, $currentId = false)
567 $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId);
568 if ($currentId) $query = $query->where('id', '!=', $currentId);
569 return $query->count() > 0;
573 * Changes the related book for the specified page.
574 * Changes the book id of any relations to the page that store the book id.
579 public function changeBook($bookId, Page $page)
581 $page->book_id = $bookId;
582 foreach ($page->activity as $activity) {
583 $activity->book_id = $bookId;
586 $page->slug = $this->findSuitableSlug($page->name, $bookId, $page->id);
593 * Change the page's parent to the given entity.
595 * @param Entity $parent
597 public function changePageParent(Page $page, Entity $parent)
599 $book = $parent->isA('book') ? $parent : $parent->book;
600 $page->chapter_id = $parent->isA('chapter') ? $parent->id : 0;
602 $page = $this->changeBook($book->id, $page);
604 $this->permissionService->buildJointPermissionsForEntity($book);
608 * Gets a suitable slug for the resource
609 * @param string $name
611 * @param bool|false $currentId
614 public function findSuitableSlug($name, $bookId, $currentId = false)
616 $slug = Str::slug($name);
617 if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
618 while ($this->doesSlugExist($slug, $bookId, $currentId)) {
619 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
625 * Destroy a given page along with its dependencies.
628 public function destroy(Page $page)
630 Activity::removeEntity($page);
631 $page->views()->delete();
632 $page->tags()->delete();
633 $page->revisions()->delete();
634 $page->permissions()->delete();
635 $this->permissionService->deleteJointPermissionsForEntity($page);
640 * Get the latest pages added to the system.
643 public function getRecentlyCreatedPaginated($count = 20)
645 return $this->pageQuery()->orderBy('created_at', 'desc')->paginate($count);
649 * Get the latest pages added to the system.
652 public function getRecentlyUpdatedPaginated($count = 20)
654 return $this->pageQuery()->orderBy('updated_at', 'desc')->paginate($count);