Jump to content

Module:WorkersVoices: Difference between revisions

Your free guide through every stage of migration
No edit summary
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 8: Line 8:
driver_transport = "Driver / transport", general_expat_life = "General expat life",
driver_transport = "Driver / transport", general_expat_life = "General expat life",
study_work_settlement = "Study, work & settlement",
study_work_settlement = "Study, work & settlement",
other = "Other", unclear = "Unclear",
other = "Other",
}
}


local SENTIMENT_LABELS = {
local SENTIMENT_ICONS = {
positive = "Positive", mixed = "Mixed", negative = "Negative",
positive = "🟢 Positive", mixed = "🟡 Mixed", negative = "🔴 Negative",
}
}


local function wikiEscape(s)
local function htmlEscape(s)
if not s then return "" end
if not s then return "" end
return (s:gsub("|", "|"):gsub("%[", "["):gsub("%]", "]"))
return (s:gsub("&", "&amp;"):gsub("<", "&lt;"):gsub(">", "&gt;"):gsub('"', "&quot;"))
end
 
local function tlen(t)
local n = 0
for _ in ipairs(t) do n = n + 1 end
return n
end
end


Line 23: Line 29:
if g == "man" then return "Man" end
if g == "man" then return "Man" end
if g == "woman" then return "Woman" end
if g == "woman" then return "Woman" end
return "Unclear"
return "Not specified"
end
 
local function sectorLabel(s)
if not s or s == "unclear" then return "Not yet confirmed" end
return SECTOR_LABELS[s] or htmlEscape(s)
end
 
local function sentimentLabel(s)
return SENTIMENT_ICONS[s] or "Unknown"
end
 
local function warningHtml(video)
if not (video.contentWarning and video.contentWarning.flag) then
return ""
end
local label = video.contentWarning.label or ""
if label:find("automated discovery") then
return '<p>🔍 <i>Pending review</i></p>'
end
return '<p>⚠️ <b>' .. htmlEscape(label) .. '</b></p>'
end
 
local function youtubeThumbnail(url)
if not url then return nil end
local id = url:match("[?&]v=([%w_%-]+)")
if not id then
id = url:match("youtu%.be/([%w_%-]+)")
end
if id then
return "https://img.youtube.com/vi/" .. id .. "/mqdefault.jpg"
end
return nil
end
 
local function buildCard(region, country, video)
local resource = ""
if video.verifiedResource and video.verifiedResource.url then
resource = ' &middot; [' .. video.verifiedResource.url .. ' ' ..
htmlEscape(video.verifiedResource.label) .. ']'
end
 
local note = video.curatorNote and video.curatorNote ~= ""
and ('<p><i>' .. htmlEscape(video.curatorNote) .. '</i></p>') or ""
 
local sourceLink = '[' .. video.sourceUrl .. ' ▶ Watch source]'
 
local thumb = ""
local thumbUrl = youtubeThumbnail(video.sourceUrl)
if thumbUrl then
thumb = '<p>[' .. video.sourceUrl .. ' ' .. thumbUrl .. ']</p>'
end
 
return table.concat({
'<h4>' .. country.flag .. ' ' .. htmlEscape(country.name) .. ' — ' .. sentimentLabel(video.sentiment) .. '</h4>',
thumb,
'<p><b>Sector:</b> ' .. sectorLabel(video.sector) ..
' &middot; <b>Gender:</b> ' .. genderLabel(video.gender) ..
' &middot; <b>Added:</b> ' .. htmlEscape(video.addedDate) .. '</p>',
note,
warningHtml(video),
'<p>' .. sourceLink .. resource .. '</p>',
}, "\n")
end
end


Line 37: Line 105:
end
end


local out = {}


for _, region in ipairs(data.regions) do
local cardTexts = {}
local noVideoLines = {}


if frame.args.debug then
    if not data then
        return "DEBUG: data is nil — JSON failed to load entirely."
    end
    if not data.regions then
        return "DEBUG: data loaded but data.regions is nil. Keys found: " .. table.concat((function()
            local keys = {}
            for k in pairs(data) do table.insert(keys, tostring(k)) end
            return keys
        end)(), ", ")
    end
    local regionCount = #data.regions
    local countryCount = 0
    local videoCount = 0
    for _, region in ipairs(data.regions) do
        for _, country in ipairs(region.countries) do
            countryCount = countryCount + 1
            videoCount = videoCount + #country.videos
        end
    end
    return string.format("DEBUG: %d regions, %d countries, %d total videos found.",
        regionCount, countryCount, videoCount)
