Module:Common

From DataTrek
Revision as of 23:24, 10 June 2020 by Lucamauri (talk | contribs)
Jump to navigation Jump to search

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

-- Keyword: wikitrek
local p = {}
function p.SiteAllP(frame)
	local MaxP
	local AllP = {}
	local PDetails = {}
	
	if tonumber(frame.args[1]) < 1 then
		MaxP = 10
	else
		MaxP = tonumber(frame.args[1])
	end
	
	AllP[#AllP + 1] = {"ID", "Label IT", "Label EN", "DataType", "Description IT", "Description EN"}
	for PNumber = 1, MaxP, 1 do
      if mw.wikibase.entityExists('P' .. PNumber) then
      	local PDetails = {}
      	local Property = mw.wikibase.getEntity('P' .. PNumber)
      	
       	PDetails[#PDetails + 1] = "[[Property:" .. Property.id .. "|" .. Property.id .. "]]"
       	
       	if Property.labels["it"] then
      		PDetails[#PDetails + 1] = Property.labels["it"].value
      	else
      		PDetails[#PDetails + 1] = "()"
      	end
      	
      	if Property.labels["en"] then
      		PDetails[#PDetails + 1] = Property.labels["en"].value
      	else
      		PDetails[#PDetails + 1] = "()"
      	end
      	
      	PDetails[#PDetails + 1] = Property.datatype
      	
      	if next(Property.labels.descriptions) == nil then
      		PDetails[#PDetails + 1] = "()"
      		PDetails[#PDetails + 1] = "()"
      	else
      		if Property.labels.descriptions[it] then
      			PDetails[#PDetails + 1] = Property.labels.descriptions[it].value
      		else
      			PDetails[#PDetails + 1] = "()"
      		end
      		if Property.labels.descriptions[en] then
      			PDetails[#PDetails + 1] = Property.labels.descriptions[en].value
      		else
      			PDetails[#PDetails + 1] = "()"
      		end
      	end
      	
      	--AllP[#AllP + 1] = 'P' .. PNumber
      	--AllP[#AllP + 1] = table.concat(PDetails, Separator)
      	AllP[#AllP + 1] = PDetails
      end
    end
	
	--return table.concat(AllP, "<br />" .. string.char(10))
	return AllP
end

function p.TableFromArray(AllRows, HeaderRow, HeaderColumn)
	local Table = mw.html.create('table')
	local FirstRow
	local FirstColumn
	local Tr
	local Cell
	
	--[[if HeaderRow == true then
		HeaderColumn = false
	elseif HeaderColumn == true then
		HeaderRow = false
	else
		HeaderRow = false
		HeaderColumn = false
	end]]
	if not HeaderRow then
		HeaderRow = false
	end
	if not HeaderColumn then
		HeaderColumn = false
	end
	
	FirstRow = true
	for _, Row in pairs(AllRows) do
		FirstColumn = true
		Tr = mw.html.create('tr')
		
		for _, Field in pairs(Row) do
			if (FirstRow and HeaderRow) or (FirstColumn and HeaderColumn) then
				Cell = mw.html.create('th')
			else
				Cell = mw.html.create('td')
			end
			Cell
				:wikitext(Field)
			
			
			--[[
			if First then
				First = false
				Cell = mw.html.create('th')
				if (type(Field) == "table") then
					Cell
						:attr('id', Field[1])
						:attr('title', Field[1])
						:wikitext(Field[2])
				else
					Cell
						:wikitext(Field)
				end
			else
				Cell = mw.html.create('td')
				if #Field > 1 then
					List = mw.html.create('ul')
					for _, Item in pairs(Field) do
						LI = mw.html.create('li')
						LI:wikitext(Item)
						List:node(LI)
					end
					Cell:node(List)
				else
					Cell
						:wikitext(Field[1])
				end
			end]]
			
			Tr:node(Cell)
			FirstColumn = false
		end
		Table:node(Tr)
		FirstRow = false
	end
	
	--[==[if mw.wikibase.getEntity() then
		Text = "Modifica i dati nella [[:datatrek:Item:" .. mw.wikibase.getEntityIdForCurrentPage() .. "|pagina della entità]] su ''DataTrek''"
	else
		Text = "Impossibile trovare l'entità collegata"
	end
	
	Table
	   :css('font-size', 'smaller')
       :css('text-align', 'right')
       :css('margin', '1px')
       ]==]
    --return tostring(Table)
    Table:addClass("wikitable")
    return Table
end
function p.SiteAllPText(frame)
	local Separator = "|"
	local AllRows = {}
	
	for _, Row in pairs(p.SiteAllP(frame)) do
		--for _, Item in pairs(Row) do
			AllRows[#AllRows + 1] = table.concat(Row, Separator)
		--end
	end
	return table.concat(AllRows, "<br />" .. string.char(10))
end
function p.SiteAllPTable(frame)
	return p.TableFromArray(p.SiteAllP(frame), true, true)
end
return p