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:22, 1 April 2024 (unused functions). 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')
local lang = mw.getContentLanguage()

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 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 abbrevMonth = function(month)
	local monthcode = month<10 and ('0'..tostring(month)) or tostring(month)
	local timestamp = '2024' .. monthcode .. '01000000'
	return lang:formatDate('M', timestamp)
end

local _findLatest = function()
	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
end
p.findLatest = function(frame)
	return _findLatest() + (tonumber(frame.args.add) or 0)
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 buildTable = function(option, show, frame)
	local meetupCell = function(n)
		return mw.html.create('th')
			:css('background-color', value(n, 'background') and ('#' .. value(n, 'background')))
			:css('text-align', 'center')
			:wikitext(n)
		:done()
	end
	local total = 0
	option.meetup = true
	option.name = true
	option.date = true
	local header = mw.html.create('tr')
	local cols = 0
	for _, col in ipairs({'meetup', 'logo', 'series', 'name', 'date', 'template', 'wrapper', 'category'}) do
		if option[col] then
			cols = cols + 1
			header:tag('th'):wikitext(lang:ucfirst(col)):done()
		end
	end
	local rows = {}
	for n, event in pairs(data) do
		if not show or show(n, event) then
			local row = mw.html.create('tr')
				:node(meetupCell(n))
			if option.logo then
				local logo = event.image and '[[File:' .. event.image .. '|50px]]'
				row:tag('td'):wikitext(logo):done()
			end
			if option.series then
				local series = event.series and '[[:Category:WikiProject Women in Red ' .. event.series .. ' articles|' .. event.series .. ']]' or ''
				row:tag('td')
				:wikitext(series)
				:done()
			end
			local name = option.series and event.name or _name(n)
			local link = '[[Wikipedia:WikiProject Women in Red/Meetup/' .. n .. '|' .. name .. ']]'
			row:tag('td'):wikitext(link):done()
			:node(showDate(n))
			if option.template then
				local template = frame:extensionTag('code', '{{WIR|' .. n .. '}}')
				row:tag('td'):css('text-align', 'center'):wikitext(template):done()
			end
			if option.wrapper then
				row:tag('td')
				:css('text-align', 'center')
				:wikitext('{{[[:Template:WIR-' .. n .. '|WIR-' .. n .. ']]}}')
				:done()
			end
			if option.category then
				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'
				})
				local pagesInCat = mw.site.stats.pagesInCategory(cat)
				total = total + pagesInCat
				local catText = '[[' .. ':Category:' .. cat .. '|' .. pagesInCat .. ']]'
				if pagesInCat==0 and not mw.title.new(':Category:' .. cat).exists then
					catText = catText .. ' ([' .. tostring(url) .. ' create])'
				end
				row:tag('td')
					:css('text-align', 'center')
					:wikitext(catText)
					:done()
			end
			table.insert(rows, tostring(row))
		end
	end
	local footer
	if option.category then
		footer = mw.html.create('tr')
			:tag('th'):attr('colspan', cols-1):wikitext('Total'):done()
			:tag('th'):css('text-align', 'center'):wikitext(tostring(total)):done()
		end
	return mw.html.create('table')
		:addClass('wikitable'):addClass('sortable')
		:node(header)
		:node(table.concat(rows))
		:node(footer)
end

p.list = function(frame)
	local yesno = require('Module:Yesno')
	local first = tonumber(frame.args.first) or 1
	local last = tonumber(frame.args.last) or 9999
	if frame.args.latest then
		last = _findLatest()
		first = last - tonumber(frame.args.latest) + 1
	end
	local option = {}
	for key, value in pairs(frame.args) do
		option[key] = yesno(value)
	end
	return buildTable(
		option,
		function(n, _) return n>=first and n<=last end,
		frame
	)
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 eventStatus = function(event)
--function to decide whether an event is upcoming, current, recently completed or past
	if formatDate(event)==current_month then
		return 'current'
	elseif formatDate(event)==last_month then
		return 'recent'
	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
				return 'ongoing'
			end
		else
			return 'ongoing'
		end
	elseif event.year>date.year or (event.month and event.year==date.year and event.month>date.month) then
		return 'upcoming'
	else
		return 'past'
	end
end

local searchEvents = function()
	local events = {current = {}, recent = {}, ongoing = {}, upcoming = {}, past = {}}
	for n, event in ipairs(data) do
		local status = eventStatus(event)
		table.insert(events[status], n)
	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(extra))
			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.current))
		: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('current')
	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 past events for Template:Women in Red navigation
	local ret = mw.html.create('ul')
	local year = frame.args[1] and tonumber(frame.args[1]) or 2023
	local events = {}
	for n, event in ipairs(data) do
		if event.year==year and eventStatus(event)=='past' then
			local type = event.type or 'edit-a-thon'
			if not events[type] then
				events[type] = {}
			end
			local month = value(n, 'month', 0)
			if not events[type][month] then
				events[type][month] = {}
			end
			table.insert(events[type][month], n)
		end
	end
	for type, eventsOfType in pairs(events) do
		ret:wikitext(lang:uc(type)..'S: ')
		for _, n in ipairs(eventsOfType[0] or {}) do
			ret:tag('li'):wikitext(link(n, true)):done()
		end
		for month = 1, 12 do
			if eventsOfType[month] then
				local sublist = mw.html.create('ul')
				for _, n in ipairs(eventsOfType[month]) do
					sublist:tag('li'):wikitext(link(n, true)):done()
				end
				ret:tag('li'):wikitext(abbrevMonth(month) .. ': '):node(sublist):done()
			end
		end
	end
	return tostring(ret)
