Sari la conținut

Modul:InfoboxMilitaryConflict

De la Wikipedia, enciclopedia liberă

Implementează {{Infocaseta Conflict militar}}


local getArgs = require('Modul:Arguments').getArgs
local TableTools = require('Modul:TableTools')
local Wikidata = require('Modul:Wikidata')
local StringUtils = require('Modul:StringUtils')
local pencil = require('Modul:EditAtWikidata').displayMessage
local LAndC = require('Modul:LocationAndCountry')
local infoboxImage = require('Modul:InfoboxImage').InfoboxImage
local geoboxCoord = require('Modul:Geobox coor')._coord
local locationMap = require('Modul:Location map').main
local ibconfig = mw.loadData('Modul:InfoboxMilitaryConflict/config')
local stackify = require('Modul:Stackify').fromConfig
local campaignbox = require('Modul:Campaignbox').fromArgs
local WdUTI = require('Modul:WikidataUtils/Time/Interval')

local p = {}

local function renderCombatants(tbl, mc, sectionTitle, combatantKey, innerIdx)
	local colspan = #(mc.combatants)
	if innerIdx > 0 then
		local renderable = false
		for _,eachCombatant in ipairs(mc.combatants) do
			renderable = renderable or (type(eachCombatant[combatantKey]) == 'table' and #(eachCombatant[combatantKey]) <= innerIdx)
		end
		if not renderable then
			return nil
		end
	end
	local txts = {}
	local txtspresent = false
	for cmbidx,combatant in ipairs(mc.combatants) do
		local txt = ''
		if innerIdx == 0 then
			txt = combatant[combatantKey]
		elseif innerIdx < #(combatant[combatantKey]) then
			txt = combatant[combatantKey][innerIdx]
		end
		if txt and mw.ustring.len(txt) > 0 then
			txts[cmbidx] = txt
			txtspresent = true
		end
	end
	if sectionTitle and txtspresent then
		tbl:tag('tr'):tag('th')
						:attr('colspan', tostring(colspan))
						:cssText(ibconfig.header_raw)
						:wikitext(sectionTitle)
	end
	local combatantsRow = tbl:tag('tr')

	for cmbidx = 1,#(mc.combatants) do
		if txts[cmbidx] then
			combatantsRow:tag('td')
				:attr('width', tostring(math.floor(100 / colspan)) .. '%')
				:css('text-align', 'left')
				:css('vertical-align', 'top')
				:css('border-right', cmbidx ~= #(mc.combatants) and ibconfig.internal_border or nil)
				:css('border-top', not sectionTitle and ibconfig.internal_border or nil)
				:wikitext(txts[cmbidx])
		end
	end
	if mc.total[combatantKey] and innerIdx < 1 then
		tbl:tag('tr'):tag('td')
						:attr('colspan', tostring(colspan))
						:css('text-align', 'center')
						:css('border-top', ibconfig.internal_border)
						:wikitext(mc.total[combatantKey])
	end
end

p.render = function(mc)
	local colspan = #(mc.combatants)
	--#(mc.combatants) > 2 or #(mc.combatants) == 4 and 3 or 2

	local tbl = mw.html.create('table')
	tbl:addClass('infocaseta')
	tbl:attr('cellspacing', '2')
	
	tbl:tag('tr'):tag('th')
					:addClass('antet'):addClass('militar')
					:attr('colspan', tostring(colspan))
					:css('background-color', '#B0C4DE')
					:css('color', '#000')
					:wikitext(mc.title.arg)
	
	if mc.parentConflictN.arg or mc.parentConflictG.arg then
		local partofTag = tbl:tag('tr'):tag('th')
						:attr('colspan', tostring(colspan))
						:css('text-align', 'center')
						:css('background-color', 'white')
		if mc.parentConflictG.arg then
			partofTag:wikitext('Parte a ', mc.parentConflictG.arg)
		elseif mc.parentConflictN.arg then
			partofTag:wikitext('Parte din ', mc.parentConflictN.arg)
		end
	end
	if mc.illustration.image then
		tbl:tag('tr'):tag('td')
					:attr('colspan', tostring(colspan))
					:css('text-align', 'center')
					:css('font-size', '90%')
					:wikitext(infoboxImage{image = mc.illustration.image, sizedefault = 'frameless', suppressplaceholder = 'yes'},
						tostring(mw.html.create('br')),
						mc.illustration.caption)
	end
	tbl:tag('tr'):tag('th')
					:attr('colspan', tostring(colspan))
					:cssText(ibconfig.header_raw)
					:wikitext('Informații generale')
	local innerDataTable = tbl:tag('tr'):tag('td'):attr('colspan', tostring(colspan)):tag('table')
	
	if mc.timestamp.arg then
		innerDataTable:tag('tr'):tag('th')
						:css('padding-right', '1em')
						:wikitext('Perioadă'):done()
					 :tag('td')
						:wikitext(mc.timestamp.arg)
	end
	if mc.location.arg then
		innerDataTable:tag('tr'):tag('th')
						:css('padding-right', '1em')
						:wikitext('Loc'):done()
					 :tag('td')
						:wikitext(mc.location.arg,
							tostring(mw.html.create('br')),
							mc.location.coords)
	end
	if mc.result.arg then
		innerDataTable:tag('tr'):tag('th')
						:css('padding-right', '1em')
						:wikitext('Rezultat'):done()
					 :tag('td')
						:wikitext(mc.result.arg)
	end
	if mc.casus.arg then
		innerDataTable:tag('tr'):tag('th')
						:css('padding-right', '1em')
						:wikitext('[[Casus belli]]'):done()
					 :tag('td')
						:wikitext(mc.casus.arg)
	end
	if mc.territory.arg then
		innerDataTable:tag('tr'):tag('th')
						:css('padding-right', '1em')
						:wikitext('Modificări teritoriale')
					 :tag('td')
						:wikitext(mc.territory.arg)
	end
	
	if #mc.combatants > 1 then
		local maxExtras = 0
		for _,eachCombatant in ipairs(mc.combatants) do
			if eachCombatant.extraentities then
				maxExtras = math.max(#(eachCombatant.extraentities), maxExtras)
			end
		end
		renderCombatants(tbl, mc, 'Beligeranți', 'entity', 0)
		if maxExtras > 0 then for extraIdx=1,maxExtras do
			renderCombatants(tbl, mc, nil, 'extraentities', extraIdx)
		end end
		renderCombatants(tbl, mc, 'Conducători', 'commander', 0)
		renderCombatants(tbl, mc, 'Efective', 'strength', 0)
		renderCombatants(tbl, mc, 'Pierderi', 'casualties', 0)
	end
	if mc.map.mtype then
		tbl:tag('tr'):tag('td')
						:attr('colspan', tostring(colspan))
						:css('border-top', ibconfig.internal_border)
						:tag('div')
							:css('text-align', 'center')
							:wikitext(locationMap(mw.getCurrentFrame(), {mc.map.mtype, lat=mc.location.latitude, long=mc.location.longitude, width=mc.map.msize or '220', float='center', border='none', caption=mc.map.caption or ('Localizarea în ' .. mc.map.mtype)}))
	elseif mc.map.static then
		tbl:tag('tr'):tag('td')
			:attr('colspan', tostring(colspan))
			:css('text-align', 'center')
			:css('font-size', '90%')
			:wikitext(infoboxImage{image = mc.map.static, sizedefault = 'frameless', suppressplaceholder = 'yes'},
				tostring(mw.html.create('br')),
				mc.map.caption)
	end
		
	tbl:tag('tr'):wikitext(mw.getCurrentFrame():expandTemplate{title='infodoc', args={colspan=colspan, culoare='B0C4DE', link='Infocaseta Conflict militar', wikidata='y'}})
	local out = tostring(tbl)
	if mc.campaignbox.text and mw.ustring.len(mc.campaignbox.text) > 0 then
		out = stackify({float='right'}, {out, mc.campaignbox.text})
	end
	return out .. mw.getCurrentFrame():extensionTag{ name = 'templatestyles', args = { src = 'Modul:Infobox/styles.css'} }
end

local objectify = function(args)
	local mc = {}
	local qid = args.qid or mw.wikibase.getEntityIdForCurrentPage()
	mc.title = {}
	mc.title.arg = args['conflict']
	if not mc.title.arg then 
		mc.title.arg = StringUtils._capitalize(Wikidata.findRoLabel(qid)) or Wikidata.findNativeOrEnglishLabel(qid, true)
	end
	
	mc.parentConflictN = {}
	mc.parentConflictN.arg = args['partedin'] or args['parte_din'] or args['partof']
	mc.parentConflictN.prop = {'P361', 'P607'}
	mc.parentConflictG = {}
	mc.parentConflictG.arg = args['partea'] or args['parte a'] or args['parte_a']
	for _,prop in ipairs(mc.parentConflictN.prop) do
		local wdParentConflictN = StringUtils._emptyToNil(Wikidata._getValueListWithSeparator(', ', qid, prop, 5, true))
		if wdParentConflictN then
			mc.parentConflictN.usedprop = prop
			mc.parentConflictN.arg = wdParentConflictN .. ' ' .. pencil(prop, qid)
			break
		end
	end
	
	mc.illustration = {}
	mc.illustration.image = args['imagine'] or args['image']
	mc.illustration.caption = args['captură'] or args['caption'] or args['descriere']
	if not mc.illustration.image then
		mc.illustration.image, mc.illustration.caption = Wikidata.findImageAndCaption(qid)
	end
	
	mc.timestamp = {}
	mc.timestamp.arg = args['data'] or args['dată'] or args['date']
	if not mc.timestamp.arg then
		mc.timestamp.arg, mc.timestamp.prop = WdUTI.fromArgs(nil, qid)
		if mc.timestamp.arg and mc.timestamp.prop then
			mc.timestamp.arg = mc.timestamp.arg .. ' ' .. pencil(mc.timestamp.prop, qid)
		end
	end
	
	mc.location = {}
	mc.location.arg = args['locul'] or args['loc'] or args['locație'] or args['place']
	if not mc.location.arg then
		mc.location.arg = LAndC.displayFromParams('P276', qid, mc.timestamp.prop)
	end
	mc.location.latitude = args.latitude
	mc.location.longitude = args.longitude
	mc.location.coords = args.coordinates or geoboxCoord({wikidata = 'yes'}, mw.getCurrentFrame())
	
	mc.result = {}
	mc.result.arg = args['rezultat'] or args['consecințe'] or args['status'] or args['result']
	
	mc.casus = {}
	mc.casus.arg = args['casus']
	if not mc.casus.arg then
		local causeList = Wikidata.findValueListWithDecoratedQualifiers(qid, 'P828', true, '$P4900')
		if causeList and #causeList > 0 then
			mc.casus.arg = table.concat(causeList, tostring(mw.html.create('br'))) .. mw.text.decode('&nbsp;') .. pencil('P828', qid)
		end
	end
	
	mc.territory = {}
	mc.territory.arg = args['teritorii'] or args['teritoriu'] or args['territory']
	
	mc.combatants = {}
	local cbtdata = TableTools.numData(args)
	for cbtidx,cbt in pairs(cbtdata) do
		local combatant = {}
		combatant.entity = cbt.combatant
		combatant.commander = cbt.conducator or cbt.commander or cbt.comandant or cbt['conducător']
		combatant.strength = cbt.strength or cbt['forța'] or cbt['forță']
		combatant.casualties = cbt.decese or cbt.victime or cbt.casualties
		combatant.extraentities = {}
		for _,postletter in ipairs({'a', 'b', 'c', 'd', 'e'}) do
			if args['combatant' .. tostring(cbtidx) .. postletter] then
				table.insert(combatant.extraentities, args['combatant' .. tostring(cbtidx) .. postletter])
			end
		end
		mc.combatants[cbtidx] = combatant
	end
	mc.combatants = TableTools.compressSparseArray(mc.combatants)
	if #mc.combatants == 0 then
		local participants = Wikidata.findBestClaimsForProperty(qid, 'P710')
		if participants and #participants > 0 then
			for _,participant in ipairs(participants) do
				if Wikidata.hasValueSnak(participant) then
					local combatant = {}
					combatant.entity = Wikidata.printSnak(participant.mainsnak)
					local cmdrs = Wikidata.findQualifierValueListForClaim(participant, 'P4791')
					if cmdrs and #cmdrs > 0 then
						combatant.commander = table.concat(cmdrs, tostring(mw.html.create('br')))
					end
					table.insert(mc.combatants, combatant)
				end
			end
		end
	end
	mc.total = {}
	mc.total.strength = args['forță_totală'] or args['total_strength']
	mc.total.casualties = args['pierderi_totale'] or args['total_casualties']
	
	mc.map = {}
	mc.map.mtype = args.map_type or args['hartă_localizare'] or args.location_map
	mc.map.msize = args.map_size or args['hartă_localizare_dimensiune'] or args.location_map_size
	mc.map.caption = args.map_caption or args['descriere_hartă']
	
	if not mc.map.mtype then
		mc.map.static = args['imagine_hartă']
		if not mc.map.static then
			mc.map.static, mc.map.caption = Wikidata.findImageAndCaption(qid, {'P242'})
		end
	end
	
	mc.campaignbox = {}
	mc.campaignbox.text = args.campaignbox or args.campanii
	if not mc.campaignbox.text then
		local supercampaignQId = mc.parentConflictN.usedprop and Wikidata.loadOneValueInChain(qid, mc.parentConflictN.usedprop, 'raw')
		local supercampaignBox = supercampaignQId and campaignbox({supercampaignQId, state = 'plain'})
		local subcampaignBox = campaignbox({qid, state = 'plain'})
		local campaignboxes = TableTools.compressSparseArray({supercampaignBox, subcampaignBox})
		if #campaignboxes > 0 then
			mc.campaignbox.text = table.concat(campaignboxes)
		end
	end
	mc.notes = args.note or args.notes
	
	return mc
end

p.fromArgs = function(args)
	local mc = objectify(args)
	return p.render(mc)
end

p.fromFrame = function(frame)
	local args = getArgs(frame)
	return p.fromArgs(args)
end

return p