Modulo:Coord: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
aggiornato stile documentazione a quello usato in Mediawiki per Lua, semplificazioni in checkRequestFormat
non generare indicatore quando non va mostrato, fix per Special:Permalink/140491317#Anomalia_coordinate
 
(9 versioni intermedie di 4 utenti non mostrate)
Riga 3:
]]
 
require('Modulo:No globalsstrict')
 
local mWikidata = require('Modulo:Wikidata')
Riga 61:
debug = args.display == 'debug'
}
end
 
local function getZoom( extraparams )
local scale = extraparams:match( '%f[%w]scale: ?(%d+)' )
if scale then
return math.floor(math.log10( 5 / tonumber( scale ) ) * 3 + 25)
end
 
local extraType = extraparams:match( '%f[%w]type: ?(%w+)' )
if extraType then
local zoomType = {
country = 5,
state = 6,
adm1st = 7,
adm2nd = 8,
city = 9,
isle = 10,
mountain = 10,
waterbody = 10,
airport = 12,
landmark = 13,
}
return zoomType[ extraType ]
end
return 13
end
 
Riga 223 ⟶ 249:
function Coord:new(args)
local decLat, decLong, dmsLat, dmsLong
local display = getDisplay(args)
local self = { args = args }
 
setmetatable(self, { __index = Coord })
 
--if nelargs.from namespace principale e conand display=.title (o con il parametro "prop")then
error('il parametro "from" è valido solo con display=inline', 3)
end
-- con display=title o con i parametri "prop" o "from"
-- legge le coordinate da P625 per utilizzarle o per confrontarle con quelle inserite
if mw.title.getCurrentTitle().namespace == 0 and (getDisplay(self.args).title or self.args.prop) or args.from then
self:_checkWikidata()
-- con "from", senza coordinate utente e su Wikidata non esegue i controlli successivi
if self.args.from and #self.args < 2 and not tonumber(args[1]) then
return self
end
end
 
Riga 276 ⟶ 310:
self.wdLat == round(decLat:getDeg(), 6) and
self.wdLong == round(decLong:getDeg(), 6) then
self.wdCat = '[[Categoria:CoordinateP625 ugualiuguale asu Wikidata]]'
end
 
