Module:Sandbox/Miria~01/sumMedals
Usage
[edit source]This Lua module is designed to calculate and return the sum of medal values (Gold, Silver, Bronze, or Total) from a specified class within a table on Wikipedia pages. It is particularly useful for automatically updating medal totals in infoboxes and tables.
Syntax:
{{#invoke:Sandbox/Miria~01/sumMedals|calculateSum|targetClass|column|{{FULLPAGENAME}}}}
for information:
module name= Sandbox/Miria~01/sumMedals
{{FULLPAGENAME}} = pagename of the page module is used (FULLPAGENAME to get full page name)
Parameters:
- `targetClass` (string, required): The class name of the table to be scanned for medal values (e.g., "summerMedals" or "winterMedals").
- `column` (string, required): The type of medal value to be calculated and returned. Valid options are "Gold," "Silver," "Bronze," "Total," or "All" (for a combined string of all medal types).
Examples:
Calculate the total number of Gold medals for "summerMedals" class on the current page:
{{#invoke:Sandbox/Miria~01/sumMedals|calculateSum|summerMedals|Gold|{{FULLPAGENAME}}}}
Calculate the total number of Silver medals for "winterMedals" class on a specific page:
{{#invoke:Sandbox/Miria~01/sumMedals|calculateSum|winterMedals|Silver|{{FULLPAGENAME}}}}
Calculate the total medal counts for Gold, Silver, Bronze, and Total for "summerMedals" class and return it with four cells (for Gold, Silver, Bronze, and Total) in the last row of the table:
{{#invoke:Sandbox/Miria~01/sumMedals|calculateSum|summerMedals|All|{{FULLPAGENAME}}}}
Instructions:
Infobox Usage:
1. To automatically update infoboxes with medal counts for e.g two targetCalss (like summerMedals or winterMedals), use the module in the infobox template as shown in this example:
| gold = {{#expr: {{#invoke:Sandbox/Miria~01/sumMedals|calculateSum|summerMedals|Gold|{{FULLPAGENAME}}}} + {{#invoke:Sandbox/Miria~01/sumMedals|winterMedals|targetClass|Gold|{{FULLPAGENAME}}}}}}
| silver = {{#expr: {{#invoke:Sandbox/Miria~01/sumMedals|calculateSum|summerMedals|Silver|{{FULLPAGENAME}}}} + {{#invoke:Sandbox/Miria~01/sumMedals|winterMedals|targetClass|Gold|{{FULLPAGENAME}}}}}}
| bronze = {{#expr: {{#invoke:Sandbox/Miria~01/sumMedals|calculateSum|summerMedals|Bronze|{{FULLPAGENAME}}}} + {{#invoke:Sandbox/Miria~01/sumMedals|winterMedals|targetClass|Gold|{{FULLPAGENAME}}}}}}
Table Usage:
2. In your table, ensure that each line starts with the corresponding class attribute (`class="xxx"`) to specify whether it belongs to "summerMedals" or "winterMedalse as shown in this example:
|-
|align=left class="summerMedals"| {{Flagicon|RUS}} {{GamesName|WOG|2014}} || [[Czech Republic at the 2014 Winter Olympics|88]] || 2 || 4 || 3 || '''9''' || [[2014 Winter Olympics medal table|15]]
Replace `xxx` with the appropriate class name.
Note:
If the target class is not found in the table, the module will return 0 for all medal types.
local p = {}
function p.calculateSum(frame)
local args = frame.args
local targetClass = args[1] or '' -- Moved targetClass to the first argument
local column = args[2]
local wikitext = mw.title.new(args[3]):getContent()
local sum = 0
local sumGold = 0
local sumSilver = 0
local sumBronze = 0
local sumTotal = 0
-- Define a pattern to capture numbers enclosed in triple apostrophes or unformatted numbers
local numPattern = "|%s*([%s']*%d+[%s']*)%s*|"
local trimmedRow = ""
-- Iterate through the rows of the table
for row in mw.text.gsplit(wikitext, "\n") do
-- Check if the row contains the specified class
if mw.ustring.find(row, 'class="' .. targetClass .. '"') then
trimmedRow = row:match('.*class="' .. targetClass .. '"(.*)') -- Extract everything after the class attribute
-- Extract the value from the specified column
local i = 0
for value in mw.ustring.gmatch(trimmedRow, numPattern) do
i = i + 1
if value then
-- Remove triple apostrophes, but treat as a string
local stringValue = value:gsub("['%s]", "")
if i == 1 then
sumGold = sumGold + tonumber(stringValue) end
if i == 2 then
sumSilver = sumSilver + tonumber(stringValue) end
if i == 3 then
sumBronze = sumBronze + tonumber(stringValue) end
if i == 4 then
sumTotal = sumTotal + tonumber(stringValue) end
end
end
end
end
if column == "Gold" then return sumGold end
if column == "Silver" then return sumSilver end
if column == "Bronze" then return sumBronze end
if column == "Total" then return sumTotal end
if column == "All" then
return tostring(sumGold) .. '||' .. tostring(sumSilver) .. '||' .. tostring(sumBronze) .. '||' .. tostring(sumTotal)
end
return tostring(sum)
end
return p