]> BookStack Code Mirror - bookstack/blob - app/Repos/PageRepo.php
Added AJAX-based search to books, Fixes #15
[bookstack] / app / Repos / PageRepo.php
1 <?php namespace Oxbow\Repos;
2
3
4 use Illuminate\Support\Facades\Auth;
5 use Illuminate\Support\Str;
6 use Oxbow\Page;
7 use Oxbow\PageRevision;
8
9 class PageRepo
10 {
11     protected $page;
12     protected $pageRevision;
13
14     /**
15      * PageRepo constructor.
16      * @param Page         $page
17      * @param PageRevision $pageRevision
18      */
19     public function __construct(Page $page, PageRevision $pageRevision)
20     {
21         $this->page = $page;
22         $this->pageRevision = $pageRevision;
23     }
24
25     public function idExists($id)
26     {
27         return $this->page->where('page_id', '=', $id)->count() > 0;
28     }
29
30     public function getById($id)
31     {
32         return $this->page->findOrFail($id);
33     }
34
35     public function getAll()
36     {
37         return $this->page->all();
38     }
39
40     public function getBySlug($slug, $bookId)
41     {
42         return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
43     }
44
45     public function newFromInput($input)
46     {
47         $page = $this->page->fill($input);
48         return $page;
49     }
50
51     public function countBySlug($slug, $bookId)
52     {
53         return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
54     }
55
56     public function destroyById($id)
57     {
58         $page = $this->getById($id);
59         $page->delete();
60     }
61
62     public function getBySearch($term, $whereTerms = [])
63     {
64         $terms = explode(' ', preg_quote(trim($term)));
65         $pages = $this->page->fullTextSearch(['name', 'text'], $terms, $whereTerms);
66
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
71
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
75             $results = [];
76             foreach ($matches as $line) {
77                 $results[] = htmlspecialchars($line[0], 0, 'UTF-8');
78             }
79             $matchLimit = 6;
80             if (count($results) > $matchLimit) {
81                 $results = array_slice($results, 0, $matchLimit);
82             }
83             $result = join('... ', $results);
84
85             //highlight
86             $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $result);
87             if (strlen($result) < 5) {
88                 $result = $page->getExcerpt(80);
89             }
90             $page->searchSnippet = $result;
91         }
92         return $pages;
93     }
94
95     /**
96      * Updates a page with any fillable data and saves it into the database.
97      * @param Page $page
98      * @param      $book_id
99      * @param      $data
100      * @return Page
101      */
102     public function updatePage(Page $page, $book_id, $data)
103     {
104         $page->fill($data);
105         $page->slug = $this->findSuitableSlug($page->name, $book_id, $page->id);
106         $page->text = strip_tags($page->html);
107         $page->updated_by = Auth::user()->id;
108         $page->save();
109         $this->saveRevision($page);
110         return $page;
111     }
112
113     /**
114      * Saves a page revision into the system.
115      * @param Page $page
116      * @return $this
117      */
118     public function saveRevision(Page $page)
119     {
120         $lastRevision = $this->getLastRevision($page);
121         if ($lastRevision && ($lastRevision->html === $page->html && $lastRevision->name === $page->name)) {
122             return $page;
123         }
124         $revision = $this->pageRevision->fill($page->toArray());
125         $revision->page_id = $page->id;
126         $revision->created_by = Auth::user()->id;
127         $revision->save();
128         // Clear old revisions
129         if ($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) {
130             $this->pageRevision->where('page_id', '=', $page->id)
131                 ->orderBy('created_at', 'desc')->skip(50)->take(5)->delete();
132         }
133         return $revision;
134     }
135
136     /**
137      * Gets the most recent revision for a page.
138      * @param Page $page
139      * @return mixed
140      */
141     public function getLastRevision(Page $page)
142     {
143         return $this->pageRevision->where('page_id', '=', $page->id)
144             ->orderBy('created_at', 'desc')->first();
145     }
146
147     /**
148      * Gets a single revision via it's id.
149      * @param $id
150      * @return mixed
151      */
152     public function getRevisionById($id)
153     {
154         return $this->pageRevision->findOrFail($id);
155     }
156
157     /**
158      * Checks if a slug exists within a book already.
159      * @param            $slug
160      * @param            $bookId
161      * @param bool|false $currentId
162      * @return bool
163      */
164     public function doesSlugExist($slug, $bookId, $currentId = false)
165     {
166         $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId);
167         if ($currentId) {
168             $query = $query->where('id', '!=', $currentId);
169         }
170         return $query->count() > 0;
171     }
172
173     /**
174      * Gets a suitable slug for the resource
175      *
176      * @param            $name
177      * @param            $bookId
178      * @param bool|false $currentId
179      * @return string
180      */
181     public function findSuitableSlug($name, $bookId, $currentId = false)
182     {
183         $slug = Str::slug($name);
184         while ($this->doesSlugExist($slug, $bookId, $currentId)) {
185             $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
186         }
187         return $slug;
188     }
189
190
191 }