Module:Wikidata: Difference between revisions

From Vigyanwiki
Template>RexxS
(catch the script error if the spouse property P26 doesn't exist for this article - just show a message)
Template>RexxS
(test against nil, rather than empty string)
Line 4: Line 4:
     local spouse_input = mw.text.trim(frame.args[1] or "")
     local spouse_input = mw.text.trim(frame.args[1] or "")
     if spouse_input == "FETCH_WIKIDATA" then
     if spouse_input == "FETCH_WIKIDATA" then
        -- get from wikidata
         local entity = mw.wikibase.getEntity()
         local entity = mw.wikibase.getEntity()
         if entity.claims.p26 > "" then
         if entity.claims.p26 ~= nil then
             local out = {}
             local out = {}
             for k, v in pairs(entity.claims.p26) do
             for k, v in pairs(entity.claims.p26) do
                 out[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"]) .. "]]"
                 out[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"]) .. "]]"
             end
             end
            -- TODO: hlist return
             return table.concat(out, ", ")
             return table.concat(out, ", ")
         else
         else

Revision as of 00:28, 26 August 2013

Documentation for this module may be created at Module:Wikidata/doc

local p = {}

p.getSpouse = function(frame)
    local spouse_input = mw.text.trim(frame.args[1] or "")
    if spouse_input == "FETCH_WIKIDATA" then
        local entity = mw.wikibase.getEntity()
        if entity.claims.p26 ~= nil then
            local out = {}
            for k, v in pairs(entity.claims.p26) do
                out[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"]) .. "]]"
            end
            return table.concat(out, ", ")
        else
            return "Not Found"
        end
    else
        return spouse_input
    end
end

return p