Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get link to an image on wikipedia from the infobox?

I'm parsing wikipedia infoboxes and I noticed that some infoboxes have image fields - these fields hold names of image files stashed on wikipedia somewhere. However they just contain the name of the file as is as opposed to the actual link.

I checked the links of the images on real live infoboxes and the links do not seem to be from one source but the sources vary. How can I hyperlink to an image on wikipedia considering I only have the name of the image from an infobox entry.

like image 239
Ali Avatar asked Jun 22 '09 09:06

Ali


2 Answers

According to What are the strangely named components in Wikipedia file paths, you need to run md5 to find out url. Now wikipedia allows hotlinking, so:

If you have utf-8 encoded $name, you need to do the following:

$filename = replace($name, ' ', '_');
$digest = md5($filename);
$folder = $digest[0] . '/' . $digest[0] . $digest[1] . '/' .  urlencode($filename);
$url = 'http://upload.wikimedia.org/wikipedia/commons/' . $folder;

The same can be used for thumbnails.

like image 96
Yuri Baburov Avatar answered Sep 20 '22 12:09

Yuri Baburov


Here is a JavaScript implementation of a working PHP answer (credits to Yuri Baburov):

var name = "filename.jpg";
var filename = name.replace(/ /g, "_"); 
var digest = md5(filename);
var folder = digest[0] + '/' + digest[0] + digest[1] + '/' + encodeURIComponent(filename);
var url = 'http://upload.wikimedia.org/wikipedia/commons/' + folder;

Note that you must include external md5() function (https://github.com/blueimp/JavaScript-MD5); it is not native to JS.

like image 37
Damian Pavlica Avatar answered Sep 18 '22 12:09

Damian Pavlica