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 find the root language
-- Function to sanitize page titles
function p.findRootLanguage(frame, languageName)
local function sanitizeTitle(title)
     local rootLanguage = languageName
    -- Replace spaces with underscores
     local varietyProperty = "Is Variety Of"
    title = title:gsub(" ", "_")
     local maxIterations = 10 -- Prevent infinite loops
   
    -- Remove any problematic characters
    title = title:gsub("[<>%[%]|{}]", "")
   
    return title
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
   
    -- Sanitize the language name
    local sanitizedName = sanitizeTitle(languageName)
   
    -- First, try to find the root language
     local rootLanguage = sanitizedName
     local varietyProperty = "Is_Variety_Of"
     local maxIterations = 10
     local iterations = 0
     local iterations = 0
      
      
     while iterations < maxIterations do
     while iterations < maxIterations do
         -- Construct a query to find the parent language
         -- Query to find parent language
         local askQuery = string.format('{{#ask: [[%s]][[%s::+]] |?%s |limit=1 |format=list}}',  
         local askQuery = string.format('{{#ask: [[%s]][[%s::+]] |?%s |limit=1 |format=list}}',  
             mw.text.encode(rootLanguage), varietyProperty, varietyProperty)
             rootLanguage, varietyProperty, varietyProperty)
          
          
         local parentLanguage = frame:preprocess(askQuery):match("^%s*(.-)%s*$")
         local parentLanguage = frame:preprocess(askQuery):match("^%s*(.-)%s*$")
Line 20: Line 41:
         end
         end
          
          
         -- Update the root language to the parent
         -- Update root language
         rootLanguage = parentLanguage
         rootLanguage = sanitizeTitle(parentLanguage)
         iterations = iterations + 1
         iterations = iterations + 1
     end
     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
     -- Construct the tree query
     local treeQuery = string.format('[[Category:Language]] |format=tree |parent=Is Variety Of |limit=5000 |root=%s',  
     local treeQuery = string.format('[[Category:Language]] |format=tree |parent=Is_Variety_Of |limit=5000 |root=%s',  
         mw.text.encode(rootLanguage))
         rootLanguage)
      
      
     -- Return the tree using preprocessing
     -- Return the tree using preprocessing

Revision as of 11:06, 25 March 2025

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

local p = {}

-- Function to sanitize page titles
local function sanitizeTitle(title)
    -- Replace spaces with underscores
    title = title:gsub(" ", "_")
    
    -- Remove any problematic characters
    title = title:gsub("[<>%[%]|{}]", "")
    
    return title
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
    
    -- Sanitize the language name
    local sanitizedName = sanitizeTitle(languageName)
    
    -- First, try to find the root language
    local rootLanguage = sanitizedName
    local varietyProperty = "Is_Variety_Of"
    local maxIterations = 10
    local iterations = 0
    
    while iterations < maxIterations do
        -- Query to find 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
        
        -- Update root language
        rootLanguage = sanitizeTitle(parentLanguage)
        iterations = iterations + 1
    end
    
    -- 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