Module:RootFinder: Difference between revisions
From The Seven Sages of Rome
No edit summary |
No edit summary |
||
| Line 19: | Line 19: | ||
-- Extract the parent language from the query results | -- Extract the parent language from the query results | ||
local parentLanguage = | local parentLanguage = string.match(results, "Is Variety Of=(.-)") | ||
-- If there is no parent language, we have found the root | -- If there is no parent language, we have found the root | ||
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 = string.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