diff --git a/app/assets/javascripts/autocomplete.js b/app/assets/javascripts/autocomplete.js index 9f25788f7..40279a8e6 100644 --- a/app/assets/javascripts/autocomplete.js +++ b/app/assets/javascripts/autocomplete.js @@ -9,6 +9,7 @@ Danbooru.Autocomplete.initialize_all = function() { if (Danbooru.meta("enable-auto-complete") === "true") { Danbooru.Autocomplete.enable_local_storage = this.test_local_storage(); + this.update_static_metatags(); this.initialize_tag_autocomplete(); this.initialize_mention_autocomplete(); this.prune_local_storage(); @@ -94,7 +95,7 @@ var $fields_multiple = $('[data-autocomplete="tag-query"], [data-autocomplete="tag-edit"]'); var $fields_single = $('[data-autocomplete="tag"]'); - var prefixes = "-|~|general:|gen:|artist:|art:|copyright:|copy:|co:|character:|char:|ch:"; + var prefixes = "-|~|" + $.map(JSON.parse(Danbooru.meta("tag-category-names")), function (category) { return category + ':'}).join('|'); var metatags = "order|-status|status|-rating|rating|-locked|locked|child|filetype|-filetype|" + "-user|user|-approver|approver|commenter|comm|noter|noteupdater|artcomm|-fav|fav|ordfav|" + "-pool|pool|ordpool|favgroup|-search|search"; @@ -326,10 +327,6 @@ "portrait", "landscape", "filesize", "filesize_asc", "tagcount", "tagcount_asc", - "gentags", "gentags_asc", - "arttags", "arttags_asc", - "chartags", "chartags_asc", - "copytags", "copytags_asc", "rank", "random" ], @@ -350,6 +347,11 @@ ], } + //This must be done as a separate function as Danbooru.meta does not exist at program initialization + Danbooru.Autocomplete.update_static_metatags = function () { + Array.prototype.push.apply(Danbooru.Autocomplete.static_metatags.order,$.map(JSON.parse(Danbooru.meta("short-tag-category-names")), function(shorttag) { return [shorttag + "tags", shorttag + "tags_asc"]})); + } + Danbooru.Autocomplete.static_metatag_source = function(term, resp, metatag) { var sub_metatags = this.static_metatags[metatag]; diff --git a/app/assets/javascripts/related_tag.js b/app/assets/javascripts/related_tag.js index def95d0af..ade76e0a0 100644 --- a/app/assets/javascripts/related_tag.js +++ b/app/assets/javascripts/related_tag.js @@ -2,7 +2,7 @@ Danbooru.RelatedTag = {}; Danbooru.RelatedTag.initialize_all = function() { - if ($("#c-posts").length || $("#c-uploads").length) { + if ($("#c-posts #a-show").length || $("#c-uploads #a-new").length) { this.initialize_buttons(); $("#related-tags-container").hide(); $("#artist-tags-container").hide(); @@ -12,10 +12,9 @@ Danbooru.RelatedTag.initialize_buttons = function() { this.common_bind("#related-tags-button", ""); - this.common_bind("#related-general-button", "general"); - this.common_bind("#related-artists-button", "artist"); - this.common_bind("#related-characters-button", "character"); - this.common_bind("#related-copyrights-button", "copyright"); + $.each(JSON.parse(Danbooru.meta("related-tag-button-list")), function(i,category) { + Danbooru.RelatedTag.common_bind("#related-" + category + "-button", category); + }); $("#find-artist-button").click(Danbooru.RelatedTag.find_artist); } diff --git a/app/assets/stylesheets/specific/posts.scss b/app/assets/stylesheets/specific/posts.scss index 93d15ba01..4c39fe95d 100644 --- a/app/assets/stylesheets/specific/posts.scss +++ b/app/assets/stylesheets/specific/posts.scss @@ -211,6 +211,14 @@ body[data-user-can-approve-posts="true"] .post-preview { } } +.category-5 a, a.tag-type-5, .ui-state-focus a.tag-type-5 { + color: #F80; + + &:hover { + color: #FA6; + } +} + .category-banned a, a.tag-type-banned,, .ui-state-focus a.tag-type-banned { color: black; background-color: red; diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 5a5873dfe..75ba7f592 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -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) + TagCategory.categories.each 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) @@ -513,29 +512,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 /(#{TagCategory.short_name_regex})tags(?:\Z|_desc)/ + relation = relation.order("posts.tag_count_#{TagCategory.short_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 /(#{TagCategory.short_name_regex})tags_asc/ + relation = relation.order("posts.tag_count_#{TagCategory.short_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") diff --git a/app/logical/tag_category.rb b/app/logical/tag_category.rb new file mode 100644 index 000000000..6495a05b4 --- /dev/null +++ b/app/logical/tag_category.rb @@ -0,0 +1,78 @@ +class TagCategory + module Mappings + # Returns a hash mapping various tag categories to a numerical value. + def mapping + @@mapping ||= Hash[ + Danbooru.config.full_tag_config_info.map {|k,v| v["extra"].map {|y| [y,v["category"]]}} + .reduce([],:+)] + .update(Hash[Danbooru.config.full_tag_config_info.map {|k,v| [v["short"],v["category"]]}]) + .update( Hash[Danbooru.config.full_tag_config_info.map {|k,v| [k,v["category"]]}]) + end + + # Returns a hash mapping more suited for views + def canonical_mapping + @@canonical_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [k.capitalize,v["category"]]}] + end + + # Returns a hash mapping numerical category values to their string equivalent. + def reverse_mapping + @@reverse_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [v["category"],k]}] + end + + # Returns a hash mapping for the short name usage in metatags + def short_name_mapping + @@short_name_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [v["short"],k]}] + end + + # Returns a hash mapping for humanized_essential_tag_string (models/post.rb) + def humanized_mapping + @@humanized_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [k,v["humanized"]]}] + end + + # Returns a hash mapping for humanized_essential_tag_string (models/post.rb) + def header_mapping + @@header_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [k,v["header"]]}] + end + + # Returns a hash mapping for related tag buttons (javascripts/related_tag.js) + def related_button_mapping + @@related_button_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [k,v["relatedbutton"]]}] + end + end + + module Lists + def categories + @@categories ||= Danbooru.config.full_tag_config_info.keys + end + + def short_name_list + @@short_name_list ||= short_name_mapping.keys + end + + def humanized_list + Danbooru.config.humanized_tag_category_list + end + + def split_header_list + Danbooru.config.split_tag_header_list + end + + def categorized_list + Danbooru.config.categorized_tag_list + end + + def related_button_list + Danbooru.config.related_tag_button_list + end + end + + module Regexes + def short_name_regex + @@short_name_regex ||= short_name_list.join("|") + end + end + + extend Mappings + extend Lists + extend Regexes +end \ No newline at end of file diff --git a/app/models/post.rb b/app/models/post.rb index 7135f1ceb..2f052a8b1 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 - + TagCategory.categories {|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(TagCategory.reverse_mapping[category]) end end @@ -918,31 +909,11 @@ 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 tag_array.select do |tag| - tag_categories[tag] == Danbooru.config.tag_category_mapping[name] + tag_categories[tag] == TagCategory.mapping[name] end end end @@ -955,52 +926,36 @@ 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" + TagCategory.humanized_list.each do |category| + typetags = typed_tags(category) - TagCategory.humanized_mapping[category]["exclusion"] + if TagCategory.humanized_mapping[category]["slice"] > 0 + typetags = typetags.slice(0,TagCategory.humanized_mapping[category]["slice"]) + (typetags.length > TagCategory.humanized_mapping[category]["slice"] ? ["others"] : []) end - chartags = chartags.map do |tag| - tag.match(/^(.+?)(?:_\(.+\))?$/)[1] + if TagCategory.humanized_mapping[category]["regexmap"] != // + typetags = typetags.map do |tag| + tag.match(TagCategory.humanized_mapping[category]["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 << TagCategory.humanized_mapping[category]["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(" ") + TagCategory.categories.each do |category| + define_method("tag_string_#{category}") do + typed_tags(category).join(" ") + end end end + module FavoriteMethods def clean_fav_string? true @@ -1182,15 +1137,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[TagCategory.categories.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 +1522,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] + TagCategory.categories.map {|x| "tag_string_#{x}".to_sym} if visible? list += [:file_url, :large_file_url, :preview_file_url] end diff --git a/app/models/tag.rb b/app/models/tag.rb index 4cbd5d87e..9045295cb 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -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|" + + TagCategory.short_name_list.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] @@ -26,18 +27,18 @@ class Tag < ApplicationRecord end class CategoryMapping - Danbooru.config.reverse_tag_category_mapping.each do |value, category| - define_method(category.downcase) do + TagCategory.reverse_mapping.each do |value, category| + define_method(category) do value end end def regexp - @regexp ||= Regexp.compile(Danbooru.config.tag_category_mapping.keys.sort_by {|x| -x.size}.join("|")) + @regexp ||= Regexp.compile(TagCategory.mapping.keys.sort_by {|x| -x.size}.join("|")) end def value_for(string) - Danbooru.config.tag_category_mapping[string.to_s.downcase] || 0 + TagCategory.mapping[string.to_s.downcase] || 0 end end @@ -124,7 +125,7 @@ class Tag < ApplicationRecord end def category_name - Danbooru.config.reverse_tag_category_mapping[category] + TagCategory.reverse_mapping[category].capitalize end def update_category_cache_for_all @@ -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[TagCategory.categories {|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 @@ -175,7 +177,7 @@ class Tag < ApplicationRecord counts = counts.to_a.select {|x| x[1] > trending_count_limit} counts = counts.map do |tag_name, recent_count| tag = Tag.find_or_create_by_name(tag_name) - if tag.category == Danbooru.config.tag_category_mapping["artist"] + if tag.category == Tag.categories.artist # we're not interested in artists in the trending list [tag_name, 0] else @@ -317,19 +319,19 @@ class Tag < ApplicationRecord when :filesize object =~ /\A(\d+(?:\.\d*)?|\d*\.\d+)([kKmM]?)[bB]?\Z/ - size = $1.to_f - unit = $2 + size = $1.to_f + unit = $2 - conversion_factor = case unit - when /m/i - 1024 * 1024 - when /k/i - 1024 - else - 1 - end + conversion_factor = case unit + when /m/i + 1024 * 1024 + when /k/i + 1024 + else + 1 + end - (size * conversion_factor).to_i + (size * conversion_factor).to_i end end @@ -429,296 +431,289 @@ class Tag < ApplicationRecord q[:tag_count] += 1 unless token == "status:deleted" || token =~ /\Alimit:.+\Z/ if token =~ /\A(#{METATAGS}):(.+)\Z/i - case $1.downcase + g1 = $1.downcase + g2 = $2 + case g1 when "-user" q[:uploader_id_neg] ||= [] - user_id = User.name_to_id($2) + user_id = User.name_to_id(g2) q[:uploader_id_neg] << user_id unless user_id.blank? when "user" - user_id = User.name_to_id($2) + user_id = User.name_to_id(g2) q[:uploader_id] = user_id unless user_id.blank? when "-approver" - if $2 == "none" + if g2 == "none" q[:approver_id] = "any" - elsif $2 == "any" + elsif g2 == "any" q[:approver_id] = "none" else q[:approver_id_neg] ||= [] - user_id = User.name_to_id($2) + user_id = User.name_to_id(g2) q[:approver_id_neg] << user_id unless user_id.blank? end when "approver" - if $2 == "none" + if g2 == "none" q[:approver_id] = "none" - elsif $2 == "any" + elsif g2 == "any" q[:approver_id] = "any" else - user_id = User.name_to_id($2) + user_id = User.name_to_id(g2) q[:approver_id] = user_id unless user_id.blank? end when "flagger" q[:flagger_ids] ||= [] - if $2 == "none" + if g2 == "none" q[:flagger_ids] << "none" - elsif $2 == "any" + elsif g2 == "any" q[:flagger_ids] << "any" else - user_id = User.name_to_id($2) + user_id = User.name_to_id(g2) q[:flagger_ids] << user_id unless user_id.blank? end when "-flagger" - if $2 == "none" + if g2 == "none" q[:flagger_ids] ||= [] q[:flagger_ids] << "any" - elsif $2 == "any" + elsif g2 == "any" q[:flagger_ids] ||= [] q[:flagger_ids] << "none" else q[:flagger_ids_neg] ||= [] - user_id = User.name_to_id($2) + user_id = User.name_to_id(g2) q[:flagger_ids_neg] << user_id unless user_id.blank? end when "appealer" q[:appealer_ids] ||= [] - if $2 == "none" + if g2 == "none" q[:appealer_ids] << "none" - elsif $2 == "any" + elsif g2 == "any" q[:appealer_ids] << "any" else - user_id = User.name_to_id($2) + user_id = User.name_to_id(g2) q[:appealer_ids] << user_id unless user_id.blank? end when "-appealer" - if $2 == "none" + if g2 == "none" q[:appealer_ids] ||= [] q[:appealer_ids] << "any" - elsif $2 == "any" + elsif g2 == "any" q[:appealer_ids] ||= [] q[:appealer_ids] << "none" else q[:appealer_ids_neg] ||= [] - user_id = User.name_to_id($2) + user_id = User.name_to_id(g2) q[:appealer_ids_neg] << user_id unless user_id.blank? end when "commenter", "comm" q[:commenter_ids] ||= [] - if $2 == "none" + if g2 == "none" q[:commenter_ids] << "none" - elsif $2 == "any" + elsif g2 == "any" q[:commenter_ids] << "any" else - user_id = User.name_to_id($2) + user_id = User.name_to_id(g2) q[:commenter_ids] << user_id unless user_id.blank? end when "noter" q[:noter_ids] ||= [] - if $2 == "none" + if g2 == "none" q[:noter_ids] << "none" - elsif $2 == "any" + elsif g2 == "any" q[:noter_ids] << "any" else - user_id = User.name_to_id($2) + user_id = User.name_to_id(g2) q[:noter_ids] << user_id unless user_id.blank? end when "noteupdater" q[:note_updater_ids] ||= [] - user_id = User.name_to_id($2) + user_id = User.name_to_id(g2) q[:note_updater_ids] << user_id unless user_id.blank? when "artcomm" q[:artcomm_ids] ||= [] - user_id = User.name_to_id($2) + user_id = User.name_to_id(g2) q[:artcomm_ids] << user_id unless user_id.blank? when "-pool" - if $2.downcase == "none" + if g2.downcase == "none" q[:pool] = "any" - elsif $2.downcase == "any" + elsif g2.downcase == "any" q[:pool] = "none" - elsif $2.downcase == "series" + elsif g2.downcase == "series" q[:tags][:exclude] << "pool:series" - elsif $2.downcase == "collection" + elsif g2.downcase == "collection" q[:tags][:exclude] << "pool:collection" else - q[:tags][:exclude] << "pool:#{Pool.name_to_id($2)}" + q[:tags][:exclude] << "pool:#{Pool.name_to_id(g2)}" end when "pool" - if $2.downcase == "none" + if g2.downcase == "none" q[:pool] = "none" - elsif $2.downcase == "any" + elsif g2.downcase == "any" q[:pool] = "any" - elsif $2.downcase == "series" + elsif g2.downcase == "series" q[:tags][:related] << "pool:series" - elsif $2.downcase == "collection" + elsif g2.downcase == "collection" q[:tags][:related] << "pool:collection" - elsif $2.include?("*") - pools = Pool.name_matches($2).select("id").limit(Danbooru.config.tag_query_limit).order("post_count DESC") + elsif g2.include?("*") + pools = Pool.name_matches(g2).select("id").limit(Danbooru.config.tag_query_limit).order("post_count DESC") q[:tags][:include] += pools.map {|pool| "pool:#{pool.id}"} else - q[:tags][:related] << "pool:#{Pool.name_to_id($2)}" + q[:tags][:related] << "pool:#{Pool.name_to_id(g2)}" end when "ordpool" - pool_id = Pool.name_to_id($2) + pool_id = Pool.name_to_id(g2) q[:tags][:related] << "pool:#{pool_id}" q[:ordpool] = pool_id when "-favgroup" - favgroup_id = FavoriteGroup.name_to_id($2) + favgroup_id = FavoriteGroup.name_to_id(g2) q[:favgroups_neg] ||= [] q[:favgroups_neg] << favgroup_id when "favgroup" - favgroup_id = FavoriteGroup.name_to_id($2) + favgroup_id = FavoriteGroup.name_to_id(g2) q[:favgroups] ||= [] q[:favgroups] << favgroup_id when "-fav" - q[:tags][:exclude] << "fav:#{User.name_to_id($2)}" + q[:tags][:exclude] << "fav:#{User.name_to_id(g2)}" when "fav" - q[:tags][:related] << "fav:#{User.name_to_id($2)}" + q[:tags][:related] << "fav:#{User.name_to_id(g2)}" when "ordfav" - user_id = User.name_to_id($2) + user_id = User.name_to_id(g2) q[:tags][:related] << "fav:#{user_id}" q[:ordfav] = user_id when "search" q[:saved_searches] ||= [] - q[:saved_searches] << $2 + q[:saved_searches] << g2 when "md5" - q[:md5] = $2.downcase.split(/,/) + q[:md5] = g2.downcase.split(/,/) when "-rating" - q[:rating_negated] = $2.downcase + q[:rating_negated] = g2.downcase when "rating" - q[:rating] = $2.downcase + q[:rating] = g2.downcase when "-locked" - q[:locked_negated] = $2.downcase + q[:locked_negated] = g2.downcase when "locked" - q[:locked] = $2.downcase + q[:locked] = g2.downcase when "id" - q[:post_id] = parse_helper($2) + q[:post_id] = parse_helper(g2) when "-id" - q[:post_id_negated] = $2.to_i + q[:post_id_negated] = g2.to_i when "width" - q[:width] = parse_helper($2) + q[:width] = parse_helper(g2) when "height" - q[:height] = parse_helper($2) + q[:height] = parse_helper(g2) when "mpixels" - q[:mpixels] = parse_helper_fudged($2, :float) + q[:mpixels] = parse_helper_fudged(g2, :float) when "ratio" - q[:ratio] = parse_helper($2, :ratio) + q[:ratio] = parse_helper(g2, :ratio) when "score" - q[:score] = parse_helper($2) + q[:score] = parse_helper(g2) when "favcount" - q[:fav_count] = parse_helper($2) + q[:fav_count] = parse_helper(g2) when "filesize" - q[:filesize] = parse_helper_fudged($2, :filesize) + q[:filesize] = parse_helper_fudged(g2, :filesize) when "source" - src = $2.gsub(/\A"(.*)"\Z/, '\1') + src = g2.gsub(/\A"(.*)"\Z/, '\1') q[:source] = (src.to_escaped_for_sql_like + "%").gsub(/%+/, '%') when "-source" - src = $2.gsub(/\A"(.*)"\Z/, '\1') + src = g2.gsub(/\A"(.*)"\Z/, '\1') q[:source_neg] = (src.to_escaped_for_sql_like + "%").gsub(/%+/, '%') when "date" - q[:date] = parse_helper($2, :date) + q[:date] = parse_helper(g2, :date) when "age" - q[:age] = reverse_parse_helper(parse_helper($2, :age)) + q[:age] = reverse_parse_helper(parse_helper(g2, :age)) when "tagcount" - q[:post_tag_count] = parse_helper($2) + q[:post_tag_count] = parse_helper(g2) - when "gentags" - q[:general_tag_count] = parse_helper($2) - - when "arttags" - q[:artist_tag_count] = parse_helper($2) - - when "chartags" - q[:character_tag_count] = parse_helper($2) - - when "copytags" - q[:copyright_tag_count] = parse_helper($2) + when /(#{TagCategory.short_name_regex})tags/ + q["#{TagCategory.short_name_mapping[$1]}_tag_count".to_sym] = parse_helper(g2) when "parent" - q[:parent] = $2.downcase + q[:parent] = g2.downcase when "-parent" - if $2.downcase == "none" + if g2.downcase == "none" q[:parent] = "any" - elsif $2.downcase == "any" + elsif g2.downcase == "any" q[:parent] = "none" else q[:parent_neg_ids] ||= [] - q[:parent_neg_ids] << $2.downcase + q[:parent_neg_ids] << g2.downcase end when "child" - q[:child] = $2.downcase + q[:child] = g2.downcase when "order" - q[:order] = $2.downcase + q[:order] = g2.downcase when "limit" # Do nothing. The controller takes care of it. when "-status" - q[:status_neg] = $2.downcase + q[:status_neg] = g2.downcase when "status" - q[:status] = $2.downcase + q[:status] = g2.downcase when "filetype" - q[:filetype] = $2.downcase + q[:filetype] = g2.downcase when "-filetype" - q[:filetype_neg] = $2.downcase + q[:filetype_neg] = g2.downcase when "pixiv_id", "pixiv" - q[:pixiv_id] = parse_helper($2) + q[:pixiv_id] = parse_helper(g2) when "upvote" if CurrentUser.user.is_moderator? - q[:upvote] = User.name_to_id($2) + q[:upvote] = User.name_to_id(g2) end when "downvote" if CurrentUser.user.is_moderator? - q[:downvote] = User.name_to_id($2) + q[:downvote] = User.name_to_id(g2) end end diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index 069384286..d9ca80ac1 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -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(" ") + TagCategory.categorized_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) diff --git a/app/presenters/tag_set_presenter.rb b/app/presenters/tag_set_presenter.rb index 9be7f6630..1751d8935 100644 --- a/app/presenters/tag_set_presenter.rb +++ b/app/presenters/tag_set_presenter.rb @@ -25,40 +25,16 @@ class TagSetPresenter < Presenter def split_tag_list_html(template, options = {}) html = "" - if copyright_tags.any? - html << '

