Module:Location map: Difference between revisions

Content added Content deleted
m (1 revision imported)
en>Centrist16
No edit summary
Line 16: Line 16:
local moduletitle = mw.title.new('Module:Location map/data/' .. map)
local moduletitle = mw.title.new('Module:Location map/data/' .. map)
if not moduletitle then
if not moduletitle then
error(string.format('%q is not a valid name for a location map definition', map), 2)
error('"' .. map .. '" is not a valid name for a location map definition', 2)
elseif moduletitle.exists then
elseif moduletitle.exists then
local mapData = mw.loadData('Module:Location map/data/' .. map)
local mapData = mw.loadData('Module:Location map/data/' .. map)
Line 74: Line 74:


local function decdeg(degrees, minutes, seconds, hemisphere, decimal, direction)
local function decdeg(degrees, minutes, seconds, hemisphere, decimal, direction)
if decimal then
if not degrees then
if degrees then
if not decimal then
return nil
error('Decimal and DMS degrees cannot both be provided for ' .. direction, 2)
elseif minutes then
error('Minutes can only be provided with DMS degrees for ' .. direction, 2)
elseif seconds then
error('Seconds can only be provided with DMS degrees for ' .. direction, 2)
elseif hemisphere then
error('A hemisphere can only be provided with DMS degrees for ' .. direction, 2)
end
end
local retval = tonumber(decimal)
local retval = tonumber(decimal)
if retval then
if not retval then
error('The value "' .. decimal .. '" provided for ' .. direction .. ' is not valid', 2)
return retval
end
end
return retval
error('The value "' .. decimal .. '" provided for ' .. direction .. ' is not valid', 2)
elseif seconds and not minutes then
error('Seconds were provided for ' .. direction .. ' without minutes also being provided', 2)
elseif not degrees then
if minutes then
error('Minutes were provided for ' .. direction .. ' without degrees also being provided', 2)
elseif hemisphere then
error('A hemisphere was provided for ' .. direction .. ' without degrees also being provided', 2)
end
return nil
end
end
decimal = tonumber(degrees)
decimal = tonumber(degrees)
if not decimal then
if not decimal then
error('The degree value "' .. degrees .. '" provided for ' .. direction .. ' is not valid', 2)
error('The degree value "' .. degrees .. '" provided for ' .. direction .. ' is not valid', 2)
end
elseif minutes and not tonumber(minutes) then
if minutes and not tonumber(minutes) then
error('The minute value "' .. minutes .. '" provided for ' .. direction .. ' is not valid', 2)
error('The minute value "' .. minutes .. '" provided for ' .. direction .. ' is not valid', 2)
end
elseif seconds and not tonumber(seconds) then
if seconds and not tonumber(seconds) then
error('The second value "' .. seconds .. '" provided for ' .. direction .. ' is not valid', 2)
error('The second value "' .. seconds .. '" provided for ' .. direction .. ' is not valid', 2)
end
end
Line 116: Line 103:
end
end
return decimal
return decimal
end

-- Finds a parameter in a transclusion of {{Coord}}.
local function coord2text(para,coord) -- this should be changed for languages which do not use Arabic numerals or the degree sign
local result = mw.text.split(mw.ustring.match(coord,'%-?[%.%d]+°[NS] %-?[%.%d]+°[EW]') or '', '[ °]')
if para == 'longitude' then result = {result[3], result[4]} end
if not tonumber(result[1]) or not result[2] then return error('Malformed coordinates value', 2) end
return tonumber(result[1]) * hemisphereMultipliers[para][result[2]]
end
end


