Module:Airport statistics filtered

Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules


Known issues:

Config:

UsageUsage

{{#chart:Airport statistics filtered.chart|data=Airport statistics filtered.tab|arg:id=Qid|arg:debut=2000}}

Example: PassengersYear150,000200,000250,000300,000350,000400,000450,000500,00019901995200020052010201520202025PassengersAnnual passenger traffic


Code

-- Written to replace Template:Airport-Statistics (Q64415993). Not a drop-in replacement, does not support the iata, icao, faa, width and title paramaters. 
-- For iata, icao and faa use second parameter instead with an Wikidata Q id item. Width and title can not be parameters, those are defined in the chart, 
-- while we are changing the data here. Create a custom chart page for width and title.
-- Use with "Airport statistics filtered.data" and "Airport statistics filtered.chart", like so "{{#chart:Airport statistics filtered.chart|data=Airport statistics filtered.tab
-- |arg:id=Qid|arg:debut=2000}}" where Qid and 2000 has been replaced. "start=2000" also works, instead of debut.
-- Any edits to this module should also change the tab and/or chart pages. For example, if the arguments are to be changed, the chart page also needs to be changed.
--
-- For debugging, open https://commons.wikimedia.org/w/index.php?title=Module:Airport_statistics_filtered&action=edit
-- and execute `mw.logObject(p.main(mw.ext.data.get('Airport statistics filtered.tab'), {id='Q694434'}).data)` in the Lua Debug console
-- (see https://en.wikipedia.org/wiki/Help:Lua_debugging for help)
local p = {}

function p._main(frame)
	--For testing/usage outside of charts
	local pframe = frame:getParent()
	local args = frame.args
	local tab = mw.ext.data.get ('Airport statistics filtered.tab')
	return mw.text.jsonEncode(p.main(tab, args))
end

function p.main(tab, args)
	local item = nil
	local conf = require("Module:Airport_statistics_filtered/configure")
	local wiki = mw.language.getContentLanguage():getCode() .. "." .. mw.site.siteName
	if args.id and mw.wikibase.entityExists( args.id ) then
		item = args.id
	else
		item = mw.wikibase.getEntityIdForCurrentPage()
	end
	if not args.debut then
		args.debut = args.start
	end
	local part = nil
	local years = {}
	local lined = {}
	tab.data = {}
	if item and mw.wikibase.getAllStatements(item, "P3872")[1] then
		local AllStat = mw.wikibase.getAllStatements(item, "P3872")
		if AllStat[1].references and AllStat[1].references[1].snaks["P854"] and AllStat[1].references[1].snaks["P854"][1].datavalue then
			tab["sources"] = AllStat[1].references[1].snaks["P854"][1].datavalue.value
		end
		for _, Bstat in pairs(AllStat) do
			local refs = conf.tr(wiki, "ref") == "false" or (Bstat.references and Bstat.references[1]) -- no refs required or ref present
			if Bstat.mainsnak.datavalue and refs and Bstat.qualifiers and Bstat.qualifiers['P585'] then
				if conf.tr(wiki, "part") == "false" then
					if Bstat.qualifiers['P518'] then
						part = false
					else
						part = true
					end
				end
				if conf.tr(wiki, "part") == "true" then
					if Bstat.qualifiers['P518'] then
						part = true
					else
						part = true
					end
				end
				local statement = Bstat.mainsnak.datavalue.value.amount
				if mw.wikibase.entityExists( 'P585' ) and Bstat.qualifiers['P585'][1] and 
				part and Bstat.qualifiers['P585'][1].datavalue then
					local value = Bstat.qualifiers['P585'][1].datavalue.value
					-- Replace "+2024-00-00T00:00:00Z" with "+2024-01-01T00:00:00Z", otherwise the result of `formatDate` is 2023
					local time = value.time:gsub("%-00%-00T", "-01-01T")
					local year = tonumber(mw.language.getContentLanguage():formatDate(("Y"), time, nil))
					if statement and value.precision == 9 and not lined[year] and year > (tonumber(args.debut) or 0) then
						statement = statement + 0
						local row = {}
						table.insert(row, year)
						table.insert(row, statement)
						lined[year] = row
						table.insert(years, year)
					end
				end
			else 
				break
			end
		end
		-- reordering
		table.sort(years)
		for _, year in ipairs(years) do
			if table.maxn(tab.data) >= 1000 then
				-- 1000 is echarts limit of max parameters. If you are hitting it, increase the debut argument to get newer data.
				break
			end
			table.insert(tab.data, lined[year])
		end
	end
	return tab
end

function p.direct(tab, args)
	tab.data = {}
	tab.data = args.data
	return tab
end

return p
Category:Modules in beta Category:Pages using the Chart extension