Module:NarratorListModule

From The Seven Sages of Rome
Revision as of 19:32, 27 February 2025 by Noeth (talk | contribs)

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

local p = {}

local function getSubobjectsAndPages(shortTitle)
  local query = "[[Has Short Title::" .. shortTitle .. "]]|?Has Narrator|?Has subobject"
  local results = mw.smw.ask(query)
  local groupedResults = {}

  for _, result in ipairs(results) do
    local narrator = result["Has Narrator"] and result["Has Narrator"][1]
    local subobject = result["Has subobject"] and result["Has subobject"][1]

    if narrator and subobject then
      local pageName = mw.title.new(subobject).getParent().getText()
      if pageName then
        if not groupedResults[narrator] then
          groupedResults[narrator] = {}
        end
        groupedResults[narrator][#groupedResults[narrator] + 1] = "[[" .. pageName .. "]]"
      end
    end
  end

  return groupedResults
end

function p.getNarratorList(frame)
  local shortTitle = frame.args.shortTitle
  if not shortTitle then
    return "Error: shortTitle parameter is missing."
  end

  local groupedResults = getSubobjectsAndPages(shortTitle)

  if not next(groupedResults) then
    return "* No matching subobjects found."
  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