Modul:Mapshape utilities

Dokumentaci tohoto modulu lze vytvořit na stránce Modul:Mapshape utilities/Dokumentace

-- base functions for Mapshapes

-- documentation

-- kopie z německých wikicest (https://de.wikivoyage.org/w/index.php?title=Modul:Mapshape_utilities&oldid=1539277)
-- copy from dewikivoyage (https://de.wikivoyage.org/w/index.php?title=Modul:Mapshape_utilities&oldid=1539277)
local MapshapeUtilities = {
	suite  = 'Mapshape utilities',
	serial = '2023-05-03',
	item   = 65447201
}

-- module import
-- require( 'strict' )
local mg = mw.loadData( 'Module:Marker utilities/Groups' )
local mi = mw.loadData( 'Module:Mapshape utilities/i18n' )
local wu = require( 'Module:Wikidata utilities' )

-- module variable and administration
local mu = {
	moduleInterface = MapshapeUtilities
}

-- helper functions
function mu.isSet( param )
	return param and param ~= '';
end

function mu.getParameter( arg, default )
	local r = arg or default
	if r == '' then
		r = default
	end
	return r
end

function mu.getNumber( arg, default )
	return tonumber( arg or default ) or default
end

function mu.getSize( arg, default )
	arg = arg:gsub( 'px$', '' )
	return mu.getNumber( arg, default )
end

function mu.getFirstId( ids )
	if not mu.isSet( ids ) then
		return ''
	end
	
	local sep = ids:find( ',' )
	if sep then
		return mw.text.trim( ids:sub( 1, sep - 1 ) )
	end
	return ids
end

function mu.getColor( id )
	local color, colorId

	if not mu.isSet( id ) then
		color = ''
	else
		-- getting P465: sRGB color hex triplet
		color = wu.getValue( id, 'P465' )
		if color == '' then
			-- getting P462: color id
			colorId = wu.getId( id, 'P462' )
			if colorId ~= '' then
				color = wu.getValue( colorId, 'P465' )
			end
		end
		if color ~= '' then
			color = '#' .. color
		end
	end
	return color
end

function mu.getImage( id )
	if not mu.isSet( id ) then
		return ''
	end
	return wu.getValue( id, 'P18' )
end

function mu.getTitle( id )
	if not mu.isSet( id ) then
		return nil
	end
	local label = mw.wikibase.getSitelink( id )
	if label then
		return label
	end
	label = mw.wikibase.getLabel( id ) or
		mw.wikibase.getLabelByLang( id, 'en' )
	if label then
		return label
	end
	return mw.title.getCurrentTitle().subpageText
end

-- checking and copying parameters
function mu.checkParams( frameArgs, list, moduleName, errorCateg )
	local complete = {}
	local args = {}
	local mismatch = {}
	moduleName = '<span class="error">' .. moduleName .. ': </span>'

	-- creating complete parameter list for check
	for key, value in pairs( list ) do
		if type( value ) == 'table' then
			for key2, value2 in ipairs( value ) do
				complete[ value2 ] = key
			end
		elseif value ~= '' then
			complete[ value ] = key
		else
			complete[ key ] = key
		end
	end

	local number, param
	for key, value in pairs( frameArgs ) do
		value = mw.text.trim( value )
		if value == '' then
			value = nil
		end

		-- splitting numbered parameters
		param = key
		number = ''
		local count
		if not tonumber( key ) then
			number, count = mw.ustring.gsub( key, '^([%a%-]+)(%d+)$', '%2' )
			if number == key then
				number = ''
			else
				param, count = mw.ustring.gsub( key, '^([%a%-]+)(%d+)$', '%1' )
			end
		end

		-- complete[ param ] makes translation
		local key2 = complete[ param ]
		if not key2 and not tonumber( key ) then
			table.insert( mismatch, "''" .. key .. "''" )
		else
			-- copying parameters to result array args
			if tonumber( key ) and not key2 then
				-- param can be a number, too
				args[ key ] = args[ key ] or value
			else
				args[ key2 .. number ] = args[ key2 .. number ] or value
			end
		end
	end

	for key, value in pairs( list ) do
		-- only for unnumbered parameters
		args[ key ] = args[ key ] or ''
	end

	local errorMsgs = ''
	if #mismatch == 1 then
		errorMsgs = mw.ustring.format( moduleName .. mi.unknownParam, mismatch[ 1 ] )
			.. errorCateg
	elseif #mismatch > 1 then
		errorMsgs = mw.ustring.format( moduleName .. mi.unknownParams,
			table.concat( mismatch, ', ' ) ) .. errorCateg
	end

	return args, errorMsgs
end

function mu.getCategories( formatStr )
	return wu.getCategories( formatStr )
end


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

function mu.getMapshapes( wdId )
	local values
	for i, property in ipairs( mi.mapshapesProps ) do
		values = wu.getValues( wdId, property, nil )
		if #values > 0 then
			break
		end
	end
	return values
end

-- create a table of aliases for groups
function mu.getAliases( tab, indx )
	local result = {}
	if not tab then
		return result
	end
	local iItem
	for key, item in pairs( tab ) do
		iItem = item[ indx ]
		if iItem then
			if type( iItem ) == 'table' then
				for i, q in ipairs( iItem ) do
					result[ q ] = key
				end
			else
				result[ iItem ] = key
			end
		end
	end
	return result
end

-- check if group is allowed or return a default value
function mu.checkGroup( group )
	if mu.isSet( group ) then
		local aliases = mu.getAliases( mg.groups, 'alias' )
		if aliases[ group ] then
			group = aliases[ group ]
		end
		return group
	else
		return mi.defaultGroup
	end
end

-- group translation for map legend into Wiki language
function mu.translateGroup( group )
	if not mu.isSet( group ) then
		group = mi.defaultGroup
	end
	local t = mg.groups[ group ]
	if t then
		t = t.map or t.label or t.alias or group
		if type( t ) == 'table' then
			t = t[ 1 ]
		end
		return t
	else
		return group
	end
end

return mu