javaeye3/script/check_harmony.rb

129 lines
5.6 KiB
Ruby

#!/usr/bin/env ruby
ENV['RAILS_ENV'] = ARGV[0] || 'production'
require File.dirname(__FILE__) + '/../config/environment'
HIGH_SENSITIVE = Regexp.new(IO.readlines("#{RAILS_ROOT}/data/high_sensitive_words.txt").reject{ |l| l.match(/^\s*$/) }.map{ |l| Regexp.escape(l.strip) }.join('|'), Regexp::IGNORECASE)
LOW_SENSITIVE = Regexp.new(IO.readlines("#{RAILS_ROOT}/data/low_sensitive_words.txt").reject{ |l| l.match(/^\s*$/) }.map{ |l| Regexp.escape(l.strip) }.join('|'), Regexp::IGNORECASE)
# check topic
topics_count = Topic.count(:conditions => "status_flag <> 'hidden' and status_flag <> 'delete' and status_flag <> 'draft' and forum_id is not null")
(0..topics_count/10000).each do |i|
topics = Topic.find(:all, :select => 'topics.id, topics.title, topics.post_id', :conditions => "status_flag <> 'hidden' and status_flag <> 'delete' and status_flag <> 'draft' and forum_id is not null",
:order => "id DESC", :limit => 10000, :offset => i * 10000)
Topic.transaction do
topics.each do |topic|
if match = HIGH_SENSITIVE.match(topic.title) || match = HIGH_SENSITIVE.match(topic.body) rescue nil
STDOUT.puts "http://www.iteye.com/topic/#{topic.id}\t\t#{match[0]}"
topic.posts.each {|p| p.post_text.destroy; p.destroy}; topic.destroy
elsif match = LOW_SENSITIVE.match(topic.title) rescue nil
topic.title.gsub!(LOW_SENSITIVE, "")
topic.save!
elsif match = LOW_SENSITIVE.match(topic.body) rescue nil
topic.post.post_text.body = topic.body.gsub(LOW_SENSITIVE, "")
topic.post.post_text.save!
end
end
end
end
# check blog
topics_count = Blog.count(:conditions => "status_flag <> 'hidden' and status_flag <> 'delete' and status_flag <> 'draft' and forum_id is null")
(0..topics_count/10000).each do |i|
topics = Topic.find(:all, :select => 'topics.id, topics.title, topics.post_id, topics.user_id', :conditions => "status_flag <> 'hidden' and status_flag <> 'delete' and status_flag <> 'draft' and forum_id is null",
:order => "id DESC", :limit => 10000, :offset => i * 10000)
Topic.transaction do
topics.each do |topic|
if match = HIGH_SENSITIVE.match(topic.title) || match = HIGH_SENSITIVE.match(topic.body) rescue nil
STDOUT.puts "http://#{topic.user.domain}.iteye.com/blog/#{topic.id}\t\t#{match[0]}"
topic.posts.each {|p| p.post_text.destroy; p.destroy}; topic.destroy
elsif match = LOW_SENSITIVE.match(topic.title) rescue nil
topic.title.gsub!(LOW_SENSITIVE, "")
topic.save!
elsif match = LOW_SENSITIVE.match(topic.body) rescue nil
topic.post.post_text.body = topic.body.gsub(LOW_SENSITIVE, "")
topic.post.post_text.save!
end
end
end
end
# check blogs' guest books
ActiveRecord::Base.transaction do
GuestBook.find(:all).each do |guest|
if match = HIGH_SENSITIVE.match(guest.body) rescue nil
guest.destroy
end
end
end
# check all posts
posts_count = Post.count
(0..posts_count/10000).each do |i|
posts = Post.find :all, :order => 'id DESC', :limit => 10000, :offset => i*10000
posts.each do |post|
STDOUT.puts "#{post.id.to_s}"
if match = HIGH_SENSITIVE.match(post.body) rescue nil
STDOUT.puts "#{post.id.to_s} -------> delete"
ActiveRecord::Base.transaction { PostText.delete(post.post_text_id); post.expire_cache; Post.delete(post.id) }
elsif match = LOW_SENSITIVE.match(topic.body) rescue nil
post.post_text.body = post.body.gsub(LOW_SENSITIVE, "")
post.post_text.save!
end
end
end
# fix last post id for topic
ts = Topic.find :all, :select => "t.*", :joins => "t LEFT JOIN posts p ON t.last_post_id = p.id", :conditions => "t.last_post_id IS NOT NULL AND p.id IS NULL"
Topic.transaction do
ts.each do |topic|
p = Post.find :first, :order => "id DESC", :conditions => ["status_flag<>'hidden' AND topic_id = ?", topic.id]
topic.last_post_id = p.id
topic.save!
end
end
# check group topic
topics_count = GroupTopic.count()
(0..topics_count/10000).each do |i|
topics = GroupTopic.find(:all, :select => 'group_topics.id, group_topics.title, group_topics.post_id, group_topics.group_id', :order => "id DESC", :limit => 10000, :offset => i * 10000)
GroupTopic.transaction do
topics.each do |topic|
if match = HIGH_SENSITIVE.match(topic.title) || match = HIGH_SENSITIVE.match(topic.body) rescue nil
STDOUT.puts "http://#{topic.group.domain}.group.iteye.com/group/topic/#{topic.id}\t\t#{match[0]}"
topic.destroy
elsif match = LOW_SENSITIVE.match(topic.title) rescue nil
topic.title.gsub!(LOW_SENSITIVE, "")
topic.save!
elsif match = LOW_SENSITIVE.match(topic.body) rescue nil
topic.post.body = topic.body.gsub(LOW_SENSITIVE, "")
topic.post.save!
end
end
end
end
# check all group posts
posts_count = GroupPost.count
(0..posts_count/10000).each do |i|
posts = GroupPost.find :all, :order => 'id DESC', :limit => 10000, :offset => i*10000
posts.each do |post|
STDOUT.puts "#{post.id.to_s}"
if match = HIGH_SENSITIVE.match(post.body) rescue nil
STDOUT.puts "#{post.id.to_s} -------> delete"
post.destroy
elsif match = LOW_SENSITIVE.match(topic.body) rescue nil
post.body.gsub!(LOW_SENSITIVE, "")
post.save!
end
end
end
# fix last post id for group_topic
ts = GroupTopic.find :all, :select => "t.*", :joins => "t LEFT JOIN group_posts p ON t.last_post_id = p.id", :conditions => "t.last_post_id IS NOT NULL AND p.id IS NULL"
GroupTopic.transaction do
ts.each do |topic|
p = GroupPost.find :first, :order => "id DESC", :conditions => ["topic_id = ?", topic.id]
topic.last_post_id = p.id
topic.save!
end
end