Vorlagenprogrammierung Diskussionen Lua Test Unterseiten
Modul Deutsch English

Modul: Dokumentation

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus


local p = {}

	-- ISO country codes with country name (1), chart link (2), 									abbreviated chart publisher (3), certification publisher link (4),							abbreviated certification publisher (5), no. 1 list ID (6), no. 1 list colour scheme (7)
	local countrytable = {
		["AUT"] = { "Österreich",				"Ö3 Austria Top 40",								"Ö3",							"IFPI Austria – Verband der Österreichischen Musikwirtschaft",							"IFPI", 		"Österreich" },
		["ARG"] = { "Argentinien",				"Argentinien",										"CAPIF",						"Cámara Argentina de Productores de Fonogramas y Videogramas",							"CAPIF",		"Argentinien",			"2" },
		["AUS"] = { "Australien",				"Australische Musikcharts",							"ARIA", 						"Australian Recording Industry Association",											"ARIA", 		"Australien" },
		["BEL"] = { "Belgien",					"Musikcharts in Belgien",							"Ultratop", 					"Belgian Recorded Music Association",													"BRMA",			"Belgien",				"3" },
		["BGR"] = { "Bulgarien",				"Bulgarien",										"BAMP", 						"Bulgarian Association of the Music Producers", 										"BAMP", 		"Bulgarien",			"4" },
		["BRA"] = { "Brasilien",				"Brasilien",										"PMB",  						"Pro-Música Brasil",																	"PMB",			"Brasilien",			"4" },
		["CAN"] = { "Kanada",					"Kanadische Musikcharts",							"MC",							"Music Canada", 																		"MC",			"Kanada" },
		["CHE"] = { "Schweiz",					"Schweizer Hitparade",								"IFPI", 						"IFPI Schweiz", 																		"IFPI", 		"der Schweiz" },
		["CHL"] = { "Chile",					"Chile",											nil,							"IFPI Chile",																			"IFPI" },
		["COL"] = { "Kolumbien",				"Kolumbien",										"APDIF",						"Asociación Colombiana de Productores de Fonogramas",									"ASINCOL",		"Kolumbien",			"3" },
		["CZE"] = { "Tschechien",				"Tschechien",										"IFPI", 						"IFPI Tschechien",																		"IFPI", 		"Tschechien" },
		["DEU"] = { "Deutschland",				"Deutsche Musikcharts",								"GfK",							"Bundesverband Musikindustrie", 														"BVMI", 		"Deutschland",			"3" },
		["DNK"] = { "Dänemark",					"Dänische Musikcharts",								"IFPI/Nielsen",					"IFPI Dänemark",																		"IFPI", 		"Dänemark" },
		["ECU"] = { "Ecuador",					"Ecuador",											nil,							"Sociedad de Productores de Fonogramas",												"SOPROFON" },
		["EGY"] = { "Ägypten",					"Ägypten",											"IFPI",							"IFPI Ägypten",												                            "IFPI", 		"Ägypten" },
		["ESP"] = { "Spanien",					"Spanische Musikcharts",							"Promusicae",					"Productores de Música de España",														"Promusicae",	"Spanien", "3" },
		["EUR"] = { "Europa",					"Europa",											nil,							"International Federation of the Phonographic Industry",								"IFPI" },
		["FIN"] = { "Finnland",					"Finnische Musikcharts",							"IFPI",							"Musiikkituottajat – IFPI Finland", 													"IFPI", 		"Finnland" },
		["FRA"] = { "Frankreich",				"Frankreich",										"SNEP",							"Syndicat National de l’édition Phonographique",										"SNEP", 		"Frankreich" },
		["GRC"] = { "Griechenland",				"IFPI Griechenland#Musikcharts",					"IFPI",							"IFPI Griechenland",																	"IFPI", 		"Griechenland", 		"2" },
		["HKG"] = { "Hongkong",					"Hongkong",											nil,							"IFPI Hong Kong Group", 																"IFPI/HKRIA" },
		["HUN"] = { "Ungarn",					"Ungarn",											"MAHASZ",						"Magyar Hanglemezkiadók Szövetsége",													"MAHASZ",		"Ungarn",				"4" },
		["HRV"] = { "Kroatien",					"Kroatien",											"HDU",							"Hrvatska diskografska udruga", 														"HDU",			"Kroatien" },
		["IDN"] = { "Indonesien",				"Indonesien",										nil,							"Asosiasi Industri Rekaman Indonesia",													"ASIRI" },
		["IND"] = { "Indien",					"Indien",											"IMI",							"The Indian Music Industry",															"IMI",			"Indien",				"4"	},
		["IRL"] = { "Irland",					"Irische Musikcharts",								"IRMA",							"Irish Recorded Music Association", 													"IRMA", 		"Irland",				"4" },
		["ISL"] = { "Island",					"Island",											nil,							"IFPI Island",																			"IFPI" },
		["ISR"] = { "Israel",					"Israel",											nil,							"IFPI Israel",																			"IFPI" },
		["ITA"] = { "Italien",					"Italienische Musikcharts",							"FIMI",							"Federazione Industria Musicale Italiana",												"FIMI", 		"Italien",				"4" },
		["JPN"] = { "Japan",					"Japanische Musikcharts",							"Oricon",						"Recording Industry Association of Japan",												"RIAJ", 		"Japan" },
		["KOR"] = { "Südkorea",					"Circle Chart",										"KMCA",							"Korea Music Content Association",														"KMCA", 		"Südkorea" },
		["LBN"] = { "Libanon",					"Libanon",											nil,							"IFPI Middle East Awards",																"IFPI" },
		["LVA"] = { "Lettland",					"Lettland",											nil,							"Latvian Music Producers Association",													"LaMPA" },
		["LTU"] = { "Litauen",					"AGATA (Organisation)#Charts",						"AGATA",						nil,																					nil,			"Litauen" },
		["MEX"] = { "Mexiko",					"Mexiko",											"AMPROFON",						"Asociación Mexicana de Productores de Fonogramas y Videogramas",						"AMPROFON", 	"Mexiko",				"4" },
		["MYS"] = { "Malaysia",					"Malaysia",											"RIM",							"Recording Industry Association of Malaysia",											"RIM",			"Malaysia" },
		["NLD"] = { "Niederlande",				"Niederländische Musikcharts",						"Media Markt / Dutch Charts",	"Nederlandse Vereniging van Producenten en Importeurs van beeld- en geluidsdragers",	"NVPI", 		"den Niederlanden" },
		["NGA"] = { "Nigeria",					"Nigeria",											nil,							"TurnTable Certification System of Nigeria",											"TCSN" },
		["NOR"] = { "Norwegen",					"Norwegische Musikcharts",							"IFPI",							"IFPI Norwegen",																		"IFPI", 		"Norwegen" },
		["NZL"] = { "Neuseeland",				"Neuseeländische Musikcharts",						"RMNZ", 						"Recorded Music NZ",																	"RMNZ", 		"Neuseeland" },
		["PER"] = { "Peru",						"Peru",												nil,							"Unión Peruana de Productores Fonográficos",											"UNIMPRO" },
		["PHL"] = { "Philippinen",				"Philippinen",										nil,							"Kapisanan ng Industriya ng Plaka ng Pilipinas",										"PARI" },
		["POL"] = { "Polen",					"Związek Producentów Audio-Video#Musikcharts",		"ZPAV", 						"Związek Producentów Audio-Video",														"ZPAV", 		"Polen",				"5" },
		["PRT"] = { "Portugal",					"Associação Fonográfica Portuguesa#Musikcharts",	"AFP",							"Associação Fonográfica Portuguesa",													"AFP",			"Portugal", 			"4" },
		["ROU"] = { "Rumänien",					"Uniunea Producătorilor de Fonograme din România",	"UFPR", 						"Uniunea Producătorilor de Fonograme din România",										"UFPR", 		"Rumänien" },
		["RUS"] = { "Russland",					"Russland",											nil,							"National Federation of Phonograph Producers",											"NFPF" },
		["SVK"] = { "Slowakei",					"Slowakei",											"IFPI", 						"IFPI Tschechien",																		"IFPI", 		"der Slowakei" },
		["SVN"] = { "Slowenien",				"Slowenien",										"SAZAS",						nil,																					nil,			"Slowenien" },
		["SWE"] = { "Schweden",					"Schwedische Musikcharts",							"GLF",							"IFPI Schweden",																		"IFPI", 		"Schweden" },
		["SGP"] = { "Singapur",					"Singapur",											"RIAS", 						"Recording Industry Association (Singapore)",											"RIAS", 		"Singapur" },
		["THA"] = { "Thailand",					"Thailand",											nil,							"Thai Entertainment Content Trade Association", 										"TECA" },
		["TUR"] = { "Türkei",					"Türkei",											nil,							"Mü-YAP",																				"Mü-YAP" },
		["TWN"] = { "Taiwan",					"Republik China (Taiwan)",							nil,							"Recording Industry Foundation in Taiwan",												"RIT" },
		["GBR"] = { "Vereinigtes Königreich",	"Britische Musikcharts",							"OCC",							"British Phonographic Industry",														"BPI",			"den britischen Charts" },
		["USA"] = { "Vereinigte Staaten",		"Billboard-Charts",									"Billboard",					"Recording Industry Association of America",											"RIAA", 		"den USA" },
		["URY"] = { "Uruguay",					"Uruguay",											nil,							"Cámara Uruguaya del Disco",															"CUD" },
		["VEN"] = { "Venezuela",				"Venezuela",										nil,							"Association of Venezuelan Phonograph Producers",										"APFV" },
		["ZAF"] = { "Südafrika",				"Südafrika",										"RISA",							"The Recording Industry of South Africa",												"RISA",			"Südafrika" },

		-- special cases
		["EUR2"]  = { "Europa", 				"Europa",											nil,							"Impala (Verband)#Impala Sales Awards", 												"Impala" },
		["FRA2"]  = { "Frankreich",				"Frankreich",										nil,							"Union des Producteurs Phonographiques Français Indépendants",							"UPFI" },
		["GCC"]   = { "Golf-Kooperationsrat",	"Golf-Kooperationsrat", 							nil,							"IFPI Middle East Awards",																"IFPI" },
		["CAM"]   = { "Zentralamerika",			"Zentralamerika", 									nil,							"Certificación Fonográfica Centroamericana",											"CFC" },
		["BEF"]   = { "Flandern",				"Musikcharts in Belgien",							"Ultratop", 					"Belgian Recorded Music Association",													"BRMA",			"Belgien", "3" },
		["BEW"]   = { "Wallonie",				"Musikcharts in Belgien",							"Ultratop", 					"Belgian Recorded Music Association",													"BRMA",			"Belgien", "3" },
		["DEU-A"] = { "Deutschland",			"Deutsche Albumcharts", 							"GfK",							"Bundesverband Musikindustrie", 														"BVMI", 		"Deutschland", "3" },
		["DEU-C"] = { "Deutschland",			"Deutsche Compilationcharts", 						"GfK",							"Bundesverband Musikindustrie", 														"BVMI", 		"Deutschland", "3" },
		["DEU-S"] = { "Deutschland",			"Deutsche Singlecharts",							"GfK",							"Bundesverband Musikindustrie", 														"BVMI", 		"Deutschland", "3" },
		["NLD-A"] = { "Niederlande",			"Dutch Charts", 									"Dutch Charts", 				"Nederlandse Vereniging van Producenten en Importeurs van beeld- en geluidsdragers",	"NVPI", 		"den Niederlanden" },
		["NLD-S"] = { "Niederlande",			"Nederlandse Top 40",								"Media Markt",					"Nederlandse Vereniging van Producenten en Importeurs van beeld- en geluidsdragers",	"NVPI", 		"den Niederlanden" },
		["USA-A"] = { "Vereinigte Staaten", 	"Billboard 200",									"Billboard",					"Recording Industry Association of America",											"RIAA", 		"den USA" },
		["USA-S"] = { "Vereinigte Staaten", 	"Billboard Hot 100",								"Billboard",					"Recording Industry Association of America",											"RIAA", 		"den USA" },

		-- US genre charts
		["USA-RB"]   = { "Vereinigte Staaten",	"Billboard-Charts", 								"Billboard R&B",				"Recording Industry Association of America",											"RIAA", 		"R&B-Alben in den USA" },
		["USA-RB-A"] = { "Vereinigte Staaten",	"Top R&B/Hip-Hop Albums",							"Billboard R&B",				"Recording Industry Association of America",											"RIAA", 		"R&B-Alben in den USA" },
		["USA-RB-S"] = { "Vereinigte Staaten",	"Hot R&B/Hip-Hop Songs",							"Billboard R&B",				"Recording Industry Association of America",											"RIAA" },
		["USA-C"]    = { "Vereinigte Staaten",	"Billboard-Charts", 								"Billboard Country",			"Recording Industry Association of America",											"RIAA", 		"Country-Alben in den USA" },
		["USA-C-A"]  = { "Vereinigte Staaten",	"Billboard Top Country Albums",						"Billboard Country",			"Recording Industry Association of America",											"RIAA", 		"Country-Alben in den USA" },
		["USA-C-S"]  = { "Vereinigte Staaten",	"Billboard Hot Country Songs",						"Billboard Country",			"Recording Industry Association of America",											"RIAA" },
		["USA-R"]    = { "Vereinigte Staaten",	"Billboard-Charts", 								"Billboard Rock",				"Recording Industry Association of America",											"RIAA" },
		["USA-R-A"]  = { "Vereinigte Staaten",	"Top Rock Albums",									"Billboard Rock",				"Recording Industry Association of America",											"RIAA" },
		["USA-R-S"]  = { "Vereinigte Staaten",	"Hot Rock Songs",									"Billboard Rock",				"Recording Industry Association of America",											"RIAA" },
		["USA-D"]    = { "Vereinigte Staaten",	"Billboard-Charts", 								"Billboard Dance",				"Recording Industry Association of America",											"RIAA" },
		["USA-D-A"]  = { "Vereinigte Staaten",	"Top Dance/Electronic Albums",						"Billboard Dance",				"Recording Industry Association of America",											"RIAA",			"den US-Dancecharts" },
		["USA-D-S"]  = { "Vereinigte Staaten",	"Dance/Electronic Songs",							"Billboard Dance",				"Recording Industry Association of America",											"RIAA",			"den US-Dancecharts" },
		["USA-L"]    = { "Vereinigte Staaten",	"Billboard-Charts", 								"Billboard Latin",				"Recording Industry Association of America",											"RIAA" },
		["USA-L-A"]  = { "Vereinigte Staaten",	"Billboard Top Latin Albums",						"Billboard Latin",				"Recording Industry Association of America",											"RIAA" },
		["USA-L-S"]  = { "Vereinigte Staaten",	"Billboard Hot Latin Songs",						"Billboard Latin",				"Recording Industry Association of America",											"RIAA" },
		["USA-AC"]   = { "Vereinigte Staaten",	"Adult Contemporary (Charts)",						"Billboard Adult Contemporary", "Recording Industry Association of America",											"RIAA" },
		["USA-J"]    = { "Vereinigte Staaten",	"Jazz Albums",										"Billboard Jazz Albums",		"Recording Industry Association of America",											"RIAA" }
	}
	
	-- start album charts (1), start single charts (2), end album charts (3), end single charts (4) ...
	local chartdatetable = {
		["AUS"] 	= { 1965, 1940 },
		["AUT"] 	= { 1970, 1964, nil,  1971, nil,  1973 },
		["BEL"] 	= { 1995, 1954 },
		["BEF"] 	= { 1995, 1954 },
		["BEW"] 	= { 1995, 1956, nil,  1977, nil,  1995 },
		["BGR"] 	= { 2009, 2009, 2015, 2015, 2017, 2017 },
		["BRA"]		= { 2009, 2017, 2017 },
		["CAN"] 	= { 1968, 1957 },
		["CHE"] 	= { 1983, 1968 },
		["DEU"] 	= { 1962, 1954 },
		["DNK"] 	= { 1993, 1993 },
		["ESP"] 	= { 2005, 1999 },
		["FIN"] 	= { 1995, 1995 },
		["FRA"] 	= { 1984, 1984 },
		["GBR"] 	= { 1956, 1952 },
		["GRC"] 	= { 2002, 2001, nil,  2008, nil,  2014 },
		["IND"]		= { nil,  2021 },
		["IRL"] 	= { 2000, 1962 },
		["ITA"] 	= { 1964, 1960 },
		["JPN"] 	= { 1989, 1968 },
		["KOR"] 	= { 1999, 1999, 2008, 2008, 2010, 2010 },
		["LTU"] 	= { 2017, 2017 },
		["MEX"] 	= { 2005, 2017 },
		["MYS"] 	= { nil,  2017 },
		["NLD"] 	= { 1969, 1965 },
		["NOR"] 	= { 1967, 1958 },
		["NZL"] 	= { 1975, 1975 },
		["POL"] 	= { 2000, 2010 },
		["PRT"] 	= { 2003, 2000, nil,  2005, nil,  2012, nil,  2012, nil,  2016 },
		["ROM"] 	= { 1997, 1997, 2012, 2012 },
		["SGP"] 	= { 2001, 2018, 2005 },
		["SWE"] 	= { 1962, 1962 },
		["USA"] 	= { 1945, 1940 },
		["ZAF"]		= { nil,  2021 },
		["USA-AC"]	= { nil,  1961 },
		["USA-C"]	= { 1964, 1944 },
		["USA-D"]	= { 2001, 1974 },
		["USA-L"]	= { 1993, 1986 },
		["USA-R"]	= { 2006, 2009 },
		["USA-RB"]	= { 1965, 1942 }
	}
	
	-- flag info
	local countryflagtable = {
		["AUT"] 	= "Austria",
		["ARG"] 	= "Argentina",
		["AUS"] 	= "Australia",
		["BEL"] 	= "Belgium (civil)",
		["BE-VLG"]	= "Flanders",
		["BE-WAL"]	= "Wallonia",
		["BGR"] 	= "Bulgaria",
		["BRA"] 	= "Brazil",
		["CAN"] 	= "Canada",
		["CHE"] 	= "Switzerland within 2to3",
		["CHL"] 	= "Chile",
		["COL"] 	= "Colombia",
		["CZE"] 	= "Czech Republic",
		["DEU"] 	= "Germany",
		["DNK"] 	= "Denmark",
		["ECU"] 	= "Ecuador",
		["EGY"] 	= "Egypt",
		["ESP"] 	= "Spain",
		["EUR"] 	= "Europe",
		["FIN"] 	= "Finland",
		["FRA"] 	= "France",
		["GCC"] 	= "GCC Flag",
		["GRC"] 	= "Greece",
		["HKG"] 	= "Hong Kong",
		["HUN"] 	= "Hungary",
		["HRV"] 	= "Croatia",
		["IDN"] 	= "Indonesia",
		["IND"] 	= "India",
		["IRL"] 	= "Ireland",
		["ISR"] 	= "Israel",
		["ISL"] 	= "Iceland",
		["ITA"] 	= "Italy",
		["JPN"] 	= "Japan",
		["KOR"] 	= "South Korea",
		["LBN"] 	= "Lebanon",
		["LVA"] 	= "Latvia",
		["LTU"] 	= "Lithuania",
		["MEX"] 	= "Mexico",
		["MYS"] 	= "Malaysia",
		["NLD"] 	= "the Netherlands",
		["NGA"] 	= "Nigeria",
		["NOR"] 	= "Norway",
		["NZL"] 	= "New Zealand",
		["PER"] 	= "Peru",
		["PHL"] 	= "the Philippines",
		["POL"] 	= "Poland",
		["PRT"] 	= "Portugal",
		["ROU"] 	= "Romania",
		["RUS"] 	= "Russia",
		["SVK"] 	= "Slovakia",
		["SVN"] 	= "Slovenia",
		["SWE"] 	= "Sweden",
		["SGP"] 	= "Singapore",
		["THA"] 	= "Thailand",
		["TUR"] 	= "Turkey",
		["TWN"] 	= "the Republic of China",
		["GBR"] 	= "the United Kingdom",
		["USA"] 	= "the United States",
		["URY"] 	= "Uruguay",
		["VEN"] 	= "Venezuela",
		["ZAF"] 	= "South Africa"
	}
	
	-- colour schemes for countries in no1 table and related templates
	local colourtable = {
		["1"] = { "#5D80D0", "#9DB8F0", "#E9F0FF" },
		["2"] = { "#7BC5F7", "#C8E8FD", "#E4F4FF" },
		["3"] = { "#FFF090", "#FFFBB9", "#FFFBED" },
		["4"] = { "#3ACD3A", "#8AFC8A", "#E9FFE9" },
		["5"] = { "#EE6363", "#FF6A6A", "#FFC1C1" }
	}

	
	local lucky, ISO3166 = pcall( require, "Module:ISO3166" )
	if type( ISO3166 ) == "table" then
	ISO3166 = ISO3166.ISO3166()
	else
	-- In the event of errors, ISO3166 is an error message.
	return "<span class=\"error\">" .. ISO3166 .. "</span>"
	end


