]> BookStack Code Mirror - bookstack/blob - app/Entity.php
Updated fulltext search with custom escaped query
[bookstack] / app / Entity.php
1 <?php
2
3 namespace BookStack;
4
5 use Illuminate\Database\Eloquent\Model;
6
7 abstract class Entity extends Model
8 {
9
10     use Ownable;
11
12     /**
13      * Compares this entity to another given entity.
14      * Matches by comparing class and id.
15      * @param $entity
16      * @return bool
17      */
18     public function matches($entity)
19     {
20         return [get_class($this), $this->id] === [get_class($entity), $entity->id];
21     }
22
23     /**
24      * Checks if an entity matches or contains another given entity.
25      * @param Entity $entity
26      * @return bool
27      */
28     public function matchesOrContains(Entity $entity)
29     {
30         $matches = [get_class($this), $this->id] === [get_class($entity), $entity->id];
31
32         if ($matches) return true;
33
34         if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
35             return $entity->book_id === $this->id;
36         }
37
38         if ($entity->isA('page') && $this->isA('chapter')) {
39             return $entity->chapter_id === $this->id;
40         }
41
42         return false;
43     }
44
45     /**
46      * Gets the activity objects for this entity.
47      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
48      */
49     public function activity()
50     {
51         return $this->morphMany('BookStack\Activity', 'entity')->orderBy('created_at', 'desc');
52     }
53
54     /**
55      * Get View objects for this entity.
56      * @return mixed
57      */
58     public function views()
59     {
60         return $this->morphMany('BookStack\View', 'viewable');
61     }
62
63     /**
64      * Allows checking of the exact class, Used to check entity type.
65      * Cleaner method for is_a.
66      * @param $type
67      * @return bool
68      */
69     public static function isA($type)
70     {
71         return static::getClassName() === strtolower($type);
72     }
73
74     /**
75      * Gets the class name.
76      * @return string
77      */
78     public static function getClassName()
79     {
80         return strtolower(array_slice(explode('\\', static::class), -1, 1)[0]);
81     }
82
83     /**
84      *Gets a limited-length version of the entities name.
85      * @param int $length
86      * @return string
87      */
88     public function getShortName($length = 25)
89     {
90         if (strlen($this->name) <= $length) return $this->name;
91         return substr($this->name, 0, $length - 3) . '...';
92     }
93
94     /**
95      * Perform a full-text search on this entity.
96      * @param string[] $fieldsToSearch
97      * @param string[] $terms
98      * @param string[] array $wheres
99      * @return mixed
100      */
101     public static function fullTextSearchQuery($fieldsToSearch, $terms, $wheres = [])
102     {
103         $exactTerms = [];
104         foreach ($terms as $key => $term) {
105             $term = htmlentities($term, ENT_QUOTES);
106             $term =  preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $term);
107             if (preg_match('/\s/', $term)) {
108                 $exactTerms[] = '%' . $term . '%';
109                 $term = '"' . $term . '"';
110             } else {
111                 $term = '' . $term . '*';
112             }
113             if ($term !== '*') $terms[$key] = $term;
114         }
115         $termString = implode(' ', $terms);
116         $fields = implode(',', $fieldsToSearch);
117         $search = static::selectRaw('*, MATCH(name) AGAINST(? IN BOOLEAN MODE) AS title_relevance', [$termString]);
118         $search = $search->whereRaw('MATCH(' . $fields . ') AGAINST(? IN BOOLEAN MODE)', [$termString]);
119
120         // Ensure at least one exact term matches if in search
121         if (count($exactTerms) > 0) {
122             $search = $search->where(function($query) use ($exactTerms, $fieldsToSearch) {
123                 foreach ($exactTerms as $exactTerm) {
124                     foreach ($fieldsToSearch as $field) {
125                         $query->orWhere($field, 'like', $exactTerm);
126                     }
127                 }
128             });
129         }
130
131         // Add additional where terms
132         foreach ($wheres as $whereTerm) {
133             $search->where($whereTerm[0], $whereTerm[1], $whereTerm[2]);
134         }
135         // Load in relations
136         if (static::isA('page')) {
137             $search = $search->with('book', 'chapter', 'createdBy', 'updatedBy');
138         } else if (static::isA('chapter')) {
139             $search = $search->with('book');
140         }
141
142         return $search->orderBy('title_relevance', 'desc');
143     }
144
145     /**
146      * Get the url for this item.
147      * @return string
148      */
149     abstract public function getUrl();
150
151 }