Module:RecursiveSelectiveList: Difference between revisions
EnWikiAdmin (talk | contribs) No edit summary |
EnWikiAdmin (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local function fetchCategoryMembers(categoryName | |||
local function fetchCategoryMembers(categoryName) | |||
local url = mw.uri.fullUrl('api.php', { | local url = mw.uri.fullUrl('api.php', { | ||
action = 'query', | action = 'query', | ||
list = 'categorymembers', | list = 'categorymembers', | ||
cmtitle = 'Category:' .. categoryName, | cmtitle = 'Category:' .. categoryName, | ||
cmlimit = 'max', | cmlimit = 'max', | ||
format = 'json' | format = 'json' | ||
}) | }) | ||
Line 21: | Line 20: | ||
end | end | ||
return | return data.query.categorymembers | ||
end | end | ||
function p.main(frame) | function p.main(frame) | ||
local args = frame:getParent().args | local args = frame:getParent().args | ||
local categoryName = args['categoryName'] | local categoryName = args['categoryName'] | ||
if not categoryName then | if not categoryName then | ||
Line 79: | Line 31: | ||
end | end | ||
-- Fetch category members | |||
local | local members, err = fetchCategoryMembers(categoryName) | ||
if not members then | |||
if not | |||
return "Error: " .. err | return "Error: " .. err | ||
end | end | ||
if # | if #members == 0 then | ||
return "No pages found." | return "No pages found in the category." | ||
end | end | ||
-- Generate a bulleted list | -- Generate a bulleted list | ||
local output = {} | local output = {} | ||
for _, | for _, member in ipairs(members) do | ||
table.insert(output, "* [[" .. | table.insert(output, "* [[" .. member.title .. "]]") | ||
end | end | ||
return table.concat(output, "\n") | return table.concat(output, "\n") | ||
end | end | ||
return p | return p |
Revision as of 06:48, 22 December 2024
Documentation for this module may be created at Module:RecursiveSelectiveList/doc
local p = {}
local function fetchCategoryMembers(categoryName)
local url = mw.uri.fullUrl('api.php', {
action = 'query',
list = 'categorymembers',
cmtitle = 'Category:' .. categoryName,
cmlimit = 'max',
format = 'json'
})
local response = mw.http.get(url)
if response.status ~= 200 then
return nil, "HTTP request failed with status: " .. response.status
end
local data = mw.json.decode(response.body)
if not data then
return nil, "Failed to decode JSON response."
end
return data.query.categorymembers
end
function p.main(frame)
local args = frame:getParent().args
local categoryName = args['categoryName']
if not categoryName then
return "Error: Please provide a category name."
end
-- Fetch category members
local members, err = fetchCategoryMembers(categoryName)
if not members then
return "Error: " .. err
end
if #members == 0 then
return "No pages found in the category."
end
-- Generate a bulleted list
local output = {}
for _, member in ipairs(members) do
table.insert(output, "* [[" .. member.title .. "]]")
end
return table.concat(output, "\n")
end
return p