Module:RootFinder: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 32: Line 32:
     local rootLanguage = currentLanguage
     local rootLanguage = currentLanguage
      
      
     -- Find the root language
     -- Find the ultimate root language
     while iterations < maxIterations do
     while iterations < maxIterations do
         -- Query to find parent language
         -- Query to find parent language
Line 54: Line 54:
     end
     end
      
      
     -- Construct the tree query
     -- Construct the comprehensive 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 |depth=10',  
         rootLanguage)
         rootLanguage)
      
      

Revision as of 11:07, 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)
    if not title then return "" end
    
    -- Trim whitespace
    title = title:match("^%s*(.-)%s*$")
    
    -- 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 initial language name
    local currentLanguage = sanitizeTitle(languageName)
    local varietyProperty = "Is_Variety_Of"
    local maxIterations = 10
    local iterations = 0
    local rootLanguage = currentLanguage
    
    -- Find the ultimate root language
    while iterations < maxIterations do
        -- Query to find parent language
        local askQuery = string.format('{{#ask: [[%s]][[%s::+]] |?%s |limit=1}}', 
            currentLanguage, varietyProperty, varietyProperty)
        
        local rawResult = frame:preprocess(askQuery)
        
        -- Extract parent language using a more robust method
        local parentLanguage = rawResult:match("%[%[" .. varietyProperty .. "::([^%]]+)%]%]")
        
        -- If no parent language is found, we've reached the root
        if not parentLanguage then
            break
        end
        
        -- Sanitize and update current language
        currentLanguage = sanitizeTitle(parentLanguage)
        rootLanguage = currentLanguage
        iterations = iterations + 1
    end
    
    -- Construct the comprehensive tree query
    local treeQuery = string.format('[[Category:Language]] |format=tree |parent=Is_Variety_Of |limit=5000 |root=%s |depth=10', 
        rootLanguage)
    
    -- Return the tree using preprocessing
    return frame:preprocess('{{#ask:' .. treeQuery .. '}}')
end

return p