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

-- Module:RepairServicesMap
-- Generates a map of all repair services using SMW queries

local p = {}

function p.map(frame)
    local args = frame.args
    local height = args.height or '500'
    local zoom = args.zoom or '2'
    local filter = args.filter or ''
    
    -- Build the SMW query
    local query = '[[Category:Repair services]][[Has coordinates::+]]'
    if filter ~= '' then
        query = query .. '[[Has device types::~*' .. filter .. '*]]'
    end
    
    -- Get results using SMW
    local smw = mw.smw
    if not smw then
        return '<strong class="error">Semantic MediaWiki not available</strong>'
    end
    
    local results = smw.ask({
        query,
        '?Has coordinates',
        '?Has name',
        '?Has city',
        '?Has device types',
        '?Has mail-in service',
        limit = 500
    })
    
    if not results or #results == 0 then
        return '<p><em>No repair services with coordinates found.</em></p>'
    end
    
    -- Build coordinate string for display_map
    local coords = {}
    for _, result in ipairs(results) do
        local coord = result['Has coordinates']
        local name = result['Has name'] or result[1] or 'Unknown'
        local city = result['Has city'] or ''
        local devices = result['Has device types'] or ''
        local mailin = result['Has mail-in service'] or ''
        
        if coord then
            -- Handle if coord is a table (SMW sometimes returns tables)
            if type(coord) == 'table' then
                coord = coord[1]
            end
            
            -- Build popup text
            local popup = "'''" .. name .. "'''"
            if city ~= '' then
                popup = popup .. '<br>' .. city
            end
            if devices ~= '' then
                if type(devices) == 'table' then
                    devices = table.concat(devices, ', ')
                end
                popup = popup .. '<br>Repairs: ' .. devices
            end
            if mailin == 'Yes' then
                popup = popup .. '<br>✓ Mail-in available'
            end
            
            table.insert(coords, coord .. '~' .. popup)
        end
    end
    
    if #coords == 0 then
        return '<p><em>No valid coordinates found.</em></p>'
    end
    
    -- Use frame:preprocess to call #display_map
    local coordString = table.concat(coords, ';')
    local mapWikitext = '{{#display_map:' .. coordString .. '|height=' .. height .. '|zoom=' .. zoom .. '|width=100%}}'
    
    return frame:preprocess(mapWikitext)
end

function p.count(frame)
    local smw = mw.smw
    if not smw then return '0' end
    
    local results = smw.ask({
        '[[Category:Repair services]]',
        limit = 500
    })
    
    return results and #results or 0
end

return p
MediaWiki Appliance - Powered by TurnKey Linux