Module:RootFinder: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 5: Line 5:
     local parent_prop = "Is Variety Of"
     local parent_prop = "Is Variety Of"
     local level = 0
     local level = 0
    local output = {}


     -- Loop upwards to find the root language
     -- Loop upwards to find the root language
Line 12: Line 13:
         local result = mw.smw.ask{ query }
         local result = mw.smw.ask{ query }


         -- Debugging output: Check the result of the query
         -- Log the query result to the output
         if result then
         if result then
             mw.log("Level " .. level .. ": " .. mw.text.jsonEncode(result))
             table.insert(output, "Level " .. level .. ": " .. mw.text.jsonEncode(result))
         else
         else
             mw.log("Level " .. level .. ": No result for " .. language)
             table.insert(output, "Level " .. level .. ": No result for " .. language)
         end
         end


Line 28: Line 29:
     end
     end


     return language
    -- Return the output for debugging purposes
     return table.concat(output, "\n")
end
end


Line 35: Line 37:
     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)
    -- Log the result of the get_root_language function
    local debug_output = "Root language: " .. root_language
    local log_output = get_root_language(current_language)


     -- Ensure the root language is not the current language, otherwise show nothing
     -- Ensure the root language is not the current language, otherwise show nothing
     if root_language == current_language then
     if root_language == current_language then
         return "No root language found."
         return "No root language found.\n" .. log_output
     end
     end


Line 47: Line 53:
     )
     )


     -- Return the processed result
     -- Return the processed result along with the debug output
     return frame:preprocess('{{#ask:' .. query .. '}}')
     return frame:preprocess('{{#ask:' .. query .. '}}') .. "\n\n" .. log_output
end
end


return p
return p

Revision as of 10:49, 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"
    local level = 0
    local output = {}

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

        -- Log the query result to the output
        if result then
            table.insert(output, "Level " .. level .. ": " .. mw.text.jsonEncode(result))
        else
            table.insert(output, "Level " .. level .. ": No result for " .. language)
        end

        -- 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 the output for debugging purposes
    return table.concat(output, "\n")
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)

    -- Log the result of the get_root_language function
    local debug_output = "Root language: " .. root_language
    local log_output = get_root_language(current_language)

    -- Ensure the root language is not the current language, otherwise show nothing
    if root_language == current_language then
        return "No root language found.\n" .. log_output
    end

    -- 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 along with the debug output
    return frame:preprocess('{{#ask:' .. query .. '}}') .. "\n\n" .. log_output
end

return p