Line 166: Line 145:
end
end
local retval = args.float == 'center' and '<div class="center">' or ''
local retval = args.float == 'center' and '<div class="center">' or ''
if args.caption and args.caption ~= '' and args.border ~= 'infobox' then
if args.caption and args.caption ~= '' then
retval = retval .. '<div class="noviewer thumb '
retval = retval .. '<div class="noviewer thumb '
if args.float == '"left"' or args.float == 'left' then
if args.float == '"left"' or args.float == 'left' then
Line 220: Line 199:
local retval = '</div>'
local retval = '</div>'
if not args.caption or args.border == 'infobox' then
if not args.caption then
retval = retval .. '<div style="font-size:90%;padding-top:3px">'
if args.border then
.. ((args.label or mw.title.getCurrentTitle().text) .. ' (' .. map('name') .. ')')
retval = retval .. '<div>'
else
retval = retval .. '<div style="font-size:90%;padding-top:3px">'
end
retval = retval
.. (args.caption or (args.label or mw.title.getCurrentTitle().text) .. ' (' .. map('name') .. ')')
.. '</div>'
.. '</div>'
elseif args.caption ~= '' then
elseif args.caption == '' then
retval = retval .. '<div style="font-size:90%;padding-top:3px"></div>'
else
-- This is not the pipe trick. We're creating a link with no text on purpose, so that CSS can give us a nice image
-- This is not the pipe trick. We're creating a link with no text on purpose, so that CSS can give us a nice image
retval = retval .. '<div class="thumbcaption"><div class="magnify">[[:File:' .. getContainerImage(args, map) .. '| ]]</div>' .. args.caption .. '</div>'
retval = retval .. '<div class="thumbcaption"><div class="magnify">[[:File:' .. getContainerImage(args, map) .. '| ]]</div>' .. args.caption .. '</div>'
Line 249: Line 225:
mw.logObject(args, 'args')
mw.logObject(args, 'args')
retval = retval .. '[[Category:Location maps with possible errors|Page using removed parameter]]'
retval = retval .. '[[Category:Location maps with possible errors|Page using removed parameter]]'
end
if map('skew') ~= '' or map('lat_skew') ~= '' or map('crosses180') ~= '' or map('type') ~= '' then
mw.log('Removed parameter used in map definition ' .. map())
retval = retval .. '[[Category:Location maps with possible errors|Map using removed parameter]]'
end
if string.find(map('name'), '|', 1, true) then
mw.log('Pipe used in name of map definition ' .. map())
retval = retval .. '[[Category:Location maps with possible errors|Name containing pipe]]'
end
end
if args.float == 'center' then
if args.float == 'center' then
Line 266: Line 234:
local function markOuterDiv(x, y, imageDiv, labelDiv)
local function markOuterDiv(x, y, imageDiv, labelDiv)
return mw.html.create('div')
return mw.html.create('div')
:cssText('position:absolute;top:' .. round(y, 3) .. '%;left:' .. round(x, 3) .. '%')
:cssText('position:absolute;top:' .. round(y, 3) .. '%;left:' .. round(x, 3) .. '%;height:0;width:0;margin:0;padding:0')
:node(imageDiv)
:node(imageDiv)
:node(labelDiv)
:node(labelDiv)
Line 273: Line 241:
local function markImageDiv(mark, marksize, label, link, alt, title)
local function markImageDiv(mark, marksize, label, link, alt, title)
local builder = mw.html.create('div')
local builder = mw.html.create('div')
:cssText('position:absolute;left:-' .. round(marksize / 2) .. 'px;top:-' .. round(marksize / 2) .. 'px;line-height:0')
:cssText('position:absolute;text-align:center;left:-' .. round(marksize / 2) .. 'px;top:-' .. round(marksize / 2) .. 'px;width:' .. marksize .. 'px;font-size:' .. marksize .. 'px;line-height:0')
:attr('title', title)
:attr('title', title)
if marksize ~= 0 then
if marksize ~= 0 then
Line 290: Line 258:


local function markLabelDiv(label, label_size, label_width, position, background, x, marksize)
local function markLabelDiv(label, label_size, label_width, position, background, x, marksize)
if tonumber(label_size) == 0 then
return mw.html.create('div'):cssText('font-size:0%;position:absolute'):wikitext(label)
end
local builder = mw.html.create('div')
local builder = mw.html.create('div')
:cssText('font-size:' .. label_size .. '%;line-height:110%;position:absolute;width:' .. label_width .. 'em')
:cssText('font-size:' .. label_size .. '%;line-height:110%;position:absolute;width:' .. label_width .. 'em')
Line 345: Line 310:
longitude = decdeg(args.lon_deg, args.lon_min, args.lon_sec, args.lon_dir, args.long, 'longitude')
longitude = decdeg(args.lon_deg, args.lon_min, args.lon_sec, args.lon_dir, args.long, 'longitude')
latitude = decdeg(args.lat_deg, args.lat_min, args.lat_sec, args.lat_dir, args.lat, 'latitude')
latitude = decdeg(args.lat_deg, args.lat_min, args.lat_sec, args.lat_dir, args.lat, 'latitude')
if args.coordinates then
if not longitude and not latitude and args.useWikidata then
-- Temporarily removed to facilitate infobox conversion. See [[Wikipedia:Coordinates in infoboxes]]

