Module:Infobox mapframe: Difference between revisions

m 1 revision imported
No edit summary
 
Line 3: Line 3:
local yesno = require('Module:Yesno')
local yesno = require('Module:Yesno')
local infoboxImage = require('Module:InfoboxImage').InfoboxImage
local infoboxImage = require('Module:InfoboxImage').InfoboxImage
-- Wikibase is nil on most self-hosted wikis (unless Wikibase Client/Repository is installed)
local wikibase = mw.wikibase
local function hasWikibase()
return wikibase and type(wikibase.getEntityIdForCurrentPage) == 'function'
end


-- Defaults
-- Defaults
Line 49: Line 55:


function util.getBestStatement(item_id, property_id)
function util.getBestStatement(item_id, property_id)
if not(item_id) or not(mw.wikibase.isValidEntityId(item_id)) or not(mw.wikibase.entityExists(item_id)) then
if not hasWikibase() then
return false
return false
end
end
local statements = mw.wikibase.getBestStatements(item_id, property_id)
if not(item_id) or not(wikibase.isValidEntityId(item_id)) or not(wikibase.entityExists(item_id)) then
return false
end
local statements = wikibase.getBestStatements(item_id, property_id)
if not statements or #statements == 0 then
if not statements or #statements == 0 then
return false
return false
Line 163: Line 172:
return util.noop("no caption or switcher")
return util.noop("no caption or switcher")
end
end
if not hasWikibase() then
return util.noop("wikibase unavailable for autocaption")
end
local maskItem
local maskItem
local maskType = util.idType(args.geomask)
local maskType = util.idType(args.geomask)
Line 168: Line 182:
maskItem = args.geomask
maskItem = args.geomask
elseif maskType == "property" then
elseif maskType == "property" then
maskItem = util.relatedEntity(args.id or mw.wikibase.getEntityIdForCurrentPage(), args.geomask)
maskItem = util.relatedEntity(args.id or wikibase.getEntityIdForCurrentPage(), args.geomask)
end
end
local maskItemLabel = maskItem and mw.wikibase.getLabel( maskItem )
local maskItemLabel = maskItem and wikibase.getLabel( maskItem )
return maskItemLabel and "Location in "..maskItemLabel
return maskItemLabel and "Location in "..maskItemLabel
or util.noop("missing maskItemLabel with type " .. (maskType or "nil") .. " and item " .. (maskItem or "nil"))
or util.noop("missing maskItemLabel with type " .. (maskType or "nil") .. " and item " .. (maskItem or "nil"))
Line 195: Line 209:


-- Require wikidata item, or specified coords
-- Require wikidata item, or specified coords
local wikidataId = config.id or mw.wikibase.getEntityIdForCurrentPage()
local wikidataId = config.id or (hasWikibase() and wikibase.getEntityIdForCurrentPage() or nil)
if not(wikidataId) and not(config.coord) then
if not(wikidataId) and not(config.coord) then
return false, util.trackAndWarn('Pages using infobox mapframe with missing coordinates')
return false, util.trackAndWarn('Pages using infobox mapframe with missing coordinates')
Line 202: Line 216:
-- Require coords (specified or from wikidata), so that map will be centred somewhere
-- Require coords (specified or from wikidata), so that map will be centred somewhere
-- (P625 = coordinate location)
-- (P625 = coordinate location)
     local wdCoordinates = util.getStatementValue(util.getBestStatement(wikidataId, 'P625'))
     local wdCoordinates = (hasWikibase() and wikidataId)
and util.getStatementValue(util.getBestStatement(wikidataId, 'P625'))
or nil
     if not (config.coord or wdCoordinates) then
     if not (config.coord or wdCoordinates) then
return false, util.trackAndWarn('Pages using infobox mapframe with missing coordinates')
return false, util.trackAndWarn('Pages using infobox mapframe with missing coordinates')
Line 227: Line 243:


     -- if zoom isn't specified from config, first check wikidata
     -- if zoom isn't specified from config, first check wikidata
     local zoom = config.zoom or util.getStatementValue(util.getBestStatement(wikidataId, 'P6592'))
     local zoom = config.zoom or (hasWikibase() and wikidataId and util.getStatementValue(util.getBestStatement(wikidataId, 'P6592')) or nil)
     if not zoom then
     if not zoom then
    -- Calculate zoom from length or area (converted to km or km2)
    -- Calculate zoom from length or area (converted to km or km2)
