Module:NarratorListModule: Difference between revisions

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


-- Main function to query pages and narrators
-- Function to recursively find the root language
-- @param frame The frame object from Scribunto
function p.findRootLanguage(language)
-- @return HTML table with narrator-to-pages mapping
    -- Query to get the "Is Variety Of" value for the given language
function p.getNarratorsForShortTitle(frame)
    local query = string.format('[[%s]]|?Is Variety Of', language)
     -- Get parameters from the function call
     -- Execute the query using mw.smw.ask
     local shortTitle = frame.args[1] or frame:getParent().args[1]
     local result = mw.smw.ask(query)
      
      
    if not shortTitle or shortTitle == '' then
     -- Debugging: Output the structure of the result to help diagnose the problem
        return 'Error: Please provide a short title as parameter'
     mw.log("Query result: " .. mw.text.jsonEncode(result))
    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
    -- Check if we received a valid result
-- @param narratorToPages Table mapping narrators to pages
     if result and result[1] and result[1].values then
-- @return HTML table as string
         -- Check if the "Is Variety Of" value exists
function p.buildTable(narratorToPages)
         local parentLanguage = result[1].values["Is Variety Of"]
     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
         -- If there's a parent language, recursively find its root
         local pageLinks = {}
         if parentLanguage then
        for _, page in ipairs(pages) do
            return p.findRootLanguage(parentLanguage)
             -- Use direct page name without wiki link syntax
        else
             table.insert(pageLinks, page)
             -- Return the current language if no parent is found
             return language
         end
         end
        local pageList = table.concat(pageLinks, ', ')
    else
          
         -- If no valid result, return the original language
        html = html .. '|-\n'
         return language
         html = html .. '| ' .. narrator .. ' || ' .. pageList .. '\n'
     end
     end
   
    html = html .. '|}'
    return html
end
end


return p
return p

Revision as of 13:10, 25 March 2025

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

local p = {}

-- Function to recursively find the root language
function p.findRootLanguage(language)
    -- Query to get the "Is Variety Of" value for the given language
    local query = string.format('[[%s]]|?Is Variety Of', language)
    -- Execute the query using mw.smw.ask
    local result = mw.smw.ask(query)
    
    -- Debugging: Output the structure of the result to help diagnose the problem
    mw.log("Query result: " .. mw.text.jsonEncode(result))

    -- Check if we received a valid result
    if result and result[1] and result[1].values then
        -- Check if the "Is Variety Of" value exists
        local parentLanguage = result[1].values["Is Variety Of"]
        
        -- If there's a parent language, recursively find its root
        if parentLanguage then
            return p.findRootLanguage(parentLanguage)
        else
            -- Return the current language if no parent is found
            return language
        end
    else
        -- If no valid result, return the original language
        return language
    end
end

return p