User:Andree.sk/sandbox/Module:Marker

-- module import local mb = require( 'Modul:MarkerBase' ) local mc = require( 'Modul:Marker/Colors' ) local mi = require( 'Modul:Marker/Maki icons' ) local mp = require( 'Modul:Marker/Params' ) local pc = require( 'Modul:ParamsCheck' ) local pt = require( 'Modul:Wikidata2/POITypes' ) -- WD types to vCard/Poi types local yn = require( 'Modul:Yesno' ) local fw = require( 'Modul:FastWikidata' )

local mk = {}

mk.labelStyles = { normal = 'font-weight: normal; font-style: normal;', italic = 'font-weight: normal; font-style: italic;', kursiv = 'font-weight: normal; font-style: italic;' }

-- Wikidata related constants

local searchLimit = 4 -- count of levels for P31-P279 search local p31Limit   = 3 -- maximum P31 values to analyse

-- main marker function

function mk._marker( args, frame ) local wrongQualifier = false local mkEntity      = nil -- marker entity

args.zoom        = math.floor( tonumber( args.zoom or '' ) or 17 ) if (args.zoom < 0) or (args.zoom > 19) then args.zoom = 17 end args.name        = args.name or '' args.alt         = args.alt or '' args.symbol      = string.lower( args.symbol or '' ) args.text        = args.text or '' args['map-group'] = args['map-group'] or '' args['type']     = args['type'] or '' args.group       = args.group or '' args.url         = args.url or '' args.addMf       = args.addMf or '' args.image       = args.image or '' args.styles      = args.styles or '' args.wikidata, mkEntity, wrongQualifier = mb.checkId( args.wikidata or '' )

local thisWiki  = '' -- marker-name link to current Wikivoyage article local commonsCat = '' -- name of Commons category local sisters = { wikivoyage = '', -- link to another branch, usually en, as a sister link commons   = '', -- link to Commons category wikipedia = ''  -- link to Wikipedia }	local showSisters = false -- sisters were fetched from Wikidata local wikiLang   = mw.getContentLanguage:getCode

local s, t

-- url = y/n allow/disallow url output local noURL = false if args.url ~= '' then t = yn( args.url, nil ) if t ~= nil then -- then a is boolean args.url = '' noURL = not t		end end

-- checking coordinates given as template parameters -- converting DMS to decimal coordinates if necessary local dms = false args.lat, args.long, dms = mb.checkCoordinates( args.lat, args.long )

-- image check local errorMsgs = '' if args.image ~= '' then args.image, errorMsgs = mb.checkImage( args.image, errorMsgs, mkEntity ) end

-- getting data from Wikidata local coordFromWD = false local nameFromWD = false if args.wikidata ~= '' then -- mkEntity is available if args.name == '' then args.name = mkEntity:getLabel or mkEntity:getLabel( 'en' ) or '' if args.name ~= '' then nameFromWD = true end end if args.image == '' then args.image = fw.getEntWDvalue( mkEntity, 'P18' ) end

if args['type'] == '' then t = fw.getEntWDvalues( mkEntity, 'P31', p31Limit ) args['type'] = fw.typeSearch( t, pt, searchLimit ) end

if (args.lat == ) or (args.long == ) then t = fw.getEntWDvalue( mkEntity, 'P625' ) if t ~= '' then args.lat = t.latitude args.long = t.longitude coordFromWD = true end end

showSisters = ( args.format ~= 'f2' ) and ( args.format ~= 'f3' )

if showSisters then if mkEntity.sitelinks then t = mkEntity.sitelinks[wikiLang .. 'wikivoyage'] if t then if t.title ~= mw.title.getCurrentTitle.text then thisWiki = t.title end -- no link to the article itself end sisters.wikipedia = mb.getWikiLink( {wikiLang, 'en'}, 'wiki', mkEntity ) if thisWiki == '' then sisters.wikivoyage = mb.getWikiLink( {'en'}, 'wikivoyage', mkEntity ) end end commonsCat = fw.getEntWDvalue( mkEntity, 'P373' ) if commonsCat ~= '' then commonsCat = mw.ustring.gsub( commonsCat, ' ', '_' ) sisters.commons = tostring( mw.uri.fullUrl( 'c:Category:' .. mw.uri.encode( commonsCat, 'WIKI' ) ) ) end

if (args.url == '') and (not noURL) then args.url = fw.getEntWDvalue( mkEntity, 'P856' ) -- official website end end end

-- coordinates are neccessary if (args.lat == ) or (args.long == ) then return mk.errorMsg.missingCoord end

if dms then errorMsgs = mk.errorMsg.dmsCoordinate end

-- getting Marker type and group args['type'], args.group, errorMsgs = mb.checkTypeAndGroup( args['type'], args.group, errorMsgs ) args['group-translated'] = mb.translateGroup( args.group )

-- url check args.url, errorMsgs = mb.checkUrl( args.url, errorMsgs )

-- checking name existence, splitting name and link from name if args.name == '' then args.name = mk.errorMsg.missingName errorMsgs = errorMsgs .. ' ' .. mk.errorMsg.missingNameMsg end local name = mb.getName( args.name, thisWiki ) local altname = mb.getName( args.alt, thisWiki ) if not altname.exists then altname = name end

-- marker color local color = '' color, args.group = mc.getColor( args.group )

