Fix delimiter splitting bug — use mw.text.split with plain match instead of gmatch character class (via update-page on MediaWiki MCP Server)
Use mw.smw.ask instead of getPropertyValues — more widely available in Semantic Scribunto (via update-page on MediaWiki MCP Server)
 
(One intermediate revision by the same user not shown)
Line 5: Line 5:
     local mode = args.mode or "packed"
     local mode = args.mode or "packed"
     local heights = args.heights or "160"
     local heights = args.heights or "160"
     local pageName = args.page or mw.title.getCurrentTitle().fullText
     local pageName = args.page
    if not pageName or pageName == "" then
        pageName = mw.title.getCurrentTitle().fullText
    end


     -- Query all "Has images" values stored by {{CRT model}} on this page
     -- Use mw.smw.ask to query Has images property
     local delim = "~!~"
     local queryResult = mw.smw.ask("[[" .. pageName .. "]]|?Has images|mainlabel=-|limit=50")
    local query = string.format(
        '{{#ask:[[%s]]|?Has images|mainlabel=-|headers=hide|link=none|sep=%s|limit=50}}',
        pageName, delim
    )
    local result = frame:preprocess(query)


     if not result or result == "" then
     if not queryResult or type(queryResult) ~= "table" or #queryResult == 0 then
         return ""
         return ""
     end
     end


    -- Split on delimiter using plain string match
    local parts = mw.text.split(result, delim, true)
     local lines = {}
     local lines = {}
     for _, part in ipairs(parts) do
     for _, row in ipairs(queryResult) do
         local trimmed = mw.text.trim(part)
         local images = row["Has images"]
        if trimmed ~= "" then
        if images then
            -- Ensure File: prefix
            -- images may be a single string or a table of strings
            if not trimmed:match("^[Ff]ile:") then
            if type(images) == "string" then
                trimmed = "File:" .. trimmed
                images = { images }
            end
            for _, img in ipairs(images) do
                local trimmed = mw.text.trim(tostring(img))
                if trimmed ~= "" then
                    if not trimmed:match("^[Ff]ile:") then
                        trimmed = "File:" .. trimmed
                    end
                    table.insert(lines, trimmed)
                end
             end
             end
            table.insert(lines, trimmed)
         end
         end
     end
     end

Latest revision as of 17:05, 10 February 2026

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

local p = {}

function p.gallery(frame)
    local args = frame:getParent().args
    local mode = args.mode or "packed"
    local heights = args.heights or "160"
    local pageName = args.page
    if not pageName or pageName == "" then
        pageName = mw.title.getCurrentTitle().fullText
    end

    -- Use mw.smw.ask to query Has images property
    local queryResult = mw.smw.ask("[[" .. pageName .. "]]|?Has images|mainlabel=-|limit=50")

    if not queryResult or type(queryResult) ~= "table" or #queryResult == 0 then
        return ""
    end

    local lines = {}
    for _, row in ipairs(queryResult) do
        local images = row["Has images"]
        if images then
            -- images may be a single string or a table of strings
            if type(images) == "string" then
                images = { images }
            end
            for _, img in ipairs(images) do
                local trimmed = mw.text.trim(tostring(img))
                if trimmed ~= "" then
                    if not trimmed:match("^[Ff]ile:") then
                        trimmed = "File:" .. trimmed
                    end
                    table.insert(lines, trimmed)
                end
            end
        end
    end

    if #lines == 0 then return "" end

    local wikitext = string.format(
        '<gallery mode="%s" heights="%s">\n%s\n</gallery>',
        mode, heights, table.concat(lines, "\n")
    )

    return frame:preprocess(wikitext)
end

return p
MediaWiki Appliance - Powered by TurnKey Linux