Module:RootFinder: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
 
(49 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- Function to get the root language by traversing up the hierarchy
function p.search(frame)
local function get_root_language(language)
local startTitle = frame.args[1]
     local parent_prop = "Is Variety Of"
    if not startTitle or startTitle == "" then
     local level = 0
        return "Error: No start title provided"
    end
      
local prop = frame.args[2]
if not prop or prop == "" then
        return "Error: No property name provided"
    end
 
     local title = startTitle


    -- Loop upwards to find the root language
     while true do
     while true do
        level = level + 1
         local queryString = string.format('[[%s]]|?%s', title, prop)
         local query = "[[" .. language .. "]]|?" .. parent_prop
         local queryResult = mw.smw.getQueryResult(queryString)
         local result = mw.smw.ask{ query }


         -- Debugging output: Check the result of the query
         -- Ensure queryResult is valid and extract the first parent
         if result then
        local parent = nil
            mw.log("Level " .. level .. ": " .. mw.text.jsonEncode(result))
         if queryResult and type(queryResult) == "table" then
        else
        for k,v in pairs( queryResult.results ) do
            mw.log("Level " .. level .. ": No result for " .. language)
        if v.printouts then
            if v.printouts[prop] and  #v.printouts[prop] > 0 and v.printouts[prop][1].fulltext then
              parent = v.printouts[prop][1].fulltext
            end
            end
        end
         end
         end


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


         -- Move up the hierarchy
         -- Move up to the parent language
         language = result[language][parent_prop]
         title = parent
     end
     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)
    -- Ensure the root language is not the current language, otherwise show nothing
    if root_language == current_language then
        return "No root language found."
    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
    return frame:preprocess('{{#ask:' .. query .. '}}')
end
end


return p
return p

Latest revision as of 13:43, 26 March 2025

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

local p = {}

function p.search(frame)
	local startTitle = frame.args[1]
    if not startTitle or startTitle == "" then
        return "Error: No start title provided"
    end
    
	local prop = frame.args[2]
	if not prop or prop == "" then
        return "Error: No property name provided"
    end

    local title = startTitle

    while true do
        local queryString = string.format('[[%s]]|?%s', title, prop)
        local queryResult = mw.smw.getQueryResult(queryString)

        -- Ensure queryResult is valid and extract the first parent
        local parent = nil
        if queryResult and type(queryResult) == "table" then
        	for k,v in pairs( queryResult.results ) do
        		if v.printouts then
	            	if v.printouts[prop] and  #v.printouts[prop] > 0 and v.printouts[prop][1].fulltext then
	            	  parent = v.printouts[prop][1].fulltext
	            	end
            	end
        	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