Module:Marker

From Dive Atlas
Revision as of 00:23, 18 February 2024 by wikivoyage>Asretired (Protected "Module:Marker" ([Edit=Allow only template editors and administrators] (indefinite) [Move=Allow only template editors and administrators] (indefinite)))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

See also Template:Marker


--[[
	Source script:	https://it.wikivoyage.org/wiki/Modulo:Marker
	Maintainer:		Andyrom75

	Lazy loads:
	require('Module:WikidataIB').getSiteLink
]]
local coord2Dec = require('Module:Coordinates').toDec
local type2color = require('Module:TypeToColor').convertImpl

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

local function _makeMarkerSymbol( args, frame )
	local image4map = _isDefined(args.image) and ('[[File:' .. args.image .. '|300px]]') or ''
	local tagArgs = {
		zoom = tonumber( args.zoom ),
		latitude = tonumber( args.lat ),
		longitude = tonumber( args.long ),
		show = args.show,
		group = args.group,
		description = (_isDefined(args.description) or '') .. image4map
	}
	if _isDefined( args.text ) then
		tagArgs.text = args.text
		if not args.useIcon then
			tagArgs.class = 'no-icon'
		end
	end

	local geoJson = {
		type = 'Feature',
		geometry = {
			type = 'Point',
			coordinates = { tagArgs.longitude, tagArgs.latitude }
		},
		properties = {
			title = args.name,
			description = tagArgs.description,
			['marker-symbol'] = args.symbol,
			['marker-color'] = args.color,
			['marker-size'] = 'medium',
		}
	}

	-- frame:extensionTag is expensive
	return frame:extensionTag( 'maplink', mw.text.jsonEncode( geoJson ), tagArgs )
end

local function _Marker(frame)
	local args = frame.args
	local imageErrMsg = ''
	--shortcut variable
	local nome = args.name or ''
	local tag = ((args.islisting ~= 'yes') and mw.html.create( 'span' ):addClass( 'vcard' )) or mw.html.create()
	local anchor = ''

	if _isDefined( args.wikidata ) then
		-- Se manca una delle due coordinate, provo a estrarle entrambe da Wikidata
		if not _isDefined(args.lat) or not _isDefined(args.long) then
			local coords = mw.wikibase.getBestStatements( args.wikidata, 'P625')
			if coords[1] then
				args.lat = coords[1].mainsnak.datavalue.value.latitude
				args.long = coords[1].mainsnak.datavalue.value.longitude
			end
		end
		-- Se non è stata fornita l'immagine, provo a estrarla da Wikidata
		if not _isDefined(args.image) then
			local image = mw.wikibase.getBestStatements( args.wikidata, 'P18')
			args.image = image[1] and image[1].mainsnak.datavalue.value
		end
	end

	-- Se sono presenti le coordinate creo il marker tag
	if args.lat ~= 'NA' and args.long ~= 'NA' then
		if _isDefined(args.lat) and _isDefined(args.long) then
			-- arguments setup
			args.lat = coord2Dec(args.lat or 0, '', 6).dec
			args.long = coord2Dec(args.long or 0, '', 6).dec
			args.type = _isDefined(args.type) or 'listing'
			args.zoom = _isDefined(args.zoom) or '17'
			args.group = _isDefined(args.group) or args.type
			args.symbol = '-number-' .. ( args.counter or args.type or 'listing' )
			args.color = type2color( args.type )
			args.show = args.show or 'mask,around,buy,city,do,drink,eat,go,listing,other,see,sleep,vicinity,view'
	
			local mapTagOutput = _makeMarkerSymbol( args, frame )

			tag:tag( 'span' )
				:addClass( 'noprint listing-coordinates' )
				:css('display','none')
				:tag( 'span' )
					:addClass( 'geo' )
					:tag( 'abbr' )
						:addClass( 'latitude' )
						:wikitext( args.lat )
					:done()
					:tag( 'abbr' )
						:addClass( 'longitude' )
						:wikitext( args.long )
			:allDone()
			:tag( 'span' )
				:attr( 'title', 'See this element on the map' )
				:wikitext( mapTagOutput )
		else
			local listingTypes = {see=true, eat=true, buy=true, drink=true, sleep=true, ['do']=true, go=true, city=true, vicinity=true}
			if mw.title.getCurrentTitle().namespace == 0 and args.type and listingTypes[string.lower(args.type)] then
				anchor = anchor .. "[[Category:" .. string.lower(args.type) .. " listing with no coordinates]]"
			end
		end
	end

	-- costruisco l'ancora sul nome ed eventualmente gli associo il sitoweb
	if nome ~= '' then
		local anchor2 = ''
		anchor2 = '<span id="' .. mw.uri.anchorEncode( nome ) .. '" class="fn org listing-name" style="font-weight:bold;">' .. nome .. '</span>'
		-- se includo un sito web il nome deve essere puramente testuale
		if _isDefined(args.url) then
			anchor2 = string.match(nome, '%[%[') and ('[[Category:Listing without plain text name]]' .. anchor2)
				or ('[' .. args.url .. ' ' .. anchor2 .. ']')
		end
		anchor = anchor .. anchor2
	end
	if mw.title.getCurrentTitle().namespace == 0 then
		anchor = anchor .. '[[Category:Has map markers]]'
	end

	-- Se l'immagine fornita non esiste genero un messaggio di errore
	if _isDefined( args.image ) then
		local image = 'Media:' .. args.image
		if mw.title.new( image ) and mw.title.new( image ).exists == true then
			args.description = '[[' .. image .. '|100x100px|' .. nome .. ']]'
		else
			imageErrMsg = mw.html.create( 'span' )
				:addClass('imageinfo debuginfo')
				:css('display','none')
				:wikitext( 'NOT EXISTING LISTING IMAGE' )
		end
	end

	if (args.islisting ~= 'yes') and _isDefined( args.wikidata ) then
		local getSiteLink = require('Module:WikidataIB').getSiteLink
		if _isDefined( getSiteLink{args={wiki='enwikivoyage', qid=args.wikidata}} ) then
			anchor = anchor .. (string.match(nome, '%[%[') and '' or '[[Category:Marker with Wikivoyage article but no wikilink]]')
		else
			local wpLink = _isDefined( getSiteLink{args={wiki='enwiki', qid=args.wikidata}} )
			anchor = anchor .. (wpLink and '<span class="listing-sister-icons">&#32;[[File:Antu wikipedia.svg|15px|class=listing-sister|link=w:' .. wpLink .. '|<span>' .. wpLink .. '</span> on Wikipedia]]</span>' or '')
		end
	end

	return tostring(tag:node(imageErrMsg):wikitext(' ' .. anchor))
end

--Interfacce del modulo
local p = {}

function p.MarkerTemplate(frame)
	return _Marker(frame:getParent())
end

function p.MarkerInvoke(frame)
	return _Marker(frame)
end

function p.MarkerModule(frame)
	local Cframe = mw.getCurrentFrame()
	Cframe.args = frame.args
	return _Marker(Cframe)
end

return p