Module:RootFinder: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 3: Line 3:
-- Function to sanitize page titles
-- Function to sanitize page titles
local function sanitizeTitle(title)
local function sanitizeTitle(title)
    if not title then return "" end
   
    -- Trim whitespace
    title = title:match("^%s*(.-)%s*$")
   
     -- Replace spaces with underscores
     -- Replace spaces with underscores
     title = title:gsub(" ", "_")
     title = title:gsub(" ", "_")
Line 20: Line 25:
     end
     end
      
      
     -- Sanitize the language name
     -- Sanitize the initial language name
     local sanitizedName = sanitizeTitle(languageName)
     local currentLanguage = sanitizeTitle(languageName)
   
    -- First, try to find the root language
    local rootLanguage = sanitizedName
     local varietyProperty = "Is_Variety_Of"
     local varietyProperty = "Is_Variety_Of"
     local maxIterations = 10
     local maxIterations = 10
     local iterations = 0
     local iterations = 0
    local rootLanguage = currentLanguage
      
      
    -- Find the root language
     while iterations < maxIterations do
     while iterations < maxIterations do
         -- Query to find 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}}',  
             rootLanguage, varietyProperty, varietyProperty)
             currentLanguage, varietyProperty, varietyProperty)
       
        local rawResult = frame:preprocess(askQuery)
          
          
         local parentLanguage = frame:preprocess(askQuery):match("^%s*(.-)%s*$")
        -- 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 no parent language is found, we've reached the root
         if parentLanguage == "" or parentLanguage == nil then
         if not parentLanguage then
             break
             break
         end
         end
          
          
         -- Update root language
         -- Sanitize and update current language
         rootLanguage = sanitizeTitle(parentLanguage)
         currentLanguage = sanitizeTitle(parentLanguage)
        rootLanguage = currentLanguage
         iterations = iterations + 1
         iterations = iterations + 1
     end
     end

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)
    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 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 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