تبديل القائمة
Toggle preferences menu
تبديل القائمة الشخصية
غير مسجل للدخول
سيكون عنوان الآيبي الخاص بك مرئيًا للعامة إذا قمت بإجراء أي تعديلات.

وحدة:ContentNumber

من أرابيكا، الموسوعة العربية الحرة
المزيد من اللغات

هذه هي النسخة الحالية من هذه الصفحة، وقام بتعديلها عبد العزيز (نقاش | مساهمات) في 08:40، 24 أبريل 2025. العنوان الحالي (URL) هو وصلة دائمة لهذه النسخة.

(فرق) → نسخة أقدم | نسخة حالية (فرق) | نسخة أحدث ← (فرق)
local core = require 'Module:Hijri/core'
local hijri = require 'Module:Hijri'
--[[ ----------------- I M P O R T --------------]]
Date = core.Date
formatdate = hijri.lua_format_date
string = mw.ustring

local function md(adate)
	return ((adate.month<10) and '0' or '') .. adate.month .. '-' .. ((adate.day<10) and '0' or '') .. adate.day
end

local function getTodaynum(frame)
	local a
	local A = frame.args
	if A.date and A.date ~= '' then
		local adate = mw.text.split(A.date,"-")
		a = Date('gregorian', tonumber(adate[1]), tonumber(adate[2]), tonumber(adate[3])):get_jd()
	else
		a = Date('gregorian'):get_jd()
	end	
	
	if not tonumber(A[1]) then
		return 0
	end
	local ncount = tonumber(A[1])
	local nbaseday = tonumber(A[2]) or 0
	local duration = 1
	return math.fmod(math.floor((a-nbaseday)/duration),ncount)
end

local function getskipped(dated, dategre, nbaseday)
	local datetxt = dategre:iso()
	local baseday = Date('gregorian', nbaseday)
	local basedaytxt = baseday:iso()
	local skipped = 0
	-- if dated["هجري"] then
	-- 	datehijri = datehijri or Date('hijri', dategre:get_jd())
	-- 	for k,v in pairs(dated["هجري"]) do
	-- 	    if v <= datetxt then
		   		
	-- 	   	end
	-- 	end
	-- end
	if dated["ميلادي"] then
		local byear = baseday.year
		local bmd = md(baseday)
		local cyear = dategre.year
		local cmd = md(dategre)
		
		for k,v in pairs(dated["ميلادي"]) do
		    if v <= datetxt then
		   		local vyear, vmd
		   		if v > basedaytxt then
		   			vyear = tonumber(v:sub(1,4))
		   			vmd = v:sub(6)
		   		else
		   			vyear = byear
		   			vmd = cmd
		   		end
		   		local ydif = cyear - vyear
		   		skipped = skipped + ydif - (vmd > k and 1 or 0) + (k < cmd and 1 or 0)
		   	end
		end
	end
	
	if dated["هجري"] then
		local hbaseday = Date('hijri', nbaseday)
		local byear = hbaseday.year
		local bmd = md(hbaseday)
		local datehijri = Date('hijri', dategre:get_jd())
		local cyear = datehijri.year
		local cmd = md(datehijri)
		
		for k,v in pairs(dated["هجري"]) do
		    if v <= datetxt then
		   		local vyear, vmd
		   		if v > basedaytxt then
		   			local vsplits = mw.text.split(v,'-')
		   			local vhijri = Date('hijri', core.gregorian2jd(tonumber(vsplits[1]), tonumber(vsplits[2]), tonumber(vsplits[3])))
		   			vyear = vhijri.year
		   			vmd = md(vhijri)
		   		else
		   			vyear = byear
		   			vmd = cmd
		   		end
		   		local ydif = cyear - vyear
		   		skipped = skipped + ydif - (vmd > k and 1 or 0) + (k < cmd and 1 or 0)
		   	end
		end
	end
	return skipped
end

