وحدة:ملعب/Mr. Ibrahem/Uses Wikidata

local p = {}
local wd2 = require('Module:Wikidata2').formatStatementsFromLua

Type = {
		["wikibase-item"]= "عنصر",
		["wikibase-property"]= "خاصية",
		["commonsMedia"]= "وسائط",
		["math"]= "تعبير رياضي",
		["time"]= "وقت",
		["external-id"]= "معرف خارجي",
		["string"]= "نص",
		["globe-coordinate"]= "إحداثيات",
		["quantity"]= "كمية",
		["geo-shape"]= "شكل جغرافي",
		["url"]= "مسار",
		["monolingualtext"]= "نص بلغة",
		}
function getpropertytype(id) 
	local entity = mw.wikibase.getEntityObject(id)
		if entity
		and entity['datatype']
		and Type[entity['datatype']]
		then
			ca = Type[entity['datatype']]
			else ca =''
		end
	if ca and ca ~= '' then
	--return '<small> (' .. ca ..  ')</small>'
	return ca
	end
end

function LabelOrDes(langcode , id , lab , parent) 
	if lab == 'labels'
		then cas = 'lab'
	elseif lab == 'descriptions'
		then  cas = 'des'
	end
	----------------------
	local arg = parent.args[id .. cas] or ''
	local lan = langcode or 'ar'
	local entity = mw.wikibase.getEntityObject(id)
	----------------------
	if arg and arg ~= '' 
	then 
		return arg
	else
		if entity
		and entity[''..lab ..'']
		and entity[''..lab ..''][''..lan..'']
		and entity[''..lab ..''][''..lan..''].value 
		then
			local lang = entity[''..lab ..''][''..lan..'']
				if lang['language'] == lan 
				then
					return entity[''..lab ..''][''..lan..''].value
				else
					return nil
				end
		end
	end
end

function getprop(id , prop)
	local PO = ''
	local Property = wd2({entityId =  id , property = prop ,formatting ='sitelink', addTrackingCat ='',noicon='t',nolink ='t'}) or ''
	PO = '\n| '
	----------------------
	if Property and Property ~='' then
		local cat = mw.ustring.gsub(Property, 'تصنيف:' , '')
		local catname = mw.ustring.gsub(cat, 'صفحات تستخدم خاصية' , 'صفحات تستخدم')
		local use = mw.site.stats.pagesInCategory(cat, 'all')

		if name and name ~= '' then  catname = name end 
		PO = '\n| data-sort-value="' .. use .. '"| '
		PO = PO .. "[[:" .. Property .. "|" .. use .. " صفحة]]"-- (<abbr title='عدد الاستخدامات'>ع.</abbr> "
		--PO = PO .. use .. " صفحة)"		
	end
	return PO
end

function p.usestable(frame)
	aa = {}
	local parent = frame.getParent(frame)
	local head = '{| class="wikitable sortable" style="text-align:right; width: 100%; font-size: 95%;"\n|+ خواص ويكي بيانات المستخدمة في القالب\n'
	local r = '! data-sort-type="number"| #'
	--r =  r .. '!! data-sort-type="number" style="width: 20%;"| الخاصية'
	r =  r .. '!! data-sort-type="number"| الخاصية'
	--r =  r .. '!! style="width: 10%; " | نوع'
	r =  r .. '!! النوع'
	--r =  r .. '!! style="width: 45%; " | وصفها'
	r =  r .. '!! الوصف'
	r =  r .. '!! data-sort-type="number"|' ..  LabelOrDes('ar', 'P2875' ,'labels' , parent)
	r =  r .. '!! data-sort-type="number"|' ..  LabelOrDes('ar', 'P3734' ,'labels' , parent)
	r =  r .. '!! data-sort-type="number"|' ..  LabelOrDes('ar', 'P3709' ,'labels' , parent)
	r =  r .. '!! data-sort-type="number"|' ..  LabelOrDes('ar', 'P3713' ,'labels' , parent)
	r =  r .. '\n|-\n'
	local result = ''
	for k, p_num in pairs(parent.args) do
		--local p_num = parent.args[k] or ''
		if p_num ~= '' 
			then
				p_num = mw.text.trim( p_num )
				table.insert(aa, p_num)
			--else break
		end
	end
	--table.sort(aa, function(a, b) return a < b end)
	table.sort(aa)
	for k, p_num in pairs(aa) do
			local rr = " (انظر [[d:Special:WhatLinksHere/Property:" .. p_num .. "|الاستخدام]])"
			local label = '{{P|' .. p_num .. '}}'
			local description = ''--LabelOrDes( 'ar', p_num ,'descriptions'  , parent) or "" --"بدون وصف"
			local P2875 = getprop(p_num , 'P2875')
			local P3734 = getprop(p_num , 'P3734')
			local P3709 = getprop(p_num , 'P3709')
			local P3713 = getprop(p_num , 'P3713')
 			local properties = P2875 .. P3734 .. P3709 .. P3713
			local propertytype = getpropertytype(p_num)
			local pro = '' .. string.upper(p_num) .. ''
 			local span = '| data-sort-value="' .. mw.ustring.gsub(p_num, 'P' , '').. '"| '
 			local num   = span  .. pro .. '\n'
 			local name  = span  .. frame:preprocess(label)
			result = result .. num .. name .. "||"  .. propertytype .. "||" .. description  .. properties  .. "\n|-\n"
	end
	return head  .. r .. result .. '|}'
end

return p