Module:RootFinder: Difference between revisions

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


function p._findRoot(language)
function p.search(frame)
     local current = language
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
     while true do
        -- Query for the 'Is Variety Of' property of the current language
         local queryString = string.format('[[%s]]|?%s', title, prop)
         local results = mw.smw.ask({
        local queryResult = mw.smw.getQueryResult(queryString)
            '[[Category:Language]]',
 
            '[[' .. current .. ']]',
         -- Ensure queryResult is valid and extract the first parent
            '?Is Variety Of'
         local parent = nil
         })
         if queryResult and type(queryResult) == "table" then
          
        for k,v in pairs( queryResult.results ) do
         -- Exit if no results or invalid response
        if v.printouts then
        if not results or #results == 0 then break end
            if v.printouts[prop] and  #v.printouts[prop] > 0 and v.printouts[prop][1].fulltext then
       
              parent = v.printouts[prop][1].fulltext
        -- Extract parent data from the first result
            end
        local parentData = results[1]['Is Variety Of']
            end
          
        end
         -- Exit if no parent found
         end
         if not parentData or #parentData == 0 then break end
 
       
         -- If no parent is found, return the current title as root
         -- Move to the parent language (using fulltext for page name)
         if not parent or parent == "" then
         current = parentData[1].fulltext
            return title
        end
 
         -- Move up to the parent language
         title = parent
     end
     end
    return current
end
function p.main(frame)
    local language = frame.args[1]  -- Get input language from template
    return p._findRoot(language)
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