Module:NarratorListModule: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


local function getSubobjects(shortTitle)
local function getSubobjectsAndPages(shortTitle)
   local query = "[[Has subobject::+]][[Has Short Title::" .. shortTitle .. "]]|?Has Narrator"
   local query = "[[Has Short Title::" .. shortTitle .. "]]|?Has Narrator|?Has subobject"
   local results = mw.smw.ask(query)
   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 = {}
   local groupedResults = {}


   for _, result in ipairs(results) do
   for _, result in ipairs(results) do
     local narrator = result["Has Narrator"] and result["Has Narrator"][1]
     local narrator = result["Has Narrator"] and result["Has Narrator"][1]
     if narrator then
    local subobject = result["Has subobject"] and result["Has subobject"][1]
       local pageName = result.fulltext:match("\[\[(.-)\]\]") -- Extract page name from fulltext
 
     if narrator and subobject then
       local pageName = mw.title.new(subobject).getParent().getText()
       if pageName then
       if pageName then
         if not groupedResults[narrator] then
         if not groupedResults[narrator] then
Line 32: Line 19:
       end
       end
     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
   end



Revision as of 19:32, 27 February 2025

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