وحدة:Countdown/ملعب

من أرابيكا، الموسوعة الحرة
اذهب إلى التنقل اذهب إلى البحث
-- This module powers {{countdown}} & {{عد تنازلي}}.

local p = {}
string=mw.ustring
-- Constants
local lang = mw.language.getContentLanguage()
local getArgs = require('Module:Arguments').getArgs
local alltimeparts = {'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'}
local timeFormat
local timeparts = 2
local color ='#F00'


local function formatMessage(text, secondsLeft)
	local maj = mw.ustring.find( text, '@مدة مجرورة@', 1, true ) and true or false

    local function getTableKeys(table,limit)
        local keyset = {}
        local n=0
        for _,v in pairs(alltimeparts) do
            mw.logObject(table)
            mw.logObject(v)
            if table[v] then
            	n=n+1
            	keyset[n] = v
	            if n>=limit then
	            	break
	        	end
            end
        end
        return keyset
    end
    local mytimeformat = timeFormat or (timeparts and getTableKeys(lang:getDurationIntervals(secondsLeft, alltimeparts),timeparts)) or alltimeparts
    mw.logObject(mytimeformat)
    local timeLeft = lang:formatDuration(secondsLeft, mytimeformat)
    -- Color and bold the numbers, because it makes them look important.
	timeLeft = string.gsub(timeLeft, '(%d+)', '<span style="color: ' .. color .. '; font-weight: bold;">%1</span>')
	if maj then
        string.gsub(timeLeft,'ان، ', 'ين و');
        if string.sub(timeLeft,-2) == 'ان' then
        	timeLeft = string.sub(timeLeft,1,-3) .. 'ين'
    	end
    end
    timeLeft = string.gsub(timeLeft,'، ',' و');
	timeLeft = string.gsub(timeLeft,'%(','');
	timeLeft = string.gsub(timeLeft,'%)','');

    if text and text ~= '' then
        timeLeft = string.gsub(text,maj and '@مدة مجرورة@' or '@مدة@', timeLeft)
    end
    return timeLeft
end

local function  formatDurationList(_timeFormat)
	local allowedIntervals = {}
	for _,v in ipairs(alltimeparts) 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))
		args.date1 = lang:formatDate('c',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})
			args.date1 = lang:formatDate('c','@' .. eventTime)
		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())
			args.date2 = lang:formatDate('c',args.date2)
	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']))
			args.date2 = lang:formatDate('c', '@' .. tostring(eventTime) 
			.. ' +' .. tostring(args.duration) .. ' ' .. args['duration unit'])
		else 
			timeToEnd = timeToStart + (tonumber(args.duration) 
				or error('args.duration should be a number of seconds', 0))
			args.date2 = lang:formatDate('c', '@' .. tostring(eventTime+ args.duration))
		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 _addparam(var,varkey)
	return '<span class="cdt-param-' .. varkey .. '">' .. var .. '</span>'
end

local function  _Main(args)
	local timeToStart,timeToEnd = process(args)
	local params = '<span class="countdown-timer-params" style="display:none">'
	local otherparam = ''
	local text
	local active = (timeToStart and timeToStart > 0) or (timeToEnd and timeToEnd > 0)
    
    if active then
        local paramargs={'date2', 'eventstart', 'eventstart2', 'eventend', 'event', 'color'}
        for _,v in pairs(paramargs) do
            if args[v] then params = params .. _addparam(args[v], v) end
        end
        if timeparts then otherparam = otherparam .. timeparts end
        if otherparam ~= '' then params = params .. _addparam(otherparam, 'other') end
        if timeToStart > 0 then
            params = params .. _addparam(args.date1, 'date1')
			params = params .. (args.eventbefore and _addparam(args.eventbefore, 'eventbefore') or '')
        end
    end


	if timeToStart > 0 then
		-- Event has not begun yet
        text = formatMessage(args.eventbefore or args.event and ('بقي @مدة@ على بداية «' .. args.event  .. "».") or '', timeToStart)
	elseif timeToEnd then
		if timeToEnd > 0 then 	-- Event is in progress
            text = formatMessage(args.eventstart2 or args.event and ("بداية «" .. args.event .. "» وستكون النهاية بعد @مدة مجرورة@")  or '', timeToEnd)
		else 			-- Event had a duration and has now ended
			text = args.eventend or args.event and ("انتهاء «" .. args.event .. "».") or ''
		end
	else
		-- Event had no duration and has begun
		text = args.eventstart or args.event and ("بداية «" .. args.event .. '».') or ''
	end

	local refreshLink
	if  not active or args.refresh == 'no' or args.refresh == 'لا' then
		refreshLink = ''
	else
		refreshLink = mw.title.getCurrentTitle():fullUrl({action = 'purge'})
		refreshLink = string.format('<span class="plainlinks cdt-refreshLink"><small>([%s تحديث])</small></span>', refreshLink)
	end

    if active then
        text = '<span class="cdt-ret">' .. text .. refreshLink .. '</span>'
        params = params .. '</span>'
        text = '<span class="countdown-timer">' .. params .. text .. '</span>'
    end

    return  text
end

function p.main(frame)
	local args = getArgs(frame)
	
	if type(args.timeFormat) == 'string' then
		timeFormat = formatDurationList(args.timeFormat)
	end
	mw.logObject(args['أجزاء الوقت'])
	mw.logObject(tonumber(args['أجزاء الوقت']))
	
	timeparts = tonumber(args.timeparts) or tonumber(args['أجزاء الوقت']) or timeparts
    mw.logObject(timeparts)
    color = args.color or args['لون'] or args['اللون'] or color
	local args2 
	if args.format == 'short' then
		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']
			, refresh= 'no'
		}		
	else
		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['الحدث']
			, eventbefore = args.eventbefore or args['قبل الحدث']
			, eventstart = args.eventstart or args['بداية الحدث']
			, eventstart2 = args.eventstart2 or args['بداية الحدث 2']
			, eventend= args.eventend or args['نهاية الحدث']
			, refresh= args.refresh or args['تحديث'] or args['التحديث'] 
		}
	end
    return _Main(args2)
end

return p