No edit summary
No edit summary
Line 24: Line 24:
function util.noop(info)
function util.noop(info)
local DEFAULT_NOOP_OUTPUT = ""
local DEFAULT_NOOP_OUTPUT = ""
-- Uncomment this when debugging
-- Uncomment this when debugging
-- DEFAULT_NOOP_OUTPUT = "debug: mapframe no-op: " .. info
-- DEFAULT_NOOP_OUTPUT = "debug: mapframe no-op: " .. info
-- mw.log(DEFAULT_NOOP_OUTPUT)
-- mw.log(DEFAULT_NOOP_OUTPUT)
return DEFAULT_NOOP_OUTPUT
return DEFAULT_NOOP_OUTPUT
end
end
Line 35: Line 33:
function util.trimArgs(argsTable)
function util.trimArgs(argsTable)
local cleanArgs = {}
local cleanArgs = {}
for key, val in pairs(argsTable) do
for key, val in pairs(argsTable) do
if type(val) == 'string' then
if type(val) == 'string' then
val = val:match('^%s*(.-)%s*$')
val = val:match('^%s*(.-)%s*$')
if val ~= '' then
if val ~= '' then
cleanArgs[key] = val
cleanArgs[key] = val
Line 47: Line 43:
end
end
end
end
return cleanArgs
return cleanArgs
end
end
Line 54: Line 49:
-- Check if should be running
-- Check if should be running
local pargs = frame.getParent(frame).args
local pargs = frame.getParent(frame).args
local explicitlyOn = yesno(
local explicitlyOn = yesno(
mw.text.trim(pargs.mapframe or "")
mw.text.trim(pargs.mapframe or "")
)
)
if pargs.coordinates == "{{{coordinates}}}" then
if pargs.coordinates == "{{{coordinates}}}" then
explicitlyOn = false
explicitlyOn = false
end
end
if type(pargs.coordinates) == 'string'
if type(pargs.coordinates) == 'string'
and string.match(pargs.coordinates, 'coord.missing') then
and string.match(pargs.coordinates, 'coord.missing') then
explicitlyOn = false
explicitlyOn = false
end
end
local onByDefault = (explicitlyOn == nil)
local onByDefault = (explicitlyOn == nil)
and yesno(
and yesno(
Line 73: Line 64:
false
false
)
)
return explicitlyOn or onByDefault
return explicitlyOn or onByDefault
end
end
Line 84: Line 74:
parentFirst = true
parentFirst = true
})
})
-- Discard args not prefixed with "mapframe-", remove that prefix
-- Discard args not prefixed with "mapframe-", remove that prefix
-- from those that remain.
-- from those that remain.
local fixedArgs = {}
local fixedArgs = {}
for name, val in pairs(args) do
for name, val in pairs(args) do
local fixedName = string.match(
local fixedName = string.match(
Line 94: Line 82:
"^mapframe%-(.+)$"
"^mapframe%-(.+)$"
)
)
if fixedName then
if fixedName then
fixedArgs[fixedName] = val
fixedArgs[fixedName] = val
-- Allow coord, coordinates, etc. to be unprefixed.
-- Allow coord, coordinates, etc. to be unprefixed.
elseif name == "coordinates"
elseif (name == "coordinates"
or name == "coord"
or name == "coord"
or name == "coordinate"
or name == "coordinate")
and not fixedArgs.coord then
and not fixedArgs.coord then
fixedArgs.coord = val
fixedArgs.coord = val
 
if not fixedArgs['frame-coord'] then
fixedArgs['frame-coord'] = val
end
-- Allow id/qid to be unprefixed.
-- Allow id/qid to be unprefixed.
-- Note: Bharatpedia does not resolve these through Wikibase.
-- Note: Bharatpedia does not resolve these through Wikibase.
elseif name == "id"
elseif (name == "id" or name == "qid")
or name == "qid"
and not fixedArgs.id then
and not fixedArgs.id then
fixedArgs.id = val
fixedArgs.id = val
-- Allow captionstyle to be unprefixed,
-- Allow captionstyle to be unprefixed,
-- for compatibility with Module:Infobox.
-- for compatibility with Module:Infobox.
elseif name == "captionstyle"
elseif name == "captionstyle"
and not fixedArgs.captionstyle then
and not fixedArgs.captionstyle then
fixedArgs.captionstyle = val
fixedArgs.captionstyle = val
end
end
end
end
 
-- Parent-frame fallback: Module:Infobox sometimes expands this invoke
-- in a context where named article parameters are only on the parent.
if not fixedArgs.coord then
local parent = frame:getParent()
if parent and parent.args then
local raw = parent.args.coordinates
or parent.args.coord
or parent.args.coordinate
or parent.args['mapframe-coord']
or parent.args['mapframe-coordinates']
if type(raw) == 'string' then
raw = mw.text.trim(raw)
if raw ~= '' then
fixedArgs.coord = raw
fixedArgs['frame-coord'] = fixedArgs['frame-coord'] or raw
end
end
end
end
return fixedArgs
return fixedArgs
end
end
Line 138: Line 139:
function util.trackAndWarn(trackingCat, warning)
function util.trackAndWarn(trackingCat, warning)
local title = mw.title.getCurrentTitle()
local title = mw.title.getCurrentTitle()
local results =
local results =
title
title
Line 145: Line 145:
and '[[Category:' .. trackingCat .. ']]'
and '[[Category:' .. trackingCat .. ']]'
or ''
or ''
if warning then
if warning then
local warn = require('Module:If preview')._warning
local warn = require('Module:If preview')._warning
results = results .. warn({warning})
results = results .. warn({warning})
end
end
return results
return results
end
end
Line 156: Line 154:
function util.ternary(flag, other)
function util.ternary(flag, other)
other = other or 'other'
other = other or 'other'
flag = flag == 'none' and 'no' or flag
flag = flag == 'none' and 'no' or flag
local yesNoOut = yesno(flag, other)
local yesNoOut = yesno(flag, other)
local yes = (yesNoOut == true)
local yes = (yesNoOut == true)
local no = (yesNoOut == false)
local no = (yesNoOut == false)
return yes, no
return yes, no
end
end
Line 175: Line 169:
return util.noop("no caption or switcher")
return util.noop("no caption or switcher")
end
end
-- Bharatpedia does not have Wikidata labels to generate
-- Bharatpedia does not have Wikidata labels to generate
-- automatic "Location in ..." captions.
-- automatic "Location in ..." captions.
Line 185: Line 178:
-- All map positioning must therefore come from explicit
-- All map positioning must therefore come from explicit
-- template parameters.
-- template parameters.
local tracking = ''
local tracking = ''
-- `config` is the args passed to this module.
-- `config` is the args passed to this module.
local config = util.trimArgs(_config)
local config = util.trimArgs(_config)
-- Allow aliases for coordinates.
-- Allow aliases for coordinates.
config.coord = config.coord or config.coordinates
config.coord = config.coord or config.coordinates or config.coordinate
 
