6

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.

Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76
Ali
  • 7,353
  • 20
  • 103
  • 161

5 Answers5

12

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.

Yuri Baburov
  • 712
  • 7
  • 17
  • Thanx, this works well. I have translated your answer to JS. – Damjan Pavlica Oct 26 '15 at 11:53
  • @Yuri Baburov http://upload.wikimedia.org/wikipedia/commons/ is not fixed some time this link https://upload.wikimedia.org/wikipedia/en/7/75/Cruciblecover.jpg any idea how to get dynamic url? – Anant Shah May 04 '17 at 20:22
  • 1
    Note that the approach in this answer is now outdated and overly complex, as Wikimedia sites currently provide much more friendly URL scheme for hotlinking. For example, you can use `https://en.wikipedia.org/wiki/Special:FilePath/Example.jpg` to hotlink image "Example.jpg". Please see answers below for more details. – Magma Apr 21 '22 at 02:24
3

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.

Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76
3

There is now a simpler way to hotlink the files on Wikipedia using the Special:FilePath page. Hence, if you want to link the file Example.jpg on English Wikipedia, you can use https://en.wikipedia.org/wiki/Special:FilePath/Example.jpg.

Similar links should work for other Wikimedia Foundation sites (e. g. Wikimedia Commons).

Details and recommendations regarding hotlinking can be found on Wikimedia's Commons:Reusing_content_outside_Wikimedia.

Adinia
  • 3,722
  • 5
  • 40
  • 58
Magma
  • 130
  • 9
0

Have you tried http://en.wikipedia.org/wiki/File:filename.jpg ? Even if the files are located on Wikimedia Commons, the above URL should still work.

Edit: Are you trying to hotlink the image? If so, Wikipedia prohibits hotlinking. http://commons.wikimedia.org/wiki/Commons:Reusing_content_outside_Wikimedia#Hotlinking

Update 10-Jan-2019: Hotlinking is now permitted:

Hotlinking or InstantCommons: It is possible to use files directly on Commons within another website, by setting up a MediaWiki wiki with InstantCommons, ...

jpp
  • 159,742
  • 34
  • 281
  • 339
jimyi
  • 30,733
  • 3
  • 38
  • 34
0

I noticed that the URL prefix should be:

http://upload.wikimedia.org/wikipedia/en/thumb/

Moreover the folder must contain the repeated name of the file with a size specified as prefix:

http://upload.wikimedia.org/wikipedia/en/thumb/d/dd/Ruins-imperial-complex-milan-.jpg/220px-Ruins-imperial-complex-milan-.jpg

$filename = replace($name, ' ', '_');
$digest = md5($filename);
$urlencfile =  urlencode($filename)
$imgwidth = 220
$folder = $digest[0] . '/' . $digest[0] . $digest[1] . '/' . $urlencfile . '/' . $imgwidth . 'px-' . $urlencfile;
$url = 'http://upload.wikimedia.org/wikipedia/en/thumb/' . $folder;
Hardest
  • 348
  • 1
  • 3
  • 7