يمكن إنشاء صفحة توثيق الوحدة في وحدة:Wikidata2/P1082/شرح

local p = {}
local track = require('وحدة:Wikidata/تتبع').makecategory1

function comparedates2(a, b)  -- returns true if a is earlier than B or if a has a date but not b
	if a.year and b.year then
		return a.year > b.year
	elseif a.year then
		return true
	end
end
local function normalizeDate(date)
	date = mw.text.trim(date, "+")
	-- extract year
	local yearstr = mw.ustring.match(date, "^\-?%d+")
	local year = yearstr
	--year = tonumber(yearstr)
	-- remove leading zeros of year
	--return year .. mw.ustring.sub(date, #yearstr + 1), year
	return year
end

function p.P1082(claims , options)
	local icon = track( { property="P1082" , id= options.entityId or "" } )
   	local Teams = {}
   	local refr = options.refs
   	options.reff = ""
   	options.noref = "r"
   	if options.pup and options.pup ~="" then
   		local localvalue = {value = options.pup , year = options.year }
		table.insert( Teams, localvalue )
	end
	for i, statement in pairs( claims ) do
		local tab = {ref="" , year = "",value=""}
		local va = formatOneStatement( statement,"", options )
		if statement and statement.qualifiers and  statement.qualifiers.P585 then
			--tab.year = formatStatements({property = "P585", firstvalue= 'true'}, statement.qualifiers) or ''
			mw.log("year: " .. tab.year)
			if statement.qualifiers.P585[1].snaktype == 'value' then
				y = statement.qualifiers.P585[1].datavalue.value.time
				tab.year = normalizeDate(y)
				--mw.log("y: " .. y)
			end
		end
		if statement.references then
			tab.references = statement.references
			--if refr and refr ~= ''  then
				--tab.ref  = formatReferences( statement, options )
			--end 
		end
		if va then
			if va.v == '' then va.v = nil end
			if va.v then 
				tab.value = va.v-- addTrackingCategory( va.v ,options)
				table.insert( Teams, tab )
			end
		end
	end
	table.sort(Teams, function(a,b) return comparedates2(a, b) end )
	--if tot == '' then tot = nil end
	--return tot
	local tables = {}
	if #Teams > 0
	then
		tables = {Teams[1] }
		
		local tables2 = {}
		
		function sp(p , y)
			if p and p ~= "" then
				pup = mw.getCurrentFrame():preprocess("{{formatnum: " .. p .. " }}")
				if y and y ~= "" then
					return pup .. " <small>(إحصاء " .. y  .. ")</small>"
				else return pup end
			else return "" end
		end
		for ii, ss in pairs( tables ) do
			ba = ss.value
			local ref = ""
			if ss.references then
				if refr and refr ~= ''  then
					ref  = formatReferences( ss, options )
				end 
			end
			if ss.year and ss.year ~= "" then  
				--pup = mw.getContentLanguage():formatNum( ss.value )
				ba = sp(ss.value , ss.year) .. ref --.. icon
				if ss.value ~= options.pup then
					ba = ba .. icon
				end
		end
			table.insert( tables2, ba )
		end
		local tots = mw.text.listToText( tables2, options.separator, options.conjunction )
		return tots
	else
		
	end
end

return p