Module:MedImagNav

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

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

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

Code for {{MedImagNav}}

Code

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

local RadioN = 1
local AnatN = 2
local OtherN = 3
local FormatN = 4
local Titles = {
		'Q77604',	--Radiology
		'',			--Anatomy
		'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 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 Anat = {
		{'Cytology',	true, 'Q3009579',	false,	''},
		{'Blood films',	true, 'Q886475',	false,	''},
		{'Anatomy',		true, 'Q514',		false,	''},
		{'Histology',	true, 'Q7168',		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,	''},
	}
local Format = {	
		{'Audios',	true, 	'Q26987250',	false,	''},
		{'SVG', 	false, 	'Q2078',		false,	''},
		{'Videos',	true, 	'Q34508',		false,	''},
	}
local SCategory = 'category'

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

local p = {}
function p.main(frame)

	local function qidLab0 (qid)
		local res = WD._getLabel(qid, {['link']='-'})
		return res:gsub("^%l", string.upper)
	end	

	local function qidLab (qid)
		return HeadNoWrap..qidLab0 (qid)..trail	
	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 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
			end	
		end	
	end	--SearchCats
	
    local function WriteFoundCats (TitleN, tab, s)
		if TitleN ~= RadioN then
			CountT = 0
		end
		for i, v in next, tab do
			Exist = false
			SearchCats (TitleN, tab, i, s)
		end
		if CountT > 0 then
			local FirstItem = true
			local CountH = 0
			if SepFirst then
				SepFirst = false
			else	
				p1 = p1..' | '
			end	
			if TitleN ~= AnatN then
				p1 = p1..'<i>'..qidLab(Titles[TitleN])..'</i>: '
			end
			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)
				end	
			end
		end	
	end --WriteFoundCats
	
	--BEGIN p.main
    local suf = frame.args['s'] or '';
	local pagename = mw.title.getCurrentTitle().text
	local lang = frame:callParserFunction('Int', 'Lang')
	isRTLHere = dir.isRTL(lang)
	
	--Search functions
	lab = suf: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, suf)
	if CountUltra == 0 then
	  CountEchoC = SearchImagVideo (SEchocardio, suf)
	end
	local CountIni = CountT
	
	--Write functions
	if Home ~= '' then
		SepFirst = false
		p1 = p1..'[[File:RM-1.svg|16px|link=:Category:'..Home..']]'
	end
	WriteFoundCats (RadioN, 	Radio,	suf)
	WriteFoundCats (AnatN, 		Anat,	suf)
	WriteFoundCats (OtherN, 	Other,	suf)	
	WriteFoundCats (FormatN, 	Format,	suf)
	p1 = p1..' | [[File:Info Simple bw.svg|14px|link=:Template:MedImagNav]] </div>'
	return p1
	--END p.main
end
return p