mediawiki-apiimagesourcewikimedia-commons

Getting license information from MediaWiki API?


I want to display images from Wikimedia Commons inside my website (as links) using the following mediawiki-api call to search for images:

https://en.wikipedia.org/wiki/Special:ApiSandbox#action=query&list=allimages&format=json&aifrom=LG%20G4&aiprop=dimensions%7Cmime&ailimit=5

This returns a search result containing all images that match my query. But what I do not see in that response is the license information. I can retrieve the uploader information using "user" inside the aiprop, but how do I retrieve the license information (like CC-BY-SA)?


Solution

  • As already answered, the extmetadata API extension provides license information. The result format is a bit verbose and some fields are duplicated with different spellings, so I created the PHP wrapper image-attribution:

    $attribution = commons_image_attribution("Loewe_frontal.JPG");
    $credit = $attribution['credit'];
    

    The response has following structure (here given as JSON):

    {
        "src": "https://upload.wikimedia.org/wikipedia/commons/a/ac/Loewe_frontal.JPG",
        "url": "https://commons.wikimedia.org/wiki/File:Loewe_frontal.JPG",
        "description": "Portrait of a young lion (Panthera leo), taken at Tierpark Hellabrunn, Munich.",
        "creator": "Martin Falbisoner",
        "date": "2012-07-14 15:39:06",
        "attribution": true,
        "license": "CC BY-SA 3.0",
        "credit": "CC BY-SA 3.0: Martin Falbisoner"
    }