Module:RootFinder: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 2: Line 2:


-- Function to recursively find the root language
-- Function to recursively find the root language
function p.findRootLanguage(languageName)
function p.findRootLanguage(frame, languageName)
     local rootLanguage = languageName
     local rootLanguage = languageName
     local varietyProperty = "Is Variety Of"
     local varietyProperty = "Is Variety Of"
Line 11: Line 11:
             rootLanguage, varietyProperty, varietyProperty)
             rootLanguage, varietyProperty, varietyProperty)
          
          
         local parentLanguage = mw.uri.decode(frame:preprocess(askQuery))
         local parentLanguage = frame:preprocess(askQuery)
        parentLanguage = parentLanguage:match("^%s*(.-)%s*$")
          
          
         -- If no parent language is found, we've reached the root
         -- If no parent language is found, we've reached the root
Line 19: Line 20:
          
          
         -- Update the root language to the parent
         -- Update the root language to the parent
         rootLanguage = parentLanguage:match("^%s*(.-)%s*$")
         rootLanguage = parentLanguage
     end
     end
      
      
Line 30: Line 31:
      
      
     -- Find the root language
     -- Find the root language
     local rootLanguage = p.findRootLanguage(languageName)
     local rootLanguage = p.findRootLanguage(frame, languageName)
      
      
     -- Construct the tree query
     -- Construct the tree query

Revision as of 11:03, 25 March 2025

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

local p = {}

-- Function to recursively find the root language
function p.findRootLanguage(frame, languageName)
    local rootLanguage = languageName
    local varietyProperty = "Is Variety Of"
    
    -- Keep querying for the parent language until no parent is found
    while true do
        local askQuery = string.format('{{#ask: [[%s]][[%s::+]] |?%s |limit=1 |format=list}}', 
            rootLanguage, varietyProperty, varietyProperty)
        
        local parentLanguage = frame:preprocess(askQuery)
        parentLanguage = parentLanguage:match("^%s*(.-)%s*$")
        
        -- If no parent language is found, we've reached the root
        if parentLanguage == "" then
            break
        end
        
        -- Update the root language to the parent
        rootLanguage = parentLanguage
    end
    
    return rootLanguage
end

-- Main function to generate the language tree
function p.languageTree(frame)
    local languageName = frame.args[1]
    
    -- 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