#!/usr/bin/env ruby
# encoding: utf-8

require 'mediawiki_api'
require 'HTTParty'
require 'csv'
require 'open-uri'
require './helper'
require 'fileutils'

CATEGORY = "[[Category:Infobox NFL player with debut/final parameters|debut/final parameters]]"

PARAMS = ["debutteam", "debutyear", "finalteam", "finalyear", "coachdebutteam",
          "coachdebutyear", "coachfinalteam", "coachfinalyear"]

QUERY_URL = "https://petscan.wmflabs.org/?psid=597891&format=json"

Helper.read_env_vars

client = MediawikiApi::Client.new 'https://en.wikipedia.org/w/api.php'
client.log_in ENV['USERNAME'], ENV['PASSWORD']

json = JSON.load(open(QUERY_URL))
titles = json["*"].first["a"]["*"].map{ | page| page["title"].gsub("_"," ")}
puts titles.size

titles.each do |title|
  title.strip!
  puts title
  text = client.get_wikitext(title).body

  PARAMS.each do |param|
    text.gsub!(/\|\s*#{param}\s*=.*\n/, "\n")
  end

  client.edit(title: title, text: text, summary: "Fixing infobox not to use #{CATEGORY}")
  puts "- SUCCESS"
end

puts "DONE"