-- distinguishing marker symbols, default: number local useIcon = false -- true: add pushpin symbol if args.symbol == 'letter' then args.symbol = '-letter-' .. args.group elseif (args.symbol == '') or (args.symbol == 'number') then args.symbol = '-number-' .. args.group elseif (args.symbol:len == 1) and (args.symbol:match('%w')) then args.text = args.symbol:upper errorMsgs = errorMsgs .. mk.errorMsg.numberUsed elseif (args.symbol ~= ) and mi[args.symbol] and (args.text == ) then -- check if MAKI icon is available if mi[args.symbol].im ~= '' then args.text = mi[args.symbol].im		else -- fallback if no image is available args.text = args.symbol:sub( 1, 1 ):upper useIcon = true end elseif (args.symbol ~= '') and (mi[args.symbol] == nil) then args.symbol = 'cross' color, args.group = mc.getColor( 'error' ) args.text = mk.errorMsg.closeX errorMsgs = errorMsgs .. ' ' .. mk.errorMsg.unknownIcon end

-- generating output

-- adding marker symbol local result = -- output string, wrapper is added later mb.addingMarkerSymbol( args, altname.all, args.text, args.symbol,			useIcon, color, frame )

if args.format == 'f2' then -- marker only

elseif args.format == 'f3' then -- marker and coordinates result = result .. mb.dmsCoordinates( args.lat, args.long, name.name, coordFromWD,				'type:landmark_globe:earth', false )

elseif (args.format == 'f0') or (args.format == 'f1') then -- f0: default: marker and name -- f1: marker and name (coordinates) if name.all ~= '' then if result ~= '' then result = result .. ' ' end if args.styles ~= '' then t = mk.labelStyles[ args.styles:lower ]; if not t then t = args.styles end t = ' style="' .. t .. '"' else t = '' end s = '' .. name.all .. ' '			if args.url == '' then result = result .. s			else if name.pageTitle == '' then result = result .. '[' .. args.url .. ' ' .. s .. ']'				else -- both article and web links result = result .. s .. ' ['						.. args.url .. ' ' .. mb.Icons.internet .. '] '				end end else if args.url ~= '' then if result ~= '' then result = result .. ' ' end result = result .. '[' .. args.url .. ']'			end end

-- adding Wikimedia sister project icons

if showSisters then -- only for f0 and f1 formats s = '' local key, value for key, value in pairs( sisters ) do				if value ~= '' then s = s .. ' ['						.. value .. ' ' .. mb.Icons[key] .. '] '				end end if args.wikidata ~= '' then s = s .. ' ['					.. tostring( mw.uri.fullUrl( 'd:' .. args.wikidata ) ) .. ' ' .. mb.Icons.wikidata .. '] '			end result = result .. s		end

if args.format == 'f1' then result = result .. mb.dmsCoordinates( args.lat, args.long, name.name,					coordFromWD, 'type:landmark_globe:earth', true ) end else result = result .. ' ' .. mk.errorMsg.unknownFormat .. ' '	end if args.image ~= '' then result = result .. ' ' .. args.image			.. ' ' end

-- adding wrapper and microformats if args.addMf == 'true' then if yn( args['no-gpx'], false ) then s = '<span class="h-card vcard-nogpx Marker"' else s = '<span class="h-card vcard Marker"' end s = s .. ' data-wikilang="' .. wikiLang .. '"' .. ' data-color="#' .. color .. '"' .. ' data-type="' .. args['type'] .. '"' .. ' data-group="' .. args.group .. '"' .. ' data-group-translated="' .. args['group-translated'] .. '"' .. ' data-name="' .. mb.htmlReplace( altname.name ) .. '"' .. ' data-symbol="' .. args.symbol .. '"' if args.url ~= '' then s = s .. ' data-url="' .. mw.text.nowiki( args.url ) .. '"' end if args['map-group'] ~= '' then s = s .. ' data-map-group="' .. args['map-group'] .. '"' end if args.wikidata ~= '' then s = s .. ' data-wikidata="' .. args.wikidata .. '"' end if commonsCat ~='' then s = s .. ' data-commonscat="' .. commonsCat .. '"' end if args.image ~= '' then s = s .. ' data-image="' .. mw.ustring.gsub( args.image, ' ', '_' ) .. '"' end

result = s .. '> '			.. ' ' .. args.lat .. ' '			.. ' ' .. args.long .. ' '			.. result if (args.format == 'f2') or (args.format == 'f3') then result = result .. ' '				.. name.name .. ' '		end result = result .. ' '		-- adding coordinates to Mediawiki database -- frame:callParserFunction is expensive result = result .. frame:callParserFunction{ name = '#coordinates', args = { args.lat, args.long, 'type:landmark_globe:earth', name = name.name } } end

if wrongQualifier then result = result .. mk.errorMsg.wrongQualifier end

if args.demo == 'true' then return result else return result .. errorMsgs end end

function mk.marker( frame ) local args    = frame:getParent.args args.lat      = args.lat or '' args.long     = args.long or '' args.image    = args.image or '' args.addMf    = args.addMf or 'true' args.symbol   = args.symbol or '' args.format   = args.format or 'f0'

return mk._marker( args, frame ) .. pc.checkParams( args, mp._marker, '' ) end

function mk.poi( frame ) local args     = frame:getParent.args args[1]        = mw.text.trim( args[1] or '' ) args['type']   = mw.text.trim( args[2] or '' ) if args['type'] == '' then args['type'] = 'other' end args.lat       = pc.checkNumber( args[3] or '' ) args.long      = pc.checkNumber( args[4] or '' ) args.name      = mw.text.trim( args[5] or '' ) args.image     = mw.text.trim( args[6] or '' ) -- parameter args[7] not yet / no longer used args[7]        = mw.text.trim( args[7] or '' ) if args[7] == '' then args[7] = 'W' end args.format    = 'f2' args.addMf     = 'true'

if (args[1] ~= '') and (args[1] ~= '0') then args.symbol = args[1] end

return mk._marker( args, frame ) .. pc.checkParams( args, mp._poi, '' ) end

return mk