Módulo:Endereço

Permanently protected module
Origem: Wikipédia, a enciclopédia livre.
Documentação do módulo[ver] [editar] [histórico] [purgar]

Descrição

Este módulo tem funções auxiliares para criar infocaixas

Uso

Outra documentação:

local formats = require "Módulo:Endereço/Formatos"

local p = {}
local wikidata = require "Módulo:Infobox/Wikidata"
local linguistic = require "Módulo:Linguística"
local countrymodule = require "Módulo:Country data"

local function getCountry(item) -- get country id for formatting
    return wikidata.formatStatements{property = 'P17', entity = item, displayformat = 'raw', numvals = 1}
end

local function numberFromClaim(claim) -- recupera o número do edifício de um qualificador P670 de uma instrução Wikidata
    if not claim.qualifiers or not claim.qualifiers.P670 then
    return nil
    end
    local vals = {}
    for i, j in pairs(claim.qualifiers.P670) do
    table.insert(vals, wikidata.formatSnak(j))
    end
    return table.concat(vals, '-')
end

local function directionFromClaim(claim, area) -- por exemplo, Sherbrooke Street West
    if not claim.qualifiers or not claim.qualifiers.P560 then
    return nil
    end
    local str = ''
    for i, snak in pairs(claim.qualifiers.P560) do
    local directionlabels = area.directions or formats.default.directions
    str = str .. wikidata.formatSnak(snak,    {speciallabels = directionlabels})
    end
    return str
end

local function streetFromClaims(claim) -- reconstrói o nome da rua de uma afirmação P669 Wikidata
    return wikidata.formatStatement(claim)
end

local function formatStreet(streetname, housenumber, direction, displayformat)
    local val = displayformat.streetline or formats.default.streetline
    val = mw.ustring.gsub(val, '$number', housenumber or '')
    val = mw.ustring.gsub(val, '$street', streetname or '')
    val = mw.ustring.gsub(val, '$direction', direction or '')
    return val
end

local function wikidatastreet(claim, area) --formata dados na rua a partir de uma declaração Wikidata
    local streetname = streetFromClaims(claim, area)
    local housenumber =     numberFromClaim(claim, area)
    local direction = directionFromClaim(claim, area)
    return formatStreet(streetname, housenumber, direction, area)
end

function p.streetAddress(item, area) -- formata a linha referente à rua e ao número da rua
    local streets -- cadeia contendo a rua ou ruas e números de edifícios
    area = area or formats[getCountry(item)]
  
    -- tente preencher a rua, prioridade com P669, digite: elemento
    local streetclaims = wikidata.getClaims{entity = item, property = 'P669'}
    if streetclaims then
    for i, j in pairs(streetclaims) do
        streetclaims[i] = wikidatastreet(j, area)
    end
    streets = mw.text.listToText(streetclaims)
    streets = wikidata.formatAndCat{value = streets, entity = item, property = 'P669'}
    end
    -- senão : P969, type : string
    if not streets then
    streets =  wikidata.formatAndCat{property = 'P969', entity = item}
    end
    return streets
end

