+ public function getPageNav($pageContent)
+ {
+ if ($pageContent == '') {
+ return [];
+ }
+ libxml_use_internal_errors(true);
+ $doc = new DOMDocument();
+ $doc->loadHTML(mb_convert_encoding($pageContent, 'HTML-ENTITIES', 'UTF-8'));
+ $xPath = new DOMXPath($doc);
+ $headers = $xPath->query("//h1|//h2|//h3|//h4|//h5|//h6");
+
+ if (is_null($headers)) {
+ return [];
+ }
+
+ $tree = collect([]);
+ foreach ($headers as $header) {
+ $text = $header->nodeValue;
+ $tree->push([
+ 'nodeName' => strtolower($header->nodeName),
+ 'level' => intval(str_replace('h', '', $header->nodeName)),
+ 'link' => '#' . $header->getAttribute('id'),
+ 'text' => strlen($text) > 30 ? substr($text, 0, 27) . '...' : $text
+ ]);
+ }
+
+ // Normalise headers if only smaller headers have been used
+ if (count($tree) > 0) {
+ $minLevel = $tree->pluck('level')->min();
+ $tree = $tree->map(function ($header) use ($minLevel) {
+ $header['level'] -= ($minLevel - 2);
+ return $header;
+ });
+ }
+ return $tree->toArray();
+ }
+
+ /**
+ * Updates a page with any fillable data and saves it into the database.
+ * @param Page $page
+ * @param int $book_id
+ * @param array $input
+ * @return Page
+ */
+ public function updatePage(Page $page, $book_id, $input)
+ {
+ // Hold the old details to compare later
+ $oldHtml = $page->html;
+ $oldName = $page->name;
+
+ // Prevent slug being updated if no name change
+ if ($page->name !== $input['name']) {
+ $page->slug = $this->findSuitableSlug('page', $input['name'], $page->id, $book_id);
+ }
+
+ // Save page tags if present
+ if (isset($input['tags'])) {
+ $this->tagRepo->saveTagsToEntity($page, $input['tags']);
+ }
+
+ // Update with new details
+ $userId = user()->id;
+ $page->fill($input);
+ $page->html = $this->formatHtml($input['html']);
+ $page->text = $this->pageToPlainText($page);
+ if (setting('app-editor') !== 'markdown') {
+ $page->markdown = '';
+ }
+ $page->updated_by = $userId;
+ $page->revision_count++;
+ $page->save();
+
+ // Remove all update drafts for this user & page.
+ $this->userUpdatePageDraftsQuery($page, $userId)->delete();
+
+ // Save a revision after updating
+ if ($oldHtml !== $input['html'] || $oldName !== $input['name'] || $input['summary'] !== null) {
+ $this->savePageRevision($page, $input['summary']);
+ }
+
+ $this->searchService->indexEntity($page);
+
+ return $page;
+ }
+
+ /**
+ * The base query for getting user update drafts.
+ * @param Page $page
+ * @param $userId
+ * @return mixed
+ */
+ protected function userUpdatePageDraftsQuery(Page $page, $userId)
+ {
+ return $this->pageRevision->where('created_by', '=', $userId)
+ ->where('type', 'update_draft')
+ ->where('page_id', '=', $page->id)
+ ->orderBy('created_at', 'desc');
+ }
+
+ /**
+ * Checks whether a user has a draft version of a particular page or not.
+ * @param Page $page
+ * @param $userId
+ * @return bool
+ */
+ public function hasUserGotPageDraft(Page $page, $userId)
+ {
+ return $this->userUpdatePageDraftsQuery($page, $userId)->count() > 0;
+ }
+
+ /**
+ * Get the latest updated draft revision for a particular page and user.
+ * @param Page $page
+ * @param $userId
+ * @return mixed
+ */
+ public function getUserPageDraft(Page $page, $userId)
+ {
+ return $this->userUpdatePageDraftsQuery($page, $userId)->first();
+ }
+
+ /**
+ * Get the notification message that informs the user that they are editing a draft page.
+ * @param PageRevision $draft
+ * @return string
+ */
+ public function getUserPageDraftMessage(PageRevision $draft)
+ {
+ $message = trans('entities.pages_editing_draft_notification', ['timeDiff' => $draft->updated_at->diffForHumans()]);
+ if ($draft->page->updated_at->timestamp <= $draft->updated_at->timestamp) {
+ return $message;
+ }
+ return $message . "\n" . trans('entities.pages_draft_edited_notification');
+ }
+
+ /**
+ * Check if a page is being actively editing.
+ * Checks for edits since last page updated.
+ * Passing in a minuted range will check for edits
+ * within the last x minutes.
+ * @param Page $page
+ * @param null $minRange
+ * @return bool
+ */
+ public function isPageEditingActive(Page $page, $minRange = null)
+ {
+ $draftSearch = $this->activePageEditingQuery($page, $minRange);
+ return $draftSearch->count() > 0;
+ }
+
+ /**
+ * A query to check for active update drafts on a particular page.
+ * @param Page $page
+ * @param null $minRange
+ * @return mixed
+ */
+ protected function activePageEditingQuery(Page $page, $minRange = null)
+ {
+ $query = $this->pageRevision->where('type', '=', 'update_draft')
+ ->where('page_id', '=', $page->id)
+ ->where('updated_at', '>', $page->updated_at)
+ ->where('created_by', '!=', user()->id)
+ ->with('createdBy');
+
+ if ($minRange !== null) {
+ $query = $query->where('updated_at', '>=', Carbon::now()->subMinutes($minRange));
+ }
+
+ return $query;
+ }
+
+ /**
+ * Restores a revision's content back into a page.
+ * @param Page $page
+ * @param Book $book
+ * @param int $revisionId
+ * @return Page
+ */
+ public function restorePageRevision(Page $page, Book $book, $revisionId)
+ {
+ $page->revision_count++;
+ $this->savePageRevision($page);
+ $revision = $page->revisions()->where('id', '=', $revisionId)->first();
+ $page->fill($revision->toArray());
+ $page->slug = $this->findSuitableSlug('page', $page->name, $page->id, $book->id);
+ $page->text = $this->pageToPlainText($page);
+ $page->updated_by = user()->id;
+ $page->save();
+ $this->searchService->indexEntity($page);
+ return $page;
+ }
+
+
+ /**
+ * Save a page update draft.
+ * @param Page $page
+ * @param array $data
+ * @return PageRevision|Page
+ */
+ public function updatePageDraft(Page $page, $data = [])