MagePsycho
7/10/2015 - 2:47 PM

Magento DB Repair Tool Using CLI - Ruby + Mechanize

Magento DB Repair Tool Using CLI - Ruby + Mechanize

#!/usr/bin/env ruby

#
# Magento DB Repair tool using CLI
# Uses Ruby's Mechanize gem
#
# @author   Raj KB <magepsycho@gmail.com>
# @website  http://www.magepsycho.com
#
# Tested on Mac OS-X 10.X
#

require 'mechanize'
require 'fileutils'

#################################
# FUNCTIONS
#################################
def checkError(page)
  if page.search('.msg_error').length > 0
    puts "[ERROR]"
    page.search('.msg_error li').each do |li|
      puts li.text.strip
    end
    exit
  end
end

def checkResult(page)
  puts "[RESULT]"
  page.search('.msg_success li').each do |li|
    puts li.text.strip
  end
end

def checkNotice(page)
  puts "[NOTICE]"
  page.search('.msg-note').each do |note|
    puts note.text.strip
  end

  if page.search('.msg-note').length > 1
    puts "See log for more details"
  end
end

#################################
# SCRIPT CODE
#################################
abort "#{$0} Argument Missing" if (ARGV.size < 1)

dbRepairUrl     = ARGV[0]
mageVersion     = ARGV[1]
mageDir         = Dir.pwd
dbRepairLogDir  = "#{mageDir}/var/dp-repair-tool"

FileUtils.mkdir_p "#{dbRepairLogDir}"
fp = File.new("#{dbRepairLogDir}/mage-#{mageVersion}-result.html", "a+")

agent = Mechanize.new
page = agent.get(dbRepairUrl)
puts "Loading page: #{page.title}..."
form = page.forms.first

puts "Setting db repair form values..."

# Get DB value from app/etc/local.xml
xmlFile = File.open("#{mageDir}/app/etc/local.xml")
doc = Nokogiri::XML(xmlFile)
corruptedHostname = doc.xpath('/config/global/resources/default_setup/connection/host').text()
corruptedUsername = doc.xpath('/config/global/resources/default_setup/connection/username').text()
corruptedPassword = doc.xpath('/config/global/resources/default_setup/connection/password').text()
corruptedDatabase = doc.xpath('/config/global/resources/default_setup/connection/dbname').text()
xmlFile.close

form['post_form']           = 'true'
form['corrupted[hostname]'] = corruptedHostname
form['corrupted[database]'] = corruptedDatabase
form['corrupted[username]'] = corruptedUsername
form['corrupted[password]'] = corruptedPassword

# Edit reference database credentials
form['reference[hostname]'] = corruptedHostname
form['reference[database]'] = "magento#{mageVersion}_vanilla"
form['reference[username]'] = corruptedUsername
form['reference[password]'] = corruptedPassword

#p form; exit

puts "Submitting db repair form..."
result_page = form.submit(form.button_with(:id => "button-continue"))

# Check if there is an error & exit
checkError(result_page)

# Continue if there is not an error
# Check if it further requires submission
if result_page.search('button#button-continue').length > 0

    checkNotice(result_page)
    # Form found again?
    puts "Submitting again..."
    form = result_page.forms.first
    result_page = form.submit(form.button_with(:id => "button-continue"))
    checkResult(result_page)
    checkNotice(result_page)
else
    checkResult(result_page)
    checkNotice(result_page)
end

fp.write(result_page.body)