Module:RootFinder: Difference between revisions

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


-- Function to get the root language by traversing up the hierarchy
local function get_root_language(language)
local function get_root_language(language)
     local parent_prop = "Is Variety Of"
     local parent_prop = "Is Variety Of"


    -- Loop upwards to find the root language
     while true do
     while true do
         local query = "[[" .. language .. "]]|?" .. parent_prop
         local query = "[[" .. language .. "]]|?" .. parent_prop
         local result = mw.smw.ask{ query }
         local result = mw.smw.ask{ query }


         if result and result[language] and result[language][parent_prop] then
        -- If no parent is found, we've reached the root
            language = result[language][parent_prop]
         if not result or not result[language] or not result[language][parent_prop] then
        else
             break
             break
         end
         end
        -- Move up the hierarchy
        language = result[language][parent_prop]
     end
     end


Line 18: Line 22:
end
end


-- Main function to print the full tree for the current language
function p.tree(frame)
function p.tree(frame)
     local current_language = mw.title.getCurrentTitle().text
     local current_language = mw.title.getCurrentTitle().text
     local root_language = get_root_language(current_language)
     local root_language = get_root_language(current_language)


    -- Construct the query with the root parameter
     local query = string.format(
     local query = string.format(
         '[[%s]] [[Category:Language]] |format=tree |parent=Is Variety Of |limit=5000',
         '[[Category:Language]] |format=tree |parent=Is Variety Of |limit=5000|root=%s',
         root_language
         root_language
     )
     )


    -- Return the processed result
     return frame:preprocess('{{#ask:' .. query .. '}}')
     return frame:preprocess('{{#ask:' .. query .. '}}')
end
end


return p
return p

Revision as of 10:41, 25 March 2025

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

local p = {}

-- Function to get the root language by traversing up the hierarchy
local function get_root_language(language)
    local parent_prop = "Is Variety Of"

    -- Loop upwards to find the root language
    while true do
        local query = "[[" .. language .. "]]|?" .. parent_prop
        local result = mw.smw.ask{ query }

        -- If no parent is found, we've reached the root
        if not result or not result[language] or not result[language][parent_prop] then
            break
        end

        -- Move up the hierarchy
        language = result[language][parent_prop]
    end

    return language
end

-- Main function to print the full tree for the current language
function p.tree(frame)
    local current_language = mw.title.getCurrentTitle().text
    local root_language = get_root_language(current_language)

    -- Construct the query with the root parameter
    local query = string.format(
        '[[Category:Language]] |format=tree |parent=Is Variety Of |limit=5000|root=%s',
        root_language
    )

    -- Return the processed result
    return frame:preprocess('{{#ask:' .. query .. '}}')
end

return p