Module:RootFinder

From The Seven Sages of Rome
Revision as of 08:09, 26 March 2025 by Noeth (talk | contribs)

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

local p = {}

function p.getRootLanguage(frame)
    local title = mw.title.getCurrentTitle().fullText
    local prop = "Is Variety Of"

    while true do
        -- Perform a semantic query to get the parent language
        local queryResult = mw.smw.ask{"[[" .. title .. "]]|?" .. prop}

        -- Ensure queryResult is valid and extract the first parent
        local parent = nil
        if queryResult and type(queryResult) == "table" then
            for _, row in pairs(queryResult) do
                if row[prop] then
                    if type(row[prop]) == "table" then
                        parent = row[prop][1] -- Take the first value if it's a list
                    else
                        parent = row[prop]
                    end
                end
                break -- Only need the first row
            end
        end

        -- If no parent is found, return the current title as root
        if not parent or parent == "" then
            return title
        end

        -- Move up to the parent language
        title = parent
    end
end

return p