end
local rows = {}
local noVideoLines = {}
for _, region in ipairs(data.regions) do
for _, country in ipairs(region.countries) do
for _, country in ipairs(region.countries) do
if #country.videos == 0 then
if tlen(country.videos) == 0 then
table.insert(noVideoLines,
table.insert(noVideoLines, "🔸 " .. country.flag .. " " .. htmlEscape(country.name))
"* " .. wikiEscape(country.flag) .. " " .. wikiEscape(country.name))
else
else
for _, video in ipairs(country.videos) do
for _, video in ipairs(country.videos) do
local warning = "—"
table.insert(cardTexts, buildCard(region, country, video))
if video.contentWarning and video.contentWarning.flag then
warning = "'''Warning:''' " .. wikiEscape(video.contentWarning.label)
end
 
local resource = "—"
if video.verifiedResource and video.verifiedResource.url then
resource = "[" .. video.verifiedResource.url .. " " ..
wikiEscape(video.verifiedResource.label) .. "]"
end
 
local note = video.curatorNote and video.curatorNote ~= ""
and ("''" .. wikiEscape(video.curatorNote) .. "''") or "—"
 
local sourceLink = "[" .. video.sourceUrl .. " Source]"
 
table.insert(rows, table.concat({
"|-",
"| " .. wikiEscape(region.name),
"| " .. wikiEscape(country.flag) .. " " .. wikiEscape(country.name),
"| " .. (SECTOR_LABELS[video.sector] or wikiEscape(video.sector)),
"| " .. genderLabel(video.gender),
"| " .. (SENTIMENT_LABELS[video.sentiment] or "Unknown"),
"| " .. wikiEscape(video.addedDate),
"| " .. sourceLink,
"| " .. warning,
"| " .. note,
"| " .. resource,
}, "\n"))
end
end
end
end
end
end
end


local tableWikitext = table.concat({
if #cardTexts > 0 or #noVideoLines > 0 then
'{| class="wikitable sortable"',
table.insert(out, '<h2>' .. htmlEscape(region.name) .. '</h2>')
"! Region !! Country !! Sector !! Gender !! Sentiment !! Added !! Source !! Content warning !! Why we picked this !! Verified resource",
table.concat(rows, "\n"),
"|}",
}, "\n")


local out = { tableWikitext }
if #cardTexts > 0 then
local tableRows = {}
local i = 1
while i <= #cardTexts do
local left = cardTexts[i]
local right = cardTexts[i + 1]
local row = '<tr>\n<td valign="top" width="50%">\n' .. left .. '\n</td>\n'
if right then
row = row .. '<td valign="top" width="50%">\n' .. right .. '\n</td>\n'
else
row = row .. '<td valign="top" width="50%">&nbsp;</td>\n'
end
row = row .. '</tr>'
table.insert(tableRows, row)
i = i + 2
end
table.insert(out, '<table border="1" cellpadding="10" cellspacing="0" width="100%">\n' ..
table.concat(tableRows, "\n") .. '\n</table>')
end


if #noVideoLines > 0 then
if #noVideoLines > 0 then
table.insert(out, "\n'''No account currently featured for:'''\n")
table.insert(out, '<p><b>No account yet:</b><br>' .. table.concat(noVideoLines, "<br>") .. '</p>')
table.insert(out, table.concat(noVideoLines, "\n"))
end
end
end
end



Latest revision as of 14:50, 27 September 2026

Documentation for this module may be created at Module:WorkersVoices/doc

local p = {}

local SECTOR_LABELS = {
	construction = "Construction", domestic_work = "Domestic work",
	hospitality = "Hospitality", healthcare = "Healthcare",
	skilled_it = "Skilled / IT", agriculture = "Agriculture",
	general_worker_life = "General worker life", employment_advice = "Employment advice",
	driver_transport = "Driver / transport", general_expat_life = "General expat life",
	study_work_settlement = "Study, work & settlement",
	other = "Other",
}

local SENTIMENT_ICONS = {
	positive = "🟢 Positive", mixed = "🟡 Mixed", negative = "🔴 Negative",
}

local function htmlEscape(s)
	if not s then return "" end
	return (s:gsub("&", "&amp;"):gsub("<", "&lt;"):gsub(">", "&gt;"):gsub('"', "&quot;"))
end

