diff --git a/app/controllers/tag_corrections_controller.rb b/app/controllers/tag_corrections_controller.rb deleted file mode 100644 index 21b9c82e6..000000000 --- a/app/controllers/tag_corrections_controller.rb +++ /dev/null @@ -1,25 +0,0 @@ -class TagCorrectionsController < ApplicationController - respond_to :html, :json, :xml - before_action :builder_only, only: [:new, :create] - - def new - @correction = TagCorrection.new(params[:tag_id]) - respond_with(@correction) - end - - def show - @correction = TagCorrection.new(params[:tag_id]) - respond_with(@correction) - end - - def create - @correction = TagCorrection.new(params[:tag_id]) - - if params[:commit] == "Fix" - @correction.fix! - redirect_to tags_path(:search => {:name_matches => @correction.tag.name, :hide_empty => "no"}), :notice => "Tag will be fixed in a few seconds" - else - redirect_to tags_path(:search => {:name_matches => @correction.tag.name}) - end - end -end diff --git a/app/jobs/fix_tag_post_count_job.rb b/app/jobs/fix_tag_post_count_job.rb deleted file mode 100644 index 3f6ab4ad7..000000000 --- a/app/jobs/fix_tag_post_count_job.rb +++ /dev/null @@ -1,8 +0,0 @@ -class FixTagPostCountJob < ApplicationJob - queue_as :default - queue_with_priority 20 - - def perform(tag) - tag.fix_post_count - end -end diff --git a/app/logical/danbooru_maintenance.rb b/app/logical/danbooru_maintenance.rb index 5ab771b2e..4a6e62232 100644 --- a/app/logical/danbooru_maintenance.rb +++ b/app/logical/danbooru_maintenance.rb @@ -15,7 +15,7 @@ module DanbooruMaintenance ForumSubscription.process_all! TagAlias.update_cached_post_counts_for_all PostDisapproval.dmail_messages! - Tag.clean_up_negative_post_counts! + regenerate_post_counts! SuperVoter.init! TokenBucket.prune! TagChangeRequestPruner.warn_all @@ -38,6 +38,13 @@ module DanbooruMaintenance rescue_exception(exception) end + def regenerate_post_counts! + updated_tags = Tag.regenerate_post_counts! + updated_tags.each do |tag| + DanbooruLogger.info("Updated tag count", tag.attributes) + end + end + def rescue_exception(exception) DanbooruLogger.log(exception) raise exception diff --git a/app/logical/tag_correction.rb b/app/logical/tag_correction.rb index 6b326a89e..343075ad0 100644 --- a/app/logical/tag_correction.rb +++ b/app/logical/tag_correction.rb @@ -4,14 +4,14 @@ class TagCorrection include ActiveModel::Serializers::Xml attr_reader :tag - delegate :category, :post_count, :real_post_count, to: :tag + delegate :category, :post_count, to: :tag def initialize(tag_id) @tag = Tag.find(tag_id) end def attributes - { post_count: post_count, real_post_count: real_post_count, category: category, category_cache: category_cache, tag: tag } + { post_count: post_count, category: category, category_cache: category_cache, tag: tag } end def category_cache @@ -19,7 +19,6 @@ class TagCorrection end def fix! - FixTagPostCountJob.perform_later(tag) tag.update_category_cache end end diff --git a/app/models/tag.rb b/app/models/tag.rb index bbe47aea9..db3ebd243 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -99,24 +99,26 @@ class Tag < ApplicationRecord Tag.where(:name => tag_names).update_all("post_count = post_count - 1") end - def clean_up_negative_post_counts! - Tag.where("post_count < 0").find_each do |tag| - tag_alias = TagAlias.where("status in ('active', 'processing') and antecedent_name = ?", tag.name).first - tag.fix_post_count - if tag_alias - tag_alias.consequent_tag.fix_post_count - end - end + def regenerate_post_counts! + sql = <<~SQL + UPDATE tags + SET post_count = true_count + FROM ( + SELECT tag, COUNT(*) AS true_count + FROM posts, unnest(string_to_array(tag_string, ' ')) AS tag + GROUP BY tag + ) true_counts, tags AS old_tags + WHERE + tags.name = tag + AND tags.post_count != true_count + AND old_tags.id = tags.id + RETURNING tags.*, old_tags.post_count AS old_post_count + SQL + + updated_tags = Tag.find_by_sql(sql) + updated_tags end end - - def real_post_count - @real_post_count ||= Post.raw_tag_match(name).where("true /* Tag#real_post_count */").count - end - - def fix_post_count - update_column(:post_count, real_post_count) - end end module CategoryMethods diff --git a/app/models/tag_alias.rb b/app/models/tag_alias.rb index ad3fe6b90..d50b9da23 100644 --- a/app/models/tag_alias.rb +++ b/app/models/tag_alias.rb @@ -145,9 +145,6 @@ class TagAlias < TagRelationship post.update(tag_string: fixed_tags) end end - - antecedent_tag.fix_post_count if antecedent_tag - consequent_tag.fix_post_count if consequent_tag end end diff --git a/app/views/tag_corrections/new.html.erb b/app/views/tag_corrections/new.html.erb deleted file mode 100644 index 1aee04715..000000000 --- a/app/views/tag_corrections/new.html.erb +++ /dev/null @@ -1,19 +0,0 @@ -
-
-

