search: refactor scan_query callers to use split_query.

Refactor to use split_query instead of scan_query to split a query on
spaces. Preparation for refactoring scan_query into something smarter.
This commit is contained in:
evazion
2020-04-18 19:57:58 -05:00
parent d0c68e68c2
commit 7726563733
7 changed files with 17 additions and 13 deletions

View File

@@ -25,7 +25,7 @@ module PostsHelper
return unless post_search_counts_enabled?
return unless params[:action] == "index" && params[:page].nil? && params[:tags].present?
tags = PostQueryBuilder.scan_query(params[:tags]).sort.join(" ")
tags = PostQueryBuilder.normalize_query(params[:tags])
sig = generate_reportbooru_signature("ps-#{tags}")
render "posts/partials/index/search_count", sig: sig
end

View File

@@ -6,8 +6,8 @@ class TagBatchChangeJob < ApplicationJob
def perform(antecedent, consequent, updater, updater_ip_addr)
raise Error.new("antecedent is missing") if antecedent.blank?
normalized_antecedent = TagAlias.to_aliased(PostQueryBuilder.scan_query(antecedent.mb_chars.downcase))
normalized_consequent = TagAlias.to_aliased(PostQueryBuilder.scan_query(consequent.mb_chars.downcase))
normalized_antecedent = TagAlias.to_aliased(PostQueryBuilder.split_query(antecedent.mb_chars.downcase))
normalized_consequent = TagAlias.to_aliased(PostQueryBuilder.split_query(consequent.mb_chars.downcase))
CurrentUser.without_safe_mode do
CurrentUser.scoped(updater, updater_ip_addr) do
@@ -30,7 +30,7 @@ class TagBatchChangeJob < ApplicationJob
end
def migrate_saved_searches(normalized_antecedent, normalized_consequent)
tags = PostQueryBuilder.scan_query(normalized_antecedent.join(" "), strip_metatags: true)
tags = PostQueryBuilder.split_query(normalized_antecedent.join(" "))
# https://www.postgresql.org/docs/current/static/functions-array.html
saved_searches = SavedSearch.where("string_to_array(query, ' ') @> ARRAY[?]", tags)
@@ -53,7 +53,7 @@ class TagBatchChangeJob < ApplicationJob
begin
repl = user.blacklisted_tags.split(/\r\n|\r|\n/).map do |line|
list = PostQueryBuilder.scan_query(line)
list = PostQueryBuilder.split_query(line)
if (list & query).size != query.size
next line

View File

@@ -88,8 +88,8 @@ class AliasAndImplicationImporter
all
when :mass_update
all += PostQueryBuilder.scan_query(token[1])
all += PostQueryBuilder.scan_query(token[2])
all += PostQueryBuilder.split_query(token[1])
all += PostQueryBuilder.split_query(token[2])
all
when :change_category

View File

@@ -760,6 +760,10 @@ class PostQueryBuilder
list
end
def split_query(query)
scan_query(query)
end
def normalize_query(query, normalize_aliases: true, sort: true)
tags = scan_query(query.to_s)
tags = tags.map { |t| Tag.normalize_name(t) }

View File

@@ -4,7 +4,7 @@ module PostSets
attr_reader :tag_array, :page, :raw, :random, :post_count, :format
def initialize(tags, page = 1, per_page = nil, raw: false, random: false, format: "html")
@tag_array = PostQueryBuilder.scan_query(tags)
@tag_array = PostQueryBuilder.split_query(tags)
@page = page
@per_page = per_page
@raw = raw.to_s.truthy?

View File

@@ -527,11 +527,11 @@ class Post < ApplicationRecord
module TagMethods
def tag_array
@tag_array ||= PostQueryBuilder.scan_query(tag_string)
@tag_array ||= PostQueryBuilder.split_query(tag_string)
end
def tag_array_was
@tag_array_was ||= PostQueryBuilder.scan_query(tag_string_in_database.presence || tag_string_before_last_save || "")
@tag_array_was ||= PostQueryBuilder.split_query(tag_string_in_database.presence || tag_string_before_last_save || "")
end
def tags
@@ -595,7 +595,7 @@ class Post < ApplicationRecord
# then try to merge the tag changes together.
current_tags = tag_array_was
new_tags = tag_array
old_tags = PostQueryBuilder.scan_query(old_tag_string)
old_tags = PostQueryBuilder.split_query(old_tag_string)
kept_tags = current_tags & new_tags
@removed_tags = old_tags - kept_tags
@@ -632,7 +632,7 @@ class Post < ApplicationRecord
end
def normalize_tags
normalized_tags = PostQueryBuilder.scan_query(tag_string)
normalized_tags = PostQueryBuilder.split_query(tag_string)
normalized_tags = apply_casesensitive_metatags(normalized_tags)
normalized_tags = normalized_tags.map(&:downcase)
normalized_tags = filter_metatags(normalized_tags)

View File

@@ -256,7 +256,7 @@ class Tag < ApplicationRecord
def has_metatag?(tags, *metatags)
return nil if tags.blank?
tags = PostQueryBuilder.scan_query(tags.to_str) if tags.respond_to?(:to_str)
tags = PostQueryBuilder.split_query(tags.to_str) if tags.respond_to?(:to_str)
tags.grep(/\A(?:#{metatags.map(&:to_s).join("|")}):(.+)\z/i) { $1 }.first
end
end