Modul:Benutzer:Fundriver/Vorlage:Infobox Fußballspieler test

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Die Dokumentation für dieses Modul kann unter Modul:Benutzer:Fundriver/Vorlage:Infobox Fußballspieler test/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"]["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
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 Q1194951 (national team) is parent
			wdParameters["args"] = {}
			wdParameters["args"]["id"] = teamParentsId
			wdParameters["args"]["parent"] = "Q1194951"
			wdParameters["args"]["exitItem"] = "Q12973014"
			wdParameters["args"]["maxDepth"] = 5
			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)
	local strTeam
	local strLoanArrow = "→"
	local strLoanAdd = "(Leihe)"
	local strPositionHeld
	local strLine = "\n|style=\"padding:0;\" class=\"wikidata-content\"| %s \n|style=\"padding:0;\" 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;\" 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"] .. "–"
	else
		strTime = tostring(pElements["from"])
	end
	
	-- create link for national teams
	if isNationalteam(pElements["teamsId"]) then
		strTeam = getLinkNationalteam(pElements["teamsId"])
	else
		strTeam = pElements["teams"]
	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 (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]
			strResult = strResult .. triggerSingleLine(params, pTrainerTeams)
		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.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