Module:DiseasesNav/sandbox

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
local dir = require('Module:Dir')

local RadioN = 1
local AnatPathN = 2
local AnatPathN = 2
local OtherN = 3
local FormatN = 4
local Titles = {
		'Q77604',	--Radiology
		'Q683455',	--Pathology
		'Q1433373',	--Other
		'Q235557',	--Format
	}
local SUltrasound = 'Ultrasound'
local UltrasoundId = 'Q162564'
local SEchocardio = 'Echocardiographic'
local EchocardioId = 'Q216933'
local ImagVideo = {
		{'images',	'Q478798',	false,	''},
		{'videos', 	'Q34508',	false,	''},
	}
local Trauma = {
		{'Cast X-rays',   				true, 'Q1526584',	false,	''},
		{'External fixation X-rays',   	true, 'Q920565',	false,	''},
		{'Internal fixation X-rays',   	true, 'Q1667915',	false,	''},
	}	
local XRaysPos = 1	
local Radio = {
		{'X-rays',   					true, 'Q34777',		false,	''}, -- XRaysPos = 1
		{'CT images',					true, 'Q32566',		false,	''},
		{'MRI',							true, 'Q419039',	false,	''},
		{'PET images',					true, 'Q208376',	false,	''},
		{'Scintigraphy images',			true, 'Q1130465',	false,	''},
		{'Bone scintigraphy images', 	true, 'Q1928938',	false,	''},
	}
local AnatPath = {
		{'Cytopathology',	true, 'Q1881849',	false,	''},
		{'Blood films',		true, 'Q886475',	false,	''},
		{'Gross pathology',	true, 'Q5610516',	false,	''},
		{'Histopathology',	true, 'Q1070952',	false,	''},
	}
local Other = {	
		{'ECG',					true, 'Q79785',		false,	''},
		{'Endoscopic images',	true, 'Q212809',	false,	''},
		{'Laparoscopic images',	true, 'Q675754',	false,	''},
		{'Fundus',				true, 'Q203222',	false,	''},
		{'OCT', 				true, 'Q899552',	false,	''},
		{'Otoscopy', 			true, 'Q11801024',	false,	''},
		{'Epidemiology', 		true, 'Q133805',	false,	''},
		{'Staging',				true, 'Q1752284',	false,	''},
	}
local SMapsBegin = 'Maps'	
local SMapsEnd = 'maps'	
local SWorldMaps = 'World maps'
local MapId = 'Q4006'	
local WorldMapId = 'Q16502'	
local Format = {	
		{'Audios',	true, 	'Q26987250',	false,	''},
		{'SVG', 	false, 	'Q2078',		false,	''},
		{'Videos',	true, 	'Q34508',		false,	''},
	}
local SEpidemiology = 'Epidemiology'
local SCategory = 'category'

local HeadNoWrap, trail = '<span style="white-space:nowrap">', '</span>'

