Module:LanguageVarietyQuery

From The Seven Sages of Rome
Revision as of 18:34, 4 March 2025 by Noeth (talk | contribs)

This module retrieves language variety pages related to a given language from Semantic MediaWiki.

Usage

The module can be invoked using #invoke in a template or wiki page.

Parameters

The first unnamed parameter specifies the language name for which varieties should be retrieved.

Behavior

Queries Semantic MediaWiki for all pages categorized under Category:Language that are marked as a variety of the specified language.

If no varieties are found, it returns "No language varieties found."

Constructs a query to find entities associated with the retrieved language varieties.

Returns the query results in JSON format.

Example Output

If LanguageName = Latin and the wiki has the following varieties:

Has Variety Of = Classical Latin

Has Variety Of = Vulgar Latin

The module will return a JSON-encoded result containing entities related to Classical Latin and Vulgar Latin.

Notes

The module relies on Semantic MediaWiki (mw.smw.ask).

If no varieties exist for the given language, it returns an appropriate message.

The output is encoded in JSON for structured data handling.


local p = {}

function p.queryLanguageVarietyPages(frame)
    -- Allow passing the page name or language as a parameter
    local pageName = frame.args[1] or frame:preprocess('{{PAGENAME}}')
    
    -- First, get all language varieties
    local varietiesQuery = string.format('[[Category:Language]][[Is Variety Of::%s]]', pageName)
    local varietiesResult = mw.smw.ask(varietiesQuery)
    
    -- Prepare language conditions
    local languageConditions = {}
    for _, varietyData in ipairs(varietiesResult) do
        table.insert(languageConditions, varietyData[1])
    end
    
    -- If no varieties found, return empty
    if #languageConditions == 0 then
        return 'No language varieties found.'
    end
    
    -- Combine conditions for the query
    local combinedLanguages = {}
    for _, lang in ipairs(languageConditions) do
        table.insert(combinedLanguages, string.format('[[Has Language::%s]]', lang))
    end
    local combinedLanguagesStr = table.concat(combinedLanguages, ' OR ')
    
    -- Execute the query directly
    local queryResult = mw.smw.ask(combinedLanguagesStr .. 
        '|?Is Entity Type=Type' ..
        '|?Has Regional Language=Regional or specific language')
    
    -- Convert results to wikitext table
    local output = '{| class="wikitable"' ..
        '\n! Language' ..
        '\n! Entity Type' ..
        '\n! Regional Language'
    
    for _, result in ipairs(queryResult) do
        output = output .. '\n|-' ..
            '\n| ' .. (result['_PAGE'] or '') ..
            '\n| ' .. (result['Is Entity Type'] or '') ..
            '\n| ' .. (result['Has Regional Language'] or '')
    end
    
    output = output .. '\n|}'
    
    return output
end

return p