-- check existence of charts
p.chartexist = function (country, rformat, year, first)
	
	-- recognise countrycode
	local countryconv = ISO3166.f (country, "A3", true)
	if not countryconv then
		countryconv = "???"
	end
	if string.match (country, "US%-") then
		countryconv = string.gsub (country, "US", "USA")
	end
	local countrycode
	if chartdatetable[countryconv] then 
		countrycode = countryconv
	elseif chartdatetable[country] then
		countrycode = country
	end
	
	-- default rformat
	if not rformat then
		rformat = "Alben"
	end
	
	-- check existing years
	local firstyear
	local lastyear
	local firstyear2
	local lastyear2
	local firstyear3
	local lastyear3
	if countrycode then
		if rformat == "Singles" then
			firstyear = chartdatetable[countrycode][2]
			lastyear = chartdatetable[countrycode][4]
			firstyear2 = chartdatetable[countrycode][6]
			lastyear2 = chartdatetable[countrycode][8]
			firstyear3 = chartdatetable[countrycode][10]
			lastyear3 = chartdatetable[countrycode][12]
		else
			firstyear = chartdatetable[countrycode][1]
			lastyear = chartdatetable[countrycode][3]
			firstyear2 = chartdatetable[countrycode][5]
			lastyear2 = chartdatetable[countrycode][7]
			firstyear3 = chartdatetable[countrycode][9]
			lastyear3 = chartdatetable[countrycode][11]
		end
	end
	
	-- check given year
	local exist = true
	local yearformat
	if year then
		yearformat = string.match(year, "^(%d%d%d%d)")
	end
	if tonumber(yearformat) then
		if firstyear then
			if tonumber(yearformat) >= firstyear then
				exist = true
			else 
				exist = false
			end
		end
		if lastyear then
			if tonumber(yearformat) > lastyear then
				exist = false
			end
		end
		if firstyear2 then
			if tonumber(yearformat) >= firstyear2 then
				exist = true
			end
		end
		if lastyear2 then
			if tonumber(yearformat) > lastyear2 then
				exist = false
			end
		end
		if firstyear3 then
			if tonumber(yearformat) >= firstyear3 then
				exist = true
			end
		end
		if lastyear3 then
			if tonumber(yearformat) > lastyear3 then
				exist = false
			end
		end
	end
	
	if first then
		return firstyear
	else
		return exist
	end

