Module:WBHacks

From Wikidata
Jump to navigation Jump to search

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

In use
  • Getting label for Q234728 (en): Vanves
  • Getting description for Q234728 (en): commune in Hauts-de-Seine, France
  • Getting label for Q234728 (gu): Vanves
  • Getting label for Property:P131 (en): located in the administrative territorial entity
  • Getting datatype for Property:P131: wikibase-item

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