User:Bovlb/sandbox/Module:Airlines

require('Module:No globals')

local p = {}

-- module local variables local wiki = { langcode = mw.language.getContentLanguage.code }

-- internationalisation local i18n = { errors = { ["property-not-found"] = "Property not found.", ["entity-not-found"] = "Wikidata entity not found.", ["malformed-property"] = "Property is not in the form P123.", ["entity-not-found"] = "Wikidata entity not found.", ["unknown-claim-type"] = "Unknown claim type.", ["unknown-entity-type"] = "Unknown entity type.", ["qualifier-not-found"] = "Qualifier not found.", ["site-not-found"] = "Wikimedia project not found.", }, }

local function printError(code) return ' ' .. (i18n.errors[code] or code) .. ' ' end

local function printDatavalueEntity(data) -- data fields: entity-type [string], numeric-id [int, Wikidata id] local id   if data["entity-type"] == "item" then id = "Q" .. data["numeric-id"] else return printError("unknown-entity-type") end return mw.wikibase.label(id) or id end

local function sortedKeys(t) local a = {} for key in pairs(t) do       table.insert(a, key) end table.sort(a) return a end

function p.airportDestinations(frame) local property = frame.args[1] or "" local id = frame.args["id"]

local references = frame.args["references"] local showerrors = frame.args["showerrors"] local default = frame.args["default"] local property = frame.args["property"] or "P521" local qualifier = frame.args["qualifier"] or "P137"

if default then showerrors = nil end

if not mw.ustring.match(property, "^P%d+$") then if showerrors then return printError("malformed-property") else return default end end if not mw.ustring.match(qualifier, "^P%d+$") then if showerrors then return printError("malformed-property") else return default end end -- get wikidata entity local entity = mw.wikibase.getEntityObject(id) if not entity then if showerrors then return printError("entity-not-found") else return default end end

local claims = entity.claims[property]

if not claims then return default end local airlines = {} for i,claim in pairs(claims) do       local snak = claim.mainsnak if snak.datavalue.type == "wikibase-entityid" then local airport = mw.wikibase.label("Q" .. snak.datavalue.value["numeric-id"]) if claim.qualifiers then local qualifiers = claim.qualifiers[qualifier] if qualifiers then for i, qsnak in ipairs(qualifiers) do                       if qsnak.datavalue.type == "wikibase-entityid" then local airline = mw.wikibase.label("Q" .. qsnak.datavalue.value["numeric-id"]) if not airlines[airline] then airlines[airline] = {} end table.insert(airlines[airline], airport) end end end end end end

local root = mw.html.create('table') root :addClass('wikitable') :addClass('sortable') :css('font-size', '95%')

-- add the header row local row = root:tag('tr') local cell= row:tag('th') cell:wikitext('Airlines') cell= row:tag('th') cell:addClass('unsortable') cell:wikitext('Destinations')

for i, airline in pairs(sortedKeys(airlines)) do       row = root:tag('tr') cell = row:tag('td') cell:wikitext("" .. airline .. "") local airports = airlines[airline] table.sort(airports) local text = "" for i, airport in ipairs(airports) do           if not i == 1 then text = text .. ", " end text = text .. "" .. airport .. ", " end cell = row:tag('td') cell:wikitext(text) end return tostring(root) end

return p