search: refactor PostQueryBuilder class methods into instance methods.

* Make scan_query, parse_query, normalize_query into instance methods
  instead of class methods. This is to a) clean up the API and b)
  prepare for moving certain tag utility methods into PostQueryBuilder.

* Fix a few cases where a caller used scan_query when they should have
  used split_query or parse_tag_edit.
This commit is contained in:
evazion
2020-04-22 19:38:17 -05:00
parent d355c0e221
commit 3dab648d0e
10 changed files with 534 additions and 532 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.normalize_query(params[:tags])
tags = PostQueryBuilder.new(params[:tags]).normalize_query
sig = generate_reportbooru_signature("ps-#{tags}")
render "posts/partials/index/search_count", sig: sig
end
@@ -63,7 +63,7 @@ module PostsHelper
end
def show_tag_change_notice?
CurrentUser.user.is_member? && PostQueryBuilder.scan_query(params[:tags]).size == 1 && TagChangeNoticeService.get_forum_topic_id(params[:tags])
CurrentUser.user.is_member? && PostQueryBuilder.new(params[:tags]).split_query.size == 1 && TagChangeNoticeService.get_forum_topic_id(params[:tags])
end
private

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.split_query(antecedent.mb_chars.downcase))
normalized_consequent = TagAlias.to_aliased(PostQueryBuilder.split_query(consequent.mb_chars.downcase))
normalized_antecedent = TagAlias.to_aliased(PostQueryBuilder.new(antecedent.mb_chars.downcase).split_query)
normalized_consequent = TagAlias.to_aliased(PostQueryBuilder.new(consequent.mb_chars.downcase).parse_tag_edit)
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.split_query(normalized_antecedent.join(" "))
tags = PostQueryBuilder.new(normalized_antecedent.join(" ")).split_query
# 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.split_query(line)
list = PostQueryBuilder.new(line).split_query
if (list & query).size != query.size
next line

View File

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

File diff suppressed because it is too large Load Diff

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.split_query(tags)
@tag_array = PostQueryBuilder.new(tags).split_query
@page = page
@per_page = per_page
@raw = raw.to_s.truthy?

View File

@@ -596,7 +596,7 @@ class Post < ApplicationRecord
# If someone else committed changes to this post before we did,
# then try to merge the tag changes together.
current_tags = tag_string_was.split
new_tags = PostQueryBuilder.parse_tag_edit(tag_string)
new_tags = PostQueryBuilder.new(tag_string).parse_tag_edit
old_tags = old_tag_string.split
kept_tags = current_tags & new_tags
@@ -634,7 +634,7 @@ class Post < ApplicationRecord
end
def normalize_tags
normalized_tags = PostQueryBuilder.split_query(tag_string)
normalized_tags = PostQueryBuilder.new(tag_string).parse_tag_edit
normalized_tags = apply_casesensitive_metatags(normalized_tags)
normalized_tags = normalized_tags.map(&:downcase)
normalized_tags = filter_metatags(normalized_tags)
@@ -1070,7 +1070,7 @@ class Post < ApplicationRecord
tags = tags.to_s
tags += " rating:s" if CurrentUser.safe_mode?
tags += " -status:deleted" if CurrentUser.hide_deleted_posts? && !Tag.has_metatag?(tags, "status", "-status")
tags = PostQueryBuilder.normalize_query(tags)
tags = PostQueryBuilder.new(tags).normalize_query
# Optimize some cases. these are just estimates but at these
# quantities being off by a few hundred doesn't matter much

View File

@@ -136,17 +136,17 @@ class SavedSearch < ApplicationRecord
def queries_for(user_id, label: nil, options: {})
searches = SavedSearch.where(user_id: user_id)
searches = searches.labeled(label) if label.present?
queries = searches.pluck(:query).map { |query| PostQueryBuilder.normalize_query(query, sort: true) }
queries = searches.pluck(:query).map { |query| PostQueryBuilder.new(query).normalize_query(sort: true) }
queries.sort.uniq
end
end
def normalized_query
PostQueryBuilder.normalize_query(query, sort: true)
PostQueryBuilder.new(query).normalize_query(sort: true)
end
def normalize_query
self.query = PostQueryBuilder.normalize_query(query, sort: false)
self.query = PostQueryBuilder.new(query).normalize_query(sort: false)
end
end

View File

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

View File

@@ -854,12 +854,12 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
should "work" do
create(:tag_alias, antecedent_name: "gray", consequent_name: "grey")
assert_equal("foo", PostQueryBuilder.normalize_query("foo"))
assert_equal("foo", PostQueryBuilder.normalize_query(" foo "))
assert_equal("foo", PostQueryBuilder.normalize_query("FOO"))
assert_equal("foo", PostQueryBuilder.normalize_query("foo foo"))
assert_equal("grey", PostQueryBuilder.normalize_query("gray"))
assert_equal("aaa bbb", PostQueryBuilder.normalize_query("bbb aaa"))
assert_equal("foo", PostQueryBuilder.new("foo").normalize_query)
assert_equal("foo", PostQueryBuilder.new(" foo ").normalize_query)
assert_equal("foo", PostQueryBuilder.new("FOO").normalize_query)
assert_equal("foo", PostQueryBuilder.new("foo foo").normalize_query)
assert_equal("grey", PostQueryBuilder.new("gray").normalize_query)
assert_equal("aaa bbb", PostQueryBuilder.new("bbb aaa").normalize_query)
end
end
end

View File

@@ -93,13 +93,13 @@ class TagTest < ActiveSupport::TestCase
context "A tag parser" do
should "scan a query" do
assert_equal(%w(aaa bbb), PostQueryBuilder.split_query("aaa bbb"))
assert_equal(%w(~aaa -bbb* -bbb*), PostQueryBuilder.split_query("~AAa -BBB* -bbb*"))
assert_equal(%w(aaa bbb), PostQueryBuilder.new("aaa bbb").split_query)
assert_equal(%w(~aaa -bbb* -bbb*), PostQueryBuilder.new("~AAa -BBB* -bbb*").split_query)
end
should "not strip out valid characters when scanning" do
assert_equal(%w(aaa bbb), PostQueryBuilder.split_query("aaa bbb"))
assert_equal(%w(favgroup:yondemasu_yo,_azazel-san. pool:ichigo_100%), PostQueryBuilder.split_query("favgroup:yondemasu_yo,_azazel-san. pool:ichigo_100%"))
assert_equal(%w(aaa bbb), PostQueryBuilder.new("aaa bbb").split_query)
assert_equal(%w(favgroup:yondemasu_yo,_azazel-san. pool:ichigo_100%), PostQueryBuilder.new("favgroup:yondemasu_yo,_azazel-san. pool:ichigo_100%").split_query)
end
should "cast values" do