Module:RootFinder: Difference between revisions
From The Seven Sages of Rome
No edit summary |
No edit summary Tag: Reverted |
||
| Line 7: | Line 7: | ||
if not languageName then | if not languageName then | ||
return "No language name provided" | return "No language name provided" | ||
end | |||
-- Function to escape special characters for wiki page titles | |||
local function escapeTitle(title) | |||
-- Replace spaces with underscores | |||
title = title:gsub(" ", "_") | |||
-- Capitalize first letter | |||
title = title:sub(1,1):upper() .. title:sub(2) | |||
return title | |||
end | end | ||
-- Function to trace the language to its root | -- Function to trace the language to its root | ||
local function findRootLanguage(lang) | local function findRootLanguage(lang) | ||
local currentLang = lang | local currentLang = escapeTitle(lang) | ||
local visited = {} | local visited = {} | ||
| Line 22: | Line 31: | ||
-- Query for 'Is Variety Of' property | -- Query for 'Is Variety Of' property | ||
local rootQuery = string.format('[[%s]] | local rootQuery = { | ||
string.format('[[%s]]', currentLang), | |||
'?Is Variety Of' | |||
} | |||
local varietyResult = mw.smw.ask(rootQuery) | local varietyResult = mw.smw.ask(rootQuery) | ||
| Line 30: | Line 42: | ||
end | end | ||
-- Update current language to its parent | -- Update current language to its parent and escape it | ||
currentLang = varietyResult[1]['Is Variety Of'] | currentLang = escapeTitle(varietyResult[1]['Is Variety Of']) | ||
end | end | ||
end | end | ||
Revision as of 14:21, 25 March 2025
Documentation for this module may be created at Module:RootFinder/doc
local p = {}
function p.getLanguageTree(frame)
-- Get the language name from the first argument
local languageName = frame.args[1]
if not languageName then
return "No language name provided"
end
-- Function to escape special characters for wiki page titles
local function escapeTitle(title)
-- Replace spaces with underscores
title = title:gsub(" ", "_")
-- Capitalize first letter
title = title:sub(1,1):upper() .. title:sub(2)
return title
end
-- Function to trace the language to its root
local function findRootLanguage(lang)
local currentLang = escapeTitle(lang)
local visited = {}
while true do
-- Prevent infinite loops
if visited[currentLang] then
return currentLang
end
visited[currentLang] = true
-- Query for 'Is Variety Of' property
local rootQuery = {
string.format('[[%s]]', currentLang),
'?Is Variety Of'
}
local varietyResult = mw.smw.ask(rootQuery)
-- If no 'Is Variety Of' value found, we've reached the root
if not varietyResult or #varietyResult == 0 or not varietyResult[1]['Is Variety Of'] then
return currentLang
end
-- Update current language to its parent and escape it
currentLang = escapeTitle(varietyResult[1]['Is Variety Of'])
end
end
-- Find the root language
local rootLanguage = findRootLanguage(languageName)
-- Generate the tree query
local treeQuery = string.format('[[Category:Language]] |format=tree |parent=Is Variety Of |limit=5000|root=%s', rootLanguage)
-- Preprocess the query to render the tree
local treeResult = frame:preprocess('{{#ask:' .. treeQuery .. '}}')
return treeResult
end
return p