Jump to content

Module:Selected current events

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Evad37 (talk | contribs) at 16:56, 22 May 2018 (try title:getContent()). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

function setCleanArgs(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

-- Get current events for a "YYYY Month" date. Returns a table of list items.
function getCurrentEvents(date, pattern)
	local title = mw.title.new("Portal:Current events/2018 May 20")
	local raw = title:getContent()
	local lines = mw.text.split( raw , '\n')
	local items = {}
	for i, v in ipairs(lines) do
		if string.sub( v, 0, 2 ) == '**' then
			if mw.ustring.find(v, pattern) then
				local item = mw.ustring.gsub(v, "%*+","*")
				table.insert(items, item)
			end
		end
	end
	return items
end



local p = {}

p.main = function(frame)
	local parent = frame.getParent(frame)
	local parentArgs = parent.args
	local lang = mw.language.new('en')

	local keywords = {}
	local ii = 1
	while parentArgs[ii] do
		table.insert(keywords, parentArgs[ii])
	end
	if not keywords[1] then
		return error("Keywords not set")
	end

	local allItems = {}
	local daysAgo = 0
	while daysAgo < 4 do
		local dailyItems = getCurrentEvents(lang:formatDate('Y F j', 'now - '..daysAgo..' days'), keywords[1])
		for i, item in ipairs(dailyItems) do
			table.insert(allItems, item)
		end
	daysAgo = daysAgo + 1
	end

	if #allItems < 1 then
		return 'No recent news'
	end
	return table.concat(allItems, '\n')

end

return p