Modul:Vorlage:Infobox Fußballspieler

Die Dokumentation für dieses Modul kann unter Modul:Vorlage:Infobox Fußballspieler/Doku erstellt werden

--[=[ Modul:Vorlage:Infobox_Fußballspieler, 2015-10-09
Modul zur Unterstützung der Vorlage:Infobox_Fußballspieler
* insbesondere: Generierung der Tabellenzeilen der Vereins- und Nationalmannschaftsstationen, wobei die Daten aus Wikidata bezogen werden.

erstellt von user:Yellowcard
]=]

local wdLoadSuccess, mdlWikidata = pcall(require, "Modul:Wikidata")
assert (wdLoadSuccess, "Laden des Wikidata-Moduls fehlgeschlagen.")

local p = {}

function wdLoadStations(pTrainerTeams)
	--[[
		* load lists (separated by ';') of necessary data from Wikidata
		* split lists to Lua tables
		* store in global variables:
			listYearsFrom
			listYearsTo
			listTeams
			listTeamsId
			listGames
			listGoals
			listLoan
			listPositionHeld
	--]]
	
	local wdParameters = {}
	wdParameters["args"] = {}
	
	-- load general parameters
	if (pTrainerTeams)  then
		wdParameters["args"][1] = "P6087"
	else
		wdParameters["args"][1] = "P54"
	end
	wdParameters["args"]["list"] = ";"
	wdParameters["args"]["sort"] = "P580"
	wdParameters["args"]["sortEmptiesFirst"] = true
	wdParameters["args"]["includeempty"] = true
	
	-- start time
	wdParameters["args"]["qualifier"] = "P580"
	wdParameters["args"]["parameter"] = "time:Y"
	listYearsFrom = listSplit(mdlWikidata.claim(wdParameters)) or nil

	-- end time
	wdParameters["args"]["qualifier"] = "P582"
	listYearsTo = listSplit(mdlWikidata.claim(wdParameters)) or nil
	
	-- teams
	wdParameters["args"]["qualifier"] = nil
	wdParameters["args"]["parameter"] = "link"
	listTeams = listSplit(mdlWikidata.claim(wdParameters)) or nil
	wdParameters["args"]["parameter"] = nil
	
	-- teams' Id
	wdParameters["args"]["qualifier"] = nil
	wdParameters["args"]["parameter"] = "numeric-id"
	listTeamsId = listSplit(mdlWikidata.claim(wdParameters)) or nil
	wdParameters["args"]["parameter"] = nil
	
	-- games
	wdParameters["args"]["qualifier"] = "P1350"
	listGames = listSplit(mdlWikidata.claim(wdParameters)) or nil
	
	-- goals
	wdParameters["args"]["qualifier"] = "P1351"
	listGoals = listSplit(mdlWikidata.claim(wdParameters)) or nil
	
	--loan
	wdParameters["args"]["qualifier"] = "P1642"
	listLoan = listSplit(mdlWikidata.claim(wdParameters)) or nil
	
	-- position held (fuer Goalietrainer, Juniorentrainer, Spielertrainer,...)
	wdParameters["args"]["qualifier"] = "P39"
	listPositionHeld = listSplit(mdlWikidata.claim(wdParameters)) or nil

	-- competition class (fuer Juniorenteams)
	wdParameters["args"]["qualifier"] = "P2094"
	listCompetitionClass = listSplit(mdlWikidata.claim(wdParameters)) or nil
end

function listSplit(pList)
	if pList then
		return mw.text.split(pList, ";")	
	else
		return false
	end
end

function isNationalteam(pId)
	-- find out whether item with pId is nationalteam
	-- return true if pId is nationalteam, else false.
	-- very expensive function!
	
	if not pId or string.len(pId) < 1 then return false end
	if string.sub(pId, 1, 1) ~= "Q" then pId = "Q" .. pId end
	
	local wdParameters = {}
	wdParameters["args"] = {}
	
	wdParameters["args"]["id"] = pId
	wdParameters["args"][1] = "P31"
	wdParameters["args"]["parameter"] = "numeric-id"
	wdParameters["args"]["list"] = ";"
	local wdResult = mdlWikidata.claim(wdParameters)
	
	local teamParents, teamParentsId
	teamParents = listSplit(wdResult)
	
	for i in pairs(teamParents) do
		if string.sub(teamParents[i], 1, 1) ~= "Q" 
			then teamParentsId = "Q" .. teamParents[i] 
			else teamParentsId = teamParents[i]
		end
		if string.len(teamParentsId) >= 2 then
			-- check if Q6979593 (football national team) is parent
			if teamParentsId == "Q6979593" then return true end
			wdParameters["args"] = {}
			wdParameters["args"]["id"] = teamParentsId
			mw.log(teamParentsId)
			wdParameters["args"]["parent"] = "Q6979593"
			wdParameters["args"]["exitItem"] = "Q12973014"
			wdParameters["args"]["maxDepth"] = 3
			if mdlWikidata.isSubclass(wdParameters) then return true end
		end
	end

	return false
