Module:Roadtable row

From Wikidata
Jump to navigation Jump to search
Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

Documentation for this module may be created at Module:Roadtable row/doc

Code

local p = {}

local insert = table.insert
local item

local function languages()
	local labels = item.labels
	local descriptions = item.descriptions
	local langs = {}
	for lang,_ in pairs(labels) do
		if descriptions[lang] then
			insert(langs, lang)
		end
	end
	table.sort(langs)
	return table.concat(langs, ' ')
end

local function modelPropertyRow(modelName)
	local models = mw.loadData('Module:Roadtable row/models')
	
	local model = models[modelName] or models.route
	local modelUsed = {}
	for _,property in pairs(model) do
		modelUsed[property] = true
	end
	
	local propertyList = item:getProperties()
	local properties = {}
	for _,property in pairs(propertyList) do
		properties[property] = true
	end
	
	local cells = {}
	local function insertCell(property)
		if not modelUsed[property] then
			insert(cells, -1)
		else
			local test
			if property == 'P580' then
				test = (properties['P580'] or properties['P582']) == true
			else
				test = properties[property] == true
			end
			insert(cells, test)
		end
	end
	
	insertCell('P31') -- instance of
	insertCell('P14') -- shield
	insertCell('P15') -- map
	insertCell('P131') -- administrative division
	insertCell('P17') -- country
	insertCell('P126') -- maintenance
	insertCell('P127') -- ownership
	insertCell('P16') -- system
	insertCell('P559') -- termini
	insertCell('P609') -- termini locations
	insertCell('P580') -- dates
	return cells
end

local function propertyRow()
	local propertyList = item:getProperties()
	local properties = {}
	for _,property in pairs(propertyList) do
		properties[property] = true
	end
	
	local cells = {}
	insert(cells, properties["P31"] == true) -- instance of
	insert(cells, properties["P14"] == true) -- shield
	insert(cells, properties["P15"] == true) -- map
	insert(cells, properties["P131"] == true) -- administrative division
	insert(cells, properties["P17"] == true) -- country
	insert(cells, properties["P126"] == true) -- maintenance
	insert(cells, properties["P127"] == true) -- ownership
	insert(cells, properties["P16"] == true) -- system
	insert(cells, properties["P559"] == true) -- termini
	insert(cells, properties["P609"] == true) -- termini locations
	insert(cells, (properties["P580"] or properties["P582"]) == true) -- dates
	return cells
end

function p._row(args)
	-- No item (use string for name)
	local noitem = args.noitem or ''
	if noitem ~= '' then
		return mw.ustring.format("|-\n|colspan=13|''Item not created: %s''", noitem)
	end
	
	local q = args.q or ''
	if q == '' then
		-- return nonItemRow(args)
	end
	item = mw.wikibase.getEntityObject( 'Q' .. q)
	local langs = languages()
	local properties
	local model = args.model or ''
	if model ~= '' then
		properties = modelPropertyRow(model)
	else
		properties = propertyRow()
	end
	local row = {'|-'}
	local link = mw.ustring.format("[[Q%s|%s <small>(Q%s)</small>]]", q, item:getLabel(mw.getCurrentFrame():preprocess('{{int:Lang}}')) or ("Q" .. q), q)
	insert(row, '!' .. link)
	insert(row, '|' .. langs)
	for _,prop in ipairs(properties) do
		if prop == -1 then
			insert(row, '| style="text-align: center; background-color: #f2f2f2;" | &mdash;')
		else
			insert(row, '| style="text-align: center;" | ' .. (prop and '✓' or '✗'))
		end
	end
	return table.concat(row, '\n')
end

function p.row(frame)
	local pframe = frame:getParent()
	local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
	local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
	
	return p._row(args)
end

function p.regionLink(frame)
	local pframe = frame:getParent()
	local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
	local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
	
	local target, display
	
	-- Target
	local subpage = args.subpage or ''
	if subpage ~= '' then
		local rootPage = args.root or ''
		local thisPage = "Wikidata:WikiProject Roads" .. rootPage
		target = mw.ustring.format("Special:MyLanguage/%s/%s", thisPage, subpage)
	else
		target = args.link or ''
	end
	
	-- Display
	local q = args.regionq or ''
	if q ~= '' then -- Region item (use localized label for link display)
		display = frame:preprocess(string.format('{{label|Q%s}}', q))
	else
		display = args.region or ''
	end
	local link = mw.ustring.format("[[%s|%s]]", target, display)
	
	return link
end

return p