end


-- correct chart links for single charts
p.singlechartlink = function (content)
	
	content = content
		:gsub ("Billboard 200", "Billboard Hot 100")
		:gsub ("Top R%&B%/Hip%-Hop Albums", "Hot R&B/Hip-Hop Songs")
		:gsub ("Nummer%-eins%-R%&B%-Alben", "Nummer-eins-R&B-Hits")
		:gsub ("Top Country Albums", "Hot Country Songs")
		:gsub ("Nummer%-eins%-Country%-Alben", "Nummer-eins-Country-Hits")
		:gsub ("Top Rock Albums", "Hot Rock Songs")
		:gsub ("Top Dance%/Electronic Albums", "Dance/Electronic Songs")
		:gsub ("Billboard Top Latin Albums", "Billboard Hot Latin Songs")
		:gsub ("Dutch Charts", "Nederlandse Top 40")
		:gsub ("Deutsche Albumcharts", "Deutsche Singlecharts")
	
	return content
end

-- create chart link, flag link and country shortcut
p.countryshort = function (country, rformat, display)
	if not country then
		country = ""
	end
	local countryconv = ISO3166.f (country, "A3", true)
	if not countryconv then
		countryconv = "???"
	end
	if string.match (country, "US%-") then
		countryconv = string.gsub (country, "US", "USA")
	end
	local countrycode
	if (string.match (countryconv, "USA") and countryconv ~= "USA-AC" and countryconv ~= "USA-J") or string.match (countryconv, "DEU") or string.match (countryconv, "NLD") then
		if rformat == "Singles" then
			countrycode = countrytable[(countryconv .. "-S")]
		else
			countrycode = countrytable[(countryconv .. "-A")]
		end
	else
		countrycode = countrytable[countryconv] or countrytable[country]
	end
	
	local chartlink
	local countrydisplay
	local flaglink
	
	if countrycode then
		chartlink = countrycode[2]
	end
	if country == "BEF" then
		countrydisplay = "BE<sup>F</sup>"
	elseif country == "BEW" then
		countrydisplay = "BE<sup>W</sup>"
	elseif string.match (countryconv, "USA%-RB") then
		countrydisplay = "R&B"
	elseif string.match (countryconv, "USA%-D") then
		countrydisplay = "Dance"
	elseif string.match (countryconv, "USA%-R") then
		countrydisplay = "Rock"
	elseif string.match (countryconv, "USA%-L") then
		countrydisplay = "La&shy;tin"
	elseif string.match (countryconv, "USA%-C") then
		countrydisplay = "Coun&shy;try"
		elseif string.match (countryconv, "USA%-J") then
		countrydisplay = "Jazz"
	elseif string.match (countryconv, "USA%-AC") then
		countrydisplay = "A.&nbsp;C."
	elseif countrycode then
		countrydisplay = ISO3166.f (country, "A2", true)
		if not countrydisplay then
			countrydisplay = "???"
		elseif countrydisplay == "GB" then
			countrydisplay = "UK"
		end
	else
		countrydisplay = country
	end
	
	if country == "BEF" then
		flaglink = "BE-VLG"
	elseif country == "BEW" then
		flaglink = "BE-WAL"
	elseif string.match(countryconv, "USA") then
		flaglink = "USA"
	elseif string.match(countryconv, "DEU") then
		flaglink = "DEU"
	elseif string.match(countryconv, "NLD") then
		flaglink = "NLD"
	else
		flaglink = countryconv
	end
	
	if display == "link" then
		return chartlink
	elseif display == "display" then
		return countrydisplay
	elseif display == "flag" then
		return flaglink
	end
	
end

-- Länderkürzel für mobile Charttabellen und Chartboxen - country shortcuts for mobile chart tables and chart infoboxes
p.countrycol = function (country, c, nolink, template, rformat)
	if not country then
		country = ""
	end
	local countrydisplay = p.countryshort (country, rformat, "display")
	local chartlink = p.countryshort (country, rformat, "link")
	
	local ret

	if chartlink then
		if nolink then
			ret = countrydisplay
		else
			ret = "[[" .. chartlink .. "|" .. countrydisplay .. "]]"
		end
	else
		if nolink then
			ret = "<span class='indivcountrynolink" .. c .. "'>" .. (country or "") .. "</span>"
		else
			ret = "<span class='indivcountry" .. c .. "'>" .. (country or "") .. "</span>"
		end
	end
	
	-- service links for infobox
	if template == "infobox" then
		if string.match (country, "[^%a%s%-]") then
			ret = ret .. "<span style='display:none'>[[Template:Infobox Chartplatzierungen/Wartung/Land falsch]]</span>"
		elseif not chartlink then
			if country ~= "DDR" then
				ret = ret .. "<span style='display:none'>[[Template:Infobox Chartplatzierungen/Wartung/Land unbekannt]]</span>"
			end
		end
	end
	-- /

	return ret
end



-- Länderspalten für Charttabellen - country columns in chart table head
p.countryhead = function (countries, rformat, sortable, grey)

	local ret = ""
	
	for c = 1, #countries.countries do
		local country = countries.countries[c]
		
		local countrydisplay = p.countryshort (country, rformat, "display")
		local chartlink = p.countryshort (country, rformat, "link")
		local flaglink = p.countryshort (country, rformat, "flag")
		
		-- service links
		local slnocountry = "<span style='display:none'>[[Template:Charttabelle/Wartung/Land unbekannt"
			if string.match (country, "US") or string.match (country, "United States") then
				slnocountry = slnocountry .. " (US)"
			end
		slnocountry = slnocountry .. "]]</span>"
		-- /
		
		ret = ret .. "\n! style='width:52px;'"
		if sortable then
			ret = ret .. " data-sort-type='number'"
		end
		ret = ret .. " class='charts-th"
		if grey then
			ret = ret .. "g"
		end
		ret = ret .. "' | "
		
		if not countrydisplay or not chartlink or not flaglink then
			ret = ret .. country .. slnocountry
		else
			if not grey then
				local countryflagname = countryflagtable[flaglink] or "None"
				ret = ret .. "[[File:Flag of " .. countryflagname .. ".svg|20px|link=" .. chartlink .. "|" .. countrydisplay .. "|alt=]]&nbsp;"
			end
			ret = ret .. "[[" .. chartlink .. "|" .. countrydisplay .. "]]"
		end
	end

	return ret
end



-- flag template for chart positions table and certification table
p.countryflag = function (country, linkid, displayid, bracketsid, template)
	local countrycode
	local countryconv = ISO3166.f (country, "A3", true)
	if not countryconv then
		countryconv = "???"
	end
	if countrytable[country] then
		countrycode = country
	else
		countrycode = countryconv
	end
	
	local flagcountry
	local flagcountryname
	local flagfile
	local link
	local display
	local brackets
	local ret
	
	if countrytable[countrycode] then
		if countrycode == "EUR2" then
			flagcountry = "EUR"
		elseif countrycode == "FRA2" then
			flagcountry = "FRA"
		elseif countrycode == "BEF" then
			flagcountry = "BE-VLG"
		elseif countrycode == "BEW" then
			flagcountry = "BE-WAL"
		elseif string.match(countrycode, "USA") then
			flagcountry = "USA"
		elseif string.match(countrycode, "DEU") then
			flagcountry = "DEU"
		elseif string.match(countrycode, "NLD") then
			flagcountry = "NLD"
		else
			flagcountry = countrycode
		end
		
		flagcountryname = countryflagtable[flagcountry]
		flagfile = "[[File:"
		if flagcountry == "GCC" then
			flagfile = flagfile .. flagcountryname
		else
			flagfile = flagfile .. "Flag of " .. (flagcountryname or "None")
		end
		flagfile = flagfile .. ".svg|20px|class=noviewer"
		if flagcountry ~= "CHE" then
			flagfile = flagfile .. "|border"
		end
		flagfile = flagfile .. "|link="
		
		if linkid then
			link = countrytable[countrycode][linkid]
		else
			link = countrytable[countrycode][1]
		end
		
		if displayid then
			display = countrytable[countrycode][displayid] or countrytable[countrycode][1]
		else
			display = countrytable[countrycode][1]
		end
		
		if bracketsid then
			brackets = countrytable[countrycode][bracketsid]
			display = display .. " (" .. (brackets or "") .. ")"
		end

		ret = flagfile .. link .. "|" .. display .. "|alt=]]&nbsp;[[" .. link .. "|" .. display .. "]]"
	else
		ret = country or ""
		if template == "chartpos" then
			ret = ret .. "<span style='display:none'>[[Template:Chartplatzierungen/Wartung/Land unbekannt]]</span>"
		elseif template == "certification" then
			ret = ret .. "<span style='display:none'>[[Template:AfM-Tabelle/Wartung/Land unbekannt]]</span>"
		end
	end

	return ret
end



-- create automatic links to lists of number-one hits in selected countries
p.numberonelinks = function (position, country, year)
	
	local afterone = string.match(position, "1(.*)")
	local yearformat
	if year then
		if string.match(year, "^%d%d%d%d") then
			yearformat = string.match(year, "^(%d%d%d%d)")
		end
	end
	local ret = ""
	
	-- find country code
	local countryconv = ISO3166.f (country, "A3", true)
	if countryconv == false then
		countryconv = "???"
	end
	if string.match (country, "US%-") then
		countryconv = string.gsub (country, "US", "USA")
	end
	local countrycode
	if (string.match (countryconv, "USA") and countryconv ~= "USA-AC") or string.match (countryconv, "DEU") or string.match (countryconv, "NLD") then
		countrycode = countrytable[(countryconv .. "-A")]
	else
		countrycode = countrytable[countryconv] or countrytable[country]
	end
	-- /

	if countrycode then
		if countrycode[6] then
			ret = ret .. "[[Liste der Nummer-eins-"
			if countryconv ~= "USA-RB" and countryconv ~= "USA-C" then
				ret = ret .. "Hits in "
			end
			ret = ret .. countrycode[6]
			if yearformat then
				ret = ret .. " (" .. yearformat .. ")"
			end
			ret = ret .. "|<b>1</b>]]"
			if countryconv == "USA-RB" or countryconv == "USA-C" then
				ret = ret .. "<!--Albumcharts-->"
			end
		else
			ret = ret .. "<b>1</b>"
		end
	else
		ret = ret .. "<b>1</b>"
	end
	ret = ret .. (afterone or "")
	
	return ret

end



p.no1tablecountry = function (country, info)
	local countryid
	local colourscheme
	local countryconv
	local colour1
	local colour2
	local rowcolour
	local flag
	local nav
	local firstyear
	
	for key,value in pairs(countrytable) do
		if value[1] == country then
			country = key
		end
	end
	if country == "Großbritannien" then
		country = "GBR"
	elseif string.match (country, "...%-") then
		country = string.match (country, "(...)%-")
	end
	countryconv = ISO3166.f (country, "A3", true)
	if not countryconv then
		countryconv = "???"
	end
	if countrytable[countryconv] then
		countryid = countrytable[countryconv][6] or countryconv
		colourscheme = countrytable[countryconv][7] or "1"
	else
		countryid = "Land"
		colourscheme = "1"
	end
	
	colour1 = colourtable[colourscheme][1]
	colour2 = colourtable[colourscheme][2]
	rowcolour = colourtable[colourscheme][3]
	flag = "{{" .. countryconv .. "|#}}"
	nav = "Navigationsleiste Nummer-eins-Hits in " .. countryid
	if chartdatetable[countryconv] or chartdatetable[country] then
		local firstalbumyear = chartdatetable[countryconv][1] or chartdatetable[country][1]
		local firstsingleyear = chartdatetable[countryconv][2] or chartdatetable[country][2]
		if firstalbumyear and firstsingleyear then
			if firstsingleyear < firstalbumyear then
				firstyear = firstsingleyear
			else
				firstyear = firstalbumyear
			end
		elseif firstsingleyear then
			firstyear = firstsingleyear
		else
			firstyear = firstalbumyear
		end
	elseif mw.title.new( nav, 'Template' ).exists then
        local frame = mw.getCurrentFrame()
		firstyear = frame:expandTemplate{ title = nav, args = { 'Beginn' } }
	else
		firstyear = "1950"
	end

	local ret
	if info == "countryid" then
		ret = countryid
	elseif info == "colour1" then
		ret = colour1
	elseif info == "colour2" then
		ret = colour2
	elseif info == "rowcolour" then
		ret = rowcolour
	elseif info == "flag" then
		ret = flag
	elseif info == "firstyear" then
		ret = firstyear
	else
		ret = ""
	end
	
	return ret
end

return p