Module:Map

local util_cargo = require('Module:CargoUtil') local util_args = require ('Module:ArgsUtil') local util_vars = require('Module:VarsUtil') local cache = require('mw.ext.LuaCache') local PREFIX = 'maps2_'

local lang = mw.getLanguage('en')

local h = {} local p = {}

function h.getFromVariableOrCache(str) local key = PREFIX .. lang:lc(str) return util_vars.getObject(key) or h.getFromCacheOrCargo(str, key) end

function h.getFromCacheOrCargo(str, key) local vars = cache.get(key) or h.getFromCargo(str, key) if vars.exists == nil then vars.exists = true end util_vars.setObject(key, vars) return vars end

function h.getFromCargo(str, key) local vars = { link = h.makeAndRunMapQuery(str), exists = false }	cache.set(key, vars) return vars end

function h.makeAndRunMapQuery(str) local query = { tables = { 'Maps=MP' }, fields = { 'MP._pageName=link' }, where = { ('MP.Name="%s"'):format(str) } }	return util_cargo.getOneResult(query, 'link') end

function p.linkedDisplay(str) if str == nil then return nil end local link = p.maplinkname(str) if link == nil then return ('%s'):format(str, str:gsub('_', ' ')) end return ('%s'):format(link, str:gsub('_', ' ')) end

function p.maplinkname(str) if str == nil then return nil end local vars = h.getFromVariableOrCache(str:gsub('_', ' ')) return vars.link end

return p