Searching MediaWikis with Ruby (or treating MediaWiki a little like a database)

Here’s a little treat that I threw together to help me search Wikipedia for film data. It’s pretty simple. Example usage below:

wikipedia = MediaWiki::Search.new "http://en.wikipedia.org"
result = wikipedia.search("Firefox")    
if results.is_a? String
    # Then I've obtained the content for an actual page in a MediaWiki
else 
    # I have an Array containing Hashes with metadata about the top 20 candidates
    # Now, for giggles, I'll get the content for the first hit
    html = Net::HTTP.get_response URI.parse(result.first[:url])
end

The returned Array of Hashes, in the second case, has three keys: :url - The complete URL to the page in the MediaWiki :title - The Wikipedia page title :weight - The percentile weight supplied by the MediaWiki search.

Pretty simple? I thought so.

Code follows:

Continue reading →