Module:Sandbox/Aurelain/Cargo: Difference between revisions
From Heroes of Might and Magic: Olden Era Official Wiki
Created page with "local p = {} function p.displayOverview(frame) -- 1. Define Cargo query parameters local tables = 'Unit' local fields = '_pageName, hp, offence' local cargoArgs = { orderBy = '_pageName ASC', limit = 100 -- Good practice to set a limit } -- 2. Execute the query -- This returns an array of tables containing your data local results = mw.ext.cargo.query(tables, fields, cargoArgs) -- 3. Handle empty results if not re..." |
No edit summary |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function p. | function p.displayUnits(frame) | ||
-- 1. | local targetLang = frame.args.lang or 'en' | ||
local tables = 'Unit' | |||
local fields = ' | -- 1. Query parameters | ||
local tables = 'Unit, Translation' | |||
local joinOn = 'Unit.id = Translation.target_id' | |||
-- We select the ID instead of _pageName | |||
local fields = 'Unit.id=id, Translation.language=language, Translation.name=transName, Unit.hp=hp, Unit.attack=attack' | |||
-- We ask Cargo to give us BOTH the English row AND the target language row | |||
local cargoArgs = { | local cargoArgs = { | ||
orderBy = ' | joinOn = joinOn, | ||
limit = | where = 'Translation.language = "en" OR Translation.language = "' .. targetLang .. '"', | ||
orderBy = 'Unit.id ASC', | |||
limit = 1000 -- Increased limit since each unit might return 2 rows now | |||
} | } | ||
local results = mw.ext.cargo.query(tables, fields, cargoArgs) | local results = mw.ext.cargo.query(tables, fields, cargoArgs) | ||
if not results or #results == 0 then | if not results or #results == 0 then | ||
return "No unit data found." | return "No unit data found." | ||
end | end | ||
-- | -- 2. Group the results by Unit ID | ||
local orderedIds = {} | |||
local units = {} | |||
-- Because Cargo returns a row for English and a row for the translation, | |||
-- we fold them together into a single Lua table based on their ID. | |||
for _, row in ipairs(results) do | |||
local id = row['id'] | |||
-- If this is the first time seeing this ID, set up its basic stats | |||
if not units[id] then | |||
table.insert(orderedIds, id) | |||
units[id] = { | |||
hp = tonumber(row['hp']) or 0, | |||
attack = tonumber(row['attack']) or 0, | |||
enName = nil, | |||
targetName = nil | |||
} | |||
end | |||
-- Save the names based on which language row we are currently looping over | |||
if row['language'] == 'en' then | |||
units[id].enName = row['transName'] | |||
end | |||
if row['language'] == targetLang then | |||
units[id].targetName = row['transName'] | |||
end | |||
end | |||
-- 3. Create the HTML table | |||
local htmlTable = mw.html.create('table') | local htmlTable = mw.html.create('table') | ||
:addClass('wikitable sortable') | :addClass('wikitable sortable') | ||
htmlTable:tag('tr') | htmlTable:tag('tr') | ||
:tag('th'):wikitext('Unit Name'):done() | :tag('th'):wikitext('Unit Name'):done() | ||
| Line 29: | Line 63: | ||
:tag('th'):wikitext('Attack'):done() | :tag('th'):wikitext('Attack'):done() | ||
-- | -- 4. Loop through our newly grouped Lua data to build the display rows | ||
for _, | for _, id in ipairs(orderedIds) do | ||
-- | local u = units[id] | ||
local | |||
local | -- Fallback logic: If English is somehow missing, show the raw ID. | ||
local | -- If the translation is missing, fall back to English. | ||
local originalName = u.enName or ('UnknownUnit_' .. id) | |||
local displayName = u.targetName or originalName | |||
-- Construct the localized page link | |||
local linkTarget = originalName | |||
if targetLang ~= 'en' then | |||
linkTarget = originalName .. '/' .. targetLang | |||
end | |||
local tr = htmlTable:tag('tr') | local tr = htmlTable:tag('tr') | ||
tr:tag('td'):wikitext('[[' .. | |||
tr:tag('td'):wikitext(hp):done() | -- The link now outputs correctly using the database IDs | ||
tr:tag('td'):wikitext(attack):done() | tr:tag('td'):wikitext('[[' .. linkTarget .. '|' .. displayName .. ']]'):done() | ||
tr:tag('td'):wikitext(tostring(u.hp)):done() | |||
tr:tag('td'):wikitext(tostring(u.attack)):done() | |||
end | end | ||
return tostring(htmlTable) | return tostring(htmlTable) | ||
end | end | ||
return p | return p | ||
Revision as of 04:55, 16 May 2026
Documentation for this module may be created at Module:Sandbox/Aurelain/Cargo/doc
local p = {}
function p.displayUnits(frame)
local targetLang = frame.args.lang or 'en'
-- 1. Query parameters
local tables = 'Unit, Translation'
local joinOn = 'Unit.id = Translation.target_id'
-- We select the ID instead of _pageName
local fields = 'Unit.id=id, Translation.language=language, Translation.name=transName, Unit.hp=hp, Unit.attack=attack'
-- We ask Cargo to give us BOTH the English row AND the target language row
local cargoArgs = {
joinOn = joinOn,
where = 'Translation.language = "en" OR Translation.language = "' .. targetLang .. '"',
orderBy = 'Unit.id ASC',
limit = 1000 -- Increased limit since each unit might return 2 rows now
}
local results = mw.ext.cargo.query(tables, fields, cargoArgs)
if not results or #results == 0 then
return "No unit data found."
end
-- 2. Group the results by Unit ID
local orderedIds = {}
local units = {}
-- Because Cargo returns a row for English and a row for the translation,
-- we fold them together into a single Lua table based on their ID.
for _, row in ipairs(results) do
local id = row['id']
-- If this is the first time seeing this ID, set up its basic stats
if not units[id] then
table.insert(orderedIds, id)
units[id] = {
hp = tonumber(row['hp']) or 0,
attack = tonumber(row['attack']) or 0,
enName = nil,
targetName = nil
}
end
-- Save the names based on which language row we are currently looping over
if row['language'] == 'en' then
units[id].enName = row['transName']
end
if row['language'] == targetLang then
units[id].targetName = row['transName']
end
end
-- 3. Create the HTML table
local htmlTable = mw.html.create('table')
:addClass('wikitable sortable')
htmlTable:tag('tr')
:tag('th'):wikitext('Unit Name'):done()
:tag('th'):wikitext('Health'):done()
:tag('th'):wikitext('Attack'):done()
-- 4. Loop through our newly grouped Lua data to build the display rows
for _, id in ipairs(orderedIds) do
local u = units[id]
-- Fallback logic: If English is somehow missing, show the raw ID.
-- If the translation is missing, fall back to English.
local originalName = u.enName or ('UnknownUnit_' .. id)
local displayName = u.targetName or originalName
-- Construct the localized page link
local linkTarget = originalName
if targetLang ~= 'en' then
linkTarget = originalName .. '/' .. targetLang
end
local tr = htmlTable:tag('tr')
-- The link now outputs correctly using the database IDs
tr:tag('td'):wikitext('[[' .. linkTarget .. '|' .. displayName .. ']]'):done()
tr:tag('td'):wikitext(tostring(u.hp)):done()
tr:tag('td'):wikitext(tostring(u.attack)):done()
end
return tostring(htmlTable)
end
return p


