Module:GroupNameVariations: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
 
(13 intermediate revisions by the same user not shown)
Line 3: Line 3:


-- Return results
-- Return results
function p.ask(sage)
function p.ask(frame)
 
local queryResult = mw.smw.ask( string.format('[[Has Narrator::%s]][[Has Name Variation::+]]|?Has Name Variation', frame.args[1]) )
    if not mw.smw then
  if queryResult == nil then
        return "mw.smw module not found"
         return "(no values)"
    end
 
    if sage.args[1] == nil then
         return "no parameter found"
     end
     end


    local queryResult = mw.smw.getQueryResult( string.format('[[Has Narrator::%s]][[Has Name Variation::+]]|?Has Name Variation', sage.args[1]) )
    if queryResult == nil then
        return "(no values)"
    end
   
     if type( queryResult ) == "table" then
     if type( queryResult ) == "table" then
         local myResult = ""
         local myResult = ""
         for num, row in pairs( queryResult ) do
         for num, row in pairs( queryResult ) do
            myResult = myResult .. '* This is result #' .. num .. '\n'
             for property, data in pairs( row ) do
             for property, data in pairs( row ) do
                 local dataOutput = data
                 local dataOutput = data
Line 28: Line 17:
                     dataOutput = mw.text.listToText( data, ', ', ' and ')
                     dataOutput = mw.text.listToText( data, ', ', ' and ')
                 end
                 end
                 myResult = myResult .. '** ' .. property .. ': ' .. dataOutput .. '\n'
                 myResult = myResult .. dataOutput .. '\n'
             end
             end
         end
         end
         return myResult
         return myResult
    end
end
function groupMessages(array)
    local result = {};
    for k, v in ipairs(array) do
        if not result[v.sender] then
            result[v.sender] = {};
        end
        table.insert(result[v.sender], v);
     end
     end


    return myResult
  return result;
end
end


return p
return p

Latest revision as of 13:07, 4 June 2024

This module retrieves name variations associated with a given narrator from Semantic MediaWiki.

Usage

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

Parameters

The first unnamed parameter specifies the narrator's name whose name variations should be retrieved.

Behavior

The module queries Semantic MediaWiki for Has Name Variation properties associated with the specified Has Narrator value.

If no results are found, it returns "(no values)".

If multiple results are found, they are concatenated into a list separated by commas, with and before the last item.

Example Output

If NarratorName has the following data in Semantic MediaWiki:

Has Name Variation = John Doe

Has Name Variation = J. Doe

Has Name Variation = Jonathan D.

Then the module will return:

John Doe, J. Doe and Jonathan D.

Notes

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

If the query returns no results, (no values) is displayed.

The function groupMessages is defined but not used in this module.


-- Module:SMW
local p = {}

-- Return results
function p.ask(frame)
	local queryResult = mw.smw.ask( string.format('[[Has Narrator::%s]][[Has Name Variation::+]]|?Has Name Variation', frame.args[1]) )
	   if queryResult == nil then
        return "(no values)"
    end

    if type( queryResult ) == "table" then
        local myResult = ""
        for num, row in pairs( queryResult ) do
            for property, data in pairs( row ) do
                local dataOutput = data
                if type( data ) == 'table' then
                    dataOutput = mw.text.listToText( data, ', ', ' and ')
                end
                myResult = myResult .. dataOutput .. '\n'
            end
        end
        return myResult
    end
end

function groupMessages(array)
    local result = {};
    for k, v in ipairs(array) do
        if not result[v.sender] then
            result[v.sender] = {};
        end

        table.insert(result[v.sender], v);
    end

   return result;
end

return p