Modul:WikidataCheck
Vzhled
Převzato z anglické Wikipedie, dokumentaci vizte na en:Module:WikidataCheck.
local function compareValues(config)
local property = config.property
local value = config.value
local catbase = config.category
if not (property and value and catbase) then
return "", ""
end
local namespaces = config.namespaces or 0
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.entity or mw.wikibase.getEntityObject()
if not entity or not entity.claims or not entity.claims[property] then -- no Wikidata item or no claim of that property
return "", "[[Kategorie:Údržba:" .. catbase .. " není na Wikidatech|" .. pagename .. "]]"
end
local Statements = entity.claims[property]
local Values, Strings = {}, {}
local datatype = Statements[1].mainsnak.datatype
value = mw.ustring.gsub( value, '<([a-z]+)%f[^%w][^>]->.-</%1>%s*', '' )
if datatype == "commonsMedia" or datatype == "string" then
for _, statement in pairs(Statements) do
if statement.mainsnak.snaktype == "value" then
value = mw.ustring.gsub( value, '\s*%([^%)]+%)', '' )
if tostring(statement.mainsnak.datavalue.value) == tostring(value) then --TODO: normalizing
return true, "[[Kategorie:Údržba:" .. catbase .. " odpovídá Wikidatům|" .. pagename .. "]]" -- yay!
end
end
end
return false, "[[Kategorie:Údržba:" .. catbase .. " se liší od Wikidat|" .. pagename .. "]]"
elseif datatype == "globe-coordinate" then
return "", ""
elseif datatype == "monolingualtext" then
for _, statement in pairs(Statements) do
if statement.mainsnak.snaktype == "value" then
if statement.mainsnak.datavalue.value.text == value then
return true, "[[Kategorie:Údržba:" .. catbase .. " odpovídá Wikidatům|" .. pagename .. "]]" -- yay!
end
end
end
return false, "[[Kategorie:Údržba:" .. catbase .. " se liší od Wikidat|" .. pagename .. "]]"
elseif datatype == "quantity" then
if mw.ustring.match( value, '%d+' ) then
for value in mw.ustring.gmatch( value, '(%-?%d+)' ) do
for _, statement in pairs(Statements) do
if statement.mainsnak.snaktype == "value" then
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 true, "[[Kategorie:Údržba:" .. catbase .. " odpovídá Wikidatům|" .. pagename .. "]]" -- yay!
end
end
end
break -- only the first one
end
end
return false, "[[Kategorie:Údržba:" .. catbase .. " se liší od Wikidat|" .. pagename .. "]]"
elseif datatype == "time" then
value = mw.ustring.gsub( value, '%[%[%s*([^|%]]+)%s*[^%]]*%]%]', '%1' )
value = mw.ustring.gsub( value:lower(), '\s*%([^%)]+%)', '' )
local convertTable = function(Table)
if #Table > 3 or #Table == 0 then return nil end
local Months = {
["leden"] = 1, ["ledna"] = 1,
["únor"] = 2, ["února"] = 2,
["březen"] = 3, ["března"] = 3,
["duben"] = 4, ["dubna"] = 4,
["květen"] = 5, ["května"] = 5,
["červen"] = 6, ["června"] = 6,
["červenec"] = 7, ["července"] = 7,
["srpen"] = 8, ["srpna"] = 8,
["září"] = 9,
["říjen"] = 10, ["října"] = 10,
["listopad"] = 11, ["listopadu"] = 11,
["prosinec"] = 12, ["prosince"] = 12
}
if #Table == 1 then
return tonumber(Table[1]) and { year = tonumber(Table[1]) } or nil
elseif #Table == 2 then
if tonumber(Table[1]) and tonumber(Table[2]) then
if tonumber(Table[1]) < tonumber(Table[2]) then
return { year = tonumber(Table[2]), month = tonumber(Table[1]) }
else
return { year = tonumber(Table[1]), month = tonumber(Table[2]) }
end
elseif tonumber(Table[1]) or tonumber(Table[2]) then
return {
year = tonumber(Table[1]) or tonumber(Table[2]),
month = (tonumber(Table[1]) and Months[Table[2]]) or Months[Table[1]] or nil
}
end
return nil
else
if tonumber(Table[3]) and tonumber(Table[1]) then
return {
year = tonumber(Table[3]),
month = tonumber(Table[2]) or Months[Table[2]] or nil,
day = tonumber(Table[1]) or nil
}
end
return nil
end
end
local Time = require 'Modul:Time'
for _, statement in pairs(Statements) do
if statement.mainsnak.snaktype == "value" then
local timevalue = Time.newFromWikidataValue(statement.mainsnak.datavalue.value)
if timevalue and timevalue.precision > 8 then
for string in mw.text.gsplit( value, '%s*<[^<>%w]-br[^<>%w]->%s*' ) do
string = string:sub( 1, mw.ustring.match( string, '%d?%d%d%d()' ) or -1 )
local Table = mw.text.split( string, '[^%w]+' )
Table = convertTable(Table)
if Table and Table.year then
if timevalue.year == Table.year then
if timevalue.precision == 9 then
return true, "[[Kategorie:Údržba:" .. catbase .. " odpovídá Wikidatům|" .. pagename .. "]]"
end
if Table.month and timevalue.month == Table.month then
if timevalue.precision == 10 then
return true, "[[Kategorie:Údržba:" .. catbase .. " odpovídá Wikidatům|" .. pagename .. "]]"
end
if Table.day and timevalue.day == Table.day then
return true, "[[Kategorie:Údržba:" .. catbase .. " odpovídá Wikidatům|" .. pagename .. "]]"
end
end
end
end
end
return false, "[[Kategorie:Údržba:" .. catbase .. " se liší od Wikidat|" .. pagename .. "]]"
end
end
end
return "", ""
elseif datatype == "url" then
if mw.ustring.match( value, 'https?://' ) then
for string in mw.ustring.gmatch( value, '(https?://[^%[%]%s]+)' ) do
table.insert( Strings, string )
end
for _, statement in pairs(Statements) do
if statement.mainsnak.snaktype == "value" then
table.insert( Values, statement.mainsnak.datavalue.value )
end
end
else
return false, "[[Kategorie:Údržba:" .. catbase .. " se liší od Wikidat|" .. pagename .. "]]" -- needs human review :(
end
elseif datatype == "wikibase-item" then
for _, statement in pairs(Statements) do
if statement.mainsnak.snaktype == "value" then
local id = 'Q' .. statement.mainsnak.datavalue.value["numeric-id"]
local value = mw.wikibase.sitelink(id) or mw.wikibase.label(id)
if value then
value = value:sub(1, 1):upper() .. value:sub(2)
table.insert( Values, value )
end
end
end
if mw.ustring.match( value, '<[^<>%w]-br[^<>%w]->' ) then
for string in mw.text.gsplit( value, '<[^<>%w]-br[^<>%w]->' ) do
string = mw.ustring.gsub( string, '\s*%([^%(%)]+%)', '' )
if mw.ustring.match( string, '%[%[[^%]%[]-%]%]' ) then
for page in mw.ustring.gmatch( string, '%[%[%s*([^|%]]-)%s*[|%]]' ) do
string = string:sub(1, 1):upper() .. string:sub(2)
table.insert( Strings, page )
end
else
table.insert( Strings, mw.text.trim(string) )
end
end
elseif mw.ustring.match( value, '%[%[[^%]%[]-%]%]' ) then
for string in mw.ustring.gmatch( value, '%[%[%s*([^|%]]-)%s*[|%]]' ) do
string = string:sub(1, 1):upper() .. string:sub(2)
table.insert( Strings, string )
end
end
end
local i = 0
for _, string in pairs(Strings) do
for _, value in pairs(Values) do
if string == value then
i = i + 1
break
end
end
end
if #Strings > 0 and #Values > 0 and i == #Strings then
return true, "[[Kategorie:Údržba:" .. catbase .. " odpovídá Wikidatům|" .. pagename .. "]]" -- yay!
end
return false, "[[Kategorie:Údržba:" .. catbase .. " se liší od Wikidat|" .. pagename .. "]]" -- needs human review :(
end
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 or {} -- the arguments passed BY the template, in the wikitext of the template itself
local result, category = compareValues(config)
return category
end
function p.wikidatacheckFromLua(config)
return compareValues(config)
end
return p