if config.coord and not config['frame-coord'] and not config['frame-coordinates'] then
config['frame-coord'] = config.coord
end
----------------------------------------------------------------------
----------------------------------------------------------------------
-- COORDINATES
-- COORDINATES
Line 200: Line 192:
-- coordinates from Wikidata.
-- coordinates from Wikidata.
----------------------------------------------------------------------
----------------------------------------------------------------------
if not config.coord
if not config.coord
and not (
and not (
Line 214: Line 205:
and config['frame-longitude']
and config['frame-longitude']
) then
) then
return false, util.trackAndWarn(
return false, util.trackAndWarn(
'Pages using infobox mapframe with missing coordinates'
'Pages using infobox mapframe with missing coordinates'
)
)
end
end
----------------------------------------------------------------------
----------------------------------------------------------------------
-- MAPFRAME ARGUMENTS
-- MAPFRAME ARGUMENTS
----------------------------------------------------------------------
----------------------------------------------------------------------
local args = {}
local args = {}
-- Defaults/overrides for infobox presentation.
-- Defaults/overrides for infobox presentation.
args.display = "inline"
args.display = "inline"
args.frame = "yes"
args.frame = "yes"
args.plain = "yes"
args.plain = "yes"
args["frame-width"] =
args["frame-width"] =
config["frame-width"]
config["frame-width"]
or config.width
or config.width
or DEFAULT_FRAME_WIDTH
or DEFAULT_FRAME_WIDTH
args["frame-height"] =
args["frame-height"] =
config["frame-height"]
config["frame-height"]
or config.height
or config.height
or DEFAULT_FRAME_HEIGHT
or DEFAULT_FRAME_HEIGHT
args["frame-align"] = "center"
args["frame-align"] = "center"
args["frame-coord"] =
args["frame-coord"] =
config["frame-coordinates"]
config["frame-coordinates"]
or config["frame-coord"]
or config["frame-coord"]
or config.coord
or config.coord
-- Deprecated latitude/longitude parameters.
-- Deprecated latitude/longitude parameters.
args["frame-lat"] =
args["frame-lat"] =
config["frame-lat"]
config["frame-lat"]
or config["frame-latitude"]
or config["frame-latitude"]
args["frame-long"] =
args["frame-long"] =
config["frame-long"]
config["frame-long"]
or config["frame-longitude"]
or config["frame-longitude"]
----------------------------------------------------------------------
----------------------------------------------------------------------
-- ZOOM
-- ZOOM
Line 264: Line 244:
-- No Wikidata lookup is performed.
-- No Wikidata lookup is performed.
----------------------------------------------------------------------
----------------------------------------------------------------------
local zoom = config.zoom
local zoom = config.zoom
if not zoom then
if not zoom then
local getZoom = require('Module:Infobox dim')._zoom
local getZoom = require('Module:Infobox dim')._zoom
zoom = getZoom({
zoom = getZoom({
length_km = config.length_km,
length_km = config.length_km,
length_mi = config.length_mi,
length_mi = config.length_mi,
width_km = config.width_km,
width_km = config.width_km,
width_mi = config.width_mi,
width_mi = config.width_mi,
area_km2 = config.area_km2,
area_km2 = config.area_km2,
area_mi2 = config.area_mi2,
area_mi2 = config.area_mi2,
area_ha = config.area_ha,
area_ha = config.area_ha,
area_acre = config.area_acre,
area_acre = config.area_acre,
type = config.type,
type = config.type,
population = config.population,
population = config.population,
viewport_px = math.min(
viewport_px = math.min(
tonumber(args["frame-width"])
tonumber(args["frame-width"])
or tonumber(DEFAULT_FRAME_WIDTH),
or tonumber(DEFAULT_FRAME_WIDTH),
tonumber(args["frame-height"])
tonumber(args["frame-height"])
or tonumber(DEFAULT_FRAME_HEIGHT)
or tonumber(DEFAULT_FRAME_HEIGHT)
Line 295: Line 266:
})
})
end
end
args.zoom = zoom or DEFAULT_ZOOM
args.zoom = zoom or DEFAULT_ZOOM
----------------------------------------------------------------------
----------------------------------------------------------------------
-- MARKER
-- MARKER
----------------------------------------------------------------------
----------------------------------------------------------------------
local forcePoint, suppressPoint =
local forcePoint, suppressPoint =
util.ternary(config.point)
util.ternary(config.point)
local forceMarker, suppressMarker =
local forceMarker, suppressMarker =
util.ternary(config.marker, true)
util.ternary(config.marker, true)
forcePoint = forcePoint or forceMarker
forcePoint = forcePoint or forceMarker
suppressPoint = suppressPoint or suppressMarker
suppressPoint = suppressPoint or suppressMarker
-- With explicit coordinates, show a point marker by default
-- With explicit coordinates, show a point marker by default
-- unless it is explicitly suppressed.
-- unless it is explicitly suppressed.
Line 319: Line 284:
or config.coord ~= nil
or config.coord ~= nil
)
)
----------------------------------------------------------------------
----------------------------------------------------------------------
-- SHAPE / LINE
-- SHAPE / LINE
Line 326: Line 290:
-- Bharatpedia requires them to be explicitly requested.
-- Bharatpedia requires them to be explicitly requested.
----------------------------------------------------------------------
----------------------------------------------------------------------
local showShape = false
local showShape = false
local showLine = false
local showLine = false
local forceShape, suppressShape =
local forceShape, suppressShape =
util.ternary(config.shape)
util.ternary(config.shape)
if forceShape then
if forceShape then
showShape = true
showShape = true
Line 338: Line 299:
showShape = false
showShape = false
end
end
local forceLine, suppressLine =
local forceLine, suppressLine =
util.ternary(config.line)
util.ternary(config.line)
if forceLine then
if forceLine then
showLine = true
showLine = true
Line 347: Line 306:
showLine = false
showLine = false
end
end
local shapeType =
local shapeType =
config.shape == 'inverse'
config.shape == 'inverse'
and 'shape-inverse'
and 'shape-inverse'
or 'shape'
or 'shape'
----------------------------------------------------------------------
----------------------------------------------------------------------
-- GEOMASK / SWITCHER
-- GEOMASK / SWITCHER
----------------------------------------------------------------------
----------------------------------------------------------------------
local maskItem
local maskItem
if config.switcher == "zooms" then
if config.switcher == "zooms" then
-- Switch between zoom levels.
-- Switch between zoom levels.
local maxZoom =
local maxZoom =
Line 367: Line 321:
3
3
)
)
local minZoom = 1
local minZoom = 1
local midZoom =
local midZoom =
math.floor(
math.floor(
(maxZoom + minZoom) / 2
(maxZoom + minZoom) / 2
)
)
args.switch =
args.switch =
"zoomed in, zoomed midway, zoomed out"
"zoomed in, zoomed midway, zoomed out"
args.zoom =
args.zoom =
string.format(
string.format(
Line 385: Line 335:
minZoom
minZoom
)
)
elseif config.switcher == "auto" then
elseif config.switcher == "auto" then
-- The Wikimedia version used Wikidata P276/P131
-- The Wikimedia version used Wikidata P276/P131
-- to determine this automatically.
-- to determine this automatically.
Line 393: Line 341:
-- Bharatpedia has no such dependency, so this mode
-- Bharatpedia has no such dependency, so this mode
-- is intentionally disabled.
-- is intentionally disabled.
return false, util.trackAndWarn(
return false, util.trackAndWarn(
'Pages using infobox mapframe with unsupported automatic switcher'
'Pages using infobox mapframe with unsupported automatic switcher'
)
)
elseif config.switcher == "geomasks"
elseif config.switcher == "geomasks"
and config.geomask then
and config.geomask then
-- Explicit geomask IDs are supported.
-- Explicit geomask IDs are supported.
args.zoom = nil
args.zoom = nil
local separator =
local separator =
(
(
Line 425: Line 369:
)
)
or ","
or ","
local pattern =
local pattern =
"%s*" .. separator .. "%s*"
"%s*" .. separator .. "%s*"
local maskItems =
local maskItems =
mw.text.split(
mw.text.split(
Line 438: Line 380:
pattern
pattern
)
)
if #maskItems > 1 then
if #maskItems > 1 then
-- No external labels are available,
-- No external labels are available,
-- so use the supplied IDs as switch labels.
-- so use the supplied IDs as switch labels.
Line 448: Line 388:
"###"
"###"
)
)
maskItem =
maskItem =
"SWITCH:"
"SWITCH:"
Line 455: Line 394:
","
","
)
)
elseif #maskItems == 1 then
elseif #maskItems == 1 then
maskItem = maskItems[1]
maskItem = maskItems[1]
end
end
end
end
----------------------------------------------------------------------
----------------------------------------------------------------------
-- EXPLICIT GEOMASK
-- EXPLICIT GEOMASK
Line 467: Line 404:
-- they require an entity database.
-- they require an entity database.
----------------------------------------------------------------------
----------------------------------------------------------------------
if not maskItem and config.geomask then
if not maskItem and config.geomask then
local geomask =
local geomask =
mw.text.trim(
mw.text.trim(
config.geomask
config.geomask
)
)
if geomask ~= ''
if geomask ~= ''
and not mw.ustring.match(
and not mw.ustring.match(
Line 480: Line 414:
"^[Pp]%d+$"
"^[Pp]%d+$"
) then
) then
maskItem = geomask
maskItem = geomask
end
end
end
end
----------------------------------------------------------------------
----------------------------------------------------------------------
-- ENSURE SOMETHING CAN BE DRAWN
-- ENSURE SOMETHING CAN BE DRAWN
----------------------------------------------------------------------
----------------------------------------------------------------------
if not maskItem
if not maskItem
and not showShape
and not showShape
and not showLine
and not showLine
and not showMarker then
and not showMarker then
return false, util.trackAndWarn(
return false, util.trackAndWarn(
'Pages using infobox mapframe with no geometry',
'Pages using infobox mapframe with no geometry',
Line 499: Line 429:
)
)
end
end
----------------------------------------------------------------------
----------------------------------------------------------------------
-- ARGUMENT NUMBERING
-- ARGUMENT NUMBERING
----------------------------------------------------------------------
----------------------------------------------------------------------
local argNumber = ''
local argNumber = ''
local function incrementArgNumber()
local function incrementArgNumber()
if argNumber == '' then
if argNumber == '' then
argNumber = 2
argNumber = 2
Line 513: Line 439:
argNumber = argNumber + 1
argNumber = argNumber + 1
end
end
end
end
----------------------------------------------------------------------
----------------------------------------------------------------------
-- GEOMASK
-- GEOMASK
----------------------------------------------------------------------
----------------------------------------------------------------------
if maskItem then
if maskItem then
args["type" .. argNumber] =
args["type" .. argNumber] =
"shape-inverse"
"shape-inverse"
args["id" .. argNumber] =
args["id" .. argNumber] =
maskItem
maskItem
args["stroke-width" .. argNumber] =
args["stroke-width" .. argNumber] =
config["geomask-stroke-width"]
config["geomask-stroke-width"]
or DEFAULT_GEOMASK_STROKE_WIDTH
or DEFAULT_GEOMASK_STROKE_WIDTH
args["stroke-color" .. argNumber] =
args["stroke-color" .. argNumber] =
config["geomask-stroke-color"]
config["geomask-stroke-color"]
or config["geomask-stroke-colour"]
or config["geomask-stroke-colour"]
or DEFAULT_GEOMASK_STROKE_COLOR
or DEFAULT_GEOMASK_STROKE_COLOR
args["fill" .. argNumber] =
args["fill" .. argNumber] =
config["geomask-fill"]
config["geomask-fill"]
or DEFAULT_GEOMASK_FILL
or DEFAULT_GEOMASK_FILL
args["fill-opacity" .. argNumber] =
args["fill-opacity" .. argNumber] =
config["geomask-fill-opacity"]
config["geomask-fill-opacity"]
or DEFAULT_SHAPE_FILL_OPACITY
or DEFAULT_SHAPE_FILL_OPACITY
-- Let Kartographer determine zoom and position
-- Let Kartographer determine zoom and position
-- unless explicitly configured.
-- unless explicitly configured.
if not config.zoom
if not config.zoom
and not config.switcher then
and not config.switcher then
args.zoom = nil
args.zoom = nil
args["frame-coord"] = nil
args["frame-coord"] = nil
Line 555: Line 470:
args["frame-long"] = nil
args["frame-long"] = nil
end
end
incrementArgNumber()
incrementArgNumber()
-- Hack retained from the original module.
-- Hack retained from the original module.
if not args.zoom then
if not args.zoom then
args["type" .. argNumber] =
args["type" .. argNumber] =
"line"
"line"
args["id" .. argNumber] =
args["id" .. argNumber] =
maskItem
maskItem
args["stroke-width" .. argNumber] =
args["stroke-width" .. argNumber] =
0
0
incrementArgNumber()
incrementArgNumber()
end
end
end
end
----------------------------------------------------------------------
----------------------------------------------------------------------
-- SHAPE
-- SHAPE
----------------------------------------------------------------------
----------------------------------------------------------------------
if showShape then
if showShape then
args["type" .. argNumber] =
args["type" .. argNumber] =
shapeType
shapeType
-- Explicit ID only.
-- Explicit ID only.
if config.id then
if config.id then
args["id" .. argNumber] =
args["id" .. argNumber] =
config.id
config.id
elseif config.from then
elseif config.from then
args["from" .. argNumber] =
args["from" .. argNumber] =
config.from
config.from
end
end
args["stroke-width" .. argNumber] =
args["stroke-width" .. argNumber] =
config["shape-stroke-width"]
config["shape-stroke-width"]
or config["stroke-width"]
or config["stroke-width"]
or DEFAULT_SHAPE_STROKE_WIDTH
or DEFAULT_SHAPE_STROKE_WIDTH
args["stroke-color" .. argNumber] =
args["stroke-color" .. argNumber] =
config["shape-stroke-color"]
config["shape-stroke-color"]
Line 604: Line 506:
or config["stroke-colour"]
or config["stroke-colour"]
or DEFAULT_SHAPE_STROKE_COLOR
or DEFAULT_SHAPE_STROKE_COLOR
args["fill" .. argNumber] =
args["fill" .. argNumber] =
config["shape-fill"]
config["shape-fill"]
or DEFAULT_SHAPE_FILL
or DEFAULT_SHAPE_FILL
args["fill-opacity" .. argNumber] =
args["fill-opacity" .. argNumber] =
config["shape-fill-opacity"]
config["shape-fill-opacity"]
or DEFAULT_SHAPE_FILL_OPACITY
or DEFAULT_SHAPE_FILL_OPACITY
incrementArgNumber()
incrementArgNumber()
end
end
----------------------------------------------------------------------
----------------------------------------------------------------------
-- LINE
-- LINE
----------------------------------------------------------------------
----------------------------------------------------------------------
if showLine then
if showLine then
args["type" .. argNumber] =
args["type" .. argNumber] =
"line"
"line"
-- Explicit ID only.
-- Explicit ID only.
if config.id then
if config.id then
args["id" .. argNumber] =
args["id" .. argNumber] =
config.id
config.id
elseif config.from then
elseif config.from then
args["from" .. argNumber] =
args["from" .. argNumber] =
config.from
config.from
end
end
args["stroke-width" .. argNumber] =
args["stroke-width" .. argNumber] =
config["line-stroke-width"]
config["line-stroke-width"]
or config["stroke-width"]
or config["stroke-width"]
or DEFAULT_LINE_STROKE_WIDTH
or DEFAULT_LINE_STROKE_WIDTH
args["stroke-color" .. argNumber] =
args["stroke-color" .. argNumber] =
config["line-stroke-color"]
config["line-stroke-color"]
Line 646: Line 538:
or config["stroke-colour"]
or config["stroke-colour"]
or DEFAULT_LINE_STROKE_COLOR
or DEFAULT_LINE_STROKE_COLOR
incrementArgNumber()
incrementArgNumber()
end
end
----------------------------------------------------------------------
----------------------------------------------------------------------
-- POINT MARKER
-- POINT MARKER
----------------------------------------------------------------------
----------------------------------------------------------------------
if showMarker then
if showMarker then
args["type" .. argNumber] =
args["type" .. argNumber] =
"point"
"point"
if config.id then
if config.id then
args["id" .. argNumber] =
args["id" .. argNumber] =
config.id
config.id
end
end
if config.coord then
if config.coord then
args["coord" .. argNumber] =
args["coord" .. argNumber] =
config.coord
config.coord
end
end
if config.marker then
if config.marker then
args["marker" .. argNumber] =
args["marker" .. argNumber] =
config.marker
config.marker
end
end
args["marker-color" .. argNumber] =
args["marker-color" .. argNumber] =
config["marker-color"]
config["marker-color"]
or config["marker-colour"]
or config["marker-colour"]
or DEFAULT_MARKER_COLOR
or DEFAULT_MARKER_COLOR
incrementArgNumber()
incrementArgNumber()
end
end
----------------------------------------------------------------------
----------------------------------------------------------------------
-- FRAME CENTERING
-- FRAME CENTERING
----------------------------------------------------------------------
----------------------------------------------------------------------
if not maskItem
if not maskItem
and not showShape
and not showShape
and not showLine
and not showLine
and showMarker then
and showMarker then
if config.coord then
if config.coord then
args["frame-coord"] =
args["frame-coord"] =
args["frame-coord"]
args["frame-coord"]
or config.coord
or config.coord
end
end
end
end
----------------------------------------------------------------------
----------------------------------------------------------------------
-- PROTECT AGAINST NIL FRAME ARGUMENTS
-- Do not coerce missing frame arguments to empty strings.
-- An empty string is truthy in Lua and makes Module:Mapframe throw
-- "Coordinates must be specified in |coord=...".
----------------------------------------------------------------------
----------------------------------------------------------------------
args["frame-coord"] =
args["frame-coord"] or ""
args["frame-lat"] =
args["frame-lat"] or ""
args["frame-long"] =
args["frame-long"] or ""
----------------------------------------------------------------------
----------------------------------------------------------------------
-- RENDER MAP
-- RENDER MAP
----------------------------------------------------------------------
----------------------------------------------------------------------
local mapframe =
local mapframe =
args.switch
args.switch
and mf.multi(args)
and mf.multi(args)
or mf._main(args)
or mf._main(args)
return true, mapframe .. tracking
return true, mapframe .. tracking
end
end
Line 728: Line 595:
-- ENTRY POINTS
-- ENTRY POINTS
----------------------------------------------------------------------
----------------------------------------------------------------------
p.main = function(frame)
p.main = function(frame)
local parent =
local parent =
frame.getParent(frame)
frame.getParent(frame)
local parentArgs =
local parentArgs =
parent.args
parent.args
local _, mapframe =
local _, mapframe =
p._main(parentArgs)
p._main(parentArgs)
return frame:preprocess(mapframe)
return frame:preprocess(mapframe)
end
end


