سؤال

On our website when a user adds a link to a Wikipedia page we automatically present them with the N biggest images of the page. We then embed an appropriate Wikipedia thumb image (not the full sized image). If they link to a File: page we understand that directly and use the referenced image.

However, different language versions of Wikipedia use different "File:" prefixes. Is there another easy way to detect a Wikipedia File: page? Is there a comprehensive list of the File: prefix in all languages? I found these so far:

File:
Image:
Datei:
Bild:
ملف: or %D9%85%D9%84%D9%81:
Imagen:
Irudi:
Fichier:
Ficheiro:
Датотека: or %D0%94%D0%B0%D1%82%D0%BE%D1%82%D0%B5%D0%BA%D0%B0:
Fil:
Файл: or %D0%A4%D0%B0%D0%B9%D0%BB:
Immagine:
Податотека: or %D0%9F%D0%BE%D0%B4%D0%B0%D1%82%D0%BE%D1%82%D0%B5%D0%BA%D0%B0:
പ്രമാണം: or %E0%B4%AA%E0%B5%8D%E0%B4%B0%E0%B4%AE%E0%B4%BE%E0%B4%A3%E0%B4%82:
ファイル: or %E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB:
Слика: or %D0%A1%D0%BB%D0%B8%D0%BA%D0%B0:
Датотека: or %D0%94%D0%B0%D1%82%D0%BE%D1%82%D0%B5%D0%BA%D0%B0:
దస్త్రం: or %E0%B0%A6%E0%B0%B8%E0%B1%8D%E0%B0%A4%E0%B1%8D%E0%B0%B0%E0%B0%82:
Файл: or %D0%A4%D0%B0%D0%B9%D0%BB:
هل كانت مفيدة؟

المحلول

You shouldn't try to parse the URL. The localised namespace names across different projects are constantly changing and it is hard to get an up-to-date list. There are also cases where one localised name refer to two different namespaces on two projects.

You should be able to tell the namespace of a link by querying for its properties.

https://en.wikipedia.org/w/api.php?action=query&prop=info&format=json&titles=File%3AWiki.png

Look for the ns property.

{
    "query": {
        "pages": {
            "28680696": {
                "pageid": 28680696,
                "ns": 6,
                "title": "File:Wiki.png",
                "contentmodel": "wikitext",
                "pagelanguage": "en",
                "touched": "2013-07-18T14:04:38Z",
                "lastrevid": 560201242,
                "counter": "",
                "length": 786
            }
        }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top