Module:RootFinder: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 11: Line 11:
   table.insert(path, languageName)
   table.insert(path, languageName)


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


Line 17: Line 18:
   table.insert(path, "Query results: " .. results)
   table.insert(path, "Query results: " .. results)


   -- Handle no results
   -- Extract the parent language from the query results
   if results == "" then
   local parentLanguage = mw.text.match(results, "Is Variety Of=(.-)")
 
  -- If there is no parent language, we have found the root
  if not parentLanguage then
     return languageName, nil, path
     return languageName, nil, path
   end
   end
  -- Extract the next language
  local nextLanguage = mw.text.trim(results)


   -- Recursively find the root
   -- Recursively find the root
   return findRootLanguage(frame, nextLanguage, visited, path)
   return findRootLanguage(frame, mw.text.trim(parentLanguage), visited, path)
end
end



Revision as of 12:54, 25 March 2025

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

local p = {}

local function findRootLanguage(frame, languageName, visited, path)
  -- Prevent infinite loops in case of circular references
  visited = visited or {}
  path = path or {}
  if visited[languageName] then
    return nil, "Circular reference detected: " .. languageName, path
  end
  visited[languageName] = true
  table.insert(path, languageName)

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

  -- Debugging: Append raw query results to the path
  table.insert(path, "Query results: " .. results)

  -- Extract the parent language from the query results
  local parentLanguage = mw.text.match(results, "Is Variety Of=(.-)")

  -- If there is no parent language, we have found the root
  if not parentLanguage then
    return languageName, nil, path
  end

  -- Recursively find the root
  return findRootLanguage(frame, mw.text.trim(parentLanguage), visited, path)
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

return p