Module:RootFinder: Difference between revisions

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


function p.getLanguageTree(frame)
function p.search(frame)
    -- Get the language name from the first argument
local startTitle = frame.args[1]
    local languageName = frame.args[1]
    if not startTitle or startTitle == "" then
        return "Error: No start title provided"
    end
      
      
    if not languageName then
local prop = frame.args[2]
         return "No language name provided"
if not prop or prop == "" then
         return "Error: No property name provided"
     end
     end
   
 
    -- Function to trace the language to its root
     local title = startTitle
     local function findRootLanguage(lang)
 
        local currentLang = lang
    while true do
        local visited = {}
        local queryString = string.format('[[%s]]|?%s', title, prop)
       
        local queryResult = mw.smw.getQueryResult(queryString)
        while true do
 
            -- Prevent infinite loops
        -- Ensure queryResult is valid and extract the first parent
            if visited[currentLang] then
        local parent = nil
                return currentLang
        if queryResult and type(queryResult) == "table" then
            end
        for k,v in pairs( queryResult.results ) do
            visited[currentLang] = true
        if v.printouts then
           
            if v.printouts[prop] and  #v.printouts[prop] > 0 and v.printouts[prop][1].fulltext then
            -- Query for 'Is Variety Of' property
              parent = v.printouts[prop][1].fulltext
            local rootQuery = string.format('[[%s]]|?Is Variety Of', currentLang)
            end
            local varietyResult = mw.smw.ask(rootQuery)
            end
           
        end
            -- If no 'Is Variety Of' value found, we've reached the root
        end
            if not varietyResult or #varietyResult == 0 or not varietyResult[1]['Is Variety Of'] then
 
                return currentLang
        -- If no parent is found, return the current title as root
            end
        if not parent or parent == "" then
           
             return title
            -- Update current language to its parent
             currentLang = varietyResult[1]['Is Variety Of']
         end
         end
        -- Move up to the parent language
        title = parent
     end
     end
   
    -- Find the root language
    local rootLanguage = findRootLanguage(languageName)
   
    -- Generate the tree query
    local treeQuery = string.format('[[Category:Language]] |format=tree |parent=Is Variety Of |limit=5000|root=%s', rootLanguage)
   
    -- Preprocess the query to render the tree
    local treeResult = frame:preprocess('{{#ask:' .. treeQuery .. '}}')
   
    return treeResult
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