Riga 289 ⟶ 323:
-- Legge la P625 e la utilizza come latitudine e longitudine se non fornite dall'utente.
function Coord:_checkWikidata()
self.wdEntityId = self.args.from or mw.wikibase.getEntityIdForCurrentPage();
if self.args.prop then
self.wdLat = mWikidata._getQualifier({ self.args.prop, 'P625', coord = 'latitude', n = 1, nq = 1, from = self.wdEntityId })
self.wdLong = mWikidata._getQualifier({ self.args.prop, 'P625', coord = 'longitude', n = 1, nq = 1, from = self.wdEntityId })
else
self.wdLat = mWikidata._getProperty({ 'P625', coord = 'latitude', n = 1, from = self.wdEntityId })
self.wdLong = mWikidata._getProperty({ 'P625', coord = 'longitude', n = 1, from = self.wdEntityId })
end
if self.wdLat and self.wdLong then
Riga 303 ⟶ 338:
table.insert(self.args, 1, numberToString(self.wdLat))
table.insert(self.args, 2, numberToString(self.wdLong))
self.wdCat = '[[Categoria:CoordinateP625 letteletta da Wikidata]]'
end
else
self.wdCat = '[[Categoria:CoordinateP625 assentiassente su Wikidata]]'
end
end
Riga 362 ⟶ 397:
local currFormat = cfg.params[self.reqFormat]
local globe = self.args[#self.args]:match('globe:(%w+)')
local earthself.isEarth = not globe or globe == 'earth'
for k, v in ipairs(self.args) do
if currFormat[k] then
Riga 374 ⟶ 409:
local num = tonumber(v)
if num then
if earthself.isEarth and num < parMin then
err = string.format('* %s format: %s < %s', self.reqFormat, parName, parMin)
elseif earthself.isEarth and math.floor(num) > parMax then
err = string.format('* %s format: %s > %s', self.reqFormat, parName, parMax)
end
Riga 399 ⟶ 434:
end
end
 
 
-- Utilizza l'estensione [[mw:Extension:GeoData]].
Riga 417 ⟶ 451:
-- @return {string}
function Coord:getDebugCoords()
-- con args.from restitusce una stringa vuota se non c'è nessun dato
if self.args.from and #self.args < 2 and not tonumber(self.args[1]) then
return ''
end
return self.decLat .. ' ' .. self.decLong .. ' ' .. self.dmsLat .. ' ' .. self.dmsLong
end
 
-- Restituisce l'HTML contenentedi leun coordinateelemento in<maplink> formatodi decKartographer eper dmsle come collegamento esterno a geohack.php.coordinate
--
-- @return {string}
function Coord:getHTML_buildMaplinkHTML()
local defaultFormat, geohackParams, display, root, html, url, htmlTitle
 
-- legge il parametro display
display = getDisplay(self.args)
 
if self.args.format then
defaultFormat = self.args.format
Riga 437 ⟶ 471:
end
 
local linkText;
if defaultFormat == 'dec' then
linkText = self.decLat .. ' ' .. self.decLong
else
linkText = tostring(self.dmsLat) .. ' ' .. tostring(self.dmsLong)
end
 
local jsonParams = {
type = 'Feature',
geometry = {
type ='Point',
coordinates = {
round( self.decLong:getDeg(), 6 ), -- max precision in GeoJSON format
round( self.decLat:getDeg(), 6 )
}
},
properties = {
['marker-color'] = "228b22",
}
}
 
if self.wdEntityId then
-- geoshape da Wikidata
jsonParams = {
jsonParams,
{
type = 'ExternalData',
service = 'geoshape',
ids = self.wdEntityId,
properties = {
['fill-opacity'] = 0.2
}
}
}
end
 
return mw.getCurrentFrame():extensionTag{
name = 'maplink',
content = mw.text.jsonEncode( jsonParams ),
args = {
text = linkText,
zoom = getZoom( self.args[#self.args] or '' ),
latitude = self.decLat:getDeg(),
longitude = self.decLong:getDeg(),
}
}
end
 
 
-- Restituisce l'HTML di un link a geohack con le coordinate (usato solo quando
-- non è possibile usare maplink).
--
-- @return {string}
function Coord:_buildGeohackLink()
-- crea la stringa per il parametro params di geohack.php
local geohackParams
 
if self.reqFormat == 'dec' then
geohackParams = string.format('%s_N_%s_E', self.args[1], self.args[2])
Riga 447 ⟶ 537:
geohackParams = table.concat(self.args, '_')
end
 
-- geohack url e parametri
local url = string.format('%s&pagename=%s&params=%s', cfg.geohackUrl,
mw.uri.encode(mw.title.getCurrentTitle().prefixedText, 'WIKI'), geohackParams)
if self.args.name then
url = url .. '&title=' .. mw.uri.encode(self.args.name)
end
local linkNode = mw.html.create('span')
:addClass('plainlinks nourlexpansion')
:wikitext('[' .. url .. ' ' .. tostring(self.dmsLat) .. ' ' .. tostring(self.dmsLong) .. ']')
:done()
return tostring(linkNode)
end
 
-- Restituisce l'HTML per il microformat Geo.
root = mw.html.create('')
-- FIXME Serve ancora nel 2024? La documentazione in proposito è scarsissima, e tutti
root
-- i link a strumenti che lo usano non vanno più. Inoltre, qui usiamo un elemento nascosto;
-- va bene lo stesso? Come verificarlo? Le specifiche non ne parlano.
--
-- @return {string}
function Coord:_buildGeoMarkup()
return mw.html.create('')
:wikitext(self.args.name and '<span class="vcard">' or '')
:tag('span')
:addClassattr('plainlinksstyle', nourlexpansion'display:none')
:wikitextaddClass('[geo' .. url)
:tag('span')
:addClass(defaultFormat == 'dec' and 'geo-nondefault' or 'geo-default')
:tag('span')
:addClass('geo-dms')
:attr('title', 'Mappe, foto aeree e altri dati per questa posizione')
:tag('span')
:addClass('latitude')
:wikitext(tostring(self.dmsLat))
:done()
:wikitext(' ')
:tag('span')
:addClass('longitude')
:wikitext(tostring(self.dmsLong))
:done()
:done()
:done()
:tag('span')
:addClass('geo-multi-punctlatitude')
:wikitext('&#xfeff; / &#xfeff;'tostring(self.dmsLat))
:done()
:wikitext( ', ' )
:tag('span')
:addClass(defaultFormat == 'dec' and 'geo-default' or 'geo-nondefaultlongitude')
:wikitext(tostring(self.args.name and '<span class="vcard">' or ''dmsLong))
:tag('span')
:addClass('geo-dec')
:attr('title', 'Mappe, foto aeree e altri dati per questa posizione')
:wikitext(self.decLat .. ' ' .. self.decLong)
:done()
:tag('span')
:attr('style', 'display:none')
:tag('span')
:addClass('geo')
:wikitext(self.decLat:getDeg() .. '; ' .. self.decLong:getDeg())
:done()
:done()
:wikitext(self.args.name and ('<span style="display:none"> (<span class="fn org">' ..
self.args.name .. '</span>)</span></span>') or '')
:done()
:wikitext(self.args.name and ('<span style="display:none"> (<span class="fn org">' ..
:wikitext(']')
self.args.name .. '</span>)</span></span>') or '')
:done()
end
 
-- Restituisce l'HTML contenente le coordinate con link che utilizza <maplink> (Kartographer)
html = tostring(root) .. (self.args.notes or '')
-- se possibile, e geohack altrimenti.
--
-- @return {string}
function Coord:getHTML()
-- con args.from restitusce una stringa vuota se non c'è nessun dato
if self.args.from and #self.args < 2 and not tonumber(self.args[1]) then
return ''
elseif self.args.display == 'debug' then
return self:getDebugCoords()
end
 
local display = getDisplay(self.args)
-- formatta il risultato a seconda di args.display (nil, 'inline', 'title', 'inline,title')
 
-- se inline e title, in stampa visualizza solo il primo
local coordLink
htmlTitle = string.format('<span style="font-size: small"><span %s id="coordinates">[[Coordinate geografiche|Coordinate]]: %s</span></span>',
if self.isEarth then
display.inline and 'class="noprint"' or '', html)
-- Usa maplink se possibile. A maggio 2024 supporta solo coordinate terrestri,
-- vedi T151138.
coordLink = self:_buildMaplinkHTML()
else
coordLink = self:_buildGeohackLink()
end
 
local geo = self:_buildGeoMarkup();
 
local html = coordLink .. tostring(geo) .. (self.args.notes or '')
 
local frame = mw.getCurrentFrame()
local ret = frame:extensionTag('templatestyles', '', {src = 'Modulo:Coord/styles.css'}) ..
(display.inline and html or '');
 
if display.title then
-- formatta il risultato a seconda di args.display (nil, 'inline', 'title', 'inline,title')
-- se inline e title, in stampa visualizza solo il primo
local htmlTitle = string.format(
'<div style="font-size: small"><span %s id="coordinates">[[Coordinate geografiche|Coordinate]]: %s</span></div>',
display.inline and 'class="noprint"' or '',
html
)
ret = ret .. frame:extensionTag( 'indicator', htmlTitle, { name = 'coordinates' } )
end
 
return (display.inline and html or '')ret ..
self:_setGeoData(display.title and htmlTitle or '') ..
(mw.title.getCurrentTitle().namespace == 0 and self.wdCat or '')
self:_setGeoData(display) ..
(self.wdCat or '')
end
 
Riga 519 ⟶ 631:
 
local p = {}
 
-- Per l'utilizzo da un altro modulo
function p._main(args)
local coord = Coord:new(args)
return args.display == 'debug' and coord:getDebugCoords() or coord:getHTML()
end
 
-- Funzione importata da https://en.wikipedia.org/w/index.php?title=Module:Coordinates&oldid=789126031
-- per estrarre lat, long, type, scale, dim, region, globe, source, dal link a geohack.php generato da coord.
function p.coord2text(frame)
if frame.args[1] == '' or frame.args[2] == '' or not frame.args[2] then return nil end
Riga 546 ⟶ 652:
end
 
-- Funzione per eventuale template {{dms2dec}}.
function p.dms2dec(frame)
local args = frame.args
Riga 553 ⟶ 659:
end
 
-- Funzione per eventuale template {{dec2dms}}.
function p.dec2dms(frame)
local args = frame.args
Riga 560 ⟶ 666:
end
 
-- Funzione per ill'utilizzo templateda {{Coord}}un altro modulo.
function p._main(args)
return Coord:new(args):getHTML()
end
 
-- Funzione per il template {{Coord}}.
function p.main(frame)
return select(2, xpcall(function()