Jump to content

Module:Selected recent additions: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
try using iterator function to see if it gives better performance
update from sandbox (fix randomising and cleaning of items)
Line 23: Line 23:


function makeOutput(allItems, maxItems, more)
function makeOutput(allItems, maxItems, more)
local randomiseArgs = {
local randomisedItems = randomModule.main('list', allItems)
['t'] = allItems,
local output = ''
['limit'] = maxItems
local itemIndex = 1
}
local maxCount = math.min(#allItems, maxItems)
local randomisedItems = randomModule.main('array', randomiseArgs )
while itemIndex <= maxCount do
local output = table.concat(randomisedItems, '\n')
output = output .. allItems[itemIndex] .. '\n'
itemIndex = itemIndex + 1
end
if more then
if more then
output = output .. more
output = output .. more
Line 45: Line 43:
cleaned = mw.ustring.gsub(cleaned, "%[.-%]"," ")
cleaned = mw.ustring.gsub(cleaned, "%[.-%]"," ")
return cleaned
return cleaned
end

function cleanupRawWikitext(raw)
-- remove lead section
raw = mw.ustring.gsub(raw, ".-==","==", 1)
-- remove images
raw = mw.ustring.gsub(raw, "<div.-{{main page image.-\div>", "")
-- remove pictured notes
raw = mw.ustring.gsub(raw, "''%(.-pictured.-%)''", "")
-- remove timestamps
raw = mw.ustring.gsub(raw, "%*'''''.*(UTC)'''''", "")
return raw
end
end


Line 85: Line 71:
end
end
if keep and not skip then
if keep and not skip then
cleanItem = mw.ustring.gsub(item, "''%(.-pictured.-%)''", "")
local cleanItem = mw.ustring.gsub(item, "''%(.-pictured.-%)''", "")
table.insert(items, item)
table.insert(items, cleanItem)
end
end
end
end

Revision as of 15:31, 31 May 2018

local randomModule = require('Module:Random')

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 randomiseArgs = {
		['t'] = allItems,
		['limit'] = maxItems
	}
	local randomisedItems = randomModule.main('array', randomiseArgs )
	local output = table.concat(randomisedItems, '\n')
	if more then
		output = output .. more
	end
	return mw.text.trim(output)
end

function cleanForPatternMatching(wikitext)
	-- remove wikilink brackets
	local cleaned = mw.ustring.gsub(wikitext, "%[%[(.-)%]%]","%1")
	-- remove pipes that would have been in piped links
	cleaned = mw.ustring.gsub(cleaned, "%|"," ")
	-- remove external links
	cleaned = mw.ustring.gsub(cleaned, "%[.-%]"," ")
	return cleaned
end

-- Get current events for a "YYYY Month D" date. Returns a table of list items.
function getRecentAdditions(subpage, keepPatterns, skipPatterns)
	local title = mw.title.new('Wikipedia:Recent additions' .. subpage)
	local raw = title:getContent()
	local itemPattern = '%*%s?%.%.%.[%S ]*'
	local items = {}
	for item in mw.ustring.gmatch(raw, itemPattern) do
		local keep = false
		local skip = false
		local isListItem = ( string.sub(item, 0, 1) == '*' )
		if isListItem then
			local text = cleanForPatternMatching(item)
			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 cleanItem = mw.ustring.gsub(item, "''%(.-pictured.-%)''", "")
			table.insert(items, cleanItem)
		end
	end
	return items
end

function getItems(maxMonths, patterns, skipPatterns)
	local allItems = {}
	local lang = mw.language.new('en')
	local monthsAgo = 0
	while monthsAgo < maxMonths do
		local subpage
		if monthsAgo == 0 then
			subpage = ''
		else
			subpage = '/' .. lang:formatDate('Y/F', 'now - '..monthsAgo..' months')
		end
		local monthyItems = getRecentAdditions(subpage, patterns, skipPatterns)
		for i, item in ipairs(monthyItems) do
			table.insert(allItems, item)
		end
		monthsAgo = monthsAgo + 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("Search pattern not set")
	end

	local skipPatterns = getPatterns(args, 'not')

	local months = tonumber(args.months) or 30

	local allItems = getItems(months, patterns, skipPatterns)
	if #allItems < 1 then
		return args.none or 'No recent additions'
	end

	local maxItems = tonumber(args.max) or 6

	local more = args.more
	if isAffirmed(args.more) then
		more = "'''[[Wikipedia:Recent additions|More recent additions...]]'''"
	end

	return makeOutput(allItems, maxItems, more)


end

return p