Module:RootFinder: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 36: Line 36:
   end
   end


   return rootLanguage
  -- 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
end
end


return p
return p

Revision as of 11:49, 25 March 2025

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

local p = {}

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

  local query = "[[Is Variety Of::" .. languageName .. "]]"
  local results = frame:preprocess('{{#ask:' .. query .. '|limit=1}}')

  -- Handle no results
  if results == "" then
    return languageName, nil
  end

  -- Extract the next language
  local nextLanguage = mw.text.trim(results)

  -- Recursively find the root
  return findRootLanguage(frame, nextLanguage, visited)
end

function p.main(frame)
  local languageName = frame.args[1]
  if not languageName then
    return "Error: Language name not provided."
  end

  local rootLanguage, errorMessage = findRootLanguage(frame, languageName)

  if errorMessage then
    return errorMessage
  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
end

return p