Line 244: Line 260:
-- Use OSM relation ID if available; otherwise use geoshape if that is available
-- Use OSM relation ID if available; otherwise use geoshape if that is available
-- (geoshape is required for defunct entities, which are outside OSM's scope)
-- (geoshape is required for defunct entities, which are outside OSM's scope)
local hasOsmRelationId = util.hasWikidataProperty(wikidataId, 'P402') -- P402 is OSM relation ID
local hasOsmRelationId = (hasWikibase() and wikidataId) and util.hasWikidataProperty(wikidataId, 'P402') or false -- P402 is OSM relation ID
local hasGeoshape = util.hasWikidataProperty(wikidataId, 'P3896') -- P3896 is geoshape
local hasGeoshape = (hasWikibase() and wikidataId) and util.hasWikidataProperty(wikidataId, 'P3896') or false -- P3896 is geoshape
local wikidataProvidesGeo = hasOsmRelationId or hasGeoshape
local wikidataProvidesGeo = hasOsmRelationId or hasGeoshape


Line 287: Line 303:
-- that city's state, and that state's country
-- that city's state, and that state's country
args.zoom = nil -- let kartographer determine the zoom
args.zoom = nil -- let kartographer determine the zoom
if not hasWikibase() then
return false, util.trackAndWarn('Pages using infobox mapframe with switcher=auto but no Wikibase','Switcher "auto" requires Wikibase')
end
local maskLabels = {}
local maskLabels = {}
local maskItems = {}
local maskItems = {}
local maskItemId = util.relatedEntity(wikidataId, "P276") or  util.relatedEntity(wikidataId, "P131")  
local maskItemId = util.relatedEntity(wikidataId, "P276") or  util.relatedEntity(wikidataId, "P131")  
local maskLabel = mw.wikibase.getLabel(maskItemId)
local maskLabel = wikibase.getLabel(maskItemId)
while maskItemId and maskLabel and mw.text.trim(maskLabel) ~= "" do
while maskItemId and maskLabel and mw.text.trim(maskLabel) ~= "" do
table.insert(maskLabels, maskLabel)
table.insert(maskLabels, maskLabel)
table.insert(maskItems, maskItemId)
table.insert(maskItems, maskItemId)
maskItemId = maskItemId and util.relatedEntity(maskItemId, "P131")
maskItemId = maskItemId and util.relatedEntity(maskItemId, "P131")
maskLabel = maskItemId and mw.wikibase.getLabel(maskItemId)
maskLabel = maskItemId and wikibase.getLabel(maskItemId)
end
end
if #maskLabels > 1 then
if #maskLabels > 1 then
Line 312: Line 333:
local maskLabels = {}
local maskLabels = {}
if #maskItems > 1 then
if #maskItems > 1 then
for i, item in ipairs(maskItems) do
if hasWikibase() then
table.insert(maskLabels, mw.wikibase.getLabel(item))
for i, item in ipairs(maskItems) do
table.insert(maskLabels, wikibase.getLabel(item))
end
else
for i, item in ipairs(maskItems) do
table.insert(maskLabels, item)
end
end
end
args.switch = table.concat(maskLabels, "###")
args.switch = table.concat(maskLabels, "###")
Line 321: Line 348:
-- resolve geomask item id (if not using geomask switcher)
-- resolve geomask item id (if not using geomask switcher)
if not maskItem then -- 
if not maskItem then
local maskType = util.idType(config.geomask)
local maskType = util.idType(config.geomask)
if maskType == 'item' then
if maskType == 'item' then
Line 425: Line 452:
             args["frame-coord"] = args["frame-coord"] or config.coord
             args["frame-coord"] = args["frame-coord"] or config.coord
         else
         else
-- this branch only happens if Wikibase is available (wdCoordinates exists)
             args["frame-lat"] = args["frame-lat"] or wdCoordinates.latitude
             args["frame-lat"] = args["frame-lat"] or wdCoordinates.latitude
             args["frame-long"] = args["frame-long"] or wdCoordinates.longitude
             args["frame-long"] = args["frame-long"] or wdCoordinates.longitude