p.auto = function(frame)
p.auto = function(frame)
if not util.shouldAutoRun(frame) then
if not util.shouldAutoRun(frame) then
return util.noop(
return util.noop(
Line 750: Line 611:
)
)
end
end
local args =
local args =
util.argsFromAuto(frame)
util.argsFromAuto(frame)
if args.custom then
if args.custom then
return frame:preprocess(
return frame:preprocess(
Line 761: Line 620:
)
)
end
end
local _, mapframe =
local _, mapframe =
p._main(args)
p._main(args)
return frame:preprocess(mapframe)
return frame:preprocess(mapframe)
end
end


p.autocaption = function(frame)
p.autocaption = function(frame)
if not util.shouldAutoRun(frame) then
if not util.shouldAutoRun(frame) then
return util.noop(
return util.noop(
Line 775: Line 631:
)
)
end
end
local args =
local args =
util.argsFromAuto(frame)
util.argsFromAuto(frame)
local caption =
local caption =
p._caption(args)
p._caption(args)
return caption
return caption
end
end


p.autoWithCaption = function(frame)
p.autoWithCaption = function(frame)
if not util.shouldAutoRun(frame) then
if not util.shouldAutoRun(frame) then
return util.noop(
return util.noop(
Line 792: Line 644:
)
)
end
end
local args =
local args =
util.argsFromAuto(frame)
util.argsFromAuto(frame)
local wikitext
local wikitext
local caption
local caption
local ok
local ok
if args.custom then
if args.custom then
ok = true
ok = true
wikitext =
wikitext =
util.parseCustomWikitext(
util.parseCustomWikitext(
args.custom
args.custom
)
)
else
else
ok, wikitext =
ok, wikitext =
p._main(args)
p._main(args)
end
end
if not ok then
if not ok then
return wikitext
return wikitext
end
end
wikitext =
wikitext =
frame:preprocess(
frame:preprocess(
wikitext
wikitext
)
)
caption =
caption =
p._caption(args)
p._caption(args)
local data =
local data =
mw.html.create()
mw.html.create()
:wikitext(wikitext)
:wikitext(wikitext)
data:tag('div')
data:tag('div')
:addClass('infobox-caption')
:addClass('infobox-caption')
:cssText(args.captionstyle)
:cssText(args.captionstyle)
:wikitext(caption)
:wikitext(caption)
return tostring(data)
return tostring(data)
end
end


