Toggle menu
Toggle preferences menu
Toggle personal menu
Et ole kirjautunut
Your IP address will be publicly visible if you make any edits.

Moduuli:ListCategoryPages

Sarastus Wikistä
Versio hetkellä 11. joulukuuta 2024 kello 23.47 – tehnyt Virankannos (keskustelu | muokkaukset)

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:ListCategoryPages/ohje

local p = {}

function p.listPagesInCategory(frame)
    -- Name of the category to list pages from
    local categoryName = frame.args.category or "ExampleCategory"
    
    -- Ensure the category starts with "Category:"
    if not categoryName:match("^Category:") then
        categoryName = "Category:" .. categoryName
    end

    -- Get the category object
    local category = mw.title.new(categoryName)
    if not category or not category.exists then
        return "The category does not exist."
    end

    -- Fetch all pages in the category
    local pages = category:categoryMembers()
    local result = {}

    for pageTitle, _ in pairs(pages) do
        -- Create a title object for the page
        local page = mw.title.new(pageTitle)
        if page then
            -- Extract the page name without the namespace
            local pageLabel = page.text
            -- Add a formatted entry with a link and label
            table.insert(result, "* [[" .. page.fullText .. "|" .. pageLabel .. "]]")
        end
    end

    -- Sort the result alphabetically
    table.sort(result)

    -- Combine the results into a single string
    return table.concat(result, "\n")
end

return p