Modul:Mapshapes

Aus Wikivoyage
Zur Navigation springen Zur Suche springen
Template-info.png Dokumentation für das Modul Mapshapes[Ansicht] [Bearbeiten] [Versionsgeschichte] [Aktualisieren]

Anwendung

Der Modul wird direkt von der Vorlage {{Mapshapes}} aufgerufen. Parameterbeschreibung siehe dort.

Benötigte weitere Module

Dieses Modul benötigt folgende weitere Module: FastWikidata • Mapshapes/Params • MapshapeBase • MapshapeBase/i18n
Hinweise
-- getting a set of mapshape objects

-- module import
local fw = require( 'Module:FastWikidata' )
local mb = require( 'Module:MapshapeBase' )
local mi = require( 'Module:MapshapeBase/i18n' )
local mp = require( 'Module:Mapshapes/Params' )

local ms = {
	entityId = nil,
	wikilang
}

-- add Wikivoyage link if available
local function addLink( title, id )
	if title and title ~= '' and not title:find( '[[', 1, true ) and id ~= ms.entityId then
		local link = mw.wikibase.getSitelink( id, ms.wikiLang .. 'wikivoyage' )
		if link then
			title = '[[' .. link .. '|' .. title .. ']]'
		end
	end
	return title
end

-- check if the child item should be shown
local function idMatch( args, id )
	local key = 2
	if mw.text.trim( args[ key ] or '' ) == '' then
		-- empty list of child items to show
		return true
 	end

	while mw.text.trim( args[ key ] or '' ) ~= '' do
		if mw.text.trim( id ) == mw.text.trim( args[ key ] ) then
			return true
		end
		key = key + 1
	end
	return false
end

local function getShapes( args, frame )
	local catArray = {}
	local ns = mw.title.getCurrentTitle().namespace
	ms.entityId = mw.wikibase.getEntityIdForCurrentPage()
	ms.wikiLang = mw.getContentLanguage():getCode()
	
	-- P527: has part
	local values, catArray = fw.getValues( args[ 1 ], 'P527', nil, catArray )
	if #values == 0 then
		-- P2670: has parts of the class
		values, catArray = fw.getValues( args[ 1 ], 'P2670', nil, catArray )
		if #values == 0 then
			return '<span class="error">' .. mi.mssNoParts .. '</span>' .. mi.mssUnknown
		end
	end

	local content = {}, result, geojson, i, id, title, description, colorId, rgb
	local tagArgs = {
		text = '',
		group = args.group or mi.group,
		class = 'no-icon'
	}
	for i = 1, #values, 1 do
		id = values[ i ].id
		if idMatch( args, id ) then
			title = addLink( mw.wikibase.label( id ) or id, id )
			description, catArray = mb.getImage( id, catArray )
			if description == '' then
				description = nil
			else
				description = '[[file:' .. description .. '|141px]]'
			end

			rgb, catArray = mb.getColor( id, catArray )
			if rgb == '' then
				rgb = args.defaultColor
			end

			geojson = {
				type = 'ExternalData',
				service = args.defaultType,
				ids = id,
				properties = {
					title = title,
					description = description,
					fill = mi.defaultFill,
					[ 'fill-opacity' ] = mi.defaultFillOpacity,
					stroke = rgb,
					[ 'stroke-width' ] = args.strokeWidth,
					[ 'stroke-opacity' ] = args.strokeOpacity
				}
			}
			-- collecting multiple geojson codes
			table.insert( content, mw.text.jsonEncode( geojson ) )
		end
	end
	if #content == 0 then
		geojson = nil
	elseif #content == 1 then
		geojson = content[ 1 ]
	else
		geojson = '[' .. table.concat( content, ',') .. ']'
	end
	if geojson then
		-- only one expensive call
		result = frame:extensionTag( 'maplink', geojson, tagArgs )
	else
		result = ''
	end
	if ns == 0 then
		result = result .. fw.getCategories( catArray, mi.properties )
	end
	return result
end

function ms.show( frame )
	local args, errorMsg
	args, errorMsg = mb.checkParams( frame:getParent().args, mp, 'Mapshapes', mi.mssUnknown )
	if args[1] == '' then
		return mi.mssMissingFirst
	end

	args.strokeWidth   = mb.getNumber( args.strokeWidth, mi.defaultShapesWidth )
	args.strokeOpacity = mb.getNumber( args.strokeOpacity, mi.defaultShapesOpacity )
	args.defaultType   = mb.getParameter( args.defaultType, 'geoline' )
	args.defaultColor  = mb.getParameter( args.defaultColor, mi.defaultStroke )
	if not string.find( args.defaultColor, '#', 1, true ) then
		args.defaultColor = '#' .. args.defaultColor
	end
	if args.group == '' then
		args.group = nil
	end

	return getShapes( args, frame ) .. errorMsg
end

return ms