return p
return p

Revision as of 21:36, 28 September 2026

Documentation for this module may be created at Module:Infobox mapframe/doc

local mf = require('Module:Mapframe')
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local infoboxImage = require('Module:InfoboxImage').InfoboxImage

-- Defaults
local DEFAULT_FRAME_WIDTH = "270"
local DEFAULT_FRAME_HEIGHT = "200"
local DEFAULT_ZOOM = 10
local DEFAULT_GEOMASK_STROKE_WIDTH = "1"
local DEFAULT_GEOMASK_STROKE_COLOR = "#777777"
local DEFAULT_GEOMASK_FILL = "#888888"
local DEFAULT_GEOMASK_FILL_OPACITY = "0.25"
local DEFAULT_SHAPE_STROKE_WIDTH = "2"
local DEFAULT_SHAPE_STROKE_COLOR = "#FF0000"
local DEFAULT_SHAPE_FILL = "#606060"
local DEFAULT_SHAPE_FILL_OPACITY = "0.1"
local DEFAULT_LINE_STROKE_WIDTH = "5"
local DEFAULT_LINE_STROKE_COLOR = "#FF0000"
local DEFAULT_MARKER_COLOR = "#5E74F3"

local util = {}

function util.noop(info)
	local DEFAULT_NOOP_OUTPUT = ""
	-- Uncomment this when debugging
	-- DEFAULT_NOOP_OUTPUT = "debug: mapframe no-op: " .. info
	-- mw.log(DEFAULT_NOOP_OUTPUT)
	return DEFAULT_NOOP_OUTPUT
