مودول:Uses Wikidata



local p = {}
local wd2 = require('Module:Wikidata2').formatStatementsFromLua
local gett = require('Module:Uses_Wikidata/datatype').gettype

function p.usesProperty(frame)
	local parent = frame.getParent(frame)
	local result = ''
	local ii = 1
	while true do
		local p_num = parent.args[ii] or ''
		if p_num ~= '' then
			p_num = mw.text.trim( p_num )
			if mw.ustring.gsub( p_num, '%d', '' ) == 'P' then
				local label = mw.wikibase.label(p_num) or "بدون وصف"
			result = result .. "<li><b><i>[[d:Property:" .. p_num .. "|" .. label .. " <small>(" .. string.upper(p_num) .. ")</small>]]</i></b> (انظر [[d:Special:WhatLinksHere/Property:" .. p_num .. "|الاستخدام]])</li>"
			end

		else break
		end
		ii = ii + 1
	end
	return result
end

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

function LabelOrDes(langcode , id , lab , args) 
	if lab == 'labels'
		then cas = 'lab'
	elseif lab == 'descriptions'
		then  cas = 'des'
	end
	----------------------
	local arg = 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 makecat(P, name)
	PO = ''
	----------------------
	if P and P ~='' then
		local cat = mw.ustring.gsub(P, 'تصنيف:' , '')
		local catname = mw.ustring.gsub(cat, 'صفحات تستخدم خاصية' , 'صفحات تستخدم')
		if name and name ~= '' then	 catname = name end
		PO = "[[:" .. P .. "|" .. catname .. "]] (<abbr title='عدد الاستخدامات'>في</abbr> " .. mw.site.stats.pagesInCategory(cat, 'all') .. " صفحة)"		
	end
	----------------------
	return PO
end

function getprop(id , prop , args , name)
	local PO = ''
	local Property = wd2({entityId =  id , property = prop ,formatting ='sitelink', addTrackingCat ='',noicon='t',nolink ='t', firstvalue='s'}) or ''
	--if Property and Property ~='' then Property = '[[:' .. Property ...']] ({{PAGESINCAT:' .. mw.ustring.gsub(Property, 'تصنيف:' , '') ..	 '}} صفحة)' end
	----------------------
	if args[id .. 'cat'] and args[id .. 'cat'] ~= '' 
	then 
		Property = args[id .. 'cat'] 
	end
	----------------------
	newtrak = args['track'] or ""
	if not Property or Property == "" then 
		if prop == "P2875" and newtrak ~= "" then
			Property = "تصنيف:صفحات تستخدم خاصية " .. id
		end
	end
	if Property and Property ~='' then
		PO = makecat(Property , name)
	end
	----------------------
	return PO
end

function p._usestable(args)
	aaa = {}
	aa = {}
	local nott = "<abbr title='قيم ليست على ويكي بيانات'>ليست.</abbr>"
	local same = "<abbr title='قيم كما في ويكي بيانات'>مطابقة.</abbr>"
	local def ="<abbr title='قيم مختلفة عن ويكي بيانات'>مختلفة.</abbr>"
	local r = '! data-sort-type="number" style="width: 20%;"| الخاصية'
	r =	 r .. '!! style="width: 45%; " | وصفها'
	r =	 r .. '!! style="width: 30%; " | تصنيف تتبع الاستخدام' 
	local r1 = r .. '\n|-\n'
	r =	 r .. '!! style="width: 30%; " | ' .. nott
	r =	 r .. '!! style="width: 30%; " | ' .. same
	r =	 r .. '!! style="width: 30%; " | ' .. def
	r =	 r .. '\n|-\n'
	local temp	= args['قالب'] or 'القالب'
	local ii = 1
	while true do
		local p_num = args[ii] or ''
		mw.log("Module:Uses Wikidata: " .. p_num)
		if p_num ~= '' then
			p_num = mw.text.trim( p_num )
			if mw.ustring.gsub( p_num, '%d', '' ) == 'P' then
				table.insert(aa, p_num)
			--else
				
				--if mw.ustring.gsub( p_num, 'قالب=', '' ) ~= p_num then
					--temp = mw.ustring.gsub( p_num, 'قالب=', '' )
				--end
			end
		else break
		end	
		ii = ii + 1
	end
	if temp ~= 'القالب' 
		then temp = '[[قالب:' .. temp .. ']]' 
	end
	local head_s = '{| class="wikitable sortable" style="text-align:right; width: '
	local head2 = '%; font-size: 95%;"\n|+خواص [[ويكي بيانات]] المستخدمة في ' .. temp .. '\n'
	local width =  args['width'] or  "75"
	local head = head_s .. width .. head2

	local result = ''
	local result1 = ''
	local FFF = ''
	--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 = LabelOrDes('ar', p_num ,'labels' , args) or LabelOrDes('en', p_num ,'labels' , args) or "بدون تسمية"
			local description = LabelOrDes( 'ar', p_num ,'descriptions'	 , args) or "" --"بدون وصف"
			local P2875 = getprop(p_num , 'P2875' , args , '')
			local P3713 = getprop(p_num , 'P3713' , args , 'ليست')
			local P3734 = getprop(p_num , 'P3734' , args , 'مطابقة')
			local P3709 = getprop(p_num , 'P3709' , args , 'مختلفة')
			if P3713 ~='' or P3734 ~='' or P3709 ~='' then
				FFF = 'sa'
			end
			--local pro = '<span style="font-family: monospace, monospace;">' .. string.upper(p_num) .. '</span>'
			--local span= ' data-sort-value="' .. mw.ustring.gsub(p_num, 'P' , '').. '"| ' .. pro .. ''
			--local name  = "|" .. span	 .. "|| <b>[[d:Property:" .. p_num .. "|" .. label .. "]]</b> || "
			local propertytype = getpropertytype(p_num)
			local pro = ' (' .. string.upper(p_num) .. ')'
			local span= '| data-sort-value="' .. mw.ustring.gsub(p_num, 'P' , '').. '"| '
			--local name  = span  .. " <small>[[d:Property:" .. p_num .. "|" .. label .. pro .. "]]</small>" .. propertytype.. " ||"
			local name	= span	.. " <b>[[d:Property:" .. p_num .. "|" .. label .. pro .. "]]</b>" .. propertytype.. " ||"
			result = result .. name .. description
			result1 = result1 .. name .. description  .. "|| " .. P2875 ..	"\n|-\n"
			result = result	 .. "|| " .. P2875
			result = result	 .. "|| " .. P3713
			result = result	 .. "|| " .. P3734
			result = result	 .. "|| " .. P3709
			result = result ..	"\n|-\n"
	end
	div1 = '<div style="height:650px; width:'..width..'%; overflow:auto; padding:3px; border:solid 1px;">\n'
	down2 = mw.getCurrentFrame():preprocess('{{#تبديل:{{SUBPAGENAME}}|ملعب|doc|مختبر|نسخة|تجربة|شرح=|#default=[[تصنيف:قوالب تستخدم بيانات من ويكي بيانات]]}}' )
	down = '|}' .. down2
	if ii > 20 then 
		head = div1 .. head_s .. "100" .. head2
		down = down .. "</div>"
	end
	
	final  = head  .. r .. result .. down
	final1 = head  .. r1 .. result1 .. down
	--######################
	if FFF ~= '' then 
		return final
	else
		return final1
	end
	--######################
end

function p.usestable(frame)
	local parent = frame.getParent(frame)
	return p._usestable(parent.args)
end
return p