Module:LanguageVarietyQuery: Difference between revisions

From The Seven Sages of Rome
(Created page with "local p = {} function p.queryLanguageVarietyPages(frame) local pageName = frame:preprocess('{{PAGENAME}}') -- First, get all language varieties local varietiesQuery = 'Category:LanguageIs Variety Of::' .. pageName .. '|?Has Language' local varietiesResult = frame:extensionParse('{{#ask:' .. varietiesQuery .. '}}') -- Prepare a query condition for all found language varieties local languageConditions = {} for _, varietyData i...")
 
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 2: Line 2:


function p.queryLanguageVarietyPages(frame)
function p.queryLanguageVarietyPages(frame)
     local pageName = frame:preprocess('{{PAGENAME}}')
    -- Allow passing the page name or language as a parameter
     local pageName = frame.args[1] or frame:preprocess('{{PAGENAME}}')
      
      
     -- First, get all language varieties
     -- First, get all language varieties
     local varietiesQuery = '[[Category:Language]][[Is Variety Of::' .. pageName .. ']]|?Has Language'
     local varietiesQuery = string.format('[[Category:Language]][[Is Variety Of::%s]]', pageName)
     local varietiesResult = frame:extensionParse('{{#ask:' .. varietiesQuery .. '}}')
     local varietiesResult = mw.smw.ask(varietiesQuery)
      
      
     -- Prepare a query condition for all found language varieties
     -- Prepare language conditions
     local languageConditions = {}
     local languageConditions = {}
     for _, varietyData in ipairs(varietiesResult) do
     for _, varietyData in ipairs(varietiesResult) do
         local languageName = varietyData['Has Language']
         table.insert(languageConditions, varietyData[1])
        if languageName then
            table.insert(languageConditions, '[[Has Language::' .. languageName .. ']]')
        end
     end
     end
      
      
Line 22: Line 20:
     end
     end
      
      
     -- Combine conditions with OR
     -- Combine conditions for the query
     local combinedCondition = table.concat(languageConditions, ' OR ')
     local combinedLanguages = {}
    for _, lang in ipairs(languageConditions) do
        table.insert(combinedLanguages, string.format('[[Has Language::%s]]', lang))
    end
    local combinedLanguagesStr = table.concat(combinedLanguages, ' OR ')
      
      
     -- Final query with desired properties
     -- Debug: print combined languages and raw query result
     local finalQuery = '{{#ask:' .. combinedCondition ..  
     local queryResult = mw.smw.ask(combinedLanguagesStr ..  
         '|?Is Entity Type=Type' ..
         '|?Is Entity Type=Type' ..
         '|?Has Regional Language=Regional or specific language' ..
         '|?Has Regional Language=Regional or specific language')
        '|format=datatable}}'
      
      
     return frame:extensionParse(finalQuery)
     return mw.text.jsonEncode(queryResult)
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