Vai al contenuto

Modulo:MapLink

Da Wikivoyage.
Info Istruzioni per l'uso
Questo è un modulo scritto in Lua. Le istruzioni che seguono sono contenute nella sottopagina Modulo:MapLink/man (modifica · cronologia)
Sandbox: Modulo:MapLink/sandbox (modifica · cronologia) · Test: Modulo:MapLink/test (modifica · cronologia · Esegui)

See also


--[[
	Source script:	https://it.wikivoyage.org/wiki/Modulo:MapLink
	Maintainer:		Andyrom75
]]
local WDI = require('Module:WikidataIB')
local i18n = require( 'Module:MapLink/i18n' )

local function _isDefined(s)
	return s ~= '' and s
end

local function _toNumber(s)
    if type(s) == 'number' then return s end
    if type(s) == 'string' then
        local n = tonumber(s)
        return n
    end
    return nil
end

local function _getQid(qid, pid)
    if not _isDefined(qid) then return nil end

    local res = WDI._followQid{
        qid   = qid,
        props = pid,
        all   = false -- ritorna solo il primo valore
    }
    -- Nota: Se non trova valori per 'props', restituisce il qid di input.
    -- Trattiamo questo caso come "nessun valore" -> nil.
    if res == qid then
        return nil
    end
    return res
end

local function _getCountryQid(listingQid)
    -- 1) prova dal listing (se fornito)
    local countryQid = listingQid and _getQid(listingQid, 'P17') or nil
    if countryQid then
        return countryQid
    end

    -- 2) fallback logico: QID della pagina corrente
    local pageQid = mw.wikibase.getEntityIdForCurrentPage()
    if pageQid then
        countryQid = _getQid(pageQid, 'P17')
    end
    return countryQid
end

local function _MapLink(frame)
	local args = frame.args
	local lat  = args and args.lat and _toNumber(args.lat)  -- se args.lat  == NA => lat  = nil
	local long = args and args.long and _toNumber(args.long) -- se args.long == NA => long = nil

	if _isDefined(lat) and _isDefined(long) then
		args.map = string.lower(args.map)
		local icon = i18n.icons[args.map]
		local link = i18n.links[args.map]
		local msg  = i18n.msg[args.map]
		local size  = i18n.sizes[args.map]
		if _isDefined(icon) and _isDefined(link) and _isDefined(msg) then
			local countryQid = _getCountryQid(args.wikidata)
			local blockedCountries = i18n.blockedCountries[args.map] or {}
			if countryQid and blockedCountries[countryQid] then
				return ''
			else
				return ' [[File:' .. icon .. '|' .. size .. '|class=listing-sister-' .. args.map .. '|link=' .. link .. lat .. ','.. long ..'|' .. msg .. ']]'
			end
		end
	end

	return ''
end

local p = {}

function p.MapLinkTemplate(frame)
	return _MapLink(frame:getParent())
end

function p.MapLinkInvoke(frame)
	return _MapLink(frame)
end

function p.MapLinkModule(frame)
	local Cframe = mw.getCurrentFrame()
	Cframe.args = frame.args
	return _MapLink(Cframe)
end

return p