local p = {}
function p.main(frame)
	local lang = ''
	
	local function qidLab0 (qid)
		res = mw.wikibase.getLabelByLang (qid, lang)
		if (res == nil) or (res == '') then  
			res = mw.wikibase.getLabelByLang (qid, 'en')
		end	
		res = res:gsub("^%l", string.upper)
		return res	
	end	

	local function qidLab (qid)
		res = HeadNoWrap..qidLab0 (qid)..trail
		return res	
	end	

	local clink0 = function (categ, label)
		return '[[:Category:'..categ..'|'..qidLab (label)..']]'
	end	
	local clink = function (tab, i)
		return clink0 (tab[i][5], tab[i][3])
	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 SepFirst = true
    local MapCat = ''
	local WorldMapCat = ''
	local ultraCat = ''
	local Exist = false
	local CountT = 0
	local CountUltra = 0
	local CountEchoC = 0
	local CountTrauma = 0
	local CountUs = 0
	local UsId = ''
	local lab = ''
	local Home = ''
	
	local function SearchImagVideo (S, sufH)
		local CountHH = 0
		for i, v in next, ImagVideo do
			lab = S..' '..ImagVideo[i][1]..' of '..sufH
			local titleObj = mw.title.new(lab, SCategory)
			if titleObj and titleObj.exists then
				ImagVideo[i][3] = true
				ImagVideo[i][4] = lab
				CountHH = CountHH + 1
				CountT = CountT + 1
			end	
		end
		return CountHH
	end --SearchImagVideo
	
	local function SearchCats (TitleN, tab, i, s)
		if (Exist == false) and (s ~= '') then
			lab = tab[i][1]
			if tab[i][2] == true then
			  lab = lab..' of'
			end
			lab = lab..' '..s
			local titleObj = mw.title.new(lab, SCategory)
			if titleObj and titleObj.exists then
				tab[i][4] = true
				tab[i][5] = lab
				CountT = CountT + 1
				Exist = true
				if TitleN == OtherN and tab[i][1] == SEpidemiology then
					local filesnum = mw.site.stats.pagesInCategory(lab, subcats)
					if filesnum > 0 then
						lab = SMapsBegin..' of '..s
						local titleobj = mw.title.new(lab, SCategory)
						if titleobj and titleobj.exists then
							MapCat = lab
						end
						if MapCat == '' then
							lab = s:gsub("^%l", string.upper)..' '..SMapsEnd
							local titleobj = mw.title.new(lab, SCategory)
							if titleobj and titleobj.exists then
								MapCat = lab
							end
						end
						if MapCat ~= '' then
							lab = SWorldMaps..' of '..s
							local titleobj = mw.title.new(lab, SCategory)
							if titleobj and titleobj.exists then
								WorldMapCat = lab
							end
						end
					end	
				end		
			end	
		end	
	end	--SearchCats
	
    local function WriteFoundCats (TitleN, tab, s1, s2)
		if TitleN ~= RadioN then
			CountT = 0
		end
		for i, v in next, tab do
			Exist = false
			SearchCats (TitleN, tab, i, s1)
			SearchCats (TitleN, tab, i, s2)
		end
		if CountT > 0 then
			local FirstItem = true
			local CountH = 0
			if SepFirst then
				SepFirst = false
			else	
				p1 = p1..' | '
			end	
			p1 = p1..'<i>'..qidLab(Titles[TitleN])..'</i>: '
			if (TitleN == RadioN) and ((CountUltra > 0) or (CountEchoC > 0)) then
				if CountUltra > 0 then
					CountUs = CountUltra
					UsId = UltrasoundId
				else
					CountUs = CountEchoC
					UsId = EchocardioId
				end	
				if CountUs == 2 then
			        p1 = p1..' '..qidLab(UsId)..' ('
					for i, v in next, ImagVideo do
						ultraCat = ImagVideo[i][4]
						p1 = p1..clink0(ultraCat, ImagVideo[i][2])
						if i == 1 then
							p1 = p1..', '
						end
					end	
					p1 = p1..')'
				end	
				if CountUs == 1 then
					ultraCat = ''
					for i, v in next, ImagVideo do
						if ImagVideo[i][3] == true then
							ultraCat = ImagVideo[i][4]
						end
					end	
			        p1 = p1..' '..clink0(ultraCat, UsId)
				end
				FirstItem = false
				CountH = CountUs
			end
			for i, v in next, tab do
				if tab[i][4] == 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 (TitleN == RadioN) and (i == XRaysPos) and (CountTrauma > 0) then
						local p2 = ''
						local FirstTrauma = true
						for j, v in next, Trauma do
							if Trauma[j][4] == true then
								if FirstTrauma then
									FirstTrauma = false
								else	
									p2 = p2..', '
								end	
								p2 = p2..clink(Trauma, j)
							end	
						end	
						p1 = p1..' ('..p2..')'
					end  
					if (tab[i][1] == SEpidemiology) and (MapCat ~= '') then
						p1 = p1..' ('..clink0(MapCat, MapId)
						if  WorldMapCat ~= '' then
							local arrow = ''
							--if mwlanguage.isRTL(frame) then
							if isRTLHere then
								arrow = '←'
							else
								arrow = '→'
							end
							p1 = p1..' '..arrow..' '..clink0(WorldMapCat, WorldMapId)
						end
						p1 = p1..')'
						MapCat = ''
						WorldMapCat = ''
					end
				end	
			end
		end	
	end --WriteFoundCats
	
	--BEGIN p.main
    local suf1 = frame.args['s1'] or '';
    local suf2 = frame.args['s2'] or '';
	local pagename = mw.title.getCurrentTitle().text
	lang = frame:callParserFunction('Int', 'Lang')
	isRTLHere = dir.isRTL(lang)
	
	--Search functions
	lab = suf1:gsub("^%l", string.upper)
	local titleobj = mw.title.new(lab, SCategory)
	if titleobj and titleobj.exists and (pagename ~= lab) then
		Home = lab
	end

	CountUltra = SearchImagVideo (SUltrasound, suf1)
	if CountUltra == 0 then
	  CountEchoC = SearchImagVideo (SEchocardio, suf1)
	end
	local CountIni = CountT
	for j, w in next, Trauma do
		Exist = false
		SearchCats (RadioN, Trauma, j, suf1)
	end	
	CountTrauma = CountT - CountIni	
	
	--Write functions
	if Home ~= '' then
		SepFirst = false
		p1 = p1..'[[File:RM-1.svg|16px|link=:Category:'..Home..']]'
	end
	WriteFoundCats (RadioN, 	Radio,		suf1, '')
	WriteFoundCats (AnatPathN, 	AnatPath,	suf1, suf2)
	WriteFoundCats (OtherN, 	Other,		suf1, '')	
	WriteFoundCats (FormatN, 	Format,		suf1, '')
	p1 = p1..' | [[File:Info Simple bw.svg|14px|link=:Template:DiseasesNav]] </div>'
	return p1
	--END p.main
end
return p