Moved most of the tag category config logic to the config file

-Fixed an unused Post class method (fix_post_counts) that didn't have a parameter
This commit is contained in:
BrokenEagle
2017-11-05 23:17:32 -08:00
parent 9f4daf7d2f
commit 1e5540f3a0
6 changed files with 129 additions and 208 deletions

View File

@@ -616,30 +616,21 @@ class Post < ApplicationRecord
Post.expire_cache_for_all([""]) if new_record? || id <= 100_000
end
def set_tag_count(category,tagcount)
self.send("tag_count_#{category}=",tagcount)
end
def inc_tag_count(category)
set_tag_count(category,self.send("tag_count_#{category}") + 1)
end
def set_tag_counts
self.tag_count = 0
self.tag_count_general = 0
self.tag_count_artist = 0
self.tag_count_copyright = 0
self.tag_count_character = 0
Danbooru.config.full_tag_config_info.each_key {|x| set_tag_count(x,0)}
categories = Tag.categories_for(tag_array, :disable_caching => true)
categories.each_value do |category|
self.tag_count += 1
case category
when Tag.categories.general
self.tag_count_general += 1
when Tag.categories.artist
self.tag_count_artist += 1
when Tag.categories.copyright
self.tag_count_copyright += 1
when Tag.categories.character
self.tag_count_character += 1
end
inc_tag_count(Danbooru.config.reverse_tag_category_mapping[category])
end
end
@@ -918,26 +909,6 @@ class Post < ApplicationRecord
@tag_categories ||= Tag.categories_for(tag_array)
end
def copyright_tags
typed_tags("copyright")
end
def character_tags
typed_tags("character")
end
def artist_tags
typed_tags("artist")
end
def artist_tags_excluding_hidden
artist_tags - %w(banned_artist)
end
def general_tags
typed_tags("general")
end
def typed_tags(name)
@typed_tags ||= {}
@typed_tags[name] ||= begin
@@ -955,52 +926,37 @@ class Post < ApplicationRecord
@humanized_essential_tag_string ||= Cache.get("hets-#{id}", 1.hour.to_i) do
string = []
if character_tags.any?
chartags = character_tags.slice(0, 5)
if character_tags.length > 5
chartags << "others"
Danbooru.config.humanized_tag_category_list.each do |category|
humanizeddata = Danbooru.config.full_tag_config_info[category]["humanized"]
typetags = typed_tags(category) - humanizeddata["exclusion"]
if humanizeddata["slice"] > 0
typetags = typetags.slice(0,humanizeddata["slice"]) + (typetags.length > humanizeddata["slice"] ? ["others"] : [])
end
chartags = chartags.map do |tag|
tag.match(/^(.+?)(?:_\(.+\))?$/)[1]
if humanizeddata["regexmap"] != //
typetags = typetags.map do |tag|
tag.match(humanizeddata["regexmap"])[1]
end
end
string << chartags.to_sentence
end
if copyright_tags.any?
copytags = copyright_tags.slice(0, 5)
if copyright_tags.length > 5
copytags << "others"
if typetags.any?
if category != "copyright" || typed_tags("character").any?
string << humanizeddata["formatstr"] % typetags.to_sentence
else
string << typetags.to_sentence
end
end
copytags = copytags.to_sentence
string << (character_tags.any? ? "(#{copytags})" : copytags)
end
if artist_tags_excluding_hidden.any?
string << "drawn by"
string << artist_tags_excluding_hidden.to_sentence
end
string.empty? ? "##{id}" : string.join(" ").tr("_", " ")
end
end
def tag_string_copyright
copyright_tags.join(" ")
end
def tag_string_character
character_tags.join(" ")
end
def tag_string_artist
artist_tags.join(" ")
end
def tag_string_general
general_tags.join(" ")
Danbooru.config.full_tag_config_info.each_key do |category|
define_method("tag_string_#{category}") do
typed_tags(category).join(" ")
end
end
end
module FavoriteMethods
def clean_fav_string?
true
@@ -1182,15 +1138,10 @@ class Post < ApplicationRecord
end
module CountMethods
def fix_post_counts
def fix_post_counts(post)
post.set_tag_counts
post.update_columns(
:tag_count => post.tag_count,
:tag_count_general => post.tag_count_general,
:tag_count_artist => post.tag_count_artist,
:tag_count_copyright => post.tag_count_copyright,
:tag_count_character => post.tag_count_character
)
args = Hash[Danbooru.config.full_tag_config_info.keys.map {|x| ["tag_count_#{x}",post.send("tag_count_#{x}")]}].update(:tag_count => post.tag_count)
post.update_columns(args)
end
def get_count_from_cache(tags)
@@ -1572,7 +1523,7 @@ class Post < ApplicationRecord
end
def method_attributes
list = super + [:uploader_name, :has_large, :tag_string_artist, :tag_string_character, :tag_string_copyright, :tag_string_general, :has_visible_children, :children_ids]
list = super + [:uploader_name, :has_large, :has_visible_children, :children_ids] + Danbooru.config.full_tag_config_info.keys.map {|x| "tag_string_#{x}".to_sym}
if visible?
list += [:file_url, :large_file_url, :preview_file_url]
end