end

local meetupHeader = function(frame, n)
	local image = '[[File:' .. value(n, 'image', 'WiR Tidy Talk Pages.png') .. '|40px]]'
	local text = 'This category contains talk pages of articles which were created or improved during the <b>[[Wikipedia:WikiProject Women in Red/Meetup/'
		.. n .. '|' .. _name(n) .. ' ' .. value(n, 'type', 'edit-a-thon') .. ']]</b> hosted by the [[Wikipedia:WikiProject Women in Red|Women in Red project]] in '
		.. _date(n) .. '. It is automatically populated by '
		.. frame:expandTemplate{title = 'tlx', args = {
			[1] = 'WikiProject Women in Red',
			[2] = n
		}} .. '.'
		.. '[[Category:WikiProject Women in Red articles by meetup|' .. n .. ']]'
	if value(n, 'year') then
		text = text .. '[[Category:WikiProject Women in Red ' .. value(n, 'year') .. ' articles|' .. n .. ']]'
	end
	if value(n, 'series') then
		text = text .. '[[Category:WikiProject Women in Red ' .. value(n, 'series'):gsub('#', '') .. ' articles|' .. n .. ']]'
	end
	local empty = mw.site.stats.pagesInCategory('WikiProject Women in Red meetup ' .. n .. ' articles')==0
	local future = false
	if value(n, 'year')>date.year then
		future = true
	elseif value(n, 'year')==date.year then
		local start_month = value(n, 'month') or value(n, 'start month')
		if start_month and start_month>=date.month then
			future = true
		end
	end
	return (empty and future and frame:expandTemplate{
		title = 'Possibly empty category',
		args = {[1] = 'This category should be populated soon.'}
	} or '') .. frame:expandTemplate{title = 'cmbox', args = {
		image = image,
		text = text
	}} .. frame:expandTemplate{title = 'CatAutoTOC'}
end

local yearHeader = function(frame, year)
	local image = '[[File:Women in Red logo.svg|40px]]'
	local text = 'This category contains talk pages of articles which were created or improved in '
		.. year .. ' as part of the [[Wikipedia:WikiProject Women in Red|Women in Red project]]. The category is automatically populated by '
		.. frame:expandTemplate{title = 'tlx',	args = {
			[1] = 'WikiProject Women in Red',
			[2] = 'year=' .. year
		}} .. '.' .. frame:expandTemplate{title = 'Collapsed top', args = {
			bg = 'transparent',
			width = '80%',
			[1] = 'Articles improved during ' .. year
		}} ..  tostring(buildTable(
			{category = true},
			function(_, event) return tostring(event.year)==year end
		)) .. '</table>'
		.. '[[Category:WikiProject Women in Red in ' .. year .. '|Articles]]'
		.. '[[Category:WikiProject Women in Red articles by year|' .. year .. ']]'
	return frame:expandTemplate{title = 'cmbox', args = {
		image = image,
		text = text
	}} .. frame:expandTemplate{title = 'CatAutoTOC'}
end

local seriesHeader = function(frame, series)
	local image = '[[File:Women in Red logo.svg|40px]]'
	local text = 'This category contains talk pages of articles which were created or improved as part of the ' .. series
		.. ' series of meetups hosted by the [[Wikipedia:WikiProject Women in Red|Women in Red project]].'
		.. frame:expandTemplate{title = 'Collapsed top', args = {
			bg = 'transparent',
			width = '80%',
			[1] = 'Articles improved during ' .. series .. ' events'
		}} ..  tostring(buildTable(
			{category = true},
			function(_, event) return event.series==series end
		)) .. '</table>'
		.. '[[Category:WikiProject Women in Red articles by series|' .. series .. ']]'
	return frame:expandTemplate{title = 'cmbox', args = {
		image = image,
		text = text
	}} .. frame:expandTemplate{title = 'CatAutoTOC'}
end

p.category_header = function(frame)
	local page = mw.title.getCurrentTitle().text
	local n = page:match('WikiProject Women in Red meetup (%d+) articles')
	local year = page:match('WikiProject Women in Red (%d+) articles')
	local series = page:match('WikiProject Women in Red (.+)')
	if n then
		return meetupHeader(frame, n)
	elseif year then
		return yearHeader(frame, year)
	elseif series then
		return seriesHeader(frame, series:gsub('%sarticles', ''):gsub('1day1woman', '#1day1woman'))
	end
end

return p