Module:NarratorListModule: Difference between revisions

From The Seven Sages of Rome
No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 3: Line 3:
-- Function to recursively find the root language
-- Function to recursively find the root language
function p.findRootLanguage(language)
function p.findRootLanguage(language)
     -- Query to get the "Is Variety Of" value for the given language
     -- Query parameters
     local query = string.format('[[%s]]|?Is Variety Of', language)
     local query = {
    -- Execute the query using mw.smw.ask
        conditions = string.format('[[%s]]', language),
     local result = mw.smw.ask(query)
        printouts = { 'Is Variety Of' }
     }
      
      
     -- Debugging: Output the structure of the result to help diagnose the problem
    -- Execute the query
    local result = mw.smw.getQueryResult(query)
 
     -- Debugging: Log the result structure
     mw.log("Query result: " .. mw.text.jsonEncode(result))
     mw.log("Query result: " .. mw.text.jsonEncode(result))


     -- Check if we received a valid result
     -- Check if we received a valid result
     if result and result[1] then
     if result and result.results then
         local values = result[1].values
         for _, pageData in pairs(result.results) do
        -- Check if "Is Variety Of" exists in the values
             local parentLanguage = pageData.printouts["Is Variety Of"]
        if values and values["Is Variety Of"] then
             if parentLanguage and #parentLanguage > 0 then
             local parentLanguage = values["Is Variety Of"]
                return p.findRootLanguage(parentLanguage[1].fulltext) -- Recursively find the root language
             -- If there's a parent language, recursively find its root
            end
            return p.findRootLanguage(parentLanguage)
         end
         end
     end
     end

Revision as of 13:14, 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 parameters
    local query = {
        conditions = string.format('[[%s]]', language),
        printouts = { 'Is Variety Of' }
    }
    
    -- Execute the query
    local result = mw.smw.getQueryResult(query)

    -- Debugging: Log the result structure
    mw.log("Query result: " .. mw.text.jsonEncode(result))

    -- Check if we received a valid result
    if result and result.results then
        for _, pageData in pairs(result.results) do
            local parentLanguage = pageData.printouts["Is Variety Of"]
            if parentLanguage and #parentLanguage > 0 then
                return p.findRootLanguage(parentLanguage[1].fulltext) -- Recursively find the root language
            end
        end
    end
    
    -- If no parent is found, return the current language (this is the root language)
    return language
end

return p