وحدة:Cycling race/winners in line

من ويكيبيديا، الموسوعة الحرة

local z = {}

z.wins = {
	["Q101246973"] = "بمركز أفضل مقاتل",
	["Q20882667"] = "بالمركز الأول",
	-- ["Q20882667"] = "في التصنيف العام",
	["Q20882747"] = "بالمركز الأول",
	["Q20882668"] = "بالمركز الثاني",
	["Q20882748"] = "بالمركز الثاني",
	["Q20882669"] = "بالمركز الثالث",
	["Q20882749"] = "بالمركز الثالث",
	["Q20882764"] = "المركز الثاني",
	["Q20882765"] = "المركز الثالث",
	["Q20893979"] = "في تصنيف المجموعة",
	["Q61976872"] = "في تصنيف الجنسيات",
	["Q20883328"] = "في تصنيف السرعة",
	["Q20883007"] = "في ترتيب النقاط",
	["Q20883139"] = "في ترتيب النقاط للشباب",
	["Q20883212"] = "في تصنيف الجبال",
	["Q20893983"] = "في تصنيف القتال",
	["Q20882921"] = "في ترتيب الفرق",
	["Q27907715"] = "بتصنيف الانفصال",
	["Q27907747"] = "بتصنيف آزوري دي إيطاليا",
	["Q28092831"] = "بتصنيف الصاعد",
	["Q27067170"] = "بتصنيف الانتظام",
	["Q27067359"] = "في تصنيف سباقات السرعة",
	["Q27104269"] = "في تصنيف النقاط",
	["Q27104271"] = "في تصنيف النقاط",
	}
z.leads = {
	["Q20882763"] = "الترتيب العام في نهاية المرحلة",
	["Q20883008"] = "ترتيب النقاط",
	["Q20883213"] = "ترتيب التسلق",
	["Q20883329"] = "ترتيب السرعة",
	["Q20883140"] = "ترتيب النقاط للشباب",
	["Q20882922"] = "ترتيب الفرق",
	["Q47640757"] = "تصنيف طواف العالم",
	["Q21686770"] = "تصنيف القتال",
	["Q20893984"] = "تصنيف القتال",
	["Q61976871"] = "تصنيف الجنسيات",
	["Q20965880"] = "تصنيف المجموعة",
	["Q27104684"] = "تصنيف الانتظام",
	["Q27104688"] = "تصنيف سباقات السرعة",
	["Q27907714"] = "تصنيف الانفصال",
	["Q27907748"] = "تصنيف آزوري دي إيطاليا",
	["Q28096780"] = "تصنيف الصاعد",
	}

