]> BookStack Code Mirror - bookstack/blob - app/Repos/PageRepo.php
Change application namespace to BookStack
[bookstack] / app / Repos / PageRepo.php
1 <?php namespace BookStack\Repos;
2
3
4 use Illuminate\Support\Facades\Auth;
5 use Illuminate\Support\Str;
6 use BookStack\Page;
7 use BookStack\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      * Search for image usage.
97      * @param $imageString
98      * @return mixed
99      */
100     public function searchForImage($imageString)
101     {
102         $pages = $this->page->where('html', 'like', '%'.$imageString.'%')->get();
103         foreach($pages as $page) {
104             $page->url = $page->getUrl();
105             $page->html = '';
106             $page->text = '';
107         }
108         return count($pages) > 0 ? $pages : false;
109     }
110
111     /**
112      * Updates a page with any fillable data and saves it into the database.
113      * @param Page $page
114      * @param      $book_id
115      * @param      $data
116      * @return Page
117      */
118     public function updatePage(Page $page, $book_id, $data)
119     {
120         $page->fill($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;
124         $page->save();
125         $this->saveRevision($page);
126         return $page;
127     }
128
129     /**
130      * Saves a page revision into the system.
131      * @param Page $page
132      * @return $this
133      */
134     public function saveRevision(Page $page)
135     {
136         $lastRevision = $this->getLastRevision($page);
137         if ($lastRevision && ($lastRevision->html === $page->html && $lastRevision->name === $page->name)) {
138             return $page;
139         }
140         $revision = $this->pageRevision->fill($page->toArray());
141         $revision->page_id = $page->id;
142         $revision->created_by = Auth::user()->id;
143         $revision->save();
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();
148         }
149         return $revision;
150     }
151
152     /**
153      * Gets the most recent revision for a page.
154      * @param Page $page
155      * @return mixed
156      */
157     public function getLastRevision(Page $page)
158     {
159         return $this->pageRevision->where('page_id', '=', $page->id)
160             ->orderBy('created_at', 'desc')->first();
161     }
162
163     /**
164      * Gets a single revision via it's id.
165      * @param $id
166      * @return mixed
167      */
168     public function getRevisionById($id)
169     {
170         return $this->pageRevision->findOrFail($id);
171     }
172
173     /**
174      * Checks if a slug exists within a book already.
175      * @param            $slug
176      * @param            $bookId
177      * @param bool|false $currentId
178      * @return bool
179      */
180     public function doesSlugExist($slug, $bookId, $currentId = false)
181     {
182         $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId);
183         if ($currentId) {
184             $query = $query->where('id', '!=', $currentId);
185         }
186         return $query->count() > 0;
187     }
188
189     public function setBookId($bookId, Page $page)
190     {
191         $page->book_id = $bookId;
192         foreach($page->activity as $activity) {
193             $activity->book_id = $bookId;
194             $activity->save();
195         }
196         $page->save();
197         return $page;
198     }
199
200     /**
201      * Gets a suitable slug for the resource
202      *
203      * @param            $name
204      * @param            $bookId
205      * @param bool|false $currentId
206      * @return string
207      */
208     public function findSuitableSlug($name, $bookId, $currentId = false)
209     {
210         $slug = Str::slug($name);
211         while ($this->doesSlugExist($slug, $bookId, $currentId)) {
212             $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
213         }
214         return $slug;
215     }
216
217
218 }