Pāriet uz saturu

Modulis:Ārējās saites

Permanently protected module
Vikipēdijas lapa
Dokumentācijas ikona Moduļa dokumentācija[izveidot]
function append(str, c, length)
    while str:len() < length do
        str = c .. str
    end
    return str
end

function getIdsFromWikidata( item, property )
    local ids = {}
    if not item.claims[property] then
        return ids
    end
    for _, statement in pairs( item.claims[property] ) do
		if statement.mainsnak.datavalue then
			table.insert( ids, statement.mainsnak.datavalue.value )
		end
    end
    return ids
end

function createRow( id, label, rawValue, link, lang, invalidValueCategory )
    if link then
        return '* [' .. link .. ' ' .. label .. '] ' .. lang
    end
    
    return '* <span class="error">' .. id .. ' identifikators ' .. rawValue .. ' nav derīgs.</span>' .. invalidValueCategory
end

local p = {}

function p.main( frame )
	local subpage = frame.args.subpage
	local conf = require ('Modulis:Ārējās saites/' .. subpage)
	local config2 = conf:getConfiguredClaims()
	local categories = conf:getMessages()
    local parentArgs = frame:getParent().args
    --Create rows
    local elements = {}

    --Wikidata fallback if requested
	local item = mw.wikibase.getEntityObject()
	if item and item.claims then
		for _, params in pairs( config2 ) do
			params[6] = ''
			params[7] = ''
			if params[3] ~= 0 then
				local val = parentArgs[params[1]]
				local wikidataIds = getIdsFromWikidata( item, 'P' .. params[3] )
				if not val or val == '' then
					if wikidataIds[1] then
						parentArgs[params[1]] = wikidataIds[1]
					end
				else
					params[6] = categories.localpar
				--[=[	if wikidataIds[1]==val then
						params[7] = ' ' .. categories.wdequal
					else
						params[7] = ' ' .. categories.wdnotequal
					end]=]
			end
			
		else
			local val = parentArgs[params[1]]
			if val then
					params[6] = categories.localpar
				end
			end
		end
	end

	--Configured rows
	local rct = 0
	for k, params in pairs( config2 ) do
		local val = parentArgs[params[1]]
		if val and val ~= '' then
			local icon = ''
			if params[5] and params[5] ~= '' and params[5] ~= 'lv' then
				icon = frame:expandTemplate{ title = params[5] .. " ikona" }
			end
			table.insert( elements, createRow( params[1], params[2], val, params[4]( val ), icon, categories.invalid ) )
			--table.insert( elements, createRow( params[1], params[2], val, params[4]( val ), icon ) .. params[6] .. params[7] )
			rct = rct + 1
		end
	end

	if rct == 0 then
		return categories.empty
	end
	return table.concat( elements, "\n" )
end

return p