Module:NarratorListModule: Difference between revisions
From The Seven Sages of Rome
No edit summary |
No edit summary |
||
| Line 15: | Line 15: | ||
local results = getSubobjects(shortTitle) | local results = getSubobjects(shortTitle) | ||
if not results then | if not results then | ||
return "* No matching subobjects found." | |||
end | end | ||
| Line 24: | Line 24: | ||
local narrator = result["Has Narrator"] and result["Has Narrator"][1] | local narrator = result["Has Narrator"] and result["Has Narrator"][1] | ||
if narrator then | if narrator then | ||
if not groupedResults[narrator] then | local pageName = result.fulltext:match("\[\[(.-)\]\]") -- Extract page name from fulltext | ||
if pageName then | |||
if not groupedResults[narrator] then | |||
groupedResults[narrator] = {} | |||
end | |||
groupedResults[narrator][#groupedResults[narrator] + 1] = "[[" .. pageName .. "]]" | |||
end | end | ||
end | end | ||
end | end | ||
Revision as of 19:30, 27 February 2025
Documentation for this module may be created at Module:NarratorListModule/doc
local p = {}
local function getSubobjects(shortTitle)
local query = "[[Has subobject::+]][[Has Short Title::" .. shortTitle .. "]]|?Has Narrator"
local results = mw.smw.ask(query)
return results
end
function p.getNarratorList(frame)
local shortTitle = frame.args.shortTitle
if not shortTitle then
return "Error: shortTitle parameter is missing."
end
local results = getSubobjects(shortTitle)
if not results then
return "* No matching subobjects found."
end
local groupedResults = {}
for _, result in ipairs(results) do
local narrator = result["Has Narrator"] and result["Has Narrator"][1]
if narrator then
local pageName = result.fulltext:match("\[\[(.-)\]\]") -- Extract page name from fulltext
if pageName then
if not groupedResults[narrator] then
groupedResults[narrator] = {}
end
groupedResults[narrator][#groupedResults[narrator] + 1] = "[[" .. pageName .. "]]"
end
end
end
local output = {}
for narrator, pages in pairs(groupedResults) do
output[#output + 1] = "* " .. narrator .. ": " .. table.concat(pages, ", ")
end
return table.concat(output, "\n")
end
return p