Copyrights

' - html << "" - end - - if character_tags.any? - html << '

Characters

' - html << "" - end - - if artist_tags.any? - html << '

Artist

' - html << "" - end - - if general_tags.any? - html << '

Tags

' - html << "" 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] == TagCategory.mapping[name] + end + end end def categories diff --git a/app/views/layouts/default.html.erb b/app/views/layouts/default.html.erb index ca1e07620..968be757e 100644 --- a/app/views/layouts/default.html.erb +++ b/app/views/layouts/default.html.erb @@ -8,6 +8,8 @@ <% unless cookies[:dm] %> <% end %> + + diff --git a/app/views/posts/partials/show/_edit.html.erb b/app/views/posts/partials/show/_edit.html.erb index 4d8df1044..08db57ab6 100644 --- a/app/views/posts/partials/show/_edit.html.erb +++ b/app/views/posts/partials/show/_edit.html.erb @@ -77,10 +77,10 @@ <%= button_tag "Related tags", :id => "related-tags-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %> - <%= button_tag "General", :id => "related-general-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %> - <%= button_tag "Artists", :id => "related-artists-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %> - <%= button_tag "Characters", :id => "related-characters-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %> - <%= button_tag "Copyrights", :id => "related-copyrights-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %> + + <% TagCategory.related_button_list.each do |category| %> + <%= button_tag "#{TagCategory.related_button_mapping[category]}", :id => "related-#{category}-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %> + <% end %>
diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 603226be4..db4eecddd 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -155,6 +155,7 @@ <% end %> <% content_for(:html_header) do %> + diff --git a/app/views/related_tags/show.html.erb b/app/views/related_tags/show.html.erb index 07feb40af..f28adc71b 100644 --- a/app/views/related_tags/show.html.erb +++ b/app/views/related_tags/show.html.erb @@ -3,7 +3,7 @@
<%= form_tag(related_tag_path, :method => :get) do %> <%= text_field_tag "query", params[:query], :data => { :autocomplete => "tag" } %> - <%= select_tag "category", options_for_select([""] + Danbooru.config.canonical_tag_category_mapping.map{|x| [x.first, x.first.downcase]}, params[:category]) %> + <%= select_tag "category", options_for_select([""] + TagCategory.canonical_mapping.map{|x| [x.first, x.first.downcase]}, params[:category]) %> <%= submit_tag "Show"%> <% end %>
diff --git a/app/views/tag_aliases/index.html.erb b/app/views/tag_aliases/index.html.erb index 2f4b59f7c..d049118b5 100644 --- a/app/views/tag_aliases/index.html.erb +++ b/app/views/tag_aliases/index.html.erb @@ -3,7 +3,7 @@ <%= simple_form_for(:search, method: :get, url: tag_aliases_path, defaults: { required: false }, html: { class: "inline-form" }) do |f| %> <%= f.input :name_matches, label: "Name", input_html: { value: params[:search][:name_matches], data: { autocomplete: "tag" } } %> <%= f.input :status, label: "Status", collection: ["", "Approved", "Pending"], selected: params[:search][:status] %> - <%= f.input :category, label: "Category", collection: Danbooru.config.canonical_tag_category_mapping.to_a, include_blank: true, selected: params[:search][:category] %> + <%= f.input :category, label: "Category", collection: TagCategory.canonical_mapping.to_a, include_blank: true, selected: params[:search][:category] %> <%= f.input :order, label: "Order", collection: [%w[Status status], %w[Recently\ created created_at], %w[Recently\ updated updated_at], %w[Name name], %w[Tag\ count tag_count]], selected: params[:search][:order] %> <%= f.submit "Search" %> <% end %> diff --git a/app/views/tag_implications/index.html.erb b/app/views/tag_implications/index.html.erb index fb983ea2e..d0d4ff32f 100644 --- a/app/views/tag_implications/index.html.erb +++ b/app/views/tag_implications/index.html.erb @@ -3,7 +3,7 @@ <%= simple_form_for(:search, method: :get, url: tag_implications_path, defaults: { required: false }, html: { class: "inline-form" }) do |f| %> <%= f.input :name_matches, label: "Name", input_html: { value: params[:search][:name_matches], data: { autocomplete: "tag" } } %> <%= f.input :status, label: "Status", collection: ["", "Approved", "Pending"], selected: params[:search][:status] %> - <%= f.input :category, label: "Category", collection: Danbooru.config.canonical_tag_category_mapping.to_a, include_blank: true, selected: params[:search][:category] %> + <%= f.input :category, label: "Category", collection: TagCategory.canonical_mapping.to_a, include_blank: true, selected: params[:search][:category] %> <%= f.input :order, label: "Order", collection: [%w[Status status], %w[Recently\ created created_at], %w[Recently\ updated updated_at], %w[Name name], %w[Tag\ count tag_count]], selected: params[:search][:order] %> <%= f.submit "Search" %> <% end %> diff --git a/app/views/tags/_search.html.erb b/app/views/tags/_search.html.erb index e30e8208f..ff8100fec 100644 --- a/app/views/tags/_search.html.erb +++ b/app/views/tags/_search.html.erb @@ -1,6 +1,6 @@ <%= simple_form_for(:search, url: tags_path, method: :get, defaults: { required: false }, html: { class: "inline-form" }) do |f| %> <%= f.input :name_matches, label: "Name", hint: "Use * for wildcard", input_html: { value: params[:search][:name_matches], data: { autocomplete: "tag" } } %> - <%= f.input :category, label: "Category", collection: Danbooru.config.canonical_tag_category_mapping.to_a, include_blank: true,selected: params[:search][:category] %> + <%= f.input :category, label: "Category", collection: TagCategory.canonical_mapping.to_a, include_blank: true,selected: params[:search][:category] %> <%= f.input :order, collection: [%w[Newest date], %w[Count count], %w[Name name]], include_blank: false, selected: params[:search][:order] %> <%= f.input :hide_empty, label: "Hide empty?", collection: %w[yes no], selected: params[:search][:hide_empty] %> <%= f.input :has_wiki, label: "Has wiki?", collection: %w[yes no], include_blank: true, selected: params[:search][:has_wiki] %> diff --git a/app/views/tags/edit.html.erb b/app/views/tags/edit.html.erb index 3116df1df..744f1c829 100644 --- a/app/views/tags/edit.html.erb +++ b/app/views/tags/edit.html.erb @@ -6,7 +6,7 @@ <% if @tag.is_locked? %>

