Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script: Replace missing tags with other missing tags #1074

Closed
1 task done
jonkerz opened this issue Jul 2, 2020 · 1 comment
Closed
1 task done

Script: Replace missing tags with other missing tags #1074

jonkerz opened this issue Jul 2, 2020 · 1 comment
Labels

Comments

@jonkerz
Copy link
Member

jonkerz commented Jul 2, 2020

For #35

BATCH = 1
EDIT_SUMMARY = "Replace `missing` tags with other `missing` tags [batch #{BATCH}], see %github1074"
ANTCATBOT = User.find_by!(name: 'AntCatBot')

# For activities and PaperTrail.
RequestStore.store[:current_request_uuid] = SecureRandom.uuid
PaperTrail.request.whodunnit = ANTCATBOT.id

MAX_REPLACE_IN_BATCH = 9999
$replaced_in_batch = 0

def replace_with_other_missing_tags! to_replace, replace_with
  escaped_to_replace = to_replace.dup
  escaped_to_replace = escaped_to_replace.gsub!('(', '\\(')
  escaped_to_replace = escaped_to_replace.gsub!(')', '\\)')

  items = TaxonHistoryItem.where("taxt REGEXP ?", escaped_to_replace)
  puts "#{to_replace}: #{items.count}"

  items.find_each do |item|
    old_taxt = item.taxt.dup

    new_taxt = item.taxt.dup
    new_taxt.gsub!(to_replace, replace_with)

    if old_taxt == new_taxt
      puts "nothing was changed: #{item.id} - #{to_replace}".red
      return
    end

    item.update!(taxt: new_taxt)
    item.create_activity :update, ANTCATBOT,
      edit_summary: "Replace `#{to_replace}` with `#{replace_with}` [batch #{BATCH}], see %github1074"
    puts "OK".green
    $replaced_in_batch += 1
  end
end

# ---

replacements = [
  ["{missing Neivamyrmex (Acamatus)}", "{missing Eciton (Acamatus)}"],
  ["{missing Neivamyrmex (Neivamyrmex)}", "{missing Eciton (Neivamyrmex)}"],
  ["{missing Temnothorax (Myrafant)}", "{missing Leptothorax (Myrafant)}"],
  ["{missing Hypoponera (Hypoponera)}", "{missing Ponera (Hypoponera)}"],
  ["{missing Nesomyrmex (Goniothorax)}", "{missing Leptothorax (Goniothorax)}"],
  ["{missing Nesomyrmex (Nesomyrmex)}", "{missing Leptothorax (Nesomyrmex)}"]
]

replacements.each do |(to_replace, replace_with)|
  if $replaced_in_batch > MAX_REPLACE_IN_BATCH
    break
  end

  replace_with_other_missing_tags! to_replace, replace_with
end; nil

# ---

Activity.execute_script_activity User.find_by!(name: 'Fredrik Palmkron'), EDIT_SUMMARY
@jonkerz jonkerz added the script label Jul 2, 2020
@jonkerz jonkerz closed this as completed Jul 2, 2020
@jonkerz jonkerz reopened this Jul 2, 2020
@jonkerz
Copy link
Member Author

jonkerz commented Jul 2, 2020

Batch 2

BATCH = 2
EDIT_SUMMARY = "Replace `missing` tags with other `missing` tags [batch #{BATCH}], see %github1074"
ANTCATBOT = User.find_by!(name: 'AntCatBot')

# For activities and PaperTrail.
RequestStore.store[:current_request_uuid] = SecureRandom.uuid
PaperTrail.request.whodunnit = ANTCATBOT.id

MAX_REPLACE_IN_BATCH = 9999
$replaced_in_batch = 0

def replace_with_other_missing_tags! to_replace, replace_with
  escaped_to_replace = to_replace.dup
  escaped_to_replace = escaped_to_replace.gsub!('(', '\\(')
  escaped_to_replace = escaped_to_replace.gsub!(')', '\\)')

  items = TaxonHistoryItem.where("taxt REGEXP ?", escaped_to_replace)
  puts "#{to_replace}: #{items.count}"

  items.find_each do |item|
    old_taxt = item.taxt.dup

    new_taxt = item.taxt.dup
    new_taxt.gsub!(to_replace, replace_with)

    if old_taxt == new_taxt
      puts "nothing was changed: #{item.id} - #{to_replace}".red
      return
    end

    item.update!(taxt: new_taxt)
    item.create_activity :update, ANTCATBOT,
      edit_summary: "Replace `#{to_replace}` with `#{replace_with}` [batch #{BATCH}], see %github1074"
    puts "OK".green
    $replaced_in_batch += 1
  end
end

# ---

replacements = [
  ["{missing <i>Neivamyrmex (Acamatus)</i>}", "{missing Eciton (Acamatus)}"],
  ["{missing <i>Neivamyrmex (Labidus)</i>}", "{missing Eciton (Labidus)}"],
  ["{missing Neivamyrmex (Labidus)}", "{missing Eciton (Labidus)}"],
  ["{missing Rhytidoponera (Rhytidoponera)}", "{missing Ectatomma (Rhytidoponera)}"],
  ["{missing Temnothorax (Temnothorax)}", "{missing Leptothorax (Temnothorax)}"],
  ["{missing Paraparatrechina (Nylanderia)}", "{missing Prenolepis (Nylanderia)}"]
]

replacements.each do |(to_replace, replace_with)|
  if $replaced_in_batch > MAX_REPLACE_IN_BATCH
    break
  end

  replace_with_other_missing_tags! to_replace, replace_with
end; nil

# ---

Activity.execute_script_activity User.find_by!(name: 'Fredrik Palmkron'), EDIT_SUMMARY

@jonkerz jonkerz closed this as completed Jul 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant