Module:Airport statistics filtered

Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Documentation for this module may be created at Module:Airport statistics filtered/doc

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
-- |id=Qid|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.
-- Does not successfully submit data to extension:chart just yet.
require ("strict")
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
	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 lined = {}
	local i = 10
	local k = 0
	tab.data = {}
	if item and mw.wikibase.getAllStatements(item, "P3872")[1] then
		local AllStat = mw.wikibase.getAllStatements(item, "P3872")
		if 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
		while AllStat[i] ~= nil and table.maxn(lined) < 1000 do -- 1000 is echarts limit of max parameters. If you are hitting it, increase the debut argument to get newer data.
			if AllStat[i].mainsnak.datavalue
			and AllStat[i].references and AllStat[i].references[1] and AllStat[i].qualifiers['P585'] then
				local Bstat = AllStat[i]
				local statement = Bstat.mainsnak.datavalue.value.amount
				local qualifier = nil
				if mw.wikibase.entityExists( 'P585' ) and Bstat.qualifiers['P585'][1] and 
				not Bstat.qualifiers['P518'] and Bstat.qualifiers['P585'][1].datavalue and 
				tonumber(mw.language.getContentLanguage():formatDate("Y", Bstat.qualifiers['P585'][1].datavalue.value.time)) > (args.debut or 0) then
					qualifier = mw.language.getContentLanguage():formatDate(("Y"), 
							Bstat.qualifiers['P585'][1].datavalue.value.time)
					if statement and Bstat.qualifiers['P585'][1].datavalue.value.precision == 9 then
						statement = statement + 0
						local row = {}
						table.insert(row, tonumber(qualifier))
						table.insert(row, statement)
						table.insert(tab.data, row)
					end
				end
			else 
				break
			end
			i = i + 1
		end
	end
	return tab
end

return p