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 parameters
     -- Construct the SMW query to find the parent language
     local query = {
     local query = string.format('[[%s]]|?Is Variety Of', language)
        conditions = string.format('[[%s]]', language),
        printouts = { 'Is Variety Of' }
    }
   
     -- Execute the query
     -- Execute the query
     local result = mw.smw.getQueryResult(query)
     local result = mw.smw.ask{ query }


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


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


return p
return p

Revision as of 13:16, 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)
    -- Construct the SMW query to find the parent language
    local query = string.format('[[%s]]|?Is Variety Of', language)
    -- Execute the query
    local result = mw.smw.ask{ query }

    -- Debugging: Log the query result
    mw.logObject(result, "Query result")

    -- Check if the result contains the 'Is Variety Of' property
    if result and result[language] and result[language]["Is Variety Of"] then
        local parentLanguage = result[language]["Is Variety Of"][1]
        -- Recursively find the root language
        return p.findRootLanguage(parentLanguage)
    end

    -- If no parent language is found, return the current language as the root
    return language
end

return p