Dokumentation für das Modul Marker[Ansicht] [Bearbeiten] [Versionsgeschichte] [Aktualisieren]

Dieses Modul enthält Funktionen zur Darstellung von Karten-Markern. Die Funktionen des Moduls werden nicht direkt aufgerufen, sondern über die Vorlage {{Marker}}.

Versionsbezeichnung auf Wikidata: 2024-11-10 Ok!

Funktionen

function mk.marker( frame )

Die Funktion prüft die Parameter der Vorlage {{Marker}} und führt die Markerdarstellung aus. Die möglichen Parameter sind in der Vorlage {{Marker}} beschrieben. Im Projektnamensraum befindet sich die technische Dokumentation.

Benötigte weitere Module

Dieses Modul benötigt folgende weitere Module: CountryData • Marker/Params • Marker utilities • Marker utilities/i18n • Wikidata utilities
Hinweise
-- module variable and administration
local mk = {
	moduleInterface = {
		suite  = 'Marker',
		serial = '2024-11-10',
		item   = 40852170
	}
}

-- module import
local mi = require( 'Module:Marker utilities/i18n' )
local mu = require( 'Module:Marker utilities' )
local mp = require( 'Module:Marker/Params' )
local cm = require( 'Module:CountryData' )
local wu = require( 'Module:Wikidata utilities' )

-- adding from-WD class
local function addWdClass( key )
	return mu.addWdClass( mp.wdContent[ key ] )
end

-- taking mp.ynCheckList into account
local function checkYn( args )
	local s
	for i, key in ipairs( mp.ynCheckList ) do
		args[ key ] = args[ key ] or ''
		s = mu.yesno( args[ key ] )
		if s then -- 'y' or 'n'
			args[ key ] = ''
			mp.wdContent[ key ] = ( s == 'y' ) and true or nil
		elseif args[ key ] ~= '' then
			mp.wdContent[ key ] = nil
		end
	end
end

local function initialParametersCheck( frame )
	local entity = nil
	local wrongQualifier = false
	local show = {}

	-- checking keys and copying values to args
	local args = mu.checkArguments( frame:getParent().args, mp.p )

	-- removing control characters
	local descrDiv
	for key, v in pairs( mp.p ) do
		if args[ key ] and args[ key ] ~= '' then
			args[ key ], descrDiv = mu.removeCtrls( args[ key ], true )
		else
			args[ key ] = ''
		end
	end

	-- checking Wikidata entitity
	if args.wikidata ~= '' then
--		args.wikidata, entity, wrongQualifier = wu.getEntityId( args.wikidata or '' )
		args.wikidata, entity, wrongQualifier = wu.getEntity( args.wikidata or '' )
		if wrongQualifier then
			mu.addMaintenance( 'wrongQualifier' )
		elseif args.wikidata ~= '' then
			mu.addMaintenance( 'wikidata' )
		end
	end

	-- making web addresses table
	local web = {}
	mu.tableInsert( web, args.url )

	-- getting country-specific technical parameters
	local country = cm.getCountryData( entity, nil, args.country, args.wikidata, web )
	if country.unknownCountry then
		mu.addMaintenance( 'unknownCountry' )
	end
	-- for map support
	country.extra = mi.map.defaultSiteType
	if mu.isSet( country.iso_3166 ) then
		country.extra = country.extra .. '_region:' .. country.iso_3166
	end

	-- checking show parameter
	show = mu.getShow( mp.defaultShow, args, mp.show )

	-- y/n allow/disallow output
	if not mu.isSet( args.nameLocal ) then
		args.nameLocal = 'n'
	end
	checkYn( args )
	args.noGpx = mu.yesno( args.noGpx ) == 'y'

	mu.checkStatus( args )
	mu.checkStyles( args )
	-- checking coordinates and converting DMS to decimal coordinates if necessary
	mu.checkCoordinates( args )
	mu.checkZoom( args )
	-- removing namespace from category
	mu.checkCommonsCategory( args )
	for i, param in ipairs( mi.options.parameters ) do
		if mu.isSet( args[ param ] ) then
			mu.addMaintenance( 'parameterUsed', param )
		end
	end

	return args, entity, show, country
end