end

-- Trim whitespace from args, and remove empty args
function util.trimArgs(argsTable)
	local cleanArgs = {}
	for key, val in pairs(argsTable) do
		if type(val) == 'string' then
			val = val:match('^%s*(.-)%s*$')
			if val ~= '' then
				cleanArgs[key] = val
			end
		else
			cleanArgs[key] = val
		end
	end
	return cleanArgs
end

function util.shouldAutoRun(frame)
	-- Check if should be running
	local pargs = frame.getParent(frame).args
	local explicitlyOn = yesno(
		mw.text.trim(pargs.mapframe or "")
	)
	if pargs.coordinates == "{{{coordinates}}}" then
		explicitlyOn = false
	end
	if type(pargs.coordinates) == 'string'
		and string.match(pargs.coordinates, 'coord.missing') then
		explicitlyOn = false
	end
	local onByDefault = (explicitlyOn == nil)
		and yesno(
			mw.text.trim(frame.args.onByDefault or ""),
			false
		)
	return explicitlyOn or onByDefault
end

function util.argsFromAuto(frame)
	-- Get args from the frame (invoke call) and the parent (template call).
	-- Frame arguments are default values which are overridden by parent values
	-- when both are present.
	local args = getArgs(frame, {
		parentFirst = true
	})
	-- Discard args not prefixed with "mapframe-", remove that prefix
	-- from those that remain.
	local fixedArgs = {}
	for name, val in pairs(args) do
		local fixedName = string.match(
			name,
			"^mapframe%-(.+)$"
		)
		if fixedName then
			fixedArgs[fixedName] = val
		-- Allow coord, coordinates, etc. to be unprefixed.
		elseif (name == "coordinates"
			or name == "coord"
			or name == "coordinate")
			and not fixedArgs.coord then
			fixedArgs.coord = val
			if not fixedArgs['frame-coord'] then
				fixedArgs['frame-coord'] = val
			end
		-- Allow id/qid to be unprefixed.
		-- Note: Bharatpedia does not resolve these through Wikibase.
		elseif (name == "id" or name == "qid")
			and not fixedArgs.id then
			fixedArgs.id = val
		-- Allow captionstyle to be unprefixed,
		-- for compatibility with Module:Infobox.
		elseif name == "captionstyle"
			and not fixedArgs.captionstyle then
			fixedArgs.captionstyle = val
		end
	end
	-- Parent-frame fallback: Module:Infobox sometimes expands this invoke
	-- in a context where named article parameters are only on the parent.
	if not fixedArgs.coord then
		local parent = frame:getParent()
		if parent and parent.args then
			local raw = parent.args.coordinates
				or parent.args.coord
				or parent.args.coordinate
				or parent.args['mapframe-coord']
				or parent.args['mapframe-coordinates']
			if type(raw) == 'string' then
				raw = mw.text.trim(raw)
				if raw ~= '' then
					fixedArgs.coord = raw
					fixedArgs['frame-coord'] = fixedArgs['frame-coord'] or raw
				end
			end
		end
	end
	return fixedArgs
