Module:WikidataCheck: Difference between revisions

From Random Island Wiki
Jump to navigation Jump to search
>Legoktm
(fix if no claims exist)
>Happy5214
(I didn't add that code myself)
Line 21: Line 21:
     end
     end
     local entity = mw.wikibase.getEntity()
     local entity = mw.wikibase.getEntity()
     if not entity.claims then
     if not entity then
         return "[[Category:" .. catbase .. " not in Wikidata]]"
         return "[[Category:" .. catbase .. " not in Wikidata]]"
     end
     end

Revision as of 00:12, 30 April 2013

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

local p = {}

function p.wikidatacheck(frame)
    local pframe = frame:getParent()
    local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
    local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
    
    local property = config.property
    local value = config.value
    local catbase = config.category
    local namespaces = config.namespaces
    local ok = false
    local ns = mw.title.getCurrentTitle().namespace
    for v in mw.text.gsplit( namespaces, ",", true) do
        if tonumber(v) == ns then
            ok = true
        end
    end
    if not ok then
        return ""
    end
    local entity = mw.wikibase.getEntity()
    if not entity then
        return "[[Category:" .. catbase .. " not in Wikidata]]"
    end
    local hasProp = entity.claims[property]
    if not hasProp then
        return "[[Category:" .. catbase .. " not in Wikidata]]"
    end
    local propValue = hasProp[0].mainsnak.datavalue.value
    if propValue == value then
        return "[[Category:" .. catbase .. " same as Wikidata]]"
    else
        return "[[Category:" .. catbase .. " different from Wikidata]]"
    end
end

return p