end

function getLinkNationalteam(pId)
	local strLemmaArticle
	local strTeamTitle
	local strTeamCountry
	local strUnderage
	local strUnderageItem
	local strLinkTitle
	local wdParameters = {}
	
	-- get sitelink
	wdParameters["args"] = nil
	wdParameters["args"] = {}
	wdParameters["args"][1] = "Q" .. pId
	strLemmaArticle = mdlWikidata.sitelinkOf(wdParameters)
	
	-- get title in local language
	wdParameters["args"] = nil
	wdParameters["args"] = {}
	wdParameters["args"][1] = "Q" .. pId
	strTeamTitle = mdlWikidata.labelOf(wdParameters)
	
	-- get information about national team
	wdParameters["args"] = nil
	wdParameters["args"] = {}
	wdParameters["args"]["id"] = "Q" .. pId
	wdParameters["args"][1] = "P1532"
	strTeamCountry = mdlWikidata.claim(wdParameters) or ""
	
	-- problem: for underage national teams, there is no property to express this, yet. Therefore the information has to be triggered from the sitelink ...
	-- property to be used: P2094
	
	wdParameters["args"] = nil
	wdParameters["args"] = {}
	wdParameters["args"]["id"] = "Q" .. pId
	wdParameters["args"][1] = "P2094"
	strUnderageItem = mdlWikidata.claim(wdParameters) or ""
	
	-- if P2094 exists, look if it indicates an underage team, otherwise also proof the teamtitle
	if string.len(strUnderageItem) > 0 then
		if strUnderageItem then strUnderage = mw.ustring.match(strUnderageItem, "U[0-9]+") or mw.ustring.match(strUnderageItem, "U%-[0-9]+") or false end
		if strUnderage then strUnderage = mw.ustring.gsub(strUnderage, "-", " ") end
		if strUnderage then
			strLinkTitle = strTeamCountry .. " (" .. strUnderage .. ")"
		else
			strLinkTitle = strTeamCountry
		end
	else 
		if strTeamTitle then strUnderage = mw.ustring.match(strTeamTitle, "U%-[0-9]+") or false end
		if strUnderage then strUnderage = mw.ustring.gsub(strUnderage, "-", " ") end
		if strUnderage then
			strLinkTitle = strTeamCountry .. " (" .. strUnderage .. ")"
		else
			strLinkTitle = strTeamCountry
		end
	end
	
	if strLemmaArticle and string.len(strLemmaArticle) > 0 then	
		return "[[" .. strLemmaArticle .. "|" .. strLinkTitle .. "]]"
	else
		return strLinkTitle
	end
end

