Module:Wikibase

提供: VANGUARD FLIGHT wiki
2014年3月30日 (日) 23:14時点におけるTaku.oshino (トーク | 投稿記録)による版 (1版)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索
Set of functions for querying wikidata using JSON. The code is a modified clone of d:Module:WBHacks; however it does not seem to work.
JSON = require('Module:JSON')
local p = {}
 
 function p.get( id )
    --this just returns the json object of the id
    local id = id
    if string.find(id, "P") ~= nil then
        if string.find(id, "Property") == nil then
            id = "Property:" .. id
        end
    end
    local pg = mw.title.new("d:"..id)
    return JSON:decode(pg:getContent())
end

function p.thing(obj, typ, lang)
    -- Can be used to fetch either label or description, depends on what you want.
    local x = obj[typ][lang]
    if x == nil then
        x = obj[typ].en -- fallback to english
    end
    return x
end

function p.label(frame)
    local pframe = frame:getParent()
    local config = frame.args
    local args = pframe.args
 
    local qid = config.qid
    local lang = config.lang
    local obj = p.get(qid)
    return p.thing(obj, "label", lang)
end
 
 function p.description(frame)
    local pframe = frame:getParent()
    local config = frame.args
    local args = pframe.args
 
    local qid = config.qid
    local lang = config.lang
    local obj = p.get(qid)
    return p.thing(obj, "description", lang)
end

return p