This tag is category locked

<% else %> - <%= f.input :category, :collection => Danbooru.config.canonical_tag_category_mapping.to_a, :include_blank => false %> + <%= f.input :category, :collection => TagCategory.canonical_mapping.to_a, :include_blank => false %> <% end %> <% if CurrentUser.is_moderator? %> diff --git a/app/views/uploads/new.html.erb b/app/views/uploads/new.html.erb index 4413c9b4b..1ab2854f7 100644 --- a/app/views/uploads/new.html.erb +++ b/app/views/uploads/new.html.erb @@ -109,10 +109,10 @@
<%= button_tag "Related tags", :id => "related-tags-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %> - <%= button_tag "General", :id => "related-general-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %> - <%= button_tag "Artists", :id => "related-artists-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %> - <%= button_tag "Characters", :id => "related-characters-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %> - <%= button_tag "Copyrights", :id => "related-copyrights-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %> + + <% TagCategory.related_button_list.each do |category| %> + <%= button_tag "#{TagCategory.related_button_mapping[category]}", :id => "related-#{category}-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %> + <% end %>
@@ -141,4 +141,8 @@ Upload - <%= Danbooru.config.app_name %> <% end %> +<% content_for(:html_header) do %> + +<% end %> + <%= render "posts/partials/common/secondary_links" %> diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index 3b2e2710e..9aeba9ec2 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -214,46 +214,93 @@ module Danbooru "albert" end - # Returns a hash mapping various tag categories to a numerical value. - # Be sure to update the reverse_tag_category_mapping also. - def tag_category_mapping - @tag_category_mapping ||= { - "general" => 0, - "gen" => 0, +#TAG CONFIGURATION - "artist" => 1, - "art" => 1, - - "copyright" => 3, - "copy" => 3, - "co" => 3, - - "character" => 4, - "char" => 4, - "ch" => 4 + #Full tag configuration info for all tags + def full_tag_config_info + @full_tag_category_mapping ||= { + "general" => { + "category" => 0, + "short" => "gen", + "extra" => [], + "header" => "