function triggerSingleLine(pElements, pTrainerTeams, pIsNationalteam)
	local strTeam
	local strLoanArrow = "→"
	local strLoanAdd = "(Leihe)"
	local strPositionHeld
	local strLine = "\n|style=\"padding:0;\" class=\"wikidata-content\"| %s \n|style=\"padding:0 0 0 10px;\" class=\"wikidata-content\"| %s %s %s \n|style=\"padding:0;text-align:right;\" class=\"wikidata-content\" | %s\n|-\n"
	local strTime
	local strGamesGoals

	if (pTrainerTeams) then
		strLine = "\n|style=\"padding:0;\" class=\"wikidata-content\"| %s \n|style=\"padding:0 0 0 10px;\" class=\"wikidata-content\" colspan=\"2\"| %s %s \n|-\n"
	end
	
	if not pElements["teams"] then return false end
	
	if string.len(pElements["from"]) > 0 and string.len(pElements["to"]) > 0 and pElements["from"] < pElements["to"] then
		strTime = pElements["from"] .. "–" .. pElements["to"]
	elseif string.len(pElements["from"]) > 0 and not (string.len(pElements["to"]) > 0) then
		strTime = pElements["from"] .. "–"
	elseif not (string.len(pElements["from"]) > 0) and string.len(pElements["to"]) > 0 then
		strTime = "<span style=\"visibility:hidden;\">0000</span>–" .. pElements["to"]
	else
		strTime = tostring(pElements["from"])
	end
	
	-- create link for national teams
	if pIsNationalteam then
		strTeam = getLinkNationalteam(pElements["teamsId"])
	else
		strTeam = pElements["teams"]
	end

	if (pTrainerTeams) then
		strTeam = strTeam .. " " .. pElements["competitionclass"]
	end
	
	if not pElements["loan"] or string.len(pElements["loan"]) == 0 then
		strLoanArrow = ""
		strLoanAdd = ""
	end
	
	if string.len(pElements["games"]) > 0 and string.len(pElements["goals"]) > 0 then
		strGamesGoals = tostring(pElements["games"]) .. "&nbsp;(" .. tostring(pElements["goals"]) .. ")"
	elseif string.len(pElements["games"]) > 0 then
		strGamesGoals = tostring(pElements["games"]) .. "<span style=\"visibility:hidden;\">(0)</span>"
	else
		strGamesGoals = ""
	end

	if not pElements["positionheld"] or string.len(pElements["positionheld"]) == 0 then
		strPositionHeld = ""
	else
		strPositionHeld = " (" .. pElements["positionheld"] .. ")"
	end
	
	if (pTrainerTeams) then
		return mw.ustring.format(strLine, strTime, strTeam, strPositionHeld)
	else
		return mw.ustring.format(strLine, strTime, strLoanArrow, strTeam, strLoanAdd, strGamesGoals)
	end
end

local function triggerList(pNationalTeams, pTrainerTeams)
	strResult = ""
	local resultIsNationalteam
	
	if type(listTeams) ~= "table" or table.getn(listTeams) == 0 then return "" end 
	
	for i in pairs(listTeams) do
		resultIsNationalteam = isNationalteam(listTeamsId[i])
		if pTrainerTeams or (resultIsNationalteam and pNationalTeams) or (not resultIsNationalteam and not pNationalTeams) then
			params = nil
			params = {}
			params["from"] = listYearsFrom[i]
			params["to"] = listYearsTo[i]
			params["teams"] = listTeams[i]
			params["teamsId"] = listTeamsId[i]
			params["loan"] = listLoan[i]
			params["games"] = listGames[i]
			params["goals"] = listGoals[i]
			params["positionheld"] = listPositionHeld[i]
			params["competitionclass"] = listCompetitionClass[i]
			strResult = strResult .. triggerSingleLine(params, pTrainerTeams, resultIsNationalteam)
		end
	end
	
	-- add links to Wikidata item
	local wdPlayerId;
	local wdParameters = {}
	wdParameters["args"] = {}
	wdParameters["args"][1] = nil;
	wdPlayerId = mdlWikidata.pageId(wdParameters)
	strResult = strResult .. "| colspan=\"3\" style=\"font-size:0.8em; text-align:right; color: #555555;\" | [[wikidata:" .. wdPlayerId .. "#P54|auf Wikidata bearbeiten]]\n|-";
	
	return strResult
end

function p.Birthplace()
	local i = 1
	local strBirthPlaceLink
	local BirthPlaceId
	local InstanceOfId
	local wdParameters = {}
	strOutput = ""

	-- get P19 (birthplace)' Id of player
	wdParameters["args"] = nil
	wdParameters["args"] = {}
	wdParameters["args"][1] = "P19"
	wdParameters["args"]["parameter"] = "link"
	strBirthPlaceLink = mdlWikidata.claim(wdParameters) or ""
	wdParameters["args"]["parameter"] = "numeric-id"
	BirthPlaceId = mdlWikidata.claim(wdParameters) or nil
	
	-- abort and return empty string if there is no birthplace in Wikidata item
	if BirthPlaceId == nil then return "" end

        -- get P31 (instance of) of birthplace
	wdParameters["args"] = nil
	wdParameters["args"] = {}
	wdParameters["args"][1] = "P31"
	wdParameters["args"]["id"] = "Q" .. BirthPlaceId
	wdParameters["args"]["parameter"] = "numeric-id"
	wdParameters["args"]["list"] = ";"
	InstanceOfId = listSplit(mdlWikidata.claim(wdParameters)) or nil

        -- check out every P31 for Q6256 ("Land")
	repeat
		if InstanceOfId[i] == "6256" then
			-- set output to Wikilink of birthplace, which is a country
		        strOutput = strBirthPlaceLink
                end
		i = i + 1
	until i > 99 or i > #InstanceOfId

        -- if birthplace is not a country, give out birthplace and country
	if strOutput ~= strBirthPlaceLink then
		-- get P17 (country) of birthplace
		wdParameters["args"] = nil
		wdParameters["args"] = {}
		wdParameters["args"][1] = "P17"
		wdParameters["args"]["id"] = "Q" .. BirthPlaceId
		wdParameters["args"]["parameter"] = "link"
		strBirthCountryLink = mdlWikidata.claim(wdParameters) or ""

		strOutput = strBirthPlaceLink .. ", " .. strBirthCountryLink
	end 

	return strOutput
