Jump to content

Module:Women in Red event

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by MSGJ (talk | contribs) at 15:11, 13 March 2024 (sort past events in reverse chronological order (recent first)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('strict')
local p = {}
local data = mw.loadJsonData('Wikipedia:WikiProject Women in Red/events')
local months = {'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'}
local date = os.date('*t')

p.findLatest = function(frame)
	local add = tonumber(frame.args.add) or 0
	local _max = 1
	for n, _ in pairs(data) do
		local val = tonumber(n) or 0
		if val<1000 and val>_max then
			_max = val
		end
	end
	return _max + add
end

local value = function(event, field, default)
	local data = data[tonumber(event)]
	if data and data[field] then
		return data[field]
	else
		return default
	end
end

p.main = function(frame)
	local ret = value(frame.args[1], frame.args[2], frame.args[3])
	return ret and mw.text.nowiki(ret) -- prevent # character from starting a list
end

local _date = function(event)
	local start_month = value(event, 'month') or value(event, 'start month')
	local end_month = value(event, 'end month')
	local month = (start_month and months[start_month] or '')
		.. (end_month and ('–' .. months[end_month]) or '')
	local sortkey = os.time{
		year = value(event, 'year', 0),
		month = start_month or 1,
		day = start_month and 2 or 1
	}
	return month .. (month=='' and '' or ' ') .. value(event, 'year', ''), sortkey
end

p.date = function(frame)
	local date, _ = _date(frame.args[1])
	return date
end

local _name = function(meetup)
	local series = value(meetup, 'series')
	local name = value(meetup, 'name')
	local out = series or ''
	if series and name then
		out = out .. ': '
	end
	out = out .. (name or '')
	return out
end

p.text = function(frame)
	local meetup = frame.args[1]
	return '[[Wikipedia:WikiProject Women in Red/Meetup/' .. meetup
		.. '|' .. _name(meetup) 
		.. ' ' .. value(meetup, 'type', 'edit-a-thon')
		.. ']] in '
		.. _date(meetup)
end

p.name = function(frame)
	return mw.text.nowiki(_name(frame.args[1]))
end

local showDate = function(meetup)
	local date, sortkey = _date(meetup)
	return mw.html.create('td'):attr('data-sort-value', sortkey):wikitext(date):done()
end

local link = function(n, name)
	local label = name and _name(n) or n
	return '[[Wikipedia:WikiProject Women in Red/Meetup/' .. n .. '|' .. label .. ']]'
end

local meetupCell = function(n)
	local link = '[[Wikipedia:WikiProject Women in Red/Meetup/' .. n .. '|' .. n .. ']]'
	return mw.html.create('td')
		:css('background-color', value(n, 'background') and ('#' .. value(n, 'background')))
		:css('text-align', 'center')
		:wikitext(link)
	:done()
end

p.list = function()
	local table = mw.html.create('table')
		table:addClass('wikitable'):addClass('sortable'):tag('tr')
			:tag('th'):wikitext('Meetup'):done()
			:tag('th'):wikitext('Logo'):done()
			:tag('th'):wikitext('Series'):done()
			:tag('th'):wikitext('Name'):done()
			:tag('th'):wikitext('Date'):done()
			:tag('th'):wikitext('Wrapper'):done()
			:tag('th'):wikitext('Category'):done()
		:done()
	for n, event in pairs(data) do
		local logo = event.image and '[[File:' .. event.image .. '|50px]]'
		local cat = 'WikiProject Women in Red meetup ' .. n .. ' articles'
		local url = mw.uri.fullUrl(':Category:' .. cat, {
			action = 'edit',
			preload = 'Template:WIR category/preload',
			summary = 'Create meetup category for Women in Red',
			['preloadparams[1]'] = n,
			})
		local pagesInCat = mw.site.stats.pagesInCategory(cat)
		local catText = '[[' .. ':Category:' .. cat .. '|' .. pagesInCat .. ']]'
		if pagesInCat==0 and not mw.title.new(':Category:' .. cat).exists then
			catText = catText .. ' ([' .. tostring(url) .. ' create])'
		end
		local series = event.series
		if series then
			series = '[[:Category:WikiProject Women in Red ' .. series .. ' articles|' .. series .. ']]'
		else
			series = ''
		end
		table:tag('tr')
			:node(meetupCell(n))
			:tag('td'):wikitext(logo):done()
			:tag('td'):wikitext(series):done()
			:tag('td'):wikitext(event.name or _name(n)):done()
			:node(showDate(n))
			:tag('td'):css('text-align', 'center')
				:wikitext('{{[[:Template:WIR-' .. n .. '|WIR-' .. n .. ']]}}')
			:done()
			:tag('td'):css('text-align', 'center'):wikitext(catText)
		:done()
	end
	return table:allDone()
end

p.articleBySeries = function(frame)
	local series = frame.args[1]
	local table = mw.html.create('table')
		table:addClass('wikitable'):addClass('sortable'):tag('tr')
			:tag('th'):wikitext('Meetup'):done()
			:tag('th'):wikitext('Name'):done()
			:tag('th'):wikitext('Date'):done()
			:tag('th'):wikitext('Articles'):done()
		:done()
	for n, event in pairs(data) do
		if event.series==series then
			local cat = 'WikiProject Women in Red meetup ' .. n .. ' articles'
			local catText
			if mw.title.new(':Category:' .. cat).exists then
				catText = '[[' .. ':Category:' .. cat .. '|' .. mw.site.stats.pagesInCategory(cat) .. ']]'
			else
				catText = ''
			end
			table:tag('tr')
				:node(meetupCell(n))
				:tag('td'):wikitext(event.name or _name(n)):done()
				:node(showDate(n))
				:tag('td'):css('text-align', 'center'):wikitext(catText)
			:done()
		end
	end
	return table:allDone()
end

p.articleByYear = function(frame)
	local year = frame.args[1]
	local total = 0
	local table = mw.html.create('table')
		table:addClass('wikitable'):addClass('sortable'):tag('tr')
			:tag('th'):wikitext('Meetup'):done()
			:tag('th'):wikitext('Name'):done()
			:tag('th'):wikitext('Date'):done()
			:tag('th'):wikitext('Articles'):done()
		:done()
	for n, event in pairs(data) do
		if tostring(event.year)==year then
			local cat = 'WikiProject Women in Red meetup ' .. n .. ' articles'
			local pagesInCat = mw.site.stats.pagesInCategory(cat)
			total = total + pagesInCat
			local catText
			if mw.title.new(':Category:' .. cat).exists then
				catText = '[[' .. ':Category:' .. cat .. '|' .. tostring(pagesInCat) .. ']]'
			else
				catText = ''
			end
			table:tag('tr')
				:node(meetupCell(n))
				:tag('td'):wikitext(_name(n)):done()
				:node(showDate(n))
				:tag('td'):css('text-align', 'center'):wikitext(catText)
			:done()
		end
	end
	local pagesInCat = mw.site.stats.pagesInCategory('WikiProject Women in Red ' .. year .. ' articles', '*').pages
	total = total + pagesInCat
	table:tag('tr')
		:tag('td'):attr('colspan', '3'):css('text-align', 'center'):wikitext('Unassociated'):done()
		:tag('td'):css('text-align', 'center'):wikitext(pagesInCat)
	:done():tag('tr')
		:tag('th'):attr('colspan', '3'):wikitext('Total'):done()
		:tag('th'):css('text-align', 'center'):wikitext(tostring(total))
	:done()
	
	return table:allDone()
end

p.count_banners = function(frame)
	local title = mw.title.getCurrentTitle()
	if title.namespace==1 then
		local wikitext = title:getContent()
		local _, count = mw.ustring.gsub(wikitext,'%{%{%s*[Ww]ikiProject Women in Red','')
		if count>1 then
			return '[[Category:Pages with ' .. tostring(count) .. ' WikiProject Women in Red banners]]'
		end
	end
end

local formatDate = function(date)
	local month = date.month or 0
	month = month<=9 and ('0'..tostring(month)) or tostring(month)
	return tostring(date.year) .. month
end

local searchEvents = function()
	local current_month = formatDate(date)
	local last_month = formatDate({
		year = date.year,
		month = date.month-1
	})
	if date.month==1 then
		last_month = formatDate({
			year = date.year-1,
			month = 12
		})
	end
	local events = {}
	events.ongoing, events.monthly, events.upcoming, events.recent, events.past = {}, {}, {}, {}, {}
	for n, event in ipairs(data) do
		if formatDate(event)==current_month then
			table.insert(events.monthly, n)
		elseif formatDate(event)==last_month then
			table.insert(events.recent, n)
		elseif event.year==date.year and not event.month then
			if event['start month'] and event['end month'] then
				if event['start month']<=date.month and event['end month']>=date.month then -- current initiative
					table.insert(events.ongoing, n)
				end
			else -- year-long event	
				table.insert(events.ongoing, n)
			end
		elseif event.year>date.year or (event.month and event.year==date.year and event.month>date.month) then -- future event
			table.insert(events.upcoming, n)
		else
			table.insert(events.past, n)
		end
	end
	table.sort(events.past, function(a, b) return a > b end)
	return events
end

p.current_events = function(frame)
	local mClickableButton2 = require('Module:Clickable button 2').main
	local button = function(text, colour)
		return mClickableButton2{
			[1] = text,
			color = 'white',
			style = colour and ('background-color: #' .. colour)
		}
	end
	local links = function(text, events, extra)
		local list = {}
		if events[1] then
			for _, n in ipairs(events) do
				table.insert(list, button(
					'[[Wikipedia:WikiProject Women in Red/Meetup/' .. n .. '|' .. _name(n) .. ']]',
					value(n, 'background')
				) .. ' ')
			end
			if extra then
				table.insert(list, button('<small>' .. extra .. '</small>'))
			end
			return mw.html.create('tr')
				:tag('td'):wikitext(text .. ': '):done()
				:tag('td'):wikitext(table.concat(list)):done()
		end
	end
	local events = searchEvents()
	local ret = mw.html.create('table')
		:node(links('Recently completed', events.recent))
		:node(links(months[date.month] .. ' ' .. date.year, events.monthly))
		:node(links('Ongoing initiatives', events.ongoing))
		:node(links('Upcoming events', events.upcoming, '[[Wikipedia:WikiProject Women in Red/Ideas|Ideas]]'))
	return tostring(ret)
end

p.events = function(frame)
-- function to produce automated list of events on Wikipedia:WikiProject Women in Red/Events
	local ret = mw.html.create('div')
	local ec = searchEvents()
	local links = function(class)
		if not ec[class] or not frame.args[class] then
			return nil
		end
		ret:tag('h2'):wikitext(frame.args[class]):done()
		if ec[class][1] then
			ret:tag('ul')
			for _, n in ipairs(ec[class]) do
				local date2 = _date(n)
				if date2==tostring(tonumber(date2)) then
					date2 = date2 .. ' year-long ' .. value(n, 'type', 'editathon')
				end
				ret:tag('li'):wikitext(date2 .. ': ' .. link(n, true)):done()
			end
		else
			ret:tag('p'):css('font-style', 'italic'):wikitext('None to display.'):done()
		end
	end
	links('ongoing')
	links('monthly')
	links('recent')
	links('upcoming')
	links('past')
	return tostring(ret)
end

p.event_list = function(frame)
-- function to produce bulleted list of events for Template:Women in Red navigation
	local ec = searchEvents()
	local class = frame.args[1]
	if not ec[class] then
		return nil
	end
	local ret = mw.html.create('ul')
	for _, n in ipairs(ec[class]) do
		ret:tag('li'):wikitext(link(n, true)):done()
	end
	return tostring(ret)
end

p.event_list_by_year = function(frame)
-- function to produce bulleted list of events for Template:Women in Red navigation
	local ret = mw.html.create('ul')
	for year = date.year-1, 2015, -1 do
		local sublist = mw.html.create('ul') -- new sublist for this year
		for n, event in ipairs(data) do
			if event.year==year then
				sublist:tag('li'):wikitext(link(n, true)):done()
			end
		end
		ret:tag('li')
			:wikitext(tostring(year))
			:node(sublist)
			:done()
	end
	local collapse = frame:expandTemplate{title = 'Template:Collapse top', args = {
		bg = '#F5F5F5;',
		title = '2015 – ' .. date.year-1,
		border = '0',
		border2 = '0'
	}} .. tostring(ret) .. '</table></div>'
	return collapse
end

return p