Module:NarratorListModule: Difference between revisions

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


-- Function to recursively find the root language
function p._findRoot(language)
function p.findRootLanguage(language)
     local current = language
     -- Construct the SMW query to find the parent language
    while true do
    local query = string.format('[[%s]]|?Is Variety Of', language)
        -- Query for the 'Is Variety Of' property of the current language
    -- Execute the query
        local results = mw.smw.ask({
    local result = mw.smw.ask{ query }
            '[[Category:Language]]',
 
            '[[' .. current .. ']]',
    -- Debugging: Log the query result
            '?Is Variety Of'
    mw.logObject(result, "Query result")
        })
 
       
    -- Check if the result contains the 'Is Variety Of' property
        -- Exit if no results or invalid response
    if result and result[language] and result[language]["Is Variety Of"] then
        if not results or #results == 0 then break end
         local parentLanguage = result[language]["Is Variety Of"][1]
       
         -- Recursively find the root language
        -- Extract parent data from the first result
         return p.findRootLanguage(parentLanguage)
        local parentData = results[1]['Is Variety Of']
       
        -- Exit if no parent found
        if not parentData or #parentData == 0 then break end
          
         -- Move to the parent language (using fulltext for page name)
         current = parentData[1].fulltext
     end
     end
    return current
end


     -- If no parent language is found, return the current language as the root
function p.main(frame)
     return language
     local language = frame.args[1]  -- Get input language from template
     return p._findRoot(language)
end
end


return p
return p

Revision as of 13:21, 25 March 2025

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

local p = {}

function p._findRoot(language)
    local current = language
    while true do
        -- Query for the 'Is Variety Of' property of the current language
        local results = mw.smw.ask({
            '[[Category:Language]]',
            '[[' .. current .. ']]',
            '?Is Variety Of'
        })
        
        -- Exit if no results or invalid response
        if not results or #results == 0 then break end
        
        -- Extract parent data from the first result
        local parentData = results[1]['Is Variety Of']
        
        -- Exit if no parent found
        if not parentData or #parentData == 0 then break end
        
        -- Move to the parent language (using fulltext for page name)
        current = parentData[1].fulltext
    end
    return current
end

function p.main(frame)
    local language = frame.args[1]  -- Get input language from template
    return p._findRoot(language)
end

return p