local function tlen(t)
	local n = 0
	for _ in ipairs(t) do n = n + 1 end
	return n
end

local function genderLabel(g)
	if g == "man" then return "Man" end
	if g == "woman" then return "Woman" end
	return "Not specified"
end

local function sectorLabel(s)
	if not s or s == "unclear" then return "Not yet confirmed" end
	return SECTOR_LABELS[s] or htmlEscape(s)
end

local function sentimentLabel(s)
	return SENTIMENT_ICONS[s] or "Unknown"
end

local function warningHtml(video)
	if not (video.contentWarning and video.contentWarning.flag) then
		return ""
	end
	local label = video.contentWarning.label or ""
	if label:find("automated discovery") then
		return '<p>🔍 <i>Pending review</i></p>'
	end
	return '<p>⚠️ <b>' .. htmlEscape(label) .. '</b></p>'
end

local function youtubeThumbnail(url)
	if not url then return nil end
	local id = url:match("[?&]v=([%w_%-]+)")
	if not id then
		id = url:match("youtu%.be/([%w_%-]+)")
	end
	if id then
		return "https://img.youtube.com/vi/" .. id .. "/mqdefault.jpg"
	end
	return nil
end

local function buildCard(region, country, video)
	local resource = ""
	if video.verifiedResource and video.verifiedResource.url then
		resource = ' &middot; [' .. video.verifiedResource.url .. ' ' ..
			htmlEscape(video.verifiedResource.label) .. ']'
	end

	local note = video.curatorNote and video.curatorNote ~= ""
		and ('<p><i>' .. htmlEscape(video.curatorNote) .. '</i></p>') or ""

	local sourceLink = '[' .. video.sourceUrl .. ' ▶ Watch source]'

	local thumb = ""
	local thumbUrl = youtubeThumbnail(video.sourceUrl)
	if thumbUrl then
		thumb = '<p>[' .. video.sourceUrl .. ' ' .. thumbUrl .. ']</p>'
	end

	return table.concat({
		'<h4>' .. country.flag .. ' ' .. htmlEscape(country.name) .. ' — ' .. sentimentLabel(video.sentiment) .. '</h4>',
		thumb,
		'<p><b>Sector:</b> ' .. sectorLabel(video.sector) ..
			' &middot; <b>Gender:</b> ' .. genderLabel(video.gender) ..
			' &middot; <b>Added:</b> ' .. htmlEscape(video.addedDate) .. '</p>',
		note,
		warningHtml(video),
		'<p>' .. sourceLink .. resource .. '</p>',
	}, "\n")
end

function p.main(frame)
	local dataPageTitle = frame.args[1] or "Data:WorkersVoices.json"

	local data
	if mw.loadJsonData then
		data = mw.loadJsonData(dataPageTitle)
	else
		local content = mw.title.new(dataPageTitle):getContent()
		data = mw.text.jsonDecode(content)
	end

	local out = {}

	for _, region in ipairs(data.regions) do
		local cardTexts = {}
		local noVideoLines = {}

		for _, country in ipairs(region.countries) do
			if tlen(country.videos) == 0 then
				table.insert(noVideoLines, "🔸 " .. country.flag .. " " .. htmlEscape(country.name))
			else
				for _, video in ipairs(country.videos) do
					table.insert(cardTexts, buildCard(region, country, video))
				end
			end
		end

		if #cardTexts > 0 or #noVideoLines > 0 then
			table.insert(out, '<h2>' .. htmlEscape(region.name) .. '</h2>')

			if #cardTexts > 0 then
				local tableRows = {}
				local i = 1
				while i <= #cardTexts do
					local left = cardTexts[i]
					local right = cardTexts[i + 1]
					local row = '<tr>\n<td valign="top" width="50%">\n' .. left .. '\n</td>\n'
					if right then
						row = row .. '<td valign="top" width="50%">\n' .. right .. '\n</td>\n'
					else
						row = row .. '<td valign="top" width="50%">&nbsp;</td>\n'
					end
					row = row .. '</tr>'
					table.insert(tableRows, row)
					i = i + 2
				end
				table.insert(out, '<table border="1" cellpadding="10" cellspacing="0" width="100%">\n' ..
					table.concat(tableRows, "\n") .. '\n</table>')
			end

			if #noVideoLines > 0 then
				table.insert(out, '<p><b>No account yet:</b><br>' .. table.concat(noVideoLines, "<br>") .. '</p>')
			end
		end
	end

	return table.concat(out, "\n")
end

return p