Module:GLInfobox

From glossaLAB

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

local p = {}

function p.getRealName( frame )
  username = frame.args[1]
  realname =  mw.ext.glossalab.getRealName(username)
  return realname
end

-- Main function to generate the infobox
-- Called from wikitext like:
-- {{#invoke:gLInfobox|display
-- |query=[[Page Name]]{{!}}?Property Label 1{{!}}?Property Label 2
-- |class=my-custom-infobox-class
-- |noresults=No information found.
-- |novalue=N/A
-- }}
function p.display(frame)
    local args = frame.args
    local queryString = args.query
    local cssClass = args.class or "gl-infobox"
    local noResultsMessage = args.noresults or "—" -- Default to an em dash
    local noValueMessage = args.novalue or "—"   -- Default to an em dash

    if not queryString or queryString:match('^%s*$') then
        return string.format("<strong class='error'>Error: The 'query' parameter is missing or empty in #invoke:%s.</strong>", frame:getTitle())
    end

    local ok, query = pcall(mw.smw.getQueryResult, queryString)
    if not ok or not query then
        return string.format("<strong class='error'>Error parsing query: %s</strong>", tostring(query))
    end

    local ok_ask, askResult = pcall(mw.smw.ask, queryString)
    if not ok_ask then
        return string.format("<strong class='error'>Error executing query: %s</strong>", tostring(askResult))
    end

    if not askResult or not next(askResult) then
        return frame:preprocess(noResultsMessage)
    end

    -- Since an infobox displays one subject, get the first subject from the results.
    local subjectName, subjectData
    for k, v in pairs(askResult) do
        subjectName = k
        subjectData = v
        break
    end
mw.logObject(subjectData)

    local wikitext = {'{| class="' .. cssClass .. '"'}
	local firstRow = true
    for _, pr in ipairs(query.printrequests) do
        if pr.typeid ~= '_wpg' or pr.mode ~= 2 then -- Skip the main subject pseudo-property.
            local label = pr.label
            local prop = pr.key
            local value = subjectData[label] -- Look up the value from the askResult.

            local displayValue
            if type(value) == 'table' then
            	table.sort(value)
                displayValue = table.concat(value, "<br />")
            elseif type(value) == 'string' and not value:match('^%s*$') then
                -- If it's a single, non-empty string, use it directly.
                displayValue = value
        	elseif type(value) == 'number' then
            	-- If it's a number, convert it to a string.
            	displayValue = tostring(value)
            else
                -- If value is nil, an empty string, or another type, use the fallback message.
                displayValue = frame:preprocess(noValueMessage)
            end

            -- Add the row to the wikitext table
            if not (displayValue == noValueMessage) then
            	if firstRow then
            		firstRow = false
	            	table.insert(wikitext, '|- class="gl-infobox-firstrow"')
	            else
	            	table.insert(wikitext, '|- class="gl-infobox-row"')
	            end
	            table.insert(wikitext, '! class="gl-infobox-label" | [[Property:' .. prop .. '|' .. label .. ']]')
	           	table.insert(wikitext, '| class="gl-infobox-value" | ' .. displayValue)
	        else
	        	mw.log('INFO: Value for Property:' .. prop .. ' not found.')
    		end
        end
    end

    table.insert(wikitext, '|}')
    return table.concat(wikitext, '\n')
end

return p