function p.adminDivList(item, country) -- returns a list of admin divisions matching the criteria defined in Módulo:Endereço/Formatos
    country = country or getCountry(item)
    local query = {property = 'P131'}
    local divs = wikidata.transitiveVals(item, query, 0, 10, country)
    local validDivs = {}  
  
    -- solution 1: looks for divs of a certain type
    local function setValue(targetclasses, depth)
    local test = {}
    for _, d in pairs(divs) do
        for j, divtype in pairs(targetclasses) do
        if (divtype == '-') then
            if #validDivs > 0 then
            divs = wikidata.addVals(divs, query, 0, 1, country)
            end
            if divs[#divs] == country then
            return nil
            end
            return divs[#divs]
        end
        if wikidata.isInstance(divtype, d, 3) then
             -- restrain list to new value, will be expanded only if needed
            divs = {d}
            return d
        end
        end
    end
    if depth >= 0 then
        local num = #divs
        divs = wikidata.addVals(divs, query, 0, 10, country)
        if #divs > num then return setValue(targetclasses, depth) end
    end
    end

    -- solution2: looks for divs that are part of a closed list (way more efficient for big items)
    local function findInList(list, depth)
    for i, j in pairs(divs) do
        for k, l in pairs(list) do
        if j == l then
            divs = {l}
            return l
        end
        end
    end
    if depth >= 0 then
        local num = #divs
        divs = wikidata.addVals(divs, query, 0, 10, country)
        if #divs > num then return findInList(list, depth) end
    end
    end
          
    local displayformat = formats[country] or formats.default
    local maxdepth = 3
    if not divs then
    return nil
    end

    if displayformat.div1 then
    local val = setValue(displayformat.div1, maxdepth)
    if val and val ~= validDivs[#validDivs] then
        table.insert(validDivs, val)
    end
    end

    if displayformat.div2 then
    local val = setValue(displayformat.div2, maxdepth)
    if val and val ~= validDivs[#validDivs] then
        table.insert(validDivs, val)
    end
    elseif displayformat.div2vals then
    local val =  findInList(displayformat.div2vals, 1)
    if val and val ~= validDivs[#validDivs] then
        table.insert(validDivs, val)
    end
    end
  
    if displayformat.div3 then
    local val = setValue(displayformat.div3, maxdepth)
    if val and val ~= validDivs[#validDivs] then
        table.insert(validDivs, val)
    end
    elseif displayformat.div3vals then
    local val =  findInList(displayformat.div3vals, 0)
    if val and val ~= validDivs[#validDivs] then
        table.insert(validDivs, val)
    end
    end
  
    return validDivs
end

function p.cityLine(item, country, divlist, postcode) -- line with list of admin divisions + optional postcode
    country = country or getCountry(item)
  
    local postcode = postcode or wikidata.formatStatements{entity = item, property = 'P281'} or ''
    if postcode == '-' then -- can be disabled when it is not useful
    postcode = ''
    end
    local divstr = ''
  
    if (not divlist) or (#divlist == 0) then
    divlist = p.adminDivList(item, country)
    end
    if (not divlist) or (#divlist == 0) then
    return -- add a maintenance category ?
    end
    for i, j in pairs(divlist) do
    if mw.ustring.match(j, 'Q%d+') then
        divlist[i] = wikidata.formatEntity(j)
    end
    end
    local divstr = linguistic.conj(divlist, 'comma')

    local formatting = formats[country] or formats.default
    local str = formatting.cityline or formats.default.cityline
    str = str:gsub("$postcode", postcode or '')
    str = str:gsub("$admindivs", divstr or '')
    return str
end

function p.fullAddress(item, country, divs, streetstr, divstr, showcountry, postcode)
     -- country id used for formatting
    country = country or getCountry(item)
    local displayformat = formats[country] or formats.default

    -- line 1 street
    local streetline = streetstr or p.streetAddress(item, country)
  
    -- line 2: administrative divisions, postcodes
    local cityline = divstr
    if not cityline then
    local partiallylocated = wikidata.formatAndCat{entity = item, property = {'P3179', 'P1382'}} -- para grandes entidades
    cityline = p.cityLine(item, country, divs, postcode)
    cityline = linguistic.conj({ partiallylocated, cityline}, 'new line')
    end
    if (not cityline) or mw.text.trim(cityline) == '' then
    cityline = nil
    end

    -- line 3: country
    local countryline, success
    if (showcountry ~= '-') then
        countryline, success = countrymodule.standarddisplay(country)
        if not success then
                if (type(country) == 'string') and string.find(mw.ustring.upper(country), "[[]Q[0-9]+[]]") then
                        countryline = wikidata.formatEntity(country)
                else
                        countryline = country or wikidata.formatStatements{entity = item, property = 'P17'}
                end
        end
    end
    local str = linguistic.conj({streetline, cityline, countryline}, '<br />')
    if str and (not streetstr) and (not divstr) then -- praticamente
    str = str .. '[[Categoria:!Artigos que utilizam um endereço fornecido no Wikidata]]'
    end
    return str
end

function p.wikidataAddress(item, country) -- funções de transição
    local area = formats[country] or formats.default
    local val = p.streetAddress(item, area)
    if val then
    return val .. '[[Categoria:!Artigos que utilizam um endereço fornecido no Wikidata]]'
    end
end
return p