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 getSubobjectsAndPages(shortTitle)
-- Main function to query pages and narrators
  local query = "[[Has Short Title::" .. shortTitle .. "]]|?Has Narrator|?Has subobject"
-- @param frame The frame object from Scribunto
  local results = mw.smw.ask(query)
-- @return HTML table with narrator-to-pages mapping
  local groupedResults = {}
function p.getNarratorsForShortTitle(frame)
 
    -- Get parameters from the function call
  for _, result in ipairs(results) do
    local shortTitle = frame.args[1] or frame:getParent().args[1]
    local narrator = result["Has Narrator"] and result["Has Narrator"][1]
   
    local subobject = result["Has subobject"] and result["Has subobject"][1]
    if not shortTitle or shortTitle == '' then
 
        return 'Error: Please provide a short title as parameter'
    if narrator and subobject then
    end
      local pageName = mw.title.new(subobject).getParent().getText()
   
      if pageName then
    -- Create the semantic query
        if not groupedResults[narrator] then
    local query = mw.smw.ask({
          groupedResults[narrator] = {}
        -- Query for pages with a subobject having the specified 'Has Short Title'
        '[[Has Short Title::' .. shortTitle .. ']]',
        -- Return the page name and narrator of the subobject
        '?Has Narrator',
        -- Return the page name
        'mainlabel=Page'
    })
   
    if not query or #query == 0 then
        return 'No results found for short title: ' .. shortTitle
    end
   
    -- Organize results by narrator
    local narratorToPages = {}
   
    for _, row in ipairs(query) do
        local page = row.Page
        local narrators = row['Has Narrator']
       
        -- Handle the case where a subobject has multiple narrators
        if type(narrators) == 'table' then
            for _, narrator in ipairs(narrators) do
                if not narratorToPages[narrator] then
                    narratorToPages[narrator] = {}
                end
                table.insert(narratorToPages[narrator], page)
            end
        elseif narrators and narrators ~= '' then
            if not narratorToPages[narrators] then
                narratorToPages[narrators] = {}
            end
            table.insert(narratorToPages[narrators], page)
         end
         end
        groupedResults[narrator][#groupedResults[narrator] + 1] = "[[" .. pageName .. "]]"
      end
     end
     end
  end
   
 
    -- Now build the HTML table
  return groupedResults
    return p.buildTable(narratorToPages)
end
end


function p.getNarratorList(frame)
-- Helper function to build the HTML table
  local shortTitle = frame.args.shortTitle
-- @param narratorToPages Table mapping narrators to pages
  if not shortTitle then
-- @return HTML table as string
     return "Error: shortTitle parameter is missing."
function p.buildTable(narratorToPages)
  end
    local html = '{| class="wikitable sortable"\n'
 
    html = html .. '! Narrator !! Pages\n'
  local groupedResults = getSubobjectsAndPages(shortTitle)
   
 
    -- Sort narrators alphabetically
  if not next(groupedResults) then
    local sortedNarrators = {}
    return "* No matching subobjects found."
     for narrator, _ in pairs(narratorToPages) do
  end
        table.insert(sortedNarrators, narrator)
 
    end
  local output = {}
    table.sort(sortedNarrators)
  for narrator, pages in pairs(groupedResults) do
   
    output[#output + 1] = "* " .. narrator .. ": " .. table.concat(pages, ", ")
    -- Add each narrator and its pages to the table
  end
    for _, narrator in ipairs(sortedNarrators) do
 
        local pages = narratorToPages[narrator]
  return table.concat(output, "\n")
       
        -- Sort pages alphabetically
        table.sort(pages)
       
        -- Create comma-separated list of pages, each with a link
        local pageLinks = {}
        for _, page in ipairs(pages) do
            table.insert(pageLinks, '[[' .. page .. ']]')
        end
        local pageList = table.concat(pageLinks, ', ')
       
        html = html .. '|-\n'
        html = html .. '| ' .. narrator .. ' || ' .. pageList .. '\n'
    end
   
    html = html .. '|}'
    return html
end
end


return p
return p

Revision as of 19:40, 27 February 2025

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

local p = {}

-- Main function to query pages and narrators
-- @param frame The frame object from Scribunto
-- @return HTML table with narrator-to-pages mapping
function p.getNarratorsForShortTitle(frame)
    -- Get parameters from the function call
    local shortTitle = frame.args[1] or frame:getParent().args[1]
    
    if not shortTitle or shortTitle == '' then
        return 'Error: Please provide a short title as parameter'
    end
    
    -- Create the semantic query
    local query = mw.smw.ask({
        -- Query for pages with a subobject having the specified 'Has Short Title'
        '[[Has Short Title::' .. shortTitle .. ']]',
        -- Return the page name and narrator of the subobject
        '?Has Narrator',
        -- Return the page name
        'mainlabel=Page'
    })
    
    if not query or #query == 0 then
        return 'No results found for short title: ' .. shortTitle
    end
    
    -- Organize results by narrator
    local narratorToPages = {}
    
    for _, row in ipairs(query) do
        local page = row.Page
        local narrators = row['Has Narrator']
        
        -- Handle the case where a subobject has multiple narrators
        if type(narrators) == 'table' then
            for _, narrator in ipairs(narrators) do
                if not narratorToPages[narrator] then
                    narratorToPages[narrator] = {}
                end
                table.insert(narratorToPages[narrator], page)
            end
        elseif narrators and narrators ~= '' then
            if not narratorToPages[narrators] then
                narratorToPages[narrators] = {}
            end
            table.insert(narratorToPages[narrators], page)
        end
    end
    
    -- Now build the HTML table
    return p.buildTable(narratorToPages)
end

-- Helper function to build the HTML table
-- @param narratorToPages Table mapping narrators to pages
-- @return HTML table as string
function p.buildTable(narratorToPages)
    local html = '{| class="wikitable sortable"\n'
    html = html .. '! Narrator !! Pages\n'
    
    -- Sort narrators alphabetically
    local sortedNarrators = {}
    for narrator, _ in pairs(narratorToPages) do
        table.insert(sortedNarrators, narrator)
    end
    table.sort(sortedNarrators)
    
    -- Add each narrator and its pages to the table
    for _, narrator in ipairs(sortedNarrators) do
        local pages = narratorToPages[narrator]
        
        -- Sort pages alphabetically
        table.sort(pages)
        
        -- Create comma-separated list of pages, each with a link
        local pageLinks = {}
        for _, page in ipairs(pages) do
            table.insert(pageLinks, '[[' .. page .. ']]')
        end
        local pageList = table.concat(pageLinks, ', ')
        
        html = html .. '|-\n'
        html = html .. '| ' .. narrator .. ' || ' .. pageList .. '\n'
    end
    
    html = html .. '|}'
    return html
end

return p