A platform for community discussion. Free, open, simple.
Add optional "ignore_case" parameter to posts:remap rake task
Gerhard Schlager409ee6683919
diff --git a/app/models/post.rb b/app/models/post.rbindex 3699779ffc4..2a74e880c2b 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -106,7 +106,7 @@ class Post < ActiveRecord::Basewhen 'string'where('raw ILIKE ?', "%#{pattern}%")when 'regex'- where('raw ~ ?', "(?n)#{pattern}")+ where('raw ~* ?', "(?n)#{pattern}")end}diff --git a/lib/tasks/posts.rake b/lib/tasks/posts.rakeindex e00517abe1b..bb5c116b1eb 100644--- a/lib/tasks/posts.rake+++ b/lib/tasks/posts.rake@@ -133,15 +133,17 @@ task 'posts:normalize_code' => :environment doputs "#{i} posts normalized!"end-def remap_posts(find, type, replace = "")+def remap_posts(find, type, ignore_case, replace = "")+ ignore_case = ignore_case == 'true'i = 0Post.raw_match(find, type).find_each do |p|- new_raw =- case type- when 'string' then p.raw.gsub(/#{Regexp.escape(find)}/, replace)- when 'regex' then p.raw.gsub(/#{find}/, replace)- end+ regex = case type+ when 'string' then Regexp.new(Regexp.escape(find), ignore_case)+ when 'regex' then Regexp.new(find, ignore_case)+ end++ new_raw = p.raw.gsub(regex, replace)if new_raw != p.rawbegin@@ -158,13 +160,14 @@ def remap_posts(find, type, replace = "")enddesc 'Remap all posts matching specific string'-task 'posts:remap', [:find, :replace, :type] => [:environment] do |_, args|+task 'posts:remap', [:find, :replace, :type, :ignore_case] => [:environment] do |_, args|require 'highline/import'- args.with_defaults(type: 'string')+ args.with_defaults(type: 'string', ignore_case: 'false')find = args[:find]replace = args[:replace]type = args[:type]&.downcase+ ignore_case = args[:ignore_case]&.downcaseif !findputs "ERROR: Expecting rake posts:remap['find','replace']"@@ -173,7 +176,10 @@ task 'posts:remap', [:find, :replace, :type] => [:environment] do |_, args|puts "ERROR: Expecting rake posts:remap['find','replace']. Want to delete a word/string instead? Try rake posts:delete_word['word-to-delete']"exit 1elsif type != 'string' && type != 'regex'- puts "ERROR: Expecting rake posts:delete_word[pattern, type] where type is string or regex"+ puts "ERROR: Expecting rake posts:remap['find','replace',type] where type is string or regex"+ exit 1+ elsif ignore_case != 'true' && ignore_case != 'false'+ puts "ERROR: Expecting rake posts:remap['find','replace',type,ignore_case] where ignore_case is true or false"exit 1elseconfirm_replace = ask("Are you sure you want to replace all #{type} occurrences of '#{find}' with '#{replace}'? (Y/n)")@@ -181,17 +187,18 @@ task 'posts:remap', [:find, :replace, :type] => [:environment] do |_, args|endputs "Remapping"- total = remap_posts(find, type, replace)+ total = remap_posts(find, type, ignore_case, replace)puts "", "#{total} posts remapped!", ""enddesc 'Delete occurrence of a word/string'-task 'posts:delete_word', [:find, :type] => [:environment] do |_, args|+task 'posts:delete_word', [:find, :type, :ignore_case] => [:environment] do |_, args|require 'highline/import'- args.with_defaults(type: 'string')+ args.with_defaults(type: 'string', ignore_case: 'false')find = args[:find]type = args[:type]&.downcase+ ignore_case = args[:ignore_case]&.downcaseif !findputs "ERROR: Expecting rake posts:delete_word['word-to-delete']"@@ -199,13 +206,16 @@ task 'posts:delete_word', [:find, :type] => [:environment] do |_, args|elsif type != 'string' && type != 'regex'puts "ERROR: Expecting rake posts:delete_word[pattern, type] where type is string or regex"exit 1+ elsif ignore_case != 'true' && ignore_case != 'false'+ puts "ERROR: Expecting rake posts:delete_word[pattern, type,ignore_case] where ignore_case is true or false"+ exit 1elseconfirm_delete = ask("Are you sure you want to remove all #{type} occurrences of '#{find}'? (Y/n)")exit 1 unless (confirm_delete == "" || confirm_delete.downcase == 'y')endputs "Processing"- total = remap_posts(find, type)+ total = remap_posts(find, type, ignore_case)puts "", "#{total} posts updated!", ""end