Tags

", + "humanized" => nil, + "relatedbutton" => "General" + }, + "character" => { + "category" => 4, + "short" => "char", + "extra" => ["ch"], + "header" => "

Characters

", + "humanized" => { + "slice" => 5, + "exclusion" => [], + "regexmap" => /^(.+?)(?:_\(.+\))?$/, + "formatstr" => "%s" + }, + "relatedbutton" => "Characters" + }, + "copyright" => { + "category" => 3, + "short" => "copy", + "extra" => ["co"], + "header" => "

Copyrights

", + "humanized" => { + "slice" => 5, + "exclusion" => [], + "regexmap" => //, + "formatstr" => "(%s)" + }, + "relatedbutton" => "Copyrights" + }, + "artist" => { + "category" => 1, + "short" => "art", + "extra" => [], + "header" => "

Artist

", + "humanized" => { + "slice" => 0, + "exclusion" => %w(banned_artist), + "regexmap" => //, + "formatstr" => "drawn by %s" + }, + "relatedbutton" => "Artists" + }, + "meta" => { + "category" => 5, + "short" => "meta", + "extra" => [], + "header" => "

Meta

", + "humanized" => nil, + "relatedbutton" => nil + } } end - def canonical_tag_category_mapping - @canonical_tag_category_mapping ||= { - "General" => 0, - "Artist" => 1, - "Copyright" => 3, - "Character" => 4 - } +#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 - # Returns a hash maping numerical category values to their - # string equivalent. Be sure to update the tag_category_mapping also. - def reverse_tag_category_mapping - @reverse_tag_category_mapping ||= { - 0 => "General", - 1 => "Artist", - 3 => "Copyright", - 4 => "Character" - } + #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","meta"] end + #Sets the order of the categorized tag string (presenters/post_presenter.rb) + def categorized_tag_list + @categorized_tag_list ||= ["copyright","character","artist","meta","general"] + end + + #Sets the order of the related tag buttons (javascripts/related_tag.js) + def related_tag_button_list + @related_tag_button_list ||= ["general","artist","character","copyright"] + end + +#END TAG + # If enabled, users must verify their email addresses. def enable_email_verification? false diff --git a/db/migrate/20171106075030_add_tag_count_meta_to_posts.rb b/db/migrate/20171106075030_add_tag_count_meta_to_posts.rb new file mode 100644 index 000000000..c82afaf66 --- /dev/null +++ b/db/migrate/20171106075030_add_tag_count_meta_to_posts.rb @@ -0,0 +1,7 @@ +class AddTagCountMetaToPosts < ActiveRecord::Migration + def change + Post.without_timeout do + add_column :posts, :tag_count_meta, :integer, default: 0, null: false + end + end +end diff --git a/db/structure.sql b/db/structure.sql index d2010f071..5796ea19d 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2776,7 +2776,8 @@ CREATE TABLE posts ( pixiv_id integer, last_commented_at timestamp without time zone, has_active_children boolean DEFAULT false, - bit_flags bigint DEFAULT 0 NOT NULL + bit_flags bigint DEFAULT 0 NOT NULL, + tag_count_meta integer DEFAULT 0 NOT NULL ); @@ -7518,3 +7519,5 @@ INSERT INTO schema_migrations (version) VALUES ('20170709190409'); INSERT INTO schema_migrations (version) VALUES ('20170914200122'); +INSERT INTO schema_migrations (version) VALUES ('20171106075030'); + diff --git a/test/unit/tag_test.rb b/test/unit/tag_test.rb index 3d9778f90..6e4a1f32b 100644 --- a/test/unit/tag_test.rb +++ b/test/unit/tag_test.rb @@ -61,6 +61,7 @@ class TagTest < ActiveSupport::TestCase assert_equal(1, Tag.categories.artist) assert_equal(3, Tag.categories.copyright) assert_equal(4, Tag.categories.character) + assert_equal(5, Tag.categories.meta) end should "have a regular expression for matching category names and shortcuts" do @@ -74,6 +75,7 @@ class TagTest < ActiveSupport::TestCase assert_match(regexp, "character") assert_match(regexp, "char") assert_match(regexp, "ch") + assert_match(regexp, "meta") assert_no_match(regexp, "c") assert_no_match(regexp, "woodle") end @@ -83,6 +85,7 @@ class TagTest < ActiveSupport::TestCase assert_equal(0, Tag.categories.value_for("gen")) assert_equal(1, Tag.categories.value_for("artist")) assert_equal(1, Tag.categories.value_for("art")) + assert_equal(5, Tag.categories.value_for("meta")) assert_equal(0, Tag.categories.value_for("unknown")) end end @@ -215,7 +218,7 @@ class TagTest < ActiveSupport::TestCase should_not allow_value("café").for(:name).on(:create) should_not allow_value("東方").for(:name).on(:create) - metatags = Tag::METATAGS.split("|") + Tag::SUBQUERY_METATAGS.split("|") + Danbooru.config.tag_category_mapping.keys + metatags = Tag::METATAGS.split("|") + Tag::SUBQUERY_METATAGS.split("|") + TagCategory.mapping.keys metatags.split("|").each do |metatag| should_not allow_value("#{metatag}:foo").for(:name).on(:create) end