From 35834a9c406c0cbe4d56a2443a2ea625a6ce1298 Mon Sep 17 00:00:00 2001
From: Neil Lalonde <neillalonde@gmail.com>
Date: Wed, 4 Jun 2014 10:37:43 -0400
Subject: [PATCH] Import scripts: add method close_inactive_topics

---
 script/import_scripts/base.rb | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/script/import_scripts/base.rb b/script/import_scripts/base.rb
index 7a1f7e51dd0..2db79eeee63 100644
--- a/script/import_scripts/base.rb
+++ b/script/import_scripts/base.rb
@@ -239,6 +239,21 @@ class ImportScripts::Base
     Post.exec_sql("update topics t set bumped_at = (select max(created_at) from posts where topic_id = t.id)")
   end
 
+  def close_inactive_topics(opts={})
+    num_days = opts[:days] || 30
+    puts '', "Closing topics that have been inactive for more than #{num_days} days."
+
+    query = Topic.where('last_posted_at < ?', num_days.days.ago).where(closed: false)
+    total_count = query.count
+    closed_count = 0
+
+    query.find_each do |topic|
+      topic.update_status('closed', true, Discourse.system_user)
+      closed_count += 1
+      print_status(closed_count, total_count)
+    end
+  end
+
   def print_status(current, max)
     print "\r%9d / %d (%5.1f%%)    " % [current, max, ((current.to_f / max.to_f) * 100).round(1)]
   end