Module:LanguageVarietyQuery

From The Seven Sages of Rome
Revision as of 18:24, 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]]|?Has Language', pageName)
    local varietiesResult = mw.smw.ask(varietiesQuery)
    
    -- Prepare a query condition for all found language varieties
    local languageConditions = {}
    for _, varietyData in ipairs(varietiesResult) do
        local languageName = varietyData['Has Language']
        if languageName then
            table.insert(languageConditions, string.format('[[Has Language::%s]]', languageName))
        end
    end
    
    -- If no varieties found, return empty
    if #languageConditions == 0 then
        return 'No language varieties found.'
    end
    
    -- Combine conditions with OR
    local combinedCondition = table.concat(languageConditions, ' OR ')
    
    -- Construct the full query string
    local fullQuery = string.format('{{#ask:%s|?Is Entity Type=Type|?Has Regional Language=Regional or specific language|format=datatable}}', combinedCondition)
    
    -- Return the query string to be processed by MediaWiki
    return fullQuery
end

return p