Module:LanguageVarietyQuery: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 12: Line 12:
     local languageConditions = {}
     local languageConditions = {}
     for _, varietyData in ipairs(varietiesResult) do
     for _, varietyData in ipairs(varietiesResult) do
         table.insert(languageConditions, varietyData['_PAGE'])
         table.insert(languageConditions, varietyData[1])
     end
     end
      
      
Line 21: Line 21:
      
      
     -- Combine conditions for the query
     -- Combine conditions for the query
     local combinedLanguages = table.concat(
     local combinedLanguages = {}
         vim.tbl_map(function(lang) return '[[Has Language::' .. lang .. ']]' end, languageConditions),  
    for _, lang in ipairs(languageConditions) do
        ' OR '
         table.insert(combinedLanguages, string.format('[[Has Language::%s]]', lang))
    )
    end
    local combinedLanguagesStr = table.concat(combinedLanguages, ' OR ')
      
      
     -- Construct the full query string
     -- Debug: print combined languages and raw query result
     local fullQuery = string.format('{{#ask:%s|?Is Entity Type=Type|?Has Regional Language=Regional or specific language|format=datatable}}', combinedLanguages)
     local queryResult = mw.smw.ask(combinedLanguagesStr ..
        '|?Is Entity Type=Type' ..
        '|?Has Regional Language=Regional or specific language')
      
      
    -- Return the query string to be processed by MediaWiki
     return mw.text.jsonEncode(queryResult)
     return fullQuery
end
end


return p
return p

Latest revision as of 18:35, 4 March 2025

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 ')
    
    -- Debug: print combined languages and raw query result
    local queryResult = mw.smw.ask(combinedLanguagesStr .. 
        '|?Is Entity Type=Type' ..
        '|?Has Regional Language=Regional or specific language')
    
    return mw.text.jsonEncode(queryResult)
end

return p