Module:GeoSetsNav/sandbox
Appearance
From Wikimedia Commons, the free media repository
Lua
Documentation for this module may be created at Module:GeoSetsNav/sandbox/doc
Code
function p.renderMap(frame) local args = frame.args local currentTitle = mw.title.getCurrentTitle() local categoryName -- Category name handling (same as before) if args[1] then categoryName = args[1] else if currentTitle.namespace ~= 14 then return "'''Error:''' This template must be placed on a category page." end categoryName = currentTitle.text end local debugInfo = "Category: " .. categoryName .. "<br>" local markers = {} local count = 0 mw.log("Starting renderMap for category: " .. categoryName) -- Log start -- Use MediaWiki API to get category members and their content if mw.http and mw.http.fetch then local apiUrl = mw.uri.fullUrl("api.php", { action = "query", generator = "categorymembers", gcmtitle = "Category:" .. categoryName, gcmlimit = "500", -- Adjust limit as needed prop = "revisions", rvprop = "content", format = "json" }) mw.log("API URL: " .. apiUrl) -- Log API URL local response = mw.http.fetch(apiUrl) if response then mw.log("API response status: " .. response.status) -- Log response status if response.status == 200 then -- Check for successful response local apiResult local successJSON, jsonError = pcall(mw.text.jsonDecode, response.body) if successJSON then apiResult = jsonError mw.logObject(apiResult) -- Log the decoded JSON result if apiResult and apiResult.query and apiResult.query.pages then for _, page in pairs(apiResult.query.pages) do if page.revisions and page.revisions[1] and page.revisions[1].content then local content = page.revisions[1].content local filename = page.title -- Page title is the filename -- Parse for {{Location|latitude|longitude|...}} template local locationTemplate = content:match('{{Location%s*|(%S+)|(%S+)') if locationTemplate then local lat, lon = locationTemplate[1], locationTemplate[2] if lat and lon then table.insert(markers, { lat = lat, lon = lon, label = "[[:" .. filename .. "|" .. filename:gsub("^File:", "") .. "]]" }) count = count + 1 end end end end else debugInfo = debugInfo .. "API query successful, but no pages or revisions found.<br>" mw.log(debugInfo) -- Log debugInfo end else debugInfo = debugInfo .. "Error decoding JSON: " .. jsonError .. "<br>" debugInfo = debugInfo .. "Response body was: " .. response.body .. "<br>" mw.log(debugInfo) -- Log debugInfo end else debugInfo = debugInfo .. "API call failed with status: " .. (response and response.status or "unknown") .. "<br>" if response and response.body then debugInfo = debugInfo .. "API response body: " .. response.body .. "<br>" mw.log(debugInfo) -- Log debugInfo end end else debugInfo = debugInfo .. "API response is nil (fetch failed?).<br>" mw.log(debugInfo) -- Log debugInfo end else debugInfo = debugInfo .. "mw.http is not available (this is unexpected on Wikimedia Commons). API fallback not executed.<br>" mw.log(debugInfo) -- Log debugInfo end if #markers == 0 then return "No geotagged images found in this category (searched through " .. count .. " files).<br>" .. debugInfo else return frame:expandTemplate{ title = "Mapframe", args = { width = "100%", height = "400", zoom = "10", coordinates = markers[1].lat .. "_" .. markers[1].lon, -- Center on the first marker markers = mw.text.jsonEncode(markers) } } end end