Sari la conținut

Modul:StandardWikidataIntro

De la Wikipedia, enciclopedia liberă

local p = {}
local Wikidata = require ('Modul:Wikidata')
local StringUtils = require ('Modul:StringUtils')
local join = require ('Modul:Separated entries').main

local propDescriptions = {
	['loc'] = {
		['intro'] = 'se află %1',
		['props'] = {'P131', 'P206', 'P501', 'P2044', 'P610', 'P2046'},
		['intro_props'] = { 
			['P131'] = 'în $1',
			['P206'] = 'lângă $1',
			['P2044'] = 'la o altitudine de $1 deasupra nivelului mării',
			['P501'] = 'enclavă în $1',
			['P610'] = 'cu cel mai înalt punct în $1',
			['P2046'] = 'având o suprafață de $1'
		},
		['qualifiers'] = {
			['P610'] = {'P2044'},
			['intro'] = 'la $1',
		}
	},
	['con'] = {
		['intro'] = 'se leagă de $1',
		['props'] = { 'P2789' },
		['qualifiers'] = {}
	},
	['pop'] = {
		['intro'] = 'are o populație de $1',
		['props'] = { 'P1082' },
		['qualifiers'] = {
			['P1082'] = {'P585', 'P459'},
			['intro'] = 'determinată $1',
			['intro_props'] = {
				['P459'] = 'prin $1',
				['P585'] = 'în $1'
			}
		}
	}
}

local function createPhraseForChapter(chapter, q)
	if not chapter['props'] then return nil end
	
	local sentences = {}
	for _,eachProp in ipairs(chapter['props']) do
		local propValueClaims = Wikidata.findBestClaimsForProperty(q, eachProp)
		if propValueClaims then
			local propValues = {}
			for _,eachPropValueClaim in ipairs(propValueClaims) do
				if eachPropValueClaim.type == 'statement' and eachPropValueClaim.mainsnak and eachPropValueClaim.mainsnak.snaktype == 'value' then
					local claimValue = Wikidata.printSnak(eachPropValueClaim.mainsnak)
					table.insert(propValues, claimValue)
				end
			end
			if #propValues > 0 then
				propValues.separator = ', '
				propValues.conjunction = ' și '
				table.insert(sentences, mw.ustring.gsub(chapter['intro_props'] and chapter['intro_props'][eachProp] or '$1', '$1', join(propValues)))
			end
		end
	end
	
	if #sentences > 0 then
		sentences.separator = '; '
		sentences.conjunction = '; și '
		return join(sentences)
	end
	return nil
end

local function display(frame)
	local q = frame.args.q or mw.wikibase.getEntityIdForCurrentPage()
	local outphrases = {}

	for _,eachChapter in ipairs(propDescriptions) do
		local crtPhrase = createPhraseForChapter(eachChapter, q)
		if crtPhrase then
			table.insert(outphrases, crtPhrase)
		end
	end

	local capitalizedOutphrases = {}
	for _,eachoutphrase in ipairs(outphrases) do
		table.insert(capitalizedOutphrases, StringUtils._capitalize({eachoutphrase}))
	end
	return table.concat(capitalizedOutphrases, '. ')
end

p.display = display
return p