Jump to content

User:ZackBot/infobox islands

From Wikipedia, the free encyclopedia
require 'mediawiki_api'
require 'HTTParty'
require 'timeout'
require '../helper'
require 'uri'
require 'colorize'
require 'json'

Helper.read_env_vars(file = '../vars.csv')

client = MediawikiApi::Client.new 'https://en.wikipedia.org/w/api.php'
client.log_in ENV['USERNAME'], ENV['PASSWORD']
url = 'https://petscan.wmflabs.org/?psid=6300261&format=json'

titles = Helper.get_wmf_pages(url)


titles.each do |title|
  puts title.colorize(:blue)
  page = client.get_wikitext(title).body
  
  templates = page.scan(/(?=\{\{(?:Infobox))(\{\{(?>[^{}]++|\g<1>)*}})/i).flatten
  next unless templates.size == 1
  template = templates.first
  old_text = template.dup

  template.gsub!('additional info', 'additional_info')
  template.gsub!('area acre', 'area_acre')
  template.gsub!('area footnotes', 'area_footnotes')
  template.gsub!('area ha', 'area_ha')
  template.gsub!('area km2', 'area_km2')
  template.gsub!('area m2', 'area_m2')
  template.gsub!('area sqft', 'area_sqft')
  template.gsub!('area sqmi', 'area_sqmi')
  template.gsub!('coastline footnotes', 'coastline_footnotes')
  template.gsub!('coastline ft', 'coastline_ft')
  template.gsub!('coastline km', 'coastline_km')
  template.gsub!('coastline m', 'coastline_m')
  template.gsub!('coastline mi', 'coastline_mi')

  template.gsub!(/country (\d) admin divisions (\d)/,    'country_\1_admin_divisions_\2')
  template.gsub!(/country (\d) admin divisions title/,   'country_\1_admin_divisions_title')
  template.gsub!(/country (\d) admin divisions title (\d)/, 'country_\1_admin_divisions_title_\2')

  template.gsub!(/country (\d) area acre/,               'country_\1_area_acre')
  template.gsub!(/country (\d) area ha/,                 'country_\1_area_ha')
  template.gsub!(/country (\d) area km2/,                'country_\1_area_km2')
  template.gsub!(/country (\d) area m2/,                 'country_\1_area_m2')
  template.gsub!(/country (\d) area sqft/,               'country_\1_area_sqft')
  template.gsub!(/country (\d) area sqmi/,               'country_\1_area_sqmi')
  template.gsub!(/country (\d) capital and largest city/,'country_\1_capital_and_largest_city')
  template.gsub!(/country (\d) capital type/,            'country_\1_capital_type')
  template.gsub!(/country (\d) largest city population/, 'country_\1_largest_city_population')
  template.gsub!(/country (\d) largest city type/,       'country_\1_largest_city_type')
  template.gsub!(/country (\d) largest city/,            'country_\1_largest_city')
  template.gsub!(/country (\d) leader name/,             'country_\1_leader_name')
  template.gsub!(/country (\d) leader title/,            'country_\1_leader_title')
  template.gsub!(/country (\d) capital/,                 'country_\1_capital')
  template.gsub!(/country (\d) area/,                    'country_\1_area')
  template.gsub!(/country admin divisions title (\d)/, 'country_admin_divisions_title_\1')
  template.gsub!(/country admin divisions (\d)/, 'country_admin_divisions_\1')
  template.gsub!('country admin divisions title', 'country_admin_divisions_title')
  template.gsub!('country admin divisions', 'country_admin_divisions')
  template.gsub!(/country (\d) admin divisions/,         'country_\1_admin_divisions')
  template.gsub!('country area', 'country_area')
  template.gsub!('country area acre', 'country_area_acre')
  template.gsub!('country area ha', 'country_area_ha')
  template.gsub!('country area km2', 'country_area_km2')
  template.gsub!('country area m2', 'country_area_m2')
  template.gsub!('country area sqft', 'country_area_sqft')
  template.gsub!('country area sqmi', 'country_area_sqmi')
  
  template.gsub!('country capital and largest city', 'country_capital_and_largest_city')
  template.gsub!('country capital type', 'country_capital_type')
  
  template.gsub!('country largest city population', 'country_largest_city_population')
  template.gsub!('country largest city type', 'country_largest_city_type')
  template.gsub!('country leader name', 'country_leader_name')
  template.gsub!('country leader title', 'country_leader_title')
  template.gsub!('density footnotes', 'density_footnotes')
  template.gsub!('density km2', 'density_km2')
  template.gsub!('density rank', 'density_rank')
  template.gsub!('density sqmi', 'density_sqmi')
  template.gsub!('elevation footnotes', 'elevation_footnotes')
  template.gsub!('elevation ft', 'elevation_ft')
  template.gsub!('elevation m', 'elevation_m')
  template.gsub!('ethnic groups', 'ethnic_groups')
  template.gsub!('GridReference', 'grid_reference')
  template.gsub!('highest mount', 'highest_mount')
  template.gsub!('image alt', 'image_alt')
  template.gsub!('image caption', 'image_caption')
  template.gsub!('image map', 'image_map')
  template.gsub!('image name', 'image_name')
  template.gsub!('image size', 'image_size')
  template.gsub!('length footnotes', 'length_footnotes')
  template.gsub!('length ft', 'length_ft')
  template.gsub!('length km', 'length_km')
  template.gsub!('length m', 'length_m')
  template.gsub!('length mi', 'length_mi')
  template.gsub!('local name', 'local_name')
  template.gsub!('major islands', 'major_islands')
  template.gsub!('map alt', 'map_alt')
  template.gsub!('map caption', 'map_caption')
  template.gsub!('map image', 'map_image')
  template.gsub!('map size', 'map_size')
  template.gsub!('native name', 'native_name')
  template.gsub!('native name lang', 'native_name_lang')
  template.gsub!('native name link', 'native_name_link')
  template.gsub!('population as of', 'population_as_of')
  template.gsub!('population footnotes', 'population_footnotes')
  template.gsub!('population rank', 'population_rank')
  template.gsub!('population rank max', 'population_rank_max')
  template.gsub!('total islands', 'total_islands')
  template.gsub!('treaty system', 'treaty_system')
  template.gsub!('width footnotes', 'width_footnotes')
  template.gsub!('width ft', 'width_ft')
  template.gsub!('width km', 'width_km')
  template.gsub!('width m', 'width_m')
  template.gsub!('width max ft', 'width_max_ft')
  template.gsub!('width max km', 'width_max_km')
  template.gsub!('width max m', 'width_max_m')
  template.gsub!('width max mi', 'width_max_mi')
  template.gsub!('width mi', 'width_mi')
  template.gsub!('width min ft', 'width_min_ft')
  template.gsub!('width min km', 'width_min_km')
  template.gsub!('width min m', 'width_min_m')
  template.gsub!('width min mi', 'width_min_mi')
  template.gsub!(/country (\d)/, 'country_\1')
  template.gsub!('country largest city', 'country_largest_city')
  template.gsub!('country capital', 'country_capital')
  template.gsub!('capital city', 'capital_city')
  
  page.gsub!(old_text, template)

  client.edit(title: title, text: page, summary: 'fixing deprecated params')
  puts ' - success'.colorize(:green)
end
puts 'DONE!'