mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 03:10:46 -06:00
092eeb5ca3
Created a rake task for destroying multiple categories along with any subcategories and topics the belong to those categories. Also created a rake task for listing all of your categories. Refactored existing destroy rake tasks to use new logging method, that allows for puts output in the console but prevents it from showing in the specs.
130 lines
4.4 KiB
Ruby
130 lines
4.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class DestroyTask
|
|
|
|
def initialize(io = $stdout)
|
|
@io = io
|
|
end
|
|
|
|
def destroy_topics(category, parent_category = nil, delete_system_topics = false)
|
|
c = Category.find_by_slug(category, parent_category)
|
|
descriptive_slug = parent_category ? "#{parent_category}/#{category}" : category
|
|
return @io.puts "A category with the slug: #{descriptive_slug} could not be found" if c.nil?
|
|
if delete_system_topics
|
|
topics = Topic.where(category_id: c.id, pinned_at: nil)
|
|
else
|
|
topics = Topic.where(category_id: c.id, pinned_at: nil).where.not(user_id: -1)
|
|
end
|
|
@io.puts "There are #{topics.count} topics to delete in #{descriptive_slug} category"
|
|
topics.each do |topic|
|
|
@io.puts "Deleting #{topic.slug}..."
|
|
first_post = topic.ordered_posts.first
|
|
if first_post.nil?
|
|
return @io.puts "Topic.ordered_posts.first was nil"
|
|
end
|
|
system_user = User.find(-1)
|
|
@io.puts PostDestroyer.new(system_user, first_post).destroy
|
|
end
|
|
end
|
|
|
|
def destroy_topics_in_category(category_id, delete_system_topics = false)
|
|
c = Category.find(category_id)
|
|
return @io.puts "A category with the id: #{category_id} could not be found" if c.nil?
|
|
if delete_system_topics
|
|
topics = Topic.where(category_id: c.id, pinned_at: nil)
|
|
else
|
|
topics = Topic.where(category_id: c.id, pinned_at: nil).where.not(user_id: -1)
|
|
end
|
|
@io.puts "There are #{topics.count} topics to delete in #{c.slug} category"
|
|
topics.each do |topic|
|
|
first_post = topic.ordered_posts.first
|
|
return @io.puts "Topic.ordered_posts.first was nil for topic: #{topic.id}" if first_post.nil?
|
|
system_user = User.find(-1)
|
|
PostDestroyer.new(system_user, first_post).destroy
|
|
end
|
|
topics = Topic.where(category_id: c.id, pinned_at: nil)
|
|
@io.puts "There are #{topics.count} topics that could not be deleted in #{c.slug} category"
|
|
end
|
|
|
|
def destroy_topics_all_categories
|
|
categories = Category.all
|
|
categories.each do |c|
|
|
@io.puts destroy_topics(c.slug, c.parent_category&.slug)
|
|
end
|
|
end
|
|
|
|
def destroy_private_messages
|
|
pms = Topic.where(archetype: "private_message")
|
|
current_user = User.find(-1) #system
|
|
pms.each do |pm|
|
|
@io.puts "Destroying #{pm.slug} pm"
|
|
first_post = pm.ordered_posts.first
|
|
@io.puts PostDestroyer.new(current_user, first_post).destroy
|
|
end
|
|
end
|
|
|
|
def destroy_category(category_id, destroy_system_topics = false)
|
|
c = Category.find_by_id(category_id)
|
|
return @io.puts "A category with the id: #{category_id} could not be found" if c.nil?
|
|
subcategories = Category.where(parent_category_id: c.id).pluck(:id)
|
|
@io.puts "There are #{subcategories.count} subcategories to delete" if subcategories
|
|
subcategories.each do |subcategory_id|
|
|
s = Category.find_by_id(subcategory_id)
|
|
category_topic_destroyer(s, destroy_system_topics)
|
|
end
|
|
category_topic_destroyer(c, destroy_system_topics)
|
|
end
|
|
|
|
def destroy_groups
|
|
groups = Group.where(automatic: false)
|
|
groups.each do |group|
|
|
@io.puts "destroying group: #{group.id}"
|
|
@io.puts group.destroy
|
|
end
|
|
end
|
|
|
|
def destroy_users
|
|
users = User.where(admin: false, id: 1..Float::INFINITY)
|
|
@io.puts "There are #{users.count} users to delete"
|
|
options = {}
|
|
options[:delete_posts] = true
|
|
current_user = User.find(-1) #system
|
|
users.each do |user|
|
|
begin
|
|
if UserDestroyer.new(current_user).destroy(user, options)
|
|
@io.puts "#{user.username} deleted"
|
|
else
|
|
@io.puts "#{user.username} not deleted"
|
|
end
|
|
rescue UserDestroyer::PostsExistError
|
|
raise Discourse::InvalidAccess.new("User #{user.username} has #{user.post_count} posts, so can't be deleted.")
|
|
rescue NoMethodError
|
|
@io.puts "#{user.username} could not be deleted"
|
|
end
|
|
end
|
|
end
|
|
|
|
def destroy_stats
|
|
ApplicationRequest.destroy_all
|
|
IncomingLink.destroy_all
|
|
UserVisit.destroy_all
|
|
UserProfileView.destroy_all
|
|
user_profiles = UserProfile.all
|
|
user_profiles.each do |user_profile|
|
|
user_profile.views = 0
|
|
user_profile.save!
|
|
end
|
|
PostAction.unscoped.destroy_all
|
|
EmailLog.destroy_all
|
|
end
|
|
|
|
private
|
|
|
|
def category_topic_destroyer(category, destroy_system_topics = false)
|
|
destroy_topics_log = destroy_topics_in_category(category.id, destroy_system_topics)
|
|
@io.puts "Destroying #{category.slug} category"
|
|
category.destroy
|
|
end
|
|
|
|
end
|