Jump to content

Module:WikidataCheck

Permanently protected module
From Wikipedia, the free encyclopedia

localp={}

functionp.wikidatacheck(frame)
localpframe=frame:getParent()
localconfig=frame.args-- the arguments passed BY the template, in the wikitext of the template itself
localargs=pframe.args-- the arguments passed TO the template, in the wikitext that transcludes the template

localproperty=config.property
localvalue=config.valueor""
localcatbase=config.category
localnamespaces=config.namespaces
localnocatsame=config.nocatsameor""
localignorecase=config.ignorecaseor""
localqid=config.qidor""
localonlysourced=(config.onlysourced=='yes')orfalse
localok=false-- one-way flag to check if we're in a good namespace
localns=mw.title.getCurrentTitle().namespace

forvinmw.text.gsplit(namespaces,",",true)do
iftonumber(v)==nsthen
ok=true
end
end
ifnotokthen-- not in one of the approved namespaces
return""
end
localentity
ifqid==""then
entity=mw.wikibase.getEntityObject()
else
entity=mw.wikibase.getEntityObject(qid)
end
ifnotentitythen-- no Wikidata item
return"[[Category:"..catbase.."not in Wikidata]]"
end
ifvalue==""then
returnnil-- Using Wikidata
end
localclaims=entity.claimsor{}
localhasProp=claims[property]
ifnothasPropthen-- no claim of that property
return"[[Category:"..catbase.."not in Wikidata]]"-- bad. Bot needs to add the property
end
ifignorecase~=""then
value=string.lower(value)
end
fori,vinipairs(hasProp)do-- Now we try to iterate over all possible values?
propValue=(v.mainsnak.datavalueor{}).value
ifignorecase~=""then
propValue=string.lower(propValue)
end
localsourced=false-- check for external refs a la Module:WikidataIB onlysourced
ifv.referencesthen
forj,vrinipairs(v.references)do
localref=mw.wikibase.renderSnaks(vr.snaks)
ifnotref:find("Wiki")then
sourced=true
break
end
end
end
ifpropValue==valueand(notonlysourcedorsourced)then
ifnocatsame==""then
return"[[Category:"..catbase.."same as Wikidata]]"-- yay!
else
returnnil-- if nocatsame, the "same as" category is not added
end
end
end
return"[[Category:"..catbase.."different from Wikidata]]"-- needs human review:(
end

returnp