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

@@ -118,10 +118,9 @@ class PostQueryBuilder
relation = add_range_relation(q[:filesize], "posts.file_size", relation)
relation = add_range_relation(q[:date], "posts.created_at", relation)
relation = add_range_relation(q[:age], "posts.created_at", relation)
relation = add_range_relation(q[:general_tag_count], "posts.tag_count_general", relation)
relation = add_range_relation(q[:artist_tag_count], "posts.tag_count_artist", relation)
relation = add_range_relation(q[:copyright_tag_count], "posts.tag_count_copyright", relation)
relation = add_range_relation(q[:character_tag_count], "posts.tag_count_character", relation)
Danbooru.config.full_tag_config_info.each_key do |category|
relation = add_range_relation(q["#{category}_tag_count".to_sym], "posts.tag_count_#{category}", relation)
end
relation = add_range_relation(q[:post_tag_count], "posts.tag_count", relation)
relation = add_range_relation(q[:pixiv_id], "posts.pixiv_id", relation)
@@ -512,29 +511,11 @@ class PostQueryBuilder
when "tagcount_asc"
relation = relation.order("posts.tag_count ASC")
when "gentags", "gentags_desc"
relation = relation.order("posts.tag_count_general DESC")
when /(#{Danbooru.config.short_tag_name_mapping.keys.join("|")})tags(?:\Z|_desc)/
relation = relation.order("posts.tag_count_#{Danbooru.config.short_tag_name_mapping[$1]} DESC")
when "gentags_asc"
relation = relation.order("posts.tag_count_general ASC")
when "arttags", "arttags_desc"
relation = relation.order("posts.tag_count_artist DESC")
when "arttags_asc"
relation = relation.order("posts.tag_count_artist ASC")
when "chartags", "chartags_desc"
relation = relation.order("posts.tag_count_character DESC")
when "chartags_asc"
relation = relation.order("posts.tag_count_character ASC")
when "copytags", "copytags_desc"
relation = relation.order("posts.tag_count_copyright DESC")
when "copytags_asc"
relation = relation.order("posts.tag_count_copyright ASC")
when /(#{Danbooru.config.short_tag_name_mapping.keys.join("|")})tags_asc/
relation = relation.order("posts.tag_count_#{Danbooru.config.short_tag_name_mapping[$1]} ASC")
when "rank"
relation = relation.order("log(3, posts.score) + (extract(epoch from posts.created_at) - extract(epoch from timestamp '2005-05-24')) / 35000 DESC")

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

View File

@@ -129,48 +129,24 @@ class PostPresenter < Presenter
@post.humanized_essential_tag_string
end
def categorized_tag_string
def categorized_tag_groups
string = []
if @post.copyright_tags.any?
string << @post.copyright_tags.join(" ")
Danbooru.config.categorized_tag_list.each do |category|
if @post.typed_tags(category).any?
string << @post.typed_tags(category).join(" ")
end
end
string
end
if @post.character_tags.any?
string << @post.character_tags.join(" ")
end
if @post.artist_tags.any?
string << @post.artist_tags.join(" ")
end
if @post.general_tags.any?
string << @post.general_tags.join(" ")
end
string.join(" \n")
def categorized_tag_string
categorized_tag_groups.join(" \n")
end
def humanized_categorized_tag_string
string = []
if @post.copyright_tags.any?
string << @post.copyright_tags
end
if @post.character_tags.any?
string << @post.character_tags
end
if @post.artist_tags.any?
string << @post.artist_tags
end
if @post.general_tags.any?
string << @post.general_tags
end
string.flatten.slice(0, 25).join(", ").tr("_", " ")
categorized_tag_groups.flatten.slice(0, 25).join(", ").tr("_", " ")
end
def image_html(template)

View File

@@ -25,40 +25,16 @@ class TagSetPresenter < Presenter
def split_tag_list_html(template, options = {})
html = ""
if copyright_tags.any?
html << '<h2>Copyrights</h2>'
html << "<ul>"
copyright_tags.keys.each do |tag|
html << build_list_item(tag, template, options)
Danbooru.config.split_tag_header_list.each do |category|
typetags = typed_tags(category)
if typetags.any?
html << Danbooru.config.full_tag_config_info[category]["header"]
html << "<ul>"
typetags.each do |tag|
html << build_list_item(tag, template, options)
end
html << "</ul>"
end
html << "</ul>"
end
if character_tags.any?
html << '<h2>Characters</h2>'
html << "<ul>"
character_tags.keys.each do |tag|
html << build_list_item(tag, template, options)
end
html << "</ul>"
end
if artist_tags.any?
html << '<h2>Artist</h2>'
html << "<ul>"
artist_tags.keys.each do |tag|
html << build_list_item(tag, template, options)
end
html << "</ul>"
end
if general_tags.any?
html << '<h1>Tags</h1>'
html << "<ul>"
general_tags.keys.each do |tag|
html << build_list_item(tag, template, options)
end
html << "</ul>"
end
html.html_safe
@@ -76,20 +52,13 @@ class TagSetPresenter < Presenter
end
private
def general_tags
@general_tags ||= categories.select {|k, v| v == Tag.categories.general}
end
def copyright_tags
@copyright_tags ||= categories.select {|k, v| v == Tag.categories.copyright}
end
def character_tags
@character_tags ||= categories.select {|k, v| v == Tag.categories.character}
end
def artist_tags
@artist_tags ||= categories.select {|k, v| v == Tag.categories.artist}
def typed_tags(name)
@typed_tags ||= {}
@typed_tags[name] ||= begin
@tags.select do |tag|
categories[tag] == Danbooru.config.tag_category_mapping[name]
end
end
end
def categories

View File

@@ -214,32 +214,59 @@ module Danbooru
"albert"
end
#TAG CONFIGURATION
#Full tag configuration info for all tags
def full_tag_config_info
@full_tag_category_mapping ||= {
"general" => {
"category" => 0,
"short" => "gen",
"extra" => []
"extra" => [],
"header" => "<h1>Tags</h1>",
"humanized" => nil
},
"character" => {
"category" => 4,
"short" => "char",
"extra" => ["ch"]
"extra" => ["ch"],
"header" => "<h2>Characters</h2>",
"humanized" => {
"slice" => 5,
"exclusion" => [],
"regexmap" => /^(.+?)(?:_\(.+\))?$/,
"formatstr" => "%s"
}
},
"copyright" => {
"category" => 3,
"short" => "copy",
"extra" => ["co"]
"extra" => ["co"],
"header" => "<h2>Copyrights</h2>",
"humanized" => {
"slice" => 5,
"exclusion" => [],
"regexmap" => //,
"formatstr" => "(%s)"
}
},
"artist" => {
"category" => 1,
"short" => "art",
"extra" => []
"extra" => [],
"header" => "<h2>Artist</h2>",
"humanized" => {
"slice" => 0,
"exclusion" => %w(banned_artist),
"regexmap" => //,
"formatstr" => "drawn by %s"
}
}
}
end
#TAG MAPPINGS
# Returns a hash mapping various tag categories to a numerical value.
def tag_category_mapping
@tag_category_mapping ||= Hash[
@@ -259,6 +286,30 @@ module Danbooru
@reverse_tag_category_mapping ||= Hash[full_tag_config_info.map {|k,v| [v["category"],k]}]
end
# Returns a hash mapping for the short name usage in metatags
def short_tag_name_mapping
@short_tag_name_mapping ||= Hash[full_tag_config_info.map {|k,v| [v["short"],k] }]
end
#TAG ORDERS
#Sets the order of the humanized essential tag string (models/post.rb)
def humanized_tag_category_list
@humanized_tag_category_list ||= ["character","copyright","artist"]
end
#Sets the order of the split tag header list (presenters/tag_set_presenter.rb)
def split_tag_header_list
@split_tag_header_list ||= ["copyright","character","artist","general"]
end
#Sets the order of the categorized tag string (presenters/post_presenter.rb)
def categorized_tag_list
@categorized_tag_list ||= ["copyright","character","artist","general"]
end
#END TAG
# If enabled, users must verify their email addresses.
def enable_email_verification?
false