Module:RootFinder: Difference between revisions
From The Seven Sages of Rome
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- Function to escape MediaWiki page titles | |||
local function escapeTitle(title) | |||
-- Replace spaces with underscores | |||
title = title:gsub(" ", "_") | |||
-- Encode special characters | |||
title = mw.uri.encode(title, true) | |||
return title | |||
end | |||
-- Function to recursively find the root language | -- Function to recursively find the root language | ||
function p.findRootLanguage(frame, languageName) | function p.findRootLanguage(frame, languageName) | ||
local rootLanguage = languageName | local rootLanguage = languageName | ||
local varietyProperty = " | local varietyProperty = "Is_Variety_Of" | ||
local maxIterations = 10 -- Prevent infinite loops | local maxIterations = 10 -- Prevent infinite loops | ||
local iterations = 0 | local iterations = 0 | ||
-- Escape the initial language name | |||
rootLanguage = escapeTitle(rootLanguage) | |||
while iterations < maxIterations do | while iterations < maxIterations do | ||
| Line 19: | Line 33: | ||
break | break | ||
end | end | ||
-- Escape the parent language name | |||
parentLanguage = escapeTitle(parentLanguage) | |||
-- Update the root language to the parent | -- Update the root language to the parent | ||
| Line 40: | Line 57: | ||
-- Construct the tree query | -- Construct the tree query | ||
local treeQuery = string.format('[[Category:Language]] |format=tree |parent= | local treeQuery = string.format('[[Category:Language]] |format=tree |parent=Is_Variety_Of |limit=5000 |root=%s', | ||
rootLanguage) | rootLanguage) | ||
Revision as of 11:04, 25 March 2025
Documentation for this module may be created at Module:RootFinder/doc
local p = {}
-- Function to escape MediaWiki page titles
local function escapeTitle(title)
-- Replace spaces with underscores
title = title:gsub(" ", "_")
-- Encode special characters
title = mw.uri.encode(title, true)
return title
end
-- Function to recursively find the root language
function p.findRootLanguage(frame, languageName)
local rootLanguage = languageName
local varietyProperty = "Is_Variety_Of"
local maxIterations = 10 -- Prevent infinite loops
local iterations = 0
-- Escape the initial language name
rootLanguage = escapeTitle(rootLanguage)
while iterations < maxIterations do
-- Construct a query to find the parent language
local askQuery = string.format('{{#ask: [[%s]][[%s::+]] |?%s |limit=1 |format=list}}',
rootLanguage, varietyProperty, varietyProperty)
local parentLanguage = frame:preprocess(askQuery):match("^%s*(.-)%s*$")
-- If no parent language is found, we've reached the root
if parentLanguage == "" or parentLanguage == nil then
break
end
-- Escape the parent language name
parentLanguage = escapeTitle(parentLanguage)
-- Update the root language to the parent
rootLanguage = parentLanguage
iterations = iterations + 1
end
return rootLanguage
end
-- Main function to generate the language tree
function p.languageTree(frame)
local languageName = frame.args[1]
if not languageName or languageName == "" then
return "No language name provided"
end
-- Find the root language
local rootLanguage = p.findRootLanguage(frame, languageName)
-- Construct the tree query
local treeQuery = string.format('[[Category:Language]] |format=tree |parent=Is_Variety_Of |limit=5000 |root=%s',
rootLanguage)
-- Return the tree using preprocessing
return frame:preprocess('{{#ask:' .. treeQuery .. '}}')
end
return p