-- if longitude or latitude then
-- error('Coordinates from [[Module:Coordinates]] and individual coordinates cannot both be provided')
-- end
longitude = coord2text('longitude', args.coordinates)
latitude = coord2text('latitude', args.coordinates)
elseif not longitude and not latitude and args.useWikidata then
-- If they didn't provide either coordinate, try Wikidata. If they provided one but not the other, don't.
-- If they didn't provide either coordinate, try Wikidata. If they provided one but not the other, don't.
local entity = mw.wikibase.getEntity()
local entity = mw.wikibase.getEntity()
if entity and entity.claims and entity.claims.P625 and entity.claims.P625[1].mainsnak.snaktype == 'value' then
if entity and entity.claims and entity.claims.p625 and entity.claims.p625[0].mainsnak.snaktype == 'value' then
local value = entity.claims.P625[1].mainsnak.datavalue.value
local value = entity.claims.p625[0].mainsnak.datavalue.value
longitude, latitude = value.longitude, value.latitude
longitude, latitude = value.longitude, value.latitude
end
end
Line 363: Line 320:
if not longitude then
if not longitude then
error('No value was provided for longitude')
error('No value was provided for longitude')
end
elseif not latitude then
if not latitude then
error('No value was provided for latitude')
error('No value was provided for latitude')
end
end
local builder = mw.html.create()
local builder = mw.html.create()
if (not args.lon_deg) ~= (not args.lat_deg) then
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Degrees]]')
elseif (not args.lon_min) ~= (not args.lat_min) then
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Minutes]]')
elseif (not args.lon_sec) ~= (not args.lat_sec) then
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Seconds]]')
elseif (not args.lon_dir) ~= (not args.lat_dir) then
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Hemisphere]]')
elseif (not args.long) ~= (not args.lat) then
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Decimal]]')
end
if args.skew or args.lon_shift or args.markhigh then
if args.skew or args.lon_shift or args.markhigh then
mw.log('Removed parameter used in invocation.')
mw.log('Removed parameter used in invocation.')
Line 386: Line 333:
mw.logObject(args, 'args')
mw.logObject(args, 'args')
builder:wikitext('[[Category:Location maps with possible errors|Page using removed parameter]]')
builder:wikitext('[[Category:Location maps with possible errors|Page using removed parameter]]')
end
if map('skew') ~= '' or map('lat_skew') ~= '' or map('crosses180') ~= '' or map('type') ~= '' then
mw.log('Removed parameter used in map definition ' .. map())
builder:wikitext('[[Category:Location maps with possible errors|Map using removed parameter]]')
end
end
if map('x') ~= '' then
if map('x') ~= '' then
Line 417: Line 368:
end
end
return builder:node(markOuterDiv(x, y, imageDiv, labelDiv))
return builder:node(markOuterDiv(x, y, imageDiv, labelDiv))
end

local function switcherSeparate(s)
if s == nil then return {} end
local retval = {}
for i in string.gmatch(s .. '#', '([^#]*)#') do
i = mw.text.trim(i)
retval[#retval + 1] = (i ~= '' and i)
end
return retval
end
end


Line 448: Line 389:
end
end
if type(map) == 'table' then
if type(map) == 'table' then
local altmaps = switcherSeparate(args.AlternativeMap)
if #altmaps > #map then
error(string.format('%d AlternativeMaps were provided, but only %d maps were provided', #altmaps, #map))
end
local overlays = switcherSeparate(args.overlay_image)
if #overlays > #map then
error(string.format('%d overlay_images were provided, but only %d maps were provided', #overlays, #map))
end
local outputs = {}
local outputs = {}
args.autoSwitcherLabel = true
args.autoSwitcherLabel = true
for k,v in ipairs(map) do
for k,v in ipairs(map) do
args.AlternativeMap = altmaps[k]
args.overlay_image = overlays[k]
outputs[k] = p.main(frame, args, v)
outputs[k] = p.main(frame, args, v)
end
end