Modul:WikidataCheck
Vzhled
Převzato z anglické Wikipedie, dokumentaci vizte na en:Module:WikidataCheck.
local p = {}
function p.wikidatacheck(frame)
-- local pframe = frame:getParent()
-- local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
local property = config.property
local value = config.value
local catbase = config.category
local namespaces = config.namespaces
local title = mw.title.getCurrentTitle()
local pagename = title.text
local ok = false -- one-way flag to check if we're in a good namespace
local ns = title.namespace
for v in mw.text.gsplit( namespaces, ",", true ) do
if tonumber(v) == ns then
ok = true
break
end
end
if not ok then -- not in one of the approved namespaces
return ""
end
local entity = config and config.entity or mw.wikibase.getEntityObject()
if not entity then -- no Wikidata item
return "[[Kategorie:Údržba:" .. catbase .. " není na Wikidatech]]"
end
local hasProp = entity.claims and entity.claims[property]
if not hasProp then -- no claim of that property
return "[[Kategorie:Údržba:" .. catbase .. " není na Wikidatech|" .. pagename .. "]]" -- bad. Bot needs to add the property
end
for _, statement in pairs(hasProp) do
if statement.mainsnak.snaktype == "value" then
if statement.mainsnak.datavalue.value == value then
return "[[Kategorie:Údržba:" .. catbase .. " odpovídá Wikidatům|" .. pagename .. "]]" -- yay!
end
end
end
--[==[ local datatype = hasProp[1].mainsnak.datatype
if datatype == "commonsMedia" or datatype == "string" then
for _, statement in pairs(hasProp) do
if statement.mainsnak.snaktype == "value" then
if statement.mainsnak.datavalue.value == value then
return "[[Kategorie:Údržba:" .. catbase .. " odpovídá Wikidatům|" .. pagename .. "]]" -- yay!
end
end
end
elseif datatype == "globe-coordinate" then
return ""
elseif datatype == "monolingualtext" then
for _, statement in pairs(hasProp) do
if statement.mainsnak.snaktype == "value" then
if statement.mainsnak.datavalue.value.text == value then
return "[[Kategorie:Údržba:" .. catbase .. " odpovídá Wikidatům|" .. pagename .. "]]" -- yay!
end
end
end
elseif datatype == "quantity" then
if mw.ustring.match( value, '%d+' ) then
for _, statement in pairs(hasProp) do
if statement.mainsnak.snaktype == "value" then
for value in mw.ustring.gmatch( value, '(%-?%d+)' ) do
local lowerBound = tonumber(statement.mainsnak.datavalue.value.lowerBound)
local upperBound = tonumber(statement.mainsnak.datavalue.value.upperBound)
value = tonumber(value)
if value >= lowerBound and value <= upperBound then
return "[[Kategorie:Údržba:" .. catbase .. " odpovídá Wikidatům|" .. pagename .. "]]" -- yay!
end
break -- only the first one
end
break -- only the first one
end
end
end
elseif datatype == "time" then
value = mw.ustring.gsub( value, '%[%[%s*([^|%]]+)%s*[^%]]*%]%]', '%1' )
value = mw.ustring.gsub( value, '\s*%([^%)]+%)', '' )
local month_convertor = function(month)
end
local parser = function(...)
return mw.getContentLanguage():formatDate(...)
end
for string in mw.text.gsplit( value, '%s*<[^<>%w]-br[^<>%w]->%s*' ) do
if mw.ustring.match( string, '^%s-%d%d?\.? -%w+\.? -%d%d%d%d%s-$' ) then
string = mw.ustring.gsub( string, '(%d%d?)\.? -(%w+)\.? -(%d%d%d%d)', '%1 %2 %3' )
local bool, result = pcall(parser, 'Ymd', string)
if bool then
end
elseif mw.ustring.match( string, '%d%d%d%d' ) then
end
end
return ""
elseif datatype == "url" then
if mw.ustring.match( value, 'https?://' ) then
local Values = {}
for value in mw.ustring.gmatch( value, '(https?://[^%[%]%s]+)' ) do
table.insert( Values, value )
end
for _, statement in pairs(hasProp) do
if statement.mainsnak.snaktype == "value" then
for i, value in pairs(Values) do
if statement.mainsnak.datavalue.value == value then
break
elseif i == #Values then
return "[[Kategorie:Údržba:" .. catbase .. " se liší od Wikidat|" .. pagename .. "]]"
end
end
end
end
return "[[Kategorie:Údržba:" .. catbase .. " odpovídá Wikidatům|" .. pagename .. "]]" -- yay!
end
elseif datatype == "wikibase-item" then
local Pages, Sitelinks = {}, {}
for _, statement in pairs(hasProp) do
if statement.mainsnak.snaktype == "value" then
local sitelink = mw.wikibase.sitelink('Q' .. statement.mainsnak.datavalue.value["numeric-id"])
if sitelink then
table.insert( Sitelinks, sitelink )
end
end
end
if mw.ustring.match( value, '<[^<>%w]-br[^<>%w]->' ) then
for string in mw.text.gsplit( value, '<[^<>%w]-br[^<>%w]->' ) do
if mw.ustring.match( string, '%[%[[^%]%[]-%]%]' ) then
for page in mw.ustring.gmatch( value, '%[%[%s*([^|%]]-)%s*[|%]]' ) do
table.insert( Pages, page )
end
else
table.insert( Pages, mw.text.trim(string) )
end
end
elseif mw.ustring.match( value, '%[%[[^%]%[]-%]%]' ) then
for page in mw.ustring.gmatch( value, '%[%[%s*([^|%]]-)%s*[|%]]' ) do
table.insert( Pages, page )
end
end
for _, sitelink in pairs(Sitelinks) do
for i, page in pairs(Pages) do
if page == sitelink then
break
elseif i == #Pages then
return "[[Kategorie:Údržba:" .. catbase .. " se liší od Wikidat|" .. pagename .. "]]"
end
end
end
return "[[Kategorie:Údržba:" .. catbase .. " odpovídá Wikidatům|" .. pagename .. "]]" -- yay!
end ]==]--
return "[[Kategorie:Údržba:" .. catbase .. " se liší od Wikidat|" .. pagename .. "]]" -- needs human review :(
end
return p