Jump to content

Module:WorkersVoices: Difference between revisions

Your free guide through every stage of migration
No edit summary
No edit summary
Line 36: Line 36:
data = mw.text.jsonDecode(content)
data = mw.text.jsonDecode(content)
end
end
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 rows = {}

Revision as of 14:25, 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", unclear = "Unclear",
}

local SENTIMENT_LABELS = {
	positive = "Positive", mixed = "Mixed", negative = "Negative",
}

local function wikiEscape(s)
	if not s then return "" end
	return (s:gsub("|", "|"):gsub("%[", "["):gsub("%]", "]"))
end

local function genderLabel(g)
	if g == "man" then return "Man" end
	if g == "woman" then return "Woman" end
	return "Unclear"
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



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
			if #country.videos == 0 then
				table.insert(noVideoLines,
					"* " .. wikiEscape(country.flag) .. " " .. wikiEscape(country.name))
			else
				for _, video in ipairs(country.videos) do
					local warning = "—"
					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

	local tableWikitext = table.concat({
		'{| class="wikitable sortable"',
		"! Region !! Country !! Sector !! Gender !! Sentiment !! Added !! Source !! Content warning !! Why we picked this !! Verified resource",
		table.concat(rows, "\n"),
		"|}",
	}, "\n")

	local out = { tableWikitext }

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

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

return p