Module:PublicHealthNav

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Module:PublicHealthNav (talk · edit · hist · links · doc · subpages · tests / results · sandbox · all modules)

Code for {{PublicHealthNav}}

Code

local dir = require('Module:Dir')
local WD = require('Module:Wikidata')

local function ConcatWithStrW (SBegin, SEnd, Sep) --(const SBegin, SEnd, Sep: string): string;
    if  SBegin ~= '' then  
		return SBegin..Sep..SEnd
	else  
		return SEnd
	end	
end

local VaccinN = 1
local WaterSanN = 2
local WaterN = 3
local SanitationN = 4
local COVIDN = 5
local Items = {
		{'Vaccinations', 'Q192995',	false,	''},
		{'Water supply and sanitation', {'Q1061108','Q949149'},	false,	''},
		{'Water supply', 'Q1061108',false,	''},
		{'Sanitation', 'Q949149',	false,	''},
		{'COVID-19 vaccination', 'Q84263196', false,	''},
	}
	
local p = {}
function p.main(frame)
	
	local function qidLab0 (qid)
		local res = WD._getLabel(qid, {['link']='-'})
		return res:gsub("^%l", string.upper)
	end	

	local HeadNoWrap, trail = '<span style="white-space:nowrap">', '</span>'
	local function qidLab (qid)
		return HeadNoWrap..qidLab0 (qid)..trail
	end	

	local clink0 = function (categ, label)
		local transLab 
		if type(label) == "table" then
			transLab = ''
			for i, v in next, label do
				transLab = ConcatWithStrW (transLab, qidLab(v), '-')
			end
		else
			transLab = qidLab (label)
		end	
		return '[[:Category:'..categ..'|'..transLab..']]'
	end	
	local clink = function (tab, i)
		return clink0 (tab[i][4], tab[i][2])
	end	
	
    local p1 = '<div style="width:auto;margin:0.5em;margin-left:0;margin-right:0;border:1px solid #AAA;background:#FFF;padding:2px;font-size:87%;line-height:1.3">'
	local isRTLHere = false
	local lab = ''
	local Home = ''
    local SCategory = 'category'
	local CountT = 0
    
	local function SearchCats (tab, i, s)
		lab = tab[i][1]
		lab = lab..' in '..s
		local titleObj = mw.title.new(lab, SCategory)
		if titleObj and titleObj.exists then
			tab[i][3] = true
			tab[i][4] = lab
			CountT = CountT + 1
		end	
	end	--SearchCats
	
    local function WriteFoundCats (tab, s)
		for i, v in next, tab do
			SearchCats (tab, i, s)
		end
		if CountT > 0 then
			local FirstItem = true
			local CountH = 0
			for i= VaccinN,WaterSanN do
				if tab[i][3] == true then
					if CountH < CountT then
						if FirstItem then
							FirstItem = false
						else	
							p1 = p1..' | '
						end	
					end	
					CountH = CountH + 1
			        p1 = p1..' '..clink(tab, i)
			        if (i == VaccinN) and (tab[COVIDN][3] == true) then
						p1 = p1..': '..clink(tab,COVIDN)	
			        elseif i == WaterSanN then 
			        	local p2 = ''
			        	for j = WaterN,SanitationN do
			        		if tab[j][3] == true then
			        			p2 = ConcatWithStrW (p2, clink(tab,j), ', ')
			        		end	
			        	end	
						p1 = p1..': '..p2	
			        end	
				end	
			end
		end	
	end --WriteFoundCats
	
	--BEGIN p.main
    local suf = frame.args[1];
	local pagename = mw.title.getCurrentTitle().text
	local lang = frame:callParserFunction('Int', 'Lang')
	isRTLHere = dir.isRTL(lang)
	
	--Search functions
	lab = 'Public health in '..suf
	local titleobj = mw.title.new(lab, SCategory)
	if titleobj and titleobj.exists and (pagename ~= lab) then
		Home = lab
	end

	--Write functions
	if Home ~= '' then
		local lnk = ':Category:'..Home
		p1 = p1..'[[File:RM-1.svg|16px|link='..lnk..']] [['..lnk..'|'..qidLab0('Q189603')..']] | '
	end
	WriteFoundCats (Items, suf)
	p1 = p1..' | [[File:Info Simple bw.svg|14px|link=:Template:PublicHealthNav]] </div>'
	return p1
	--END p.main
end

return p