1 <?php namespace BookStack\Repos;
4 use Illuminate\Support\Facades\Auth;
5 use Illuminate\Support\Str;
7 use BookStack\PageRevision;
12 protected $pageRevision;
15 * PageRepo constructor.
17 * @param PageRevision $pageRevision
19 public function __construct(Page $page, PageRevision $pageRevision)
22 $this->pageRevision = $pageRevision;
25 public function idExists($id)
27 return $this->page->where('page_id', '=', $id)->count() > 0;
30 public function getById($id)
32 return $this->page->findOrFail($id);
35 public function getAll()
37 return $this->page->all();
40 public function getBySlug($slug, $bookId)
42 return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
45 public function newFromInput($input)
47 $page = $this->page->fill($input);
51 public function countBySlug($slug, $bookId)
53 return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
56 public function destroyById($id)
58 $page = $this->getById($id);
62 public function getBySearch($term, $whereTerms = [])
64 $terms = explode(' ', preg_quote(trim($term)));
65 $pages = $this->page->fullTextSearch(['name', 'text'], $terms, $whereTerms);
67 // Add highlights to page text.
68 $words = join('|', $terms);
69 //lookahead/behind assertions ensures cut between words
70 $s = '\s\x00-/:-@\[-`{-~'; //character set for start/end of words
72 foreach ($pages as $page) {
73 preg_match_all('#(?<=[' . $s . ']).{1,30}((' . $words . ').{1,30})+(?=[' . $s . '])#uis', $page->text, $matches, PREG_SET_ORDER);
74 //delimiter between occurrences
76 foreach ($matches as $line) {
77 $results[] = htmlspecialchars($line[0], 0, 'UTF-8');
80 if (count($results) > $matchLimit) {
81 $results = array_slice($results, 0, $matchLimit);
83 $result = join('... ', $results);
86 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $result);
87 if (strlen($result) < 5) {
88 $result = $page->getExcerpt(80);
90 $page->searchSnippet = $result;
96 * Search for image usage.
100 public function searchForImage($imageString)
102 $pages = $this->page->where('html', 'like', '%'.$imageString.'%')->get();
103 foreach($pages as $page) {
104 $page->url = $page->getUrl();
108 return count($pages) > 0 ? $pages : false;
112 * Updates a page with any fillable data and saves it into the database.
118 public function updatePage(Page $page, $book_id, $data)
121 $page->slug = $this->findSuitableSlug($page->name, $book_id, $page->id);
122 $page->text = strip_tags($page->html);
123 $page->updated_by = Auth::user()->id;
125 $this->saveRevision($page);
130 * Saves a page revision into the system.
134 public function saveRevision(Page $page)
136 $lastRevision = $this->getLastRevision($page);
137 if ($lastRevision && ($lastRevision->html === $page->html && $lastRevision->name === $page->name)) {
140 $revision = $this->pageRevision->fill($page->toArray());
141 $revision->page_id = $page->id;
142 $revision->created_by = Auth::user()->id;
144 // Clear old revisions
145 if ($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) {
146 $this->pageRevision->where('page_id', '=', $page->id)
147 ->orderBy('created_at', 'desc')->skip(50)->take(5)->delete();
153 * Gets the most recent revision for a page.
157 public function getLastRevision(Page $page)
159 return $this->pageRevision->where('page_id', '=', $page->id)
160 ->orderBy('created_at', 'desc')->first();
164 * Gets a single revision via it's id.
168 public function getRevisionById($id)
170 return $this->pageRevision->findOrFail($id);
174 * Checks if a slug exists within a book already.
177 * @param bool|false $currentId
180 public function doesSlugExist($slug, $bookId, $currentId = false)
182 $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId);
184 $query = $query->where('id', '!=', $currentId);
186 return $query->count() > 0;
189 public function setBookId($bookId, Page $page)
191 $page->book_id = $bookId;
192 foreach($page->activity as $activity) {
193 $activity->book_id = $bookId;
201 * Gets a suitable slug for the resource
205 * @param bool|false $currentId
208 public function findSuitableSlug($name, $bookId, $currentId = false)
210 $slug = Str::slug($name);
211 while ($this->doesSlugExist($slug, $bookId, $currentId)) {
212 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);