Module:RootFinder: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 1: Line 1:
-- Module:LanguageTree
local p = {}
local p = {}
local mw = require('mw')


-- Finds the root language by following the "Is Variety Of" property chain
function p.getVariety(page)
function p.findRootLanguage(frame, languageName)
     if not page or page == "" then
     local currentLanguage = languageName
         return "Error: No page name provided."
    local visited = {}
   
    while currentLanguage and not visited[currentLanguage] do
        visited[currentLanguage] = true
       
        -- Query to get the parent language
        local query = "[[" .. currentLanguage .. "]]|?Is Variety Of"
         local result = frame:preprocess('{{#ask:' .. query .. '}}')
       
        -- Extract parent language from the result
        local parent = nil
        for line in result:gmatch("[^\r\n]+") do
            local parentMatch = line:match("Is Variety Of:%s*(.+)")
            if parentMatch and parentMatch:gsub("%s+", "") ~= "" then
                parent = parentMatch:gsub("^%s*(.-)%s*$", "%1") -- Trim whitespace
                break
            end
        end
       
        if not parent or parent == "" then
            -- No parent found, this is the root language
            return currentLanguage
        else
            -- Move up to the parent language
            currentLanguage = parent
        end
     end
     end
      
      
     -- Handle potential circular reference
     -- Query Semantic MediaWiki for the property "Is Variety Of"
    return currentLanguage
     local result = mw.smw.ask{"[[" .. page .. "::+]] |?Is Variety Of"}
end
 
-- Main function to generate the language tree
function p.main(frame)
    -- Get the language name from the frame parameters
     local languageName = frame.args[1]
      
      
     if not languageName or languageName == "" then
     if result and result[page] and result[page]["Is Variety Of"] then
         return "Error: No language name provided"
         return "Is Variety Of: " .. result[page]["Is Variety Of"]
    else
        return "No 'Is Variety Of' property found for " .. page
     end
     end
   
    -- Find the root language
    local rootLanguage = p.findRootLanguage(frame, languageName)
   
    -- Construct the query for the tree
    local treeQuery = "[[Category:Language]]|format=tree|parent=Is Variety Of|limit=5000|root=" .. rootLanguage
   
    -- Execute the query using #ask and return the result
    return frame:preprocess('{{#ask:' .. treeQuery .. '}}')
end
end


return p
return p

Revision as of 14:33, 25 March 2025

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

local p = {}
local mw = require('mw')

function p.getVariety(page)
    if not page or page == "" then
        return "Error: No page name provided."
    end
    
    -- Query Semantic MediaWiki for the property "Is Variety Of"
    local result = mw.smw.ask{"[[" .. page .. "::+]] |?Is Variety Of"}
    
    if result and result[page] and result[page]["Is Variety Of"] then
        return "Is Variety Of: " .. result[page]["Is Variety Of"]
    else
        return "No 'Is Variety Of' property found for " .. page
    end
end

return p