end

function util.parseCustomWikitext(customWikitext)
	-- infoboxImage will format an image if given wikitext containing
	-- an image, or else pass through the wikitext unmodified.
	return infoboxImage({
		args = {
			image = customWikitext
		}
	})
end

function util.trackAndWarn(trackingCat, warning)
	local title = mw.title.getCurrentTitle()
	local results =
		title
		and title.namespace == 0
		and trackingCat
		and '[[Category:' .. trackingCat .. ']]'
		or ''
	if warning then
		local warn = require('Module:If preview')._warning
		results = results .. warn({warning})
	end
	return results
end

function util.ternary(flag, other)
	other = other or 'other'
	flag = flag == 'none' and 'no' or flag
	local yesNoOut = yesno(flag, other)
	local yes = (yesNoOut == true)
	local no = (yesNoOut == false)
	return yes, no
end

local p = {}

p._caption = function(args)
	if args.caption then
		return args.caption
	elseif args.switcher then
		return util.noop("no caption or switcher")
	end
	-- Bharatpedia does not have Wikidata labels to generate
	-- automatic "Location in ..." captions.
	return util.noop("no caption")
end

p._main = function(_config)
	-- Bharatpedia does not use an external entity database.
	-- All map positioning must therefore come from explicit
	-- template parameters.
	local tracking = ''
	-- `config` is the args passed to this module.
	local config = util.trimArgs(_config)
	-- Allow aliases for coordinates.
	config.coord = config.coord or config.coordinates or config.coordinate
	if config.coord and not config['frame-coord'] and not config['frame-coordinates'] then
		config['frame-coord'] = config.coord
	end
	----------------------------------------------------------------------
	-- COORDINATES
	--
	-- Unlike the Wikimedia version, this module does not look up
	-- coordinates from Wikidata.
	----------------------------------------------------------------------
	if not config.coord
		and not (
			config['frame-coord']
			or config['frame-coordinates']
		)
		and not (
			config['frame-lat']
			and config['frame-long']
		)
		and not (
			config['frame-latitude']
			and config['frame-longitude']
		) then
		return false, util.trackAndWarn(
			'Pages using infobox mapframe with missing coordinates'
		)
	end
	----------------------------------------------------------------------
	-- MAPFRAME ARGUMENTS
	----------------------------------------------------------------------
	local args = {}
	-- Defaults/overrides for infobox presentation.
	args.display = "inline"
	args.frame = "yes"
	args.plain = "yes"
	args["frame-width"] =
		config["frame-width"]
		or config.width
		or DEFAULT_FRAME_WIDTH
	args["frame-height"] =
		config["frame-height"]
		or config.height
		or DEFAULT_FRAME_HEIGHT
	args["frame-align"] = "center"
	args["frame-coord"] =
		config["frame-coordinates"]
		or config["frame-coord"]
		or config.coord
	-- Deprecated latitude/longitude parameters.
	args["frame-lat"] =
		config["frame-lat"]
		or config["frame-latitude"]
	args["frame-long"] =
		config["frame-long"]
		or config["frame-longitude"]
	----------------------------------------------------------------------
	-- ZOOM
	--
	-- Use an explicitly supplied zoom first.
	-- If absent, calculate one from dimensions using Infobox dim.
	-- No Wikidata lookup is performed.
	----------------------------------------------------------------------
	local zoom = config.zoom
	if not zoom then
		local getZoom = require('Module:Infobox dim')._zoom
		zoom = getZoom({
			length_km = config.length_km,
			length_mi = config.length_mi,
			width_km = config.width_km,
			width_mi = config.width_mi,
			area_km2 = config.area_km2,
			area_mi2 = config.area_mi2,
			area_ha = config.area_ha,
			area_acre = config.area_acre,
			type = config.type,
			population = config.population,
			viewport_px = math.min(
				tonumber(args["frame-width"])
					or tonumber(DEFAULT_FRAME_WIDTH),
				tonumber(args["frame-height"])
					or tonumber(DEFAULT_FRAME_HEIGHT)
			)
		})
	end
	args.zoom = zoom or DEFAULT_ZOOM
	----------------------------------------------------------------------
	-- MARKER
	----------------------------------------------------------------------
	local forcePoint, suppressPoint =
		util.ternary(config.point)
	local forceMarker, suppressMarker =
		util.ternary(config.marker, true)
	forcePoint = forcePoint or forceMarker
	suppressPoint = suppressPoint or suppressMarker
	-- With explicit coordinates, show a point marker by default
	-- unless it is explicitly suppressed.
	local showMarker =
		not suppressPoint
		and (
			forcePoint
			or config.coord ~= nil
		)
	----------------------------------------------------------------------
	-- SHAPE / LINE
	--
	-- The Wikimedia version inferred these from Wikidata geometry.
	-- Bharatpedia requires them to be explicitly requested.
	----------------------------------------------------------------------
	local showShape = false
	local showLine = false
	local forceShape, suppressShape =
		util.ternary(config.shape)
	if forceShape then
		showShape = true
	elseif suppressShape then
		showShape = false
	end
	local forceLine, suppressLine =
		util.ternary(config.line)
	if forceLine then
		showLine = true
	elseif suppressLine then
		showLine = false
	end
	local shapeType =
		config.shape == 'inverse'
		and 'shape-inverse'
		or 'shape'
	----------------------------------------------------------------------
	-- GEOMASK / SWITCHER
	----------------------------------------------------------------------
	local maskItem
	if config.switcher == "zooms" then
		-- Switch between zoom levels.
		local maxZoom =
			math.max(
				tonumber(args.zoom) or DEFAULT_ZOOM,
				3
			)
		local minZoom = 1
		local midZoom =
			math.floor(
				(maxZoom + minZoom) / 2
			)
		args.switch =
			"zoomed in, zoomed midway, zoomed out"
		args.zoom =
			string.format(
				"SWITCH:%d,%d,%d",
				maxZoom,
				midZoom,
				minZoom
			)
	elseif config.switcher == "auto" then
		-- The Wikimedia version used Wikidata P276/P131
		-- to determine this automatically.
		--
		-- Bharatpedia has no such dependency, so this mode
		-- is intentionally disabled.
		return false, util.trackAndWarn(
			'Pages using infobox mapframe with unsupported automatic switcher'
		)
	elseif config.switcher == "geomasks"
		and config.geomask then
		-- Explicit geomask IDs are supported.
		args.zoom = nil
		local separator =
			(
				mw.ustring.find(
					config.geomask,
					"###",
					0,
					true
				)
				and "###"
			)
			or
			(
				mw.ustring.find(
					config.geomask,
					";",
					0,
					true
				)
				and ";"
			)
			or ","
		local pattern =
			"%s*" .. separator .. "%s*"
		local maskItems =
			mw.text.split(
				mw.ustring.gsub(
					config.geomask,
					"SWITCH:",
					""
				),
				pattern
			)
		if #maskItems > 1 then
			-- No external labels are available,
			-- so use the supplied IDs as switch labels.
			args.switch =
				table.concat(
					maskItems,
					"###"
				)
			maskItem =
				"SWITCH:"
				.. table.concat(
					maskItems,
					","
				)
		elseif #maskItems == 1 then
			maskItem = maskItems[1]
		end
	end
	----------------------------------------------------------------------
	-- EXPLICIT GEOMASK
	--
	-- Property-based geomasks such as P131 are not supported because
	-- they require an entity database.
	----------------------------------------------------------------------
	if not maskItem and config.geomask then
		local geomask =
			mw.text.trim(
				config.geomask
			)
		if geomask ~= ''
			and not mw.ustring.match(
				geomask,
				"^[Pp]%d+$"
			) then
			maskItem = geomask
		end
	end
	----------------------------------------------------------------------
	-- ENSURE SOMETHING CAN BE DRAWN
	----------------------------------------------------------------------
	if not maskItem
		and not showShape
		and not showLine
		and not showMarker then
		return false, util.trackAndWarn(
			'Pages using infobox mapframe with no geometry',
			'No geometry specified for mapframe'
		)
	end
	----------------------------------------------------------------------
	-- ARGUMENT NUMBERING
	----------------------------------------------------------------------
	local argNumber = ''
	local function incrementArgNumber()
		if argNumber == '' then
			argNumber = 2
		else
			argNumber = argNumber + 1
		end
	end
	----------------------------------------------------------------------
	-- GEOMASK
	----------------------------------------------------------------------
	if maskItem then
		args["type" .. argNumber] =
			"shape-inverse"
		args["id" .. argNumber] =
			maskItem
		args["stroke-width" .. argNumber] =
			config["geomask-stroke-width"]
			or DEFAULT_GEOMASK_STROKE_WIDTH
		args["stroke-color" .. argNumber] =
			config["geomask-stroke-color"]
			or config["geomask-stroke-colour"]
			or DEFAULT_GEOMASK_STROKE_COLOR
		args["fill" .. argNumber] =
			config["geomask-fill"]
			or DEFAULT_GEOMASK_FILL
		args["fill-opacity" .. argNumber] =
			config["geomask-fill-opacity"]
			or DEFAULT_SHAPE_FILL_OPACITY
		-- Let Kartographer determine zoom and position
		-- unless explicitly configured.
		if not config.zoom
			and not config.switcher then
			args.zoom = nil
			args["frame-coord"] = nil
			args["frame-lat"] = nil
			args["frame-long"] = nil
		end
		incrementArgNumber()
		-- Hack retained from the original module.
		if not args.zoom then
			args["type" .. argNumber] =
				"line"
			args["id" .. argNumber] =
				maskItem
			args["stroke-width" .. argNumber] =
				0
			incrementArgNumber()
		end
	end
	----------------------------------------------------------------------
	-- SHAPE
	----------------------------------------------------------------------
	if showShape then
		args["type" .. argNumber] =
			shapeType
		-- Explicit ID only.
		if config.id then
			args["id" .. argNumber] =
				config.id
		elseif config.from then
			args["from" .. argNumber] =
				config.from
		end
		args["stroke-width" .. argNumber] =
			config["shape-stroke-width"]
			or config["stroke-width"]
			or DEFAULT_SHAPE_STROKE_WIDTH
		args["stroke-color" .. argNumber] =
			config["shape-stroke-color"]
			or config["shape-stroke-colour"]
			or config["stroke-color"]
			or config["stroke-colour"]
			or DEFAULT_SHAPE_STROKE_COLOR
		args["fill" .. argNumber] =
			config["shape-fill"]
			or DEFAULT_SHAPE_FILL
		args["fill-opacity" .. argNumber] =
			config["shape-fill-opacity"]
			or DEFAULT_SHAPE_FILL_OPACITY
		incrementArgNumber()
	end
	----------------------------------------------------------------------
	-- LINE
	----------------------------------------------------------------------
	if showLine then
		args["type" .. argNumber] =
			"line"
		-- Explicit ID only.
		if config.id then
			args["id" .. argNumber] =
				config.id
		elseif config.from then
			args["from" .. argNumber] =
				config.from
		end
		args["stroke-width" .. argNumber] =
			config["line-stroke-width"]
			or config["stroke-width"]
			or DEFAULT_LINE_STROKE_WIDTH
		args["stroke-color" .. argNumber] =
			config["line-stroke-color"]
			or config["line-stroke-colour"]
			or config["stroke-color"]
			or config["stroke-colour"]
			or DEFAULT_LINE_STROKE_COLOR
		incrementArgNumber()
	end
	----------------------------------------------------------------------
	-- POINT MARKER
	----------------------------------------------------------------------
	if showMarker then
		args["type" .. argNumber] =
			"point"
		if config.id then
			args["id" .. argNumber] =
				config.id
		end
		if config.coord then
			args["coord" .. argNumber] =
				config.coord
		end
		if config.marker then
			args["marker" .. argNumber] =
				config.marker
		end
		args["marker-color" .. argNumber] =
			config["marker-color"]
			or config["marker-colour"]
			or DEFAULT_MARKER_COLOR
		incrementArgNumber()
	end
	----------------------------------------------------------------------
	-- FRAME CENTERING
	----------------------------------------------------------------------
	if not maskItem
		and not showShape
		and not showLine
		and showMarker then
		if config.coord then
			args["frame-coord"] =
				args["frame-coord"]
				or config.coord
		end
	end
	----------------------------------------------------------------------
	-- Do not coerce missing frame arguments to empty strings.
	-- An empty string is truthy in Lua and makes Module:Mapframe throw
	-- "Coordinates must be specified in |coord=...".
	----------------------------------------------------------------------
	----------------------------------------------------------------------
	-- RENDER MAP
	----------------------------------------------------------------------
	local mapframe =
		args.switch
		and mf.multi(args)
		or mf._main(args)
	return true, mapframe .. tracking