Fix Tag: <%= @correction.tag.name %>

- -
-
    -
  • category actual: <%= @correction.category %>
  • -
  • category memcache: <%= @correction.category_cache || "none" %>
  • -
  • post count db cache: <%= @correction.post_count %>
  • -
  • post count actual: <%= @correction.real_post_count %>
  • -
-
- - <%= form_tag(tag_correction_path(:tag_id => @correction.tag.id)) do %> - <%= submit_tag "Fix" %> - <%= submit_tag "Cancel" %> - <% end %> -
-
diff --git a/app/views/tags/_secondary_links.html.erb b/app/views/tags/_secondary_links.html.erb index 64f307ebc..eb6a1942f 100644 --- a/app/views/tags/_secondary_links.html.erb +++ b/app/views/tags/_secondary_links.html.erb @@ -9,8 +9,5 @@
  • |
  • <%= subnav_link_to "Posts (#{@tag.post_count})", posts_path(:tags => @tag.name) %> <%= subnav_link_to "Edit", edit_tag_path(@tag) %> - <% if @tag.post_count < 1_000 %> - <%= subnav_link_to "Fix", new_tag_correction_path(:tag_id => @tag.id) %> - <% end %> <% end %> <% end %> diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb index c09eaa3aa..8ab24c253 100644 --- a/app/views/tags/index.html.erb +++ b/app/views/tags/index.html.erb @@ -21,9 +21,6 @@ <% if tag.editable_by?(CurrentUser.user) %> <%= link_to "edit", edit_tag_path(tag) %> <% end %> - <% if CurrentUser.is_builder? %> - | <%= link_to "fix", new_tag_correction_path(:tag_id => tag.id) %> - <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 28549d9b2..9982bc794 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -260,7 +260,6 @@ Rails.application.routes.draw do end resource :source, :only => [:show] resources :tags do - resource :correction, :only => [:new, :create, :show], :controller => "tag_corrections" collection do get :autocomplete end diff --git a/public/robots.txt b/public/robots.txt index 94a3b8b64..84e8d0635 100644 --- a/public/robots.txt +++ b/public/robots.txt @@ -41,7 +41,6 @@ Disallow: /static Disallow: /tag_alias_corrections Disallow: /tag_alias_requests Disallow: /tag_aliases -Disallow: /tag_corrections Disallow: /tag_implication_requests Disallow: /tag_implications Disallow: /tags @@ -51,4 +50,4 @@ Disallow: /users Disallow: /wiki_page_versions Disallow: /wiki_pages/new Disallow: /wiki_pages/revert -Disallow: /wiki_pages/show_or_new \ No newline at end of file +Disallow: /wiki_pages/show_or_new diff --git a/test/unit/tag_test.rb b/test/unit/tag_test.rb index cc68a799d..e83da3942 100644 --- a/test/unit/tag_test.rb +++ b/test/unit/tag_test.rb @@ -274,13 +274,15 @@ class TagTest < ActiveSupport::TestCase end end - context "A tag with a negative post count" do + context "A tag with an incorrect post count" do should "be fixed" do - tag = FactoryBot.create(:tag, name: "touhou", post_count: -10) - post = FactoryBot.create(:post, tag_string: "touhou") + tag1 = FactoryBot.create(:tag, name: "touhou", post_count: -10) + tag2 = FactoryBot.create(:tag, name: "bkub", post_count: 10) + post = FactoryBot.create(:post, tag_string: "touhou bkub") - Tag.clean_up_negative_post_counts! - assert_equal(1, tag.reload.post_count) + Tag.regenerate_post_counts! + assert_equal(1, tag1.reload.post_count) + assert_equal(1, tag2.reload.post_count) end end end