Module:WBHacks

From Wikidata
Jump to navigation Jump to search
Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules
Documentation icon Module documentation[view · edit · history · purge ]

Please use Template:Label, Template:Autodescription, Template:Datatype, which call this module. This module is meant to be a temporary measure until task 49071 is resolved.

Usage Examples
Code Result
Get label for Q234728 (en) {{#invoke: WBHacks | label | qid=Q234728 | lang=en}} Vanves
Get description for Q234728 (en) {{#invoke: WBHacks | description | qid=Q234728 | lang=en}} commune in Hauts-de-Seine, France
Get label for Q234728 (gu) {{#invoke: WBHacks | label | qid=Q234728 | lang=gu}} Vanves
Get label for Property:P131 (en) {{#invoke: WBHacks | label | qid=P131 | lang=en}} located in the administrative territorial entity
Get datatype for Property:P131 {{#invoke: WBHacks | datatype | qid=P131 }} wikibase-item

Code

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(id):getContent()
    return pg and JSON:decode(pg)
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] and obj[typ][lang].value
    if x == nil then
        x = obj[typ].en.value -- fallback to english
    end
    return x
end

function p.label(frame)
    local config = frame.args
 
    local qid = config.qid
    local lang = config.lang
    local obj = p.get(qid)
    return obj and p.thing(obj, "labels", lang) or "(no label)"

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

end

 function p.howmany(frame)
    local config = frame.args
 
    local qid = config.qid
    local obj = p.get(qid)
    local count = 0
    for language,value in pairs(obj[config.what]) do
        count=count+1
    end
    return count
end

 function p.datatype(frame)
    local config = frame.args
 
    local qid = config.qid
    local obj = p.get(qid)
    return obj["datatype"]

end

return p