end

function p.Deathplace()
	local i = 1
	local strDeathPlaceLink
	local DeathPlaceId
	local InstanceOfId
	local wdParameters = {}
	strOutput = ""

	-- get P20 (deathplace)' Id of player
	wdParameters["args"] = nil
	wdParameters["args"] = {}
	wdParameters["args"][1] = "P20"
	wdParameters["args"]["parameter"] = "link"
	strDeathPlaceLink = mdlWikidata.claim(wdParameters) or ""
	wdParameters["args"]["parameter"] = "numeric-id"
	DeathPlaceId = mdlWikidata.claim(wdParameters) or nil
	
	-- abort and return empty string if there is no birthplace in Wikidata item
	if DeathPlaceId == nil then return "" end

        -- get P31 (instance of) of deathplace
	wdParameters["args"] = nil
	wdParameters["args"] = {}
	wdParameters["args"][1] = "P31"
	wdParameters["args"]["id"] = "Q" .. DeathPlaceId
	wdParameters["args"]["parameter"] = "numeric-id"
	wdParameters["args"]["list"] = ";"
	InstanceOfId = listSplit(mdlWikidata.claim(wdParameters)) or nil

        -- check out every P31 for Q6256 ("country")
	repeat
		if InstanceOfId[i] == "6256" then
			-- set output to Wikilink of deathplace, which is a country
		        strOutput = strDeathPlaceLink
                end
		i = i + 1
	until i > 99 or i > #InstanceOfId

        -- if deathplace is not a country, give out deathplace and country
	if strOutput ~= strDeathPlaceLink then
		-- get P17 (country) of birthplace
		wdParameters["args"] = nil
		wdParameters["args"] = {}
		wdParameters["args"][1] = "P17"
		wdParameters["args"]["id"] = "Q" .. DeathPlaceId
		wdParameters["args"]["parameter"] = "link"
		strDeathCountryLink = mdlWikidata.claim(wdParameters) or ""

		strOutput = strDeathPlaceLink .. ", " .. strDeathCountryLink
	end 

	return strOutput
end


function p.Teamliste(frame)
	local pNationalTeams = frame.args["nationalteams"] or false
	local pTrainerTeams = frame.args["trainerteams"] or false
	wdLoadStations(pTrainerTeams)
	return triggerList(pNationalTeams, pTrainerTeams)
end

function p.Update(frame)
	local pNationalTeams = frame.args["nationalteams"] or false
	local wdResultId, wdResultTime
	local i = 1
	local wdParams = {}
	wdParams["args"] = {}
	
	-- get all snaks of team stations without end date, ordered by start date descending, check for nationalteam, return first time value
	wdParams["args"][1] = "P54"
	wdParams["args"]["hasqualifier"] = "P585"
	wdParams["args"]["sort"] = "P580"
	wdParams["args"]["inverse"] = true
	wdParams["args"]["parameter"] = "numeric-id"
	wdParams["args"]["list"] = ";"
	wdResultId = listSplit(mdlWikidata.claim(wdParams))
		
	wdParams["args"]["parameter"] = "time:Y-m-d"
	wdParams["args"]["qualifier"] = "P585"
	wdResultTime = listSplit(mdlWikidata.claim(wdParams))
	
	if not (wdResultId and wdResultTime) then return "" end
	repeat
		if wdResultId[i] and isNationalteam(wdResultId[i]) == not not pNationalTeams and wdResultTime[i] and string.len(tostring(wdResultTime[i])) > 0 then
			return wdResultTime[i]
		end
		i = i + 1
	until i > 99 or i > #wdResultId
end

return p