انتقل إلى المحتوى

وحدة:Countdown

من ويكي مصدر، المكتبة الحرة
-- This module powers {{countdown}} & {{عد تنازلي}}.

local p = {}

-- Constants
local lang = mw.language.getContentLanguage()
local getArgs = require('Module:Arguments').getArgs
local timeFormat = {'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'}

local function formatMessage(secondsLeft, color)
	local timeLeft = lang:formatDuration(secondsLeft, timeFormat)

	-- Color and bold the numbers, because it makes them look important.
	timeLeft = string.gsub(timeLeft, '(%d+)', '<span style="color: ' .. (color or '#F00') .. '; font-weight: bold;">%1</span>')

	timeLeft = string.gsub(timeLeft,'، ',' و');

	timeLeft = string.gsub(timeLeft,'%(','');
	timeLeft = string.gsub(timeLeft,'%)','');

    return timeLeft
end

local function  formatDurationList(_timeFormat)
	local allowedIntervals = {}
	for _,v in ipairs({'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'}) do
		if string.find( _timeFormat,string.sub(v,1,1)) then 
			table.insert(allowedIntervals, v) 
		end
	end
	return allowedIntervals
end

local function  process(args)
	local eventTime
	if type(args.date1) ~= 'nil' then 
		eventTime= tonumber(lang:formatDate('U',args.date1))
		else
        if (tonumber(args.year) and tonumber(args.month) and tonumber(args.day)) then 
        	eventTime = os.time({year=args.year, month=args.month, day=args.day
        		, hour=args.hour, min=args.minute, sec=args.second})
        	else
        	error(  '<strong class="error">خطأ: يجب تحديد السنة، والشهر، واليوم.</strong>')
        end
	end
	
	local timeToStart = os.difftime(eventTime, os.time()) -- (future time - current time)

	local timeToEnd
	if args.date2 then
			timeToEnd = tonumber(lang:formatDate('U',args.date2))
	        timeToEnd = os.difftime(timeToEnd , os.time())	
	        elseif tonumber(args.duration) then 
				if args['duration unit'] then 
					-- Duration is in unit other than seconds, use formatDate to add
					timeToEnd = tonumber(lang:formatDate('U', '@' .. tostring(timeToStart) 
						.. ' +' .. tostring(args.duration) .. ' ' .. args['duration unit']))
				else 
					timeToEnd = timeToStart + (tonumber(args.duration) 
						or error('args.duration should be a number of seconds', 0))
				end
	end

	return timeToStart,timeToEnd
end

local function  _shortMain(args)
	local timeToStart,timeToEnd = process(args)
	
	if timeToStart > 0 then
		-- Event has not begun yet
		return formatMessage(timeToStart, args.color)
	elseif timeToEnd then
		if timeToEnd > 0 then 	-- Event is in progress
			return  formatMessage(timeToEnd,  args.color)
		else 			-- Event had a duration and has now ended
			return  ''
		end
	else
		-- Event had no duration and has begun
		return  ''
	end
	
end

local function  _Main(args)
	local timeToStart,timeToEnd = process(args)

	local text
	if timeToStart > 0 then
		-- Event has not begun yet
		text = (args.eventbefore and string.gsub(args.eventbefore ,'@مدة@',formatMessage(timeToStart, args.color)))
                     or'بقي ' .. formatMessage(timeToStart, args.color)
						.. " على بداية «" .. args.event  .. "»."
	elseif timeToEnd then
		if timeToEnd > 0 then 	-- Event is in progress
			text = (args.eventstart2 and string.gsub(args.eventstart2, '@مدة@', formatMessage(timeToEnd, args.color)))
					  or "بداية «" .. args.event .. "» وستكون النهاية بعد " 
						.. formatMessage(timeToEnd,  args.color)
		else 			-- Event had a duration and has now ended
			text = args.eventend or ("انتهاء «" .. args.event .. "».")
		end
	else
		-- Event had no duration and has begun
		text = args.eventstart or ("بداية «" .. args.event .. '».')
	end
	local refreshLink
	if args.refresh == 'no' or args.refresh == 'لا' then
		refreshLink = ''
	else
		refreshLink = mw.title.getCurrentTitle():fullUrl({action = 'purge'})
		refreshLink = string.format(' <small><span class="plainlinks">([%s تحديث])</span></small>', refreshLink)
	end
	return text .. refreshLink
end

function p.main(frame)
	local args = getArgs(frame)
	
	if type(args.timeFormat) == 'string' then 
		timeFormat = formatDurationList(args.timeFormat)
	end
	if #timeFormat<1 then
		timeFormat = {'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'}
	end
	
	local args2 = {
		  date1=args.date1 or args['بداية'] or args['البداية'] 
		, date2=args.date2 or args['نهاية'] or args['النهاية'] 

		, year=args.year or args['سنة'] or args['السنة']
		, month=args.month or args['شهر'] or args['الشهر']
		, day=args.day or args['يوم'] or args['اليوم']
		, hour=args.hour or args['ساعة'] or args['الساعة']
		, min=args.minute or args['دقيقة'] or args['الدقيقة']
		, sec=args.second or args['ثانية'] or args['الثانية']
		, duration = args.duration or args['مدة'] or args['المدة']
		, ['duration unit'] = args['duration unit']

		, event= args.event or args['حدث'] or args['الحدث'] or "الحدث"
		, eventbefore = args.eventbefore or args['قبل الحدث'] 
		, eventstart = args.eventstart or args['بداية الحدث'] 
		, eventstart2 = args.eventstart2 or args['بداية الحدث 2'] 
		, eventend= args.eventend or args['نهاية الحدث']

		, color= args.color or args['لون'] or args['اللون']
		, refresh= args.refresh or args['تحديث'] or args['التحديث']
	}
    if  args.format == 'short' then return _shortMain(args2) end
    return _Main(args2)
end

return p