-- getting data from Wikidata
local function getDataFromWikidata( args, show, page, country, entity )
	local function singleProperty( propDef, maxCount )
		local v = wu.getValues( entity, propDef.p, maxCount )
		if propDef.f then
			for i = 1, #v, 1 do -- formatting
				if mu.isSet( v[ i ] ) then
					v[ i ] = propDef.f:format( v [ i ] )
				end
			end
		end
		return v
	end

	local function getValues( propDef )
		local v
		local maxCount = propDef.c or 1
		local tp = type( propDef.p )
		if tp == 'string' then
			v = singleProperty( propDef, maxCount )
		elseif tp == 'table' then
			for i, sngl in ipairs( propDef.p ) do
				if type( sngl ) == 'table' then
					v = singleProperty( sngl, maxCount )
					if #v > 0 then
						break
					end
				end
			end
		end
		return table.concat( v, ', ' )
	end

	if args.wikidata == '' then
		return
	end

	mu.getTypeFromWikidata( args, entity )

	args.nameLocal = mp.wdContent.nameLocal or args.nameLocal
	mu.getNamesFromWikidata( args, mp.wdContent, page, country, entity )

	mu.getCoordinatesFromWikidata( args, mp.wdContent, entity )
	mu.getCommonsCategory( args, entity )

	if show.name then
		mu.getArticleLink( args, entity, page )
	end

	-- getting more values from Wikidata
	local p
	for key, value in pairs( mp.wdContent ) do
		p = mp.wd[ key ]
		if p and not mu.isSet( args[ key ] ) then
			args[ key ] = getValues( p )
			mp.wdContent[ key ] = args[ key ] ~= ''
		end
	end
end

local function finalParametersCheck( args, show, country, entity )
	-- creating givenName, displayName tables
	mu.prepareNames( args )

	-- coordinates are neccessary
	show.noCoord = args.lat == '' or args.long == ''
	if show.noCoord then
		show.name, show.poi, show.coord = 1, false, false
		mu.addMaintenance( 'missingCoord' )
	end

	-- getting Marker type, group, and color
	mu.checkTypeAndGroup( args )

	-- image check
	if not mp.wdContent.image or mi.options.WDmediaCheck then 
		mu.checkImage( args, entity )
	end

	mu.checkUrl( args )

	args.commonscat = args.commonscat:gsub( ' ', '_' )
end

local function makeMetadata()
	return mi.options.markerMetadata and tostring( mw.html.create( 'span' )
		:attr( 'class', 'listing-metadata listing-metadata-items' )
		:wikitext( '​' ) ) or ''
end

local function makeOutput( args, show, page, country, entity, frame )
	local result = {} -- output string table, wrapper is added later

	-- adding status icons
	mu.tableInsert( result, mu.makeStatusIcons( args ) )

	-- adding marker symbol
	args.metadata = makeMetadata()
	if show.poi or mu.isSet( args.copyMarker ) then
		table.insert( result, mu.makeMarkerSymbol( args, show, frame ) ..
			( show.name and '' or args.metadata ) )
	end
	if #result > 0 then
		result = { table.concat( result, mi.texts.space ) }
	end

	-- adding name, airport code, sister-project icons, and coordinate
	if show.name then
		local nameClass = addWdClass( 'name' )
		if not show.noCoord and not show.poi and not show.coord then
			nameClass = nameClass .. ' listing-without-marker'
		end
		if args.displayName.all ~= '' or args.nameExtra ~= '' or args.nameLocal ~= '' then
			mu.makeName( result, args, show, page, country, nameClass, addWdClass( 'nameLocal' ) )
		elseif args.url ~= '' then
			table.insert( result, mu.makeSpan( '[' .. args.url .. ']', 'listing-url' ) )
		end
		result = { table.concat( result, mi.texts.space ) ..
			mu.makeIcons( args, page, country, entity, show, mp.wdContent ) }
	end

	-- adding coordinate if requested and available
	if show.coord then
		mu.tableInsert( result, mu.dmsCoordinates( args.lat, args.long,
			args.givenName.name, mp.wdContent.lat, country.extra, not show.name ) )
	end

	return table.concat( result, mi.texts.space )
		-- removing succeeding brackets
		:gsub( '%)(</span>)%s*(<span [^>]*>)%s*%(', '%1; %2' )
end

-- main marker function
function mk.marker( frame )
	mu.initMaintenance()
	local page = mu.getPageData()

	-- copying frame:getParent().args to template arguments, args, parameter check
	-- returning Wikidata entity and display options
	local args, mkEntity, show, country = initialParametersCheck( frame )
	show.inline = true

	-- associated Wikivoyage page of the location in current Wikivoyage branch
	-- possibly modified by mu.getArticleLink()
	args.wikiPage = ''
	args.template = 'Marker'

	getDataFromWikidata( args, show, page, country, mkEntity )

	-- parameter check after data import from Wikidata
	finalParametersCheck( args, show, country, mkEntity )

	-- generating output
	local result = makeOutput( args, show, page, country, mkEntity, frame )

	return mu.makeWrapper( result, args, page, country, show, mp.markerData, frame )
		.. mu.makeMaintenance( page, { wu, mu, cm } )
end

return mk