Přeskočit na obsah

Modul:WikidataCheck

Z Wikipedie, otevřené encyklopedie

Převzato z anglické Wikipedie, dokumentaci vizte na en:Module:WikidataCheck.

function compareValues(config)
	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|" .. pagename .. "]]"
	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 .. "]]" -- bot needs to add the property
	end

	local Values, Strings = {}, {}
	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 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(hasProp) 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(hasProp) 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
		-- TODO
		value = mw.ustring.gsub( value, '%[%[%s*([^|%]]+)%s*[^%]]*%]%]', '%1' )
		value = mw.ustring.gsub( value, '\s*%([^%)]+%)', '' )
		local date_convertor = function(day, month, year)
			if not tonumber(month) then
				local Months = {
					["leden"] = "01", ["ledna"] = "01",
					["únor"] = "02", ["února"] = "02",
					["březen"] = "03", ["března"] = "03",
					["duben"] = "04", ["dubna"] = "04",	
					["květen"] = "05", ["května"] = "05",
					["červen"] = "06", ["června"] = "06",
					["červenec"] = "07", ["července"] = "07",
					["srpen"] = "08", ["srpna"] = "08",
					["září"] = "09",
					["říjen"] = "10", ["října"] = "10",
					["listopad"] = "11", ["listopadu"] = "11",
					["prosinec"] = "12", ["prosince"] = "12"
				}
				if Months[month] then
					month = Months[month]
				end
			elseif tonumber(month) < 10 then
				month = '0' .. month
			end
			return year .. '-' .. month .. '-' .. day
		end
		local parser = function(...)
			return mw.getContentLanguage():formatDate(...)
		end
		local Time = require 'Modul:Time'
		local timevalue
		for _, statement in pairs(hasProp) do
			if statement.mainsnak.snaktype == "value" then
				timevalue = Time.newFromWikidataValue(statement.mainsnak.datavalue.value):toString()
				if timevalue and timevalue ~= '' then
					for string in mw.text.gsplit( value, '%s*<[^<>%w]-br[^<>%w]->%s*' ) do
						if mw.ustring.match( string, '^%s-%d%d?[^%w]+%w+[^%w]+%d%d%d%d%s-$' ) then
							string = mw.ustring.gsub( string, '^%s-(%d%d?)[^%w]+(%w+)[^%w]+(%d%d%d%d)%s-$', date_convertor )
							local bool, result = pcall(parser, 'Ymd', string)
							if bool then
								timevalue = parser('Ymd', timevalue)
								if timevalue == result then
									return true, "[[Kategorie:Údržba:" .. catbase .. " odpovídá Wikidatům|" .. pagename .. "]]"
								end
							else
								return "", ""
							end
						elseif mw.ustring.match( string, '%d%d%d%d' ) then
							string = mw.ustring.gsub( string, '.*(%d%d%d%d).*', '%1' )
							if mw.ustring.match( timevalue, '^' .. string ) then
								return true, "[[Kategorie:Údržba:" .. catbase .. " odpovídá Wikidatům|" .. pagename .. "]]" -- yay!
							else
								return false, "[[Kategorie:Údržba:" .. catbase .. " se liší od Wikidat|" .. pagename .. "]]"
							end
						else
							return false, "[[Kategorie:Údržba:" .. catbase .. " se liší od Wikidat|" .. pagename .. "]]"
	 					end
					end
				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(hasProp) 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(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( Values, sitelink )
				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 = mw.ustring.sub(string, 1, 1):upper() .. mw.ustring.sub(string, 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 = mw.ustring.sub(string, 1, 1):upper() .. mw.ustring.sub(string, 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 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 -- 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)
	local result, category = compareValues(config)
	return result
end

return p