+
+ /**
+ * This method finalize the content of the foundPositions array,
+ * by associated all search results to their associated bookmark field,
+ * making sure that there is no overlapping results, etc.
+ *
+ * @param array $fieldLengths Start and end positions of every bookmark fields in the aggregated bookmark content.
+ * @param array $foundPositions Positions where the search results were found in the aggregated content.
+ *
+ * @return array Updated $foundPositions, by bookmark field.
+ */
+ protected function postProcessFoundPositions(array $fieldLengths, array $foundPositions): array
+ {
+ // Sort results by starting position ASC.
+ usort($foundPositions, function (array $entryA, array $entryB): int {
+ return $entryA['start'] > $entryB['start'] ? 1 : -1;
+ });
+
+ $out = [];
+ $currentMax = -1;
+ foreach ($foundPositions as $foundPosition) {
+ // we do not allow overlapping highlights
+ if ($foundPosition['start'] < $currentMax) {
+ continue;
+ }
+
+ $currentMax = $foundPosition['end'];
+ foreach ($fieldLengths as $part => $length) {
+ if ($foundPosition['start'] < $length['start'] || $foundPosition['start'] > $length['end']) {
+ continue;
+ }
+
+ $out[$part][] = [
+ 'start' => $foundPosition['start'] - $length['start'],
+ 'end' => $foundPosition['end'] - $length['start'],
+ ];
+ break;
+ }
+ }
+
+ return $out;
+ }
+
+ /**
+ * Concatenate link fields to search across fields. Adds a '\' separator for exact search terms.
+ * Also populate $length array with starting and ending positions of every bookmark field
+ * inside concatenated content.
+ *
+ * @param Bookmark $link
+ * @param array $lengths (by reference)
+ *
+ * @return string Lowercase concatenated fields content.
+ */
+ protected function buildFullTextSearchableLink(Bookmark $link, array &$lengths): string
+ {
+ $tagString = $link->getTagsString($this->conf->get('general.tags_separator', ' '));
+ $content = mb_convert_case($link->getTitle(), MB_CASE_LOWER, 'UTF-8') .'\\';
+ $content .= mb_convert_case($link->getDescription(), MB_CASE_LOWER, 'UTF-8') .'\\';
+ $content .= mb_convert_case($link->getUrl(), MB_CASE_LOWER, 'UTF-8') .'\\';
+ $content .= mb_convert_case($tagString, MB_CASE_LOWER, 'UTF-8') .'\\';
+
+ $lengths['title'] = ['start' => 0, 'end' => mb_strlen($link->getTitle())];
+ $nextField = $lengths['title']['end'] + 1;
+ $lengths['description'] = ['start' => $nextField, 'end' => $nextField + mb_strlen($link->getDescription())];
+ $nextField = $lengths['description']['end'] + 1;
+ $lengths['url'] = ['start' => $nextField, 'end' => $nextField + mb_strlen($link->getUrl())];
+ $nextField = $lengths['url']['end'] + 1;
+ $lengths['tags'] = ['start' => $nextField, 'end' => $nextField + mb_strlen($tagString)];
+
+ return $content;
+ }