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

وحدة:Selected current events

من ويكيبيديا، الموسوعة الحرة
function cleanupArgs(argsTable)
	local cleanArgs = {}
	for key, val in pairs(argsTable) do
		if type(val) == 'string' then
			val = val:match('^%s*(.-)%s*$')
			if val ~= '' then
				cleanArgs[key] = val
			end
		else
			cleanArgs[key] = val
		end
	end
	return cleanArgs
end

function isAffirmed(val)
	if not(val) then return false end
	local affirmedWords = ' add added affirm affirmed include included on true yes y '
	return string.find(affirmedWords, ' '..string.lower(val)..' ', 1, true ) and true or false
end

function makeOutput(allItems, maxItems, more)
	local output = ''
	local itemIndex = 1
	local maxCount = math.min(#allItems, maxItems)
	while itemIndex <= maxCount do
		output = output .. allItems[itemIndex] .. '\n'
		itemIndex = itemIndex + 1
	end
	if more then
		output = output .. more
	end
	return mw.text.trim(output)
end


-- Get current events for a "D Month YYYY " date. Returns a table of list items.
function getCurrentEvents(date, dmyDates, keepPatterns, skipPatterns)
	local title = mw.title.new("بوابة:أحداث جارية/" .. date)
	local raw = title:getContent()
	local lines = mw.text.split( raw , '\n')
	local items = {}
	local formattedDate
	if dmyDates then
		formattedDate =  string.gsub(date, "(.*) (.*) (.*)", "%2 %3, %1 – ")
	else
		formattedDate =  string.gsub(date, "(.*) (.*) (.*)", "%3 %2 %1 – ")
	end
	for i, v in ipairs(lines) do
		local keep = false
		local skip = false
		if string.sub( v, 0, 1 ) == '*' then
			local text = mw.ustring.gsub(v, "%[%[(.-)%]%]","%1") -- remove wikilink brackets for better pattern matching
			text = mw.ustring.gsub(text, "%|"," ") -- remove pipes that would have been in piped links
			text = mw.ustring.gsub(text, "%[.-%]"," ") -- remove external links
			for ii, keepPatt in pairs(keepPatterns) do
				if not keep and mw.ustring.find(text, keepPatt) then
					keep = true
				end
			end
			if #skipPatterns > 0 then
				for iii, skipPatt in pairs(skipPatterns) do
					if not skip and mw.ustring.find(text, skipPatt) then
						skip = true			
					end
				end
			end
		end
		if keep and not skip then


			local item = mw.ustring.gsub(v, "%*+","*"..formattedDate)
			table.insert(items, item)
		end
	end
	return items
end

function getItems(maxDays, dmyDates, patterns, skipPatterns)
	local allItems = {}
	local lang = mw.language.new('ar')
	local daysAgo = 0
	while daysAgo < maxDays do
		local day = lang:formatDate('j F Y', 'now - '..daysAgo..' days')
		local dailyItems = getCurrentEvents(day, dmyDates, patterns, skipPatterns)
		for i, item in ipairs(dailyItems) do
			table.insert(allItems, item)
		end
		daysAgo = daysAgo + 1
	end
	return allItems
end

function getPatterns(args, prefix)
	local patterns = {}
	local ii = 1
	while args[prefix and prefix..ii or ii] do
		patterns[ii] = args[prefix and prefix..ii or ii]
		ii = ii + 1
	end
	return patterns
end

local p = {}

p.main = function(frame)
	local parent = frame.getParent(frame)
	local parentArgs = parent.args
	local args = cleanupArgs(parentArgs)

	if args['not'] and not args['not1'] then
		args['not1'] = args['not']
	end
	
	local patterns = getPatterns(args)
	if #patterns < 1 then
		return error("لم يتم تعيين نمط البحث")
	end

	local skipPatterns = getPatterns(args, 'not')

	local days = tonumber(args.days) or 30

	local dmyDates = args.dates and string.lower(args.dates) == 'dmy'

	local allItems = getItems(days, dmyDates, patterns, skipPatterns)
	if #allItems < 1 then
		return args.none or 'لا يوجد أخبار حديثة'
	end

	local maxItems = tonumber(args.max) or 6

	local more = args.more
	if isAffirmed(args.more) then
		more = "'''[[بوابة:أحداث جارية|المزيد من الأحداث الجارية...]]'''"
	end

	return makeOutput(allItems, maxItems, more)


end

return p