Módulo:Geolocalizacao do artigo

De Mapa Mov Saúde

A documentação para este módulo pode ser criada em Módulo:Geolocalizacao do artigo/doc

-- Publica no GeoData somente a localização que o editor solicitou.
-- A aprovação para o mapa da página inicial é independente.
local p = {}

local function decimal(value, minimum, maximum)
    if type(value) ~= 'string' then return nil end
    value = mw.text.trim(value)
    if #value == 0 or #value > 18 then return nil end
    local unsigned = value:gsub('^[+-]', '')
    if not unsigned:match('^%d+$') and not unsigned:match('^%d+%.%d+$') then
        return nil
    end
    local integer, fractional = unsigned:match('^(%d+)%.(%d+)$')
    if not integer then integer = unsigned end
    if #integer > 3 or (fractional and #fractional > 12) then
        return nil
    end
    local number = tonumber(value)
    if not number or number < minimum or number > maximum then return nil end
    return value
end

function p.registrar(frame)
    local a = frame.args
    local request = mw.text.trim(a.exibir_no_mapa or ''):lower()
    if request ~= 'sim' and request ~= 'yes' and request ~= 'true' and request ~= '1' then
        return ''
    end

    local location = mw.text.trim(a.localizacao or '')
    local lat, lon
    if location ~= '' then
        if mw.text.trim(a.latitude or '') ~= '' or mw.text.trim(a.longitude or '') ~= '' then
            return ''
        end
        local parts = mw.text.split(location, ',', true)
        if #parts ~= 2 then return '' end
        lat = decimal(parts[1], -90, 90)
        lon = decimal(parts[2], -180, 180)
    else
        -- Compatibilidade com pontos legados já informados em dois campos.
        lat = decimal(a.latitude, -90, 90)
        lon = decimal(a.longitude, -180, 180)
    end
    if not lat or not lon then return '' end

    return frame:preprocess('{{#coordinates:' .. lat .. '|' .. lon .. '|primary}}')
end

return p