Nicolas Alpi, Web developer

A blog about productivity, startups and me.

How to Search on a MediaWiki (Wikipedia) With Ruby on Rails

I have been looking for 3 days how to search on Wikipedia with Ruby.



After having found the solution, this is really simple :

1. I found MediaCloth

MediaCloth is a MediaWiky syntax parser. You feed it with a media wiki syntax and it’s supposed to return a correct html.
It works, it’s not perfect but it works.

There is a gem so : _gem install mediacloth _will do the job.

MediaCloth on rubyforge

MediaCloth help ?

2. MediaCloth were working, so let’s code an ugly solution

[sourcecode language=‘ruby’]
def ugly

feed_url = ‘http://en.wikipedia.org/w/api.php?action=opensearch&search=avignon’
output = “Avignon”
open(feed_url) do |http|
response = http.read
output += response
end

@result = MediaCloth::wiki_to_html(output)

end
[/sourcecode]

It was working, nearly, but it was very ugly.

3. The Wikipedia Client

After that I found the Wikipedia Client (google source code website).

Install it like every plugin : ruby script/plugin install http://wikipedia-client.googlecode.com/svn/trunk/wikipedia

If you try to run the example now, it will crash, you need to install the json gem : gem install json

Then try

[sourcecode language=‘ruby’]

def pretty

page = Wikipedia.find(‘peanut’)
@title = MediaCloth::wiki_to_html(page.title)
@content = MediaCloth::wiki_to_html(page.content)
end
[/sourcecode]

And now, everything is working like a charm.

Just be careful that the MediaWiki api solution is in beta for the moment. So don’t use it in production ;)

Comments