- /**
- * Search entities of a type via a given query.
- * @param string $type
- * @param string $term
- * @param array $whereTerms
- * @param int $count
- * @param array $paginationAppends
- * @return mixed
- */
- public function getBySearch($type, $term, $whereTerms = [], $count = 20, $paginationAppends = [])
- {
- $terms = $this->prepareSearchTerms($term);
- $q = $this->permissionService->enforceEntityRestrictions($type, $this->getEntity($type)->fullTextSearchQuery($terms, $whereTerms));
- $q = $this->addAdvancedSearchQueries($q, $term);
- $entities = $q->paginate($count)->appends($paginationAppends);
- $words = join('|', explode(' ', preg_quote(trim($term), '/')));
-
- // Highlight page content
- if ($type === 'page') {
- //lookahead/behind assertions ensures cut between words
- $s = '\s\x00-/:-@\[-`{-~'; //character set for start/end of words
-
- foreach ($entities as $page) {
- preg_match_all('#(?<=[' . $s . ']).{1,30}((' . $words . ').{1,30})+(?=[' . $s . '])#uis', $page->text, $matches, PREG_SET_ORDER);
- //delimiter between occurrences
- $results = [];
- foreach ($matches as $line) {
- $results[] = htmlspecialchars($line[0], 0, 'UTF-8');
- }
- $matchLimit = 6;
- if (count($results) > $matchLimit) $results = array_slice($results, 0, $matchLimit);
- $result = join('... ', $results);
-
- //highlight
- $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $result);
- if (strlen($result) < 5) $result = $page->getExcerpt(80);
-
- $page->searchSnippet = $result;
- }
- return $entities;
- }
-
- // Highlight chapter/book content
- foreach ($entities as $entity) {
- //highlight
- $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $entity->getExcerpt(100));
- $entity->searchSnippet = $result;
- }
- return $entities;
- }