Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Module documentation[view][edit][history][purge]
This documentation is transcluded from Module:ArticleList/doc. Changes can be proposed in the talk page.

This template automatically generates a list of articles and their data based on Semantic MediaWiki properties.

Example usage

The following code is used to create the List of Coaches. It displays all articles in Category:Carriages. The columns are specified via the respective property names as anonymous parameters.

{{ArticleList
|category=Carriages
|Title
|Image
|Coupled With
|Status
|Spawn Points
|Seats
|Doors
|Gangway Doors
}}

No description.

Template parameters[Edit template data]

This template has custom formatting.

ParameterDescriptionTypeStatus
Categorycategory

The category that contains the articles to be displayed.

Example
Trains
Linerequired

local SMWUtil = require("Module:SMWUtil")
local getStatus = require("Module:Status").get

local ArticleList = {}

local methodTable = {}
local metatable = {
    __index = methodTable,
    __tostring = function(self)
        return self:render()
    end
}

local imageSize = "310px"

--- Returns the first image in a gallery, or the image if only an image is provided
--- @param input string
--- @return string
local function getFirstImage(input)
	if input == nil then
		return nil
	end

    if input == "" then
        return "Placeholder.png"
    end

	if not string.find(input, ";") then
        input = string.gsub(input, "^File:", "")
		return input -- only an image
	end

    local images = mw.text.split(input, ";")
    local firstImage = images[1]

    local image = mw.text.split(firstImage, "=")[2]
    image = string.gsub(image, "^File:", "")
    return image
end

local display = function(data, context)
    if data == "" then
        return "N/A"
    end
    return data
end

local propertyDisplayFunctions = {
    ["Title"] = function(data, context)
        local title = data
        if title == "" then
            return nil
        end
        local url = "[[" .. context.pageName .. "|" .. title .. "]]"
        return url
    end,
    ["Image"] = function(data, context)
        local image = getFirstImage(data)
        return "[[File:" .. image .. "|" .. imageSize .. "]]"
    end,
    ["Status"] = function(data, context)
        local statusText, statusClass = getStatus(display(data))

        local statusDescription = SMWUtil.get(context.frame, context.pageName, "Status Description")

        local hoverElement = string.format("<span class=\"smw-highlighter smwttinline\" data-state=\"inline\">%s<span class=\"smwttcontent\">%s</span></span>", statusText, statusDescription)
        if statusDescription == "" then
            hoverElement = nil
        end
        
        return " class=\"status-cell " .. statusClass .. "\" |" .. (hoverElement or statusText)
    end,
    ["Signal Box"] = function(data, context)
        if data ~= "" then
            return data
        end

        local signalBoxes = SMWUtil.askForPages(
    		context.frame,
    		"[[Category:Signal Boxes]] [[Crossings Controlled::"
    		.. context.pageName .. "]] [[Concept:InGame]]"
        )
        
        return display(signalBoxes, context)
    end,
}

local alternativeHeaders = {
    ["Title"] = "Name"
}

--- Creates a table header with the given headers
---@param headers table the headers to be added to the table
---@return string
local function createTableHeader(headers)
    local str = "{| class=\"wikitable sortable\"\n"
    for _, header in ipairs(headers) do
        local headerText = alternativeHeaders[header] or header
        str = str .. "!" .. headerText .. "\n"
    end
    return str
end

--- Creates a table footer
---@return string
local function createTableFooter()
    return "|}"
end

--- Creates a table row with the given properties
---@param frame table the frame object
---@param title string the title of the page
---@param properties table the properties to be added to the table
---@return string
local function createTableRow(frame, title, properties)
    local str = "|-\n"
    for _, property in ipairs(properties) do
        local value = SMWUtil.get(frame, title, property)
        local displayFunction = propertyDisplayFunctions[property] or display
        local displayValue = displayFunction(value, {
            pageName = title,
            frame = frame
        })
        if property == "Title" and displayValue == nil then
            return ""
        end
        str = str .. "|" .. displayValue .. "\n"
    end
    return str
end

-- PUBLIC FUNCTIONS

function methodTable.render(self, frame) 
    local output = createTableHeader(self.properties)

    for _, article in ipairs(self.articles) do
        output = output .. createTableRow(frame, article, self.properties)
    end

    output = output .. createTableFooter()

    return output
end

function methodTable.registerProperty(self, property)
    table.insert(self.properties, property)
end

function methodTable.registerProperties(self, properties)
    for _, property in ipairs(properties) do
        methodTable.registerProperty(self, property)
    end
end

function methodTable.addArticle(self, article)
    table.insert(self.articles, article)
end

function methodTable.addArticles(self, articles)
    for _, article in ipairs(articles) do
        methodTable.addArticle(self, article)
    end
end

function methodTable.addArticlesForCategory(self, categoryName)
    local query = SMWUtil.createQueryString(categoryName)
    local articles = SMWUtil.getPages(mw.getCurrentFrame(), query)
    methodTable.addArticles(self, articles)
end

function methodTable.addPropertyDisplayFunction(self, property, displayFunction)
    self.propertyDisplayFunctions[property] = displayFunction
end

function methodTable.addAlternativeHeader(self, property, header)
    self.alternativeHeaders[property] = header
end

function ArticleList.new()
    local instance = {
        properties = {},
        articles = {},
        propertyDisplayFunctions = {},
        alternativeHeaders = {}
    }
    setmetatable(instance, metatable)
    return instance
end

function ArticleList.fromArgs(frame)
    local instance = ArticleList:new()
    -- lazy load module
	local args = require("Module:Arguments").getArgs(frame)

    local i = 1
    while args[i] do
        instance:registerProperty(args[i])
        i = i + 1
    end

    local category = args.category
    instance:addArticlesForCategory(category)

    return instance:render(frame)
end

return ArticleList
🍪 Yum Yum Yum! Cookies help us better deliver our services. By using our site, you agree to our use of cookies.