Module:RootFinder
From The Seven Sages of Rome
Documentation for this module may be created at Module:RootFinder/doc
local p = {}
function p.getRootLanguage(startTitle)
if not startTitle or startTitle == "" then
return "Error: No start title provided"
end
local title = startTitle
local prop = "Is Variety Of"
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["Is Variety Of"] and #v.printouts["Is Variety Of"] > 0 and v.printouts["Is Variety Of"][1].fulltext then
parent = v.printouts["Is Variety Of"][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