2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
require "sanitize"
|
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
class Search
|
|
|
|
class GroupedSearchResults
|
2014-09-02 05:15:08 -04:00
|
|
|
include ActiveModel::Serialization
|
2013-05-23 14:26:51 -04:00
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
class TextHelper
|
|
|
|
extend ActionView::Helpers::TextHelper
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
|
|
|
|
2017-07-17 11:57:13 -04:00
|
|
|
attr_reader(
|
|
|
|
:type_filter,
|
|
|
|
:posts,
|
|
|
|
:categories,
|
2024-02-21 10:29:47 -05:00
|
|
|
:extra_categories,
|
2017-07-17 11:57:13 -04:00
|
|
|
:users,
|
2017-08-25 11:52:18 -04:00
|
|
|
:tags,
|
2019-03-04 04:30:09 -05:00
|
|
|
:groups,
|
2017-07-17 11:57:13 -04:00
|
|
|
:more_posts,
|
|
|
|
:more_categories,
|
|
|
|
:more_users,
|
|
|
|
:term,
|
|
|
|
:search_context,
|
2019-07-01 21:21:52 -04:00
|
|
|
:more_full_page_results,
|
|
|
|
:error,
|
2023-04-03 10:09:36 -04:00
|
|
|
:use_pg_headlines_for_excerpt,
|
2024-02-21 10:29:47 -05:00
|
|
|
:can_lazy_load_categories,
|
2017-07-17 11:57:13 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
attr_accessor :search_log_id
|
2014-09-02 05:15:08 -04:00
|
|
|
|
2020-07-13 23:05:57 -04:00
|
|
|
BLURB_LENGTH = 200
|
|
|
|
|
2022-05-24 11:31:24 -04:00
|
|
|
def initialize(
|
|
|
|
type_filter:,
|
|
|
|
term:,
|
|
|
|
search_context:,
|
|
|
|
blurb_length: nil,
|
|
|
|
blurb_term: nil,
|
2023-04-03 10:09:36 -04:00
|
|
|
is_header_search: false,
|
2024-02-21 10:29:47 -05:00
|
|
|
use_pg_headlines_for_excerpt: SiteSetting.use_pg_headlines_for_excerpt,
|
|
|
|
can_lazy_load_categories: false
|
2022-05-24 11:31:24 -04:00
|
|
|
)
|
2014-09-02 05:15:08 -04:00
|
|
|
@type_filter = type_filter
|
|
|
|
@term = term
|
2020-07-13 23:05:57 -04:00
|
|
|
@blurb_term = blurb_term || term
|
2014-09-02 05:15:08 -04:00
|
|
|
@search_context = search_context
|
2020-07-13 23:05:57 -04:00
|
|
|
@blurb_length = blurb_length || BLURB_LENGTH
|
2014-09-02 05:15:08 -04:00
|
|
|
@posts = []
|
|
|
|
@categories = []
|
2024-02-21 10:29:47 -05:00
|
|
|
@extra_categories = Set.new
|
2014-09-02 05:15:08 -04:00
|
|
|
@users = []
|
2017-08-25 11:52:18 -04:00
|
|
|
@tags = []
|
2019-03-04 04:30:09 -05:00
|
|
|
@groups = []
|
2019-07-01 21:21:52 -04:00
|
|
|
@error = nil
|
2022-05-24 11:31:24 -04:00
|
|
|
@is_header_search = is_header_search
|
2023-04-03 10:09:36 -04:00
|
|
|
@use_pg_headlines_for_excerpt = use_pg_headlines_for_excerpt
|
2024-02-21 10:29:47 -05:00
|
|
|
@can_lazy_load_categories = can_lazy_load_categories
|
2019-07-01 21:21:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def error=(error)
|
|
|
|
@error = error
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
|
|
|
|
2016-03-18 01:26:20 -04:00
|
|
|
def find_user_data(guardian)
|
|
|
|
if user = guardian.user
|
|
|
|
topics = @posts.map(&:topic)
|
|
|
|
topic_lookup = TopicUser.lookup_for(user, topics)
|
|
|
|
topics.each { |ft| ft.user_data = topic_lookup[ft.id] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-12 03:33:26 -04:00
|
|
|
OMISSION = "..."
|
|
|
|
SCRUB_HEADLINE_REGEXP =
|
|
|
|
%r{<span(?: \w+="[^"]+")* class="#{Search::HIGHLIGHT_CSS_CLASS}"(?: \w+="[^"]+")*>([^<]*)</span>}
|
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
def blurb(post)
|
2020-07-17 04:27:30 -04:00
|
|
|
opts = { term: @blurb_term, blurb_length: @blurb_length }
|
2024-03-14 13:40:46 -04:00
|
|
|
post_search_data_version = post&.post_search_data&.version
|
2020-07-17 04:27:30 -04:00
|
|
|
|
2024-03-14 13:40:46 -04:00
|
|
|
if post_search_data_version.present? &&
|
|
|
|
post_search_data_version >= SearchIndexer::MIN_POST_BLURB_INDEX_VERSION &&
|
2022-01-26 02:24:11 -05:00
|
|
|
!Search.segment_chinese? && !Search.segment_japanese?
|
2023-04-03 10:09:36 -04:00
|
|
|
if use_pg_headlines_for_excerpt
|
2020-08-12 03:33:26 -04:00
|
|
|
scrubbed_headline = post.headline.gsub(SCRUB_HEADLINE_REGEXP, '\1')
|
|
|
|
prefix_omission = scrubbed_headline.start_with?(post.leading_raw_data) ? "" : OMISSION
|
|
|
|
postfix_omission = scrubbed_headline.end_with?(post.trailing_raw_data) ? "" : OMISSION
|
|
|
|
return "#{prefix_omission}#{post.headline}#{postfix_omission}"
|
2020-08-06 02:15:31 -04:00
|
|
|
else
|
|
|
|
opts[:cooked] = post.post_search_data.raw_data
|
|
|
|
opts[:scrub] = false
|
|
|
|
end
|
2020-07-17 04:27:30 -04:00
|
|
|
else
|
|
|
|
opts[:cooked] = post.cooked
|
|
|
|
end
|
|
|
|
|
|
|
|
GroupedSearchResults.blurb_for(**opts)
|
2014-09-02 05:15:08 -04:00
|
|
|
end
|
2013-05-23 14:26:51 -04:00
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
def add(object)
|
|
|
|
type = object.class.to_s.downcase.pluralize
|
2022-05-24 11:31:24 -04:00
|
|
|
if !@is_header_search && public_send(type).length == Search.per_filter
|
2017-07-20 12:07:13 -04:00
|
|
|
@more_full_page_results = true
|
2022-05-24 11:31:24 -04:00
|
|
|
elsif @is_header_search && public_send(type).length == Search.per_facet
|
2014-09-02 05:15:08 -04:00
|
|
|
instance_variable_set("@more_#{type}".to_sym, true)
|
2013-05-23 14:26:51 -04:00
|
|
|
else
|
2019-05-06 22:05:58 -04:00
|
|
|
(self.public_send(type)) << object
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
2024-02-21 10:29:47 -05:00
|
|
|
|
|
|
|
if can_lazy_load_categories
|
|
|
|
category =
|
|
|
|
case type
|
|
|
|
when "posts"
|
|
|
|
object.topic.category
|
|
|
|
when "topics"
|
|
|
|
object.category
|
|
|
|
end
|
|
|
|
|
|
|
|
if category
|
|
|
|
extra_categories << category.parent_category if category.parent_category
|
|
|
|
extra_categories << category
|
|
|
|
end
|
|
|
|
end
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
|
|
|
|
2020-07-17 04:27:30 -04:00
|
|
|
def self.blurb_for(cooked: nil, term: nil, blurb_length: BLURB_LENGTH, scrub: true)
|
2015-06-24 15:08:22 -04:00
|
|
|
blurb = nil
|
2020-08-06 00:25:03 -04:00
|
|
|
|
|
|
|
if scrub
|
2022-03-07 16:03:10 -05:00
|
|
|
cooked = SearchIndexer::HtmlScrubber.scrub(cooked)
|
2020-08-06 00:25:03 -04:00
|
|
|
|
|
|
|
urls = Set.new
|
|
|
|
cooked.scan(Discourse::Utils::URI_REGEXP) { urls << $& }
|
|
|
|
urls.each do |url|
|
|
|
|
begin
|
|
|
|
case File.extname(URI(url).path || "")
|
|
|
|
when Oneboxer::VIDEO_REGEX
|
|
|
|
cooked.gsub!(url, I18n.t("search.video"))
|
|
|
|
when Oneboxer::AUDIO_REGEX
|
|
|
|
cooked.gsub!(url, I18n.t("search.audio"))
|
|
|
|
end
|
|
|
|
rescue URI::InvalidURIError
|
2019-11-06 10:32:15 -05:00
|
|
|
end
|
2019-10-31 09:13:24 -04:00
|
|
|
end
|
2019-10-30 13:07:16 -04:00
|
|
|
end
|
|
|
|
|
2015-06-24 15:08:22 -04:00
|
|
|
if term
|
2020-07-13 23:05:57 -04:00
|
|
|
term = Regexp.last_match[1] if term =~ Regexp.new(Search::PHRASE_MATCH_REGEXP_PATTERN)
|
2019-03-26 05:01:19 -04:00
|
|
|
|
2020-07-13 23:05:57 -04:00
|
|
|
blurb = TextHelper.excerpt(cooked, term, radius: blurb_length / 2)
|
2015-06-24 15:08:22 -04:00
|
|
|
end
|
2018-09-17 04:31:15 -04:00
|
|
|
|
2019-10-31 09:32:42 -04:00
|
|
|
blurb = TextHelper.truncate(cooked, length: blurb_length) if blurb.blank?
|
2015-06-24 15:08:22 -04:00
|
|
|
Sanitize.clean(blurb)
|
|
|
|
end
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
2014-02-16 22:34:14 -05:00
|
|
|
end
|