function z.make_sort(Table)
	local tab2 = {}
	local nu = 0
	for _, value in ipairs(Table) do
		nu = nu + 1
		key = nu + 3
		if mw.ustring.find( value, "الأول",
			1, true ) ~=nil then
			key = 1
			elseif mw.ustring.find( value, "الثاني",
				1, true ) ~=nil then
			key = 2
				elseif mw.ustring.find( value, "الثالث",
				1, true ) ~=nil then
			key = 3
				end
		table.insert(tab2, { t = key, v = value })
	end
	--mw.addWarning(tostring(#tab2))
	table.sort(tab2, function (k1, k2) return k1.t < k2.t end )
	--mw.addWarning(tostring(#Table))
	newtable = {}
	for _, va in ipairs(tab2) do
		table.insert(newtable, va.v)
	end
	return newtable
end

function z.filter_claims_o(claims)
	local new_claims = {}
	local num =0
	for i, st in pairs(claims) do
		if st.mainsnak and st.mainsnak.datavalue then
			num = num + 1
			snak_id = st.mainsnak.datavalue.value.id
			if not snak_id then
				table.insert(new_claims, st)
			else
				-- mw.addWarning("claim: " .. num .. ", snak_id: " .. snak_id)
				if new_claims[snak_id] == nil then
					new_claims[snak_id] = st
				else
					-- mw.addWarning("claim: " .. num .. ",find snak_id " .. snak_id .. "in new_claims")
					if st["qualifiers-order"] then
						if new_claims[snak_id]["qualifiers-order"] == nil then
							new_claims[snak_id]["qualifiers-order"] = st["qualifiers-order"]
						end
					end
					--
					if st.qualifiers then
						if new_claims[snak_id].qualifiers == nil then
							new_claims[snak_id].qualifiers = st.qualifiers
						else
							for qual_pid, qual in pairs(st.qualifiers) do
								--mw.addWarning("claim: " .. num .. ",find snak_id " .. snak_id .. "in new_claims")
								if new_claims[snak_id].qualifiers[qual_pid] == nil then
									new_claims[snak_id].qualifiers[qual_pid] = qual
								else
									--mw.addWarning("claim: " .. num .. ",find qual_pid " .. qual_pid)
									for _, quasnak in pairs(qual) do
										snak_hash = quasnak.hash
										if new_claims[snak_id].qualifiers[qual_pid][snak_hash] == nil then
											new_claims[snak_id].qualifiers[qual_pid][snak_hash] = quasnak
										end
									end
								end
							end
						end
					end
					-- 
					if st.references then
						if new_claims[snak_id].references == nil then
							new_claims[snak_id].references = st.references
						else
							for _, ref in pairs(st.references) do
								ref_hash = ref.hash
								if new_claims[snak_id].references[ref_hash] == nil then
									new_claims[snak_id].references[ref_hash] = ref
								end
							end
						end
					end
				end
			end
		end
	end
	return new_claims	
end

function z.get_prefixes(quals)
	if quals == nil or quals == {} then return "" end
	local pre_wins = {}
	local pre_lead = {}
	local P642 = quals["P642"]
	if P642 == nil or P642 == {} then return "" end
	-- --
    for _, quall in  pairs(P642) do
    	qua_id = quall.datavalue.value.id
    	if qua_id then 
	    	if z.wins[qua_id] then 
	    		table.insert(pre_wins, z.wins[qua_id])
	    	elseif z.leads[qua_id] then 
	    		table.insert(pre_lead, z.leads[qua_id]) 
	    	else 
	    		table.insert(pre_lead, qua_id) 
	    	end
    	end
	end
	-- --
	local and_1 = " و"
	local and_2 = "، و"
	-- --
	local text = {}
	if #pre_wins > 0 then
		table.insert(text, "فاز " .. mw.text.listToText(pre_wins, and_1, and_1) )
	end
	if #pre_lead > 0 then
		table.insert(text, "تصدر " .. mw.text.listToText(pre_lead, and_1, and_1) )
	end
	-- --
	if #text > 0 then
		return mw.text.listToText(text, and_2, and_2)
	else
		return ""
	end
end

function z.c( claims, options)
	claims = z.filter_claims_o(claims)
	local valuetable = {}
	local prefixes = {}
	local numb = 0
	for i, statement in pairs(claims) do
		numb = numb + 1
		local value
		--local va = formatOneStatement(statement, {}, options)
		--if va.v then value = va.v end
		local stat = formatStatement(statement, options)
		if stat then
		    if stat.value then
		    	value = stat.value
		    end
		end
    	snak_id = statement.mainsnak.datavalue.value.id
    	if snak_id and options[snak_id] and options[snak_id] ~= "" then
    		value = options[snak_id]
		end
		if value then
			if not options.noref or options.noref == "" then
			    local refe = formatReferences(statement, options)
			    if refe then
			        value = value .. refe
			    end
			end
			pref = z.get_prefixes(statement.qualifiers)
			if pref then 
				value = pref .. " " .. value
    		end
    		table.insert(valuetable, value)
		end
	end
	local start = ''
	local and_2 = "، و"
	if options.inpoint and options.inpoint ~= '' then
		and_2 = '\n*'
		start = and_2
	end
	valuetable = z.make_sort(valuetable)
	
	local tot = start .. mw.text.listToText(valuetable, and_2, and_2)

	return tot
end

return z