local function _getTodaypage(args)
	local dategre, datehijri, ncount, nbaseday
	local skipped = 0
	local dated = {}
	local basepage = args[1]
	
	if args.date and args.date ~= '' then
		local adate = mw.text.split(args.date,"-")
		dategre = Date('gregorian', tonumber(adate[1]), tonumber(adate[2]), tonumber(adate[3]))
	else
		dategre = Date('gregorian')
	end	
	
	local datedpage = mw.title.new( basepage  .. "/عداد/مؤرخة.json")
	if datedpage.exists then
		dated = mw.loadJsonData(basepage  .. "/عداد/مؤرخة.json") or {}
	end

	if dated["هجري"] then
		datehijri = Date('hijri', dategre:get_jd())
		if dated["هجري"][md(datehijri)] then
			return formatdate{year = datehijri.year, month = datehijri.month, day = datehijri.day, format = '~هي ~هش'}
		end
	end
	
	if dated["ميلادي"] and dated["ميلادي"][md(dategre)] then
		return formatdate{year = dategre.year, month = dategre.month, day = dategre.day, format = 'd-m'}
	end

	local frame = mw.getCurrentFrame()
	local success, result = pcall(function()
	return tonumber(frame:expandTemplate{ title = basepage .. "/عداد/مرقمة" }) or 0
	end)

	ncount = success and result or 0

	if ncount == 0 then
		return error('لا يوجد صفحات  فرعية لهذا النوع')
	end
	
	local nbasedaypage = mw.title.new( basepage  .. "/عداد/بداية")
	if nbasedaypage.exists then
		nbaseday = tonumber(mw.getCurrentFrame():expandTemplate{ title = basepage  .. "/عداد/بداية"})
		nbaseday = (nbaseday > 0) and nbaseday or 2460677 --2025-01-01
	end
	
	if dated["ميلادي"] or dated["هجري"] then
		skipped = getskipped(dated, dategre, nbaseday)
	end
	
	local duration = 1
	return math.fmod(math.floor((dategre:get_jd()-skipped-nbaseday)/duration),ncount)
end

local function getTodaypage(frame)
	local num = _getTodaypage(frame.args)
	
	return frame.args[1] .. "/" .. num
end

local function _baseday(ncount, nbaseday, dated)
	local dategre = Date('gregorian')
	local skipped = 0
	
	if dated["ميلادي"] or dated["هجري"] then
		skipped = getskipped(dated, dategre, nbaseday)
	end
	local duration = 1
	return nbaseday - skipped + math.floor((dategre:get_jd()-nbaseday)/duration/ncount) * duration * ncount 
end	

local function baseday(frame)
	local A = frame.args
	if not A[1] then
		return 0
	end
	local ncount = tonumber(A[1])
	local nbaseday = tonumber(A[2]) or 0
	return _baseday(ncount, nbaseday, {})
end	

local function basedaybypage(frame)
	local basepage = frame.args[1]
	local datedpage = mw.title.new( basepage  .. "/عداد/مؤرخة.json")
	if datedpage.exists then
		dated = mw.loadJsonData(basepage  .. "/عداد/مؤرخة.json") or {}
	end
	
	local ncountpage = mw.title.new( basepage  .. "/عداد/مرقمة")

	if ncountpage.exists then
		ncount = tonumber(ncountpage.content) or 0
	end

	local nbasedaypage = mw.title.new( basepage  .. "/عداد/بداية")
	if nbasedaypage.exists then
		nbaseday = tonumber(mw.getCurrentFrame():expandTemplate{ title = basepage  .. "/عداد/بداية"})
		nbaseday = (nbaseday > 0) and nbaseday or 2460677 --2025-01-01
	end
	return _baseday(ncount, nbaseday, dated)
end

return {
	getTodaynum = getTodaynum ,
	baseday = baseday,
	_baseday = _baseday,
	basedaybypage = basedaybypage,
	_getTodaypage = _getTodaypage,
	getTodaypage = getTodaypage
}