Module:NarratorListModule: Difference between revisions

From The Seven Sages of Rome
No edit summary
Tag: Reverted
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- Function to recursively find the root language
-- Main function to query pages and narrators
function p.findRootLanguage(language)
-- @param frame The frame object from Scribunto
     -- Query parameters
-- @return HTML table with narrator-to-pages mapping
     local query = {
function p.getNarratorsForShortTitle(frame)
        conditions = string.format('[[%s]]', language),
     -- Get parameters from the function call
        printouts = { 'Is Variety Of' }
     local shortTitle = frame.args[1] or frame:getParent().args[1]
    }
      
      
     -- Execute the query
    if not shortTitle or shortTitle == '' then
     local result = mw.smw.getQueryResult(query)
        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
   
    if rawequal(next(narratorToPages), nil) then
    return "No recorded narrations available."
end
   
    -- Now build the HTML table
    return p.buildTable(narratorToPages)
end


    -- Debugging: Log the result structure
-- Helper function to build the HTML table
    mw.log("Query result: " .. mw.text.jsonEncode(result))
-- @param narratorToPages Table mapping narrators to pages
 
-- @return HTML table as string
     -- Check if we received a valid result
function p.buildTable(narratorToPages)
     if result and result.results then
    local html = '{| class="dataTable sortable"\n'
        for _, pageData in pairs(result.results) do
    html = html .. '! Narrator !! Pages\n'
            local parentLanguage = pageData.printouts["Is Variety Of"]
   
            if parentLanguage and #parentLanguage > 0 then
    -- Sort narrators alphabetically
                return p.findRootLanguage(parentLanguage[1].fulltext) -- Recursively find the root language
    local sortedNarrators = {}
             end
    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 with links but WITHOUT brackets
        local pageLinks = {}
        for _, page in ipairs(pages) do
            -- Use direct page name without wiki link syntax
             table.insert(pageLinks, page)
         end
         end
        local pageList = table.concat(pageLinks, ', ')
       
        html = html .. '|-\n'
        html = html .. '| ' .. narrator .. ' || ' .. pageList .. '\n'
     end
     end
      
      
     -- If no parent is found, return the current language (this is the root language)
     html = html .. '|}'
     return language
     return html
end
end


return p
return p

Latest revision as of 14:30, 26 March 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
    
    if rawequal(next(narratorToPages), nil) then
    	return "No recorded narrations available."
	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="dataTable 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 with links but WITHOUT brackets
        local pageLinks = {}
        for _, page in ipairs(pages) do
            -- Use direct page name without wiki link syntax
            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