end

----------------------------------------------------------------------
-- ENTRY POINTS
----------------------------------------------------------------------
p.main = function(frame)
	local parent =
		frame.getParent(frame)
	local parentArgs =
		parent.args
	local _, mapframe =
		p._main(parentArgs)
	return frame:preprocess(mapframe)
end

p.auto = function(frame)
	if not util.shouldAutoRun(frame) then
		return util.noop(
			"auto should not autorun"
		)
	end
	local args =
		util.argsFromAuto(frame)
	if args.custom then
		return frame:preprocess(
			util.parseCustomWikitext(
				args.custom
			)
		)
	end
	local _, mapframe =
		p._main(args)
	return frame:preprocess(mapframe)
end

p.autocaption = function(frame)
	if not util.shouldAutoRun(frame) then
		return util.noop(
			"autocaption should not autorun"
		)
	end
	local args =
		util.argsFromAuto(frame)
	local caption =
		p._caption(args)
	return caption
end

p.autoWithCaption = function(frame)
	if not util.shouldAutoRun(frame) then
		return util.noop(
			"autoWithCaption should not autorun"
		)
	end
	local args =
		util.argsFromAuto(frame)
	local wikitext
	local caption
	local ok
	if args.custom then
		ok = true
		wikitext =
			util.parseCustomWikitext(
				args.custom
			)
	else
		ok, wikitext =
			p._main(args)
	end
	if not ok then
		return wikitext
	end
	wikitext =
		frame:preprocess(
			wikitext
		)
	caption =
		p._caption(args)
	local data =
		mw.html.create()
			:wikitext(wikitext)
	data:tag('div')
		:addClass('infobox-caption')
		:cssText(args.captionstyle)
		:wikitext(caption)
	return tostring(data)
end

return p