View File

@@ -1,6 +1,7 @@
class Tag < ApplicationRecord
COSINE_SIMILARITY_RELATED_TAG_THRESHOLD = 1000
METATAGS = "-user|user|-approver|approver|commenter|comm|noter|noteupdater|artcomm|-pool|pool|ordpool|-favgroup|favgroup|-fav|fav|ordfav|md5|-rating|rating|-locked|locked|width|height|mpixels|ratio|score|favcount|filesize|source|-source|id|-id|date|age|order|limit|-status|status|tagcount|gentags|arttags|chartags|copytags|parent|-parent|child|pixiv_id|pixiv|search|upvote|downvote|filetype|-filetype|flagger|-flagger|appealer|-appealer"
METATAGS = "-user|user|-approver|approver|commenter|comm|noter|noteupdater|artcomm|-pool|pool|ordpool|-favgroup|favgroup|-fav|fav|ordfav|md5|-rating|rating|-locked|locked|width|height|mpixels|ratio|score|favcount|filesize|source|-source|id|-id|date|age|order|limit|-status|status|tagcount|parent|-parent|child|pixiv_id|pixiv|search|upvote|downvote|filetype|-filetype|flagger|-flagger|appealer|-appealer|" +
Danbooru.config.short_tag_name_mapping.keys.map {|x| "#{x}tags"}.join("|")
SUBQUERY_METATAGS = "commenter|comm|noter|noteupdater|artcomm|flagger|-flagger|appealer|-appealer"
attr_accessible :category, :as => [:moderator, :gold, :platinum, :member, :anonymous, :default, :builder, :admin]
attr_accessible :is_locked, :as => [:moderator, :admin]
@@ -140,7 +141,8 @@ class Tag < ApplicationRecord
Post.raw_tag_match(name).where("true /* Tag#update_category_post_counts */").find_each do |post|
post.reload
post.set_tag_counts
Post.where(:id => post.id).update_all(:tag_count => post.tag_count, :tag_count_general => post.tag_count_general, :tag_count_artist => post.tag_count_artist, :tag_count_copyright => post.tag_count_copyright, :tag_count_character => post.tag_count_character)
args = Hash[Danbooru.config.full_tag_config_info.keys.map {|x| ["tag_count_#{x}",post.send("tag_count_#{x}")]}].update(:tag_count => post.tag_count)
Post.where(:id => post.id).update_all(args)
end
end
end
@@ -664,17 +666,8 @@ class Tag < ApplicationRecord
when "tagcount"
q[:post_tag_count] = parse_helper(g2)
when "gentags"
q[:general_tag_count] = parse_helper(g2)
when "arttags"
q[:artist_tag_count] = parse_helper(g2)
when "chartags"
q[:character_tag_count] = parse_helper(g2)
when "copytags"
q[:copyright_tag_count] = parse_helper(g2)
when /(#{Danbooru.config.short_tag_name_mapping.keys.join("|")})tags/
q["#{Danbooru.config.short_tag_name_mapping[$1]}_tag_count".to_sym] = parse_helper(g2)
when "parent"
q[:parent] = g2.downcase