Module:Common: Difference between revisions

From DataTrek
Jump to navigation Jump to search
No edit summary
No edit summary
 
(40 intermediate revisions by the same user not shown)
Line 3: Line 3:
function p.SiteAllP(frame)
function p.SiteAllP(frame)
local MaxP
local MaxP
local Separator = "|"
local EmptyValue = "-"
local AllP = {}
local AllP = {}
local PDetails = {}
local PDetails = {}
Line 13: Line 13:
end
end
AllP[#AllP + 1] = {"ID", "Label IT", "Label EN", "DataType", "Description IT", "Description EN"}
for PNumber = 1, MaxP, 1 do
for PNumber = 1, MaxP, 1 do
       if mw.wikibase.entityExists('P' .. PNumber) then
       if mw.wikibase.entityExists('P' .. PNumber) then
Line 18: Line 19:
       local Property = mw.wikibase.getEntity('P' .. PNumber)
       local Property = mw.wikibase.getEntity('P' .. PNumber)
      
      
      --PDetails[#PDetails + 1] = "Primo testo"
      PDetails[#PDetails + 1] = "[[Property:" .. Property.id .. "|" .. Property.id .. "]]"
      --PDetails[#PDetails + 1] = Property.lastrevid
     
      --PDetails[#PDetails + 1] = Property.modified
      PDetails[#PDetails + 1] = Property.id
      --PDetails[#PDetails + 1] = "Secondo testo"
      --PDetails[#PDetails + 1] = Property.title
      --PDetails[#PDetails + 1] = Property.lastrevid
       if Property.labels["it"] then
       if Property.labels["it"] then
       PDetails[#PDetails + 1] = Property.labels["it"].value
       PDetails[#PDetails + 1] = Property.labels["it"].value
       else
       else
       PDetails[#PDetails + 1] = "[IT]"
       PDetails[#PDetails + 1] = EmptyValue
       end
       end
     
       if Property.labels["en"] then
       if Property.labels["en"] then
       PDetails[#PDetails + 1] = Property.labels["en"].value
       PDetails[#PDetails + 1] = Property.labels["en"].value
       else
       else
       PDetails[#PDetails + 1] = "[EN]"
       PDetails[#PDetails + 1] = EmptyValue
       end
       end
     
       PDetails[#PDetails + 1] = Property.datatype
       PDetails[#PDetails + 1] = Property.datatype
       --[[if Property.modified then
     
       PDetails[#PDetails + 1] = Property.modified
       --[[if next(Property.labels.descriptions) == nil then
      else
       PDetails[#PDetails + 1] = "()"
       PDetails[#PDetails + 1] = "[MODIFIED]"
       PDetails[#PDetails + 1] = "()"
       end]]
       else]]
      --PDetails[#PDetails + 1] = "Terzo e ultimo testo"
      if Property:getDescription('it') then
      --PDetails[#PDetails + 1] = Property.id
      PDetails[#PDetails + 1] = Property:getDescription('it')
      --PDetails[#PDetails + 1] = Property.pageid
      else
      PDetails[#PDetails + 1] = EmptyValue
      end
      if Property:getDescription('en') then
      PDetails[#PDetails + 1] = Property:getDescription('en')
      else
      PDetails[#PDetails + 1] = EmptyValue
      end
      --end
      
      
       --AllP[#AllP + 1] = 'P' .. PNumber
       --AllP[#AllP + 1] = 'P' .. PNumber
       AllP[#AllP + 1] = table.concat(PDetails, Separator)
       --AllP[#AllP + 1] = table.concat(PDetails, Separator)
      AllP[#AllP + 1] = PDetails
       end
       end
     end
     end
return table.concat(AllP, "<br />" .. string.char(10))
--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 tostring(Table)
end
function p.TextFromArray(ArrayRows)
local Separator = "|"
local AllRows = {}
for _, Row in pairs(ArrayRows) 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.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
function p.GenerateBoth(frame)
local ListOfP = {}
ListOfP = p.SiteAllP(frame)
return "== Table == " .. string.char(10) .. p.TableFromArray(ListOfP, true, true) .. string.char(10) .. string.char(10) .. "== List ==" .. string.char(10) .. p.TextFromArray(ListOfP)
end
function p.LabelByLang(frame)
local Item = frame.args[1]
if not Item or Item == nil then
Item = 'Q1'
end
--user_lang = mw.getContentLanguage().code
return mw.wikibase.getLabel(Item)
end
end
return p
return p

Latest revision as of 00:25, 29 April 2021

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

-- Keyword: wikitrek
local p = {}
function p.SiteAllP(frame)
	local MaxP
	local EmptyValue = "-"
	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] = EmptyValue
      	end
      	
      	if Property.labels["en"] then
      		PDetails[#PDetails + 1] = Property.labels["en"].value
      	else
      		PDetails[#PDetails + 1] = EmptyValue
      	end
      	
      	PDetails[#PDetails + 1] = Property.datatype
      	
      	--[[if next(Property.labels.descriptions) == nil then
      		PDetails[#PDetails + 1] = "()"
      		PDetails[#PDetails + 1] = "()"
      	else]]
      		if Property:getDescription('it') then
      			PDetails[#PDetails + 1] = Property:getDescription('it')
      		else
      			PDetails[#PDetails + 1] = EmptyValue
      		end
      		if Property:getDescription('en') then
      			PDetails[#PDetails + 1] = Property:getDescription('en')
      		else
      			PDetails[#PDetails + 1] = EmptyValue
      		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 tostring(Table)
end
function p.TextFromArray(ArrayRows)
	local Separator = "|"
	local AllRows = {}
	
	for _, Row in pairs(ArrayRows) 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.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
function p.GenerateBoth(frame)
	local ListOfP = {}
	
	ListOfP = p.SiteAllP(frame)
	return "== Table == " .. string.char(10) .. p.TableFromArray(ListOfP, true, true) .. string.char(10) .. string.char(10) .. "== List ==" .. string.char(10) .. p.TextFromArray(ListOfP)
end
function p.LabelByLang(frame)
	local Item = frame.args[1]
	
	if not Item or Item == nil then
		Item = 'Q1'
	end
	--user_lang = mw.getContentLanguage().code
	return mw.wikibase.getLabel(Item)
end
return p