Module:RootFinder: Difference between revisions

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


-- Finds the root language by following the "Is Variety Of" property chain
function p.search(frame)
function p.findRootLanguage(frame, languageName)
local startTitle = frame.args[1]
    local currentLanguage = languageName
     if not startTitle or startTitle == "" then
     local visited = {}
        return "Error: No start title provided"
    end
      
      
    while currentLanguage and not visited[currentLanguage] do
local prop = frame.args[2]
         visited[currentLanguage] = true
if not prop or prop == "" then
       
         return "Error: No property name provided"
        -- Query to get the parent language
    end
         local query = "[[" .. currentLanguage .. "]]|?Is Variety Of"
 
         local result = frame:preprocess('{{#ask:' .. query .. '}}')
    local title = startTitle
       
 
         -- Extract parent language from the result
    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
         local parent = nil
         for line in result:gmatch("[^\r\n]+") do
         if queryResult and type(queryResult) == "table" then
            local parentMatch = line:match("Is Variety Of:%s*(.+)")
        for k,v in pairs( queryResult.results ) do
            if parentMatch and parentMatch:gsub("%s+", "") ~= "" then
        if v.printouts then
                parent = parentMatch:gsub("^%s*(.-)%s*$", "%1") -- Trim whitespace
            if v.printouts[prop] and  #v.printouts[prop] > 0 and v.printouts[prop][1].fulltext then
                break
              parent = v.printouts[prop][1].fulltext
            end
            end
            end
        end
         end
         end
          
 
         -- If no parent is found, return the current title as root
         if not parent or parent == "" then
         if not parent or parent == "" then
            -- No parent found, this is the root language
             return title
             return currentLanguage
        else
            -- Move up to the parent language
            currentLanguage = parent
         end
         end
    end
   
    -- Handle potential circular reference
    return currentLanguage
end


-- Main function to generate the language tree
        -- Move up to the parent language
function p.main(frame)
        title = parent
    -- Get the language name from the frame parameters
    local languageName = frame.args[1]
   
    if not languageName or languageName == "" then
        return "Error: No language name provided"
     end
     end
   
    -- Find the root language
    local rootLanguage = p.findRootLanguage(frame, languageName)
   
    -- Construct the query for the tree
    local treeQuery = "[[Category:Language]]|format=tree|parent=Is Variety Of|limit=5000|root=" .. rootLanguage
   
    -- Execute the query using #ask and return the result
    return frame:preprocess('{{#ask:' .. treeQuery .. '}}')
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