Module:RootFinder: Difference between revisions

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


local function findRootLanguage(frame, languageName, visited, path)
function p.search(frame)
  -- Prevent infinite loops in case of circular references
local startTitle = frame.args[1]
  visited = visited or {}
    if not startTitle or startTitle == "" then
  path = path or {}
        return "Error: No start title provided"
  if visited[languageName] then
    end
    return nil, "Circular reference detected: " .. languageName, path
   
  end
local prop = frame.args[2]
  visited[languageName] = true
if not prop or prop == "" then
  table.insert(path, languageName)
        return "Error: No property name provided"
    end


  -- Query the current language for its Is Variety Of value
    local title = startTitle
  local query = "[[ " .. languageName .. " ]] | ?Is Variety Of"
  local results = frame:preprocess('{{#ask:' .. query .. '|limit=1}}')


  -- Debugging: Append raw query results to the path
    while true do
  table.insert(path, "Query results: " .. results)
        local queryString = string.format('[[%s]]|?%s', title, prop)
        local queryResult = mw.smw.getQueryResult(queryString)


  -- Extract the parent language from the query results
        -- Ensure queryResult is valid and extract the first parent
  local parentLanguage = mw.text.match(results, "Is Variety Of=(.-)")
        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 there is no parent language, we have found the root
        -- If no parent is found, return the current title as root
  if not parentLanguage then
        if not parent or parent == "" then
    return languageName, nil, path
            return title
  end
        end


  -- Recursively find the root
        -- Move up to the parent language
  return findRootLanguage(frame, mw.text.trim(parentLanguage), visited, path)
        title = parent
end
     end
 
function p.main(frame)
  local languageName = frame.args[1]
  if not languageName then
    return "Error: Language name not provided."
  end
 
  local rootLanguage, errorMessage, path = findRootLanguage(frame, languageName)
 
  if errorMessage then
     return errorMessage .. "\nPath: " .. table.concat(path, " -> ")
  end
 
  -- Construct the Semantic MediaWiki query for the language tree
  local query = "[[Category:Language]]|format=tree|parent=Is Variety Of|limit=5000|root=" .. rootLanguage
  local tree = frame:preprocess('{{#ask:' .. query .. '}}')
 
  return tree .. "\nPath: " .. table.concat(path, " -> ")
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