views:

65

answers:

2

I've been trying to write a ruby script using mechanize to batch upload a large number of images to a MediaWiki. The script runs without any errors but I suspect there is something wrong with the way I'm handling multipart forms with mechanize. The result variable at the end of the code gives no indication of success or failure, it just shows the page with all of the values filled in, wpDestFile is DezzImage.png and so on like I specified. end.submit seems to do nothing.

Below is a the complete code for uploading a single file, a few variables need filling in for it to work.

require 'rubygems'
require 'mechanize'
require 'nokogiri'

loginName = ""
loginPassword = ""
wikiUploadPage = "http://en.wikipedia.org/wiki/Special:Upload"
wikiLoginPage = "http://en.wikipedia.org/wiki/Special:UserLogin"
pathToImage = "/home/milo/image.png"

agent = Mechanize.new {|agent| agent.user_agent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.4) Gecko/20100513 Firefox/3.6.4" }
agent.pre_connect_hooks << lambda { |params| params[:request]['Connection'] = 'keep-alive' }
agent.follow_meta_refresh = true
agent.get(wikiLoginPage) do |page|
    login_result = page.form_with(:method => /POST/) do |form|
        form.wpName = loginName
        form.wpPassword = loginPassword
    end.submit
end

uploadPage = agent.get(wikiUploadPage)

result = uploadPage.form_with(:method => /POST/) do |form|
    form.file_uploads.first.file_name = pathToImage
    form.wpDestFile = "DezzImage.png"
    form.wpUploadDescription = "DezzImage.png"
end.submit
A: 

Looks like you're not actually setting the POST parameter that submits the page for uploading. Try something like this:

result = uploadPage.form_with(:method => /POST/) do |form|
    form.file_uploads.first.file_name = pathToImage
    form.wpDestFile = "DezzImage.png"
    form.wpUploadDescription = "DezzImage.png"
    form.wpUpload = True
end.submit
perimosocordiae
form.wpUpload is just the submit button for the form if I'm not mistaken. That button is pressed by end.submit.
Dezzimal
A: 

We solved this elsewhere, but the problem seemed to be a misconfiguration in the MediaWiki install. Setting:

form.checkbox_with(:name => "wpIgnoreWarning").check

with the form submission seems to have addressed the issue.

Chris Heald
Accepting this as the answer, there is a problem with my mechanize or ruby installation however as the script works on your computer but not on mine.
Dezzimal