Jump to content

Module:Sandbox/BartVaes/Bananas

From Wikipedia, the free encyclopedia

-- Testmodule to generate the international goals by <<player>> in football player articles.
-- TODO: add references (tags), incl. global ref for whole table
local p = {}
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')

function p.hello( frame )
    -- Declare locals
	local Args = getArgs(frame, {parentFirst = true})
	local final_result = {}
	local index = 0
	local goals_count = 0
	local entries_list = {}
	
	-- Temp check for testcases
	if (Args["test"] ~= nil) then
		return "No parameters found."
	end
	
	-- Temp check for data groups
	if (Args["data5"] ~= nil) then
		mw.log("data group = " .. type(Args["data5"]))
	end
	
	--Get all the entries in nested table, index start at 1
	while (Args["date"..index+1] ~= nil) do
		index = index+1
		entries_list[index] = {
			date=Args["date"..index],
			stadium=Args["stadium"..index],
			city=Args["city"..index],
			country=Args["country"..index],
			opp=Args["opp"..index],
			scores=Args["scores"..index],
			result=Args["result"..index],
			comp=Args["comp"..index],
			comp_alt=Args["comp_alt"..index],
			home=Args["home"..index],
			ref=Args["ref"..index],
			link=Args["link"..index]
		}
	end
	mw.logObject(entries_list)
	
	--Only 1 header
	local header = p.get_header(frame, Args)
	table.insert(final_result, string.format("%s", header))
	
	--Only 1 type/gender
	local type = string.lower(Args["type"])
	
	index = 0
	while (entries_list[index+1] ~= nil) do
		index = index+1
		
		--increment goals
    	goals_count = goals_count + 1
		
		--Add date col
		local date_col = frame:expandTemplate{ title="dts", args = { format="dmy", entries_list[index]["date"] } }
		
		--Add venue col+check if wikilink format is needed (default yes)
		local venue_col = p.check_wikilink({
			link=entries_list[index]["link"],
			stadium=entries_list[index]["stadium"],
			city=entries_list[index]["city"],
			country=entries_list[index]["country"]
		})
		
		--Add opponent col (check type/gender)
		local opp_col = nil
		
		switch(type, { 
        	["m"] = function()	-- for male team
				opp_col = frame:expandTemplate{title="fb", args = {entries_list[index]["opp"]}}
			end,
			["w"] = function()	-- for female team
				opp_col = frame:expandTemplate{title="fbw", args = {entries_list[index]["opp"]}}
			end
			--Possible other options (under-{age} teams if needed)
			--["u19"] = function()	-- for case 3
			--	opp_col = frame:expandTemplate{title="fb", args = {19, entries_list[index]["opp"]}}
			--end,
			--["wu21"] = function()	-- for case 3
			--	opp_col = frame:expandTemplate{title="fbwu", args = {21, entries_list[index]["opp"]}}
			--end
		})
		
		--Add scores -> determines if/how many rowspan is needed
		local scores = mw.text.split(entries_list[index]["scores"], ",")
		local rowspan = #scores
		
		--check home/away for scores, default home=yes; format accordingly -> player goals = bold
		local is_home = yesno(entries_list[index]["home"] or "yes") and true or false
		
		--Add result
		local result_col = p.format_score(mw.text.split(entries_list[index]["result"], "-"), is_home)
		
		--Add competition
    	local comp_col = nil
    	if (entries_list[index]["comp_alt"] ~= nil) then
    		comp_col = string.format("[[%s|%s]]", entries_list[index]["comp"], entries_list[index]["comp_alt"])
    	else
    		 comp_col = string.format("[[%s]]", entries_list[index]["comp"])
    	end
    	
    	--Add references (cite_web)
    	local ref_col = p.format_ref(frame, entries_list[index]["ref"], index)
    	
    	--Put all in rows. If rowspan > 1 means that multiple scorelines have to be 'rowspanned'
    	if (rowspan > 1) then
    		score_col = p.format_score(mw.text.split(scores[1], "-"), is_home)
    		
    		--Treat 1st row as regular single row
    		local single_row = p.get_single_row({
    			goals=goals_count,
    			date=date_col,
    			venue=venue_col,
    			oppo=opp_col,
    			score=score_col,
    			result=result_col,
    			comp=comp_col,
    			ref=ref_col}, rowspan)
    		table.insert(final_result, string.format("\n|-\n%s", single_row))
    	
    		--loop over remaining scores, remove the 1st one
    		table.remove(scores , 1)
    		--mw.logObject(scores)
    	
    		--Add rest of the rowspanned rows
	    	for k,v in ipairs(scores) do
	    		--increment goals count
	    		goals_count = goals_count + 1
	    		
	    		score_col = p.format_score(mw.text.split(v, "-"), is_home)
	    		local single_row2 = string.format("|style='text-align:center;'|%d||style='text-align:center;'|%s", goals_count, score_col)
		    	
	    		local rows = string.format("\n|-\n%s", single_row2)
	    		table.insert(final_result, rows)
	    	end
	    else
	    	--no rowspan = only 1 score to format
	    	score_col = p.format_score(mw.text.split(scores[1], "-"), is_home)
	    	
	    	--Regular single row
    		local single = p.get_single_row({
    			goals=goals_count,
    			date=date_col,
    			venue=venue_col,
    			oppo=opp_col,
    			score=score_col,
    			result=result_col,
    			comp=comp_col,
    			ref=ref_col})
    		table.insert(final_result, string.format("\n|-\n%s", single))
	    end
	end
	table.insert(final_result, string.format("\n|-\n"))
	table.insert(final_result, string.format(frame:expandTemplate{title="end"}))
	--mw.log("goals=" .. goals_count)
	mw.logObject(final_result)
	return table.concat(final_result)
end

function p.get_header( frame, fields )
	local header = frame:expandTemplate{title="International goals header", args = { above=string.format("List of international goals scored by %s", fields["name"]), 
		ref=yesno(fields["ref1"] or "no")}}
	local disclaimer = string.format(":''Scores and results list %s's goal tally first, score column indicates score after each %s goal.''", fields["name"], fields["name"])
	return string.format("%s\n%s", disclaimer, header)
end

-- Get single table row, with optional rowspan
function p.get_single_row( fields, row_span )
	if (row_span ~= nil) then
		return string.format("|style='text-align:center;'|%d||rowspan=%d|%s||rowspan=%d|%s||rowspan=%d|%s||style='text-align:center;'|%s||rowspan=%d style='text-align:center;'|%s||rowspan=%d|%s||rowspan=%d|%s", 
		fields["goals"], row_span, fields["date"], row_span, fields["venue"], row_span, fields["oppo"], fields["score"], row_span, fields["result"], row_span, fields["comp"], row_span, fields["ref"])
	else
		return string.format("|style='text-align:center;'|%d||%s||%s||%s||style='text-align:center;'|%s||style='text-align:center;'|%s||%s||%s",
		fields["goals"], fields["date"], fields["venue"], fields["oppo"], fields["score"], fields["result"], fields["comp"], fields["ref"])
	end
end

-- Properly format score/result text (en dash) and inverse away goals
function p.format_score( scores, is_home )
	if (is_home) then
		return string.format("%s—%s", scores[1], scores[2])
	else
    	return string.format("%s—%s", scores[2], scores[1])
    end
end

-- Properly format references
function p.format_ref( frame, field, index )
	-- Or: frame:expandTemplate{ title = 'efn', args = { group='lower-alpha',  name=note_id, note_local} }
	return frame:expandTemplate{ title = "refn", args = { group = "lower-alpha", name = field..index, field} }
end

-- Check if venue text needs to be wikilinked
function p.check_wikilink( fields )
	local add_link = yesno(fields["link"] or "yes") and true or false
	if (add_link) then
		return string.format("[[%s]], [[%s]], %s", fields["stadium"], fields["city"], fields["country"])
	else
		 return string.format("%s, %s, %s", fields["stadium"], fields["city"], fields["country"])
	end
end

-- Switch/case struct for types
_G.switch = function(param, case_table)
    local case = case_table[param]
    if case then return case() end
    local def = case_table['default']
    return def and def() or nil
end

return p