Module:Gallery
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Gallery/doc
local p = {}
-- Helper function to parse range/list input (e.g., "1-242")
local function parseCardNumbers(input)
local numbers = {}
input = tostring(input)
input = mw.text.trim(input)
if tonumber(input) then
table.insert(numbers, tonumber(input))
return numbers
end
for part in mw.text.gsplit(input, ',') do
part = mw.text.trim(part)
local rangeStart, rangeEnd = part:match('^(%d+)[%-–](%d+)$')
if rangeStart and rangeEnd then
rangeStart = tonumber(rangeStart)
rangeEnd = tonumber(rangeEnd)
for i = rangeStart, rangeEnd do
table.insert(numbers, i)
end
else
local num = tonumber(part)
if num then
table.insert(numbers, num)
end
end
end
table.sort(numbers)
return numbers
end
-- Main diagnostic function using the ALIAS strategy.
function p.queries(frame)
local args = frame.args
if not args[1] then
args = frame:getParent().args
end
local setId = args[1] or ''
local cardInput = args[2] or ''
local dataBatchSize = tonumber(args[3]) or 15
if setId == '' or cardInput == '' then
return '<p class="error">Error: Set ID and card numbers are required.</p>'
end
local cardNumbers = parseCardNumbers(cardInput)
if #cardNumbers == 0 then
return '<p class="error">Error: No valid card numbers could be parsed.</p>'
end
local reportLines = {}
local expensiveCallCount = 0
local cardData = {}
table.insert(reportLines, "== Final Diagnostic: Property Alias Strategy ==")
table.insert(reportLines, string.format("Configuration: Data Batch Size = '''%d'''. Using `Template:CardDataRow` with aliases.", dataBatchSize))
-- Stage 1: Fetch Card Data in Batches using the helper template with aliases.
table.insert(reportLines, "\n--- Stage 1: Fetching and Mapping Card Data ---")
local totalCardsFound = 0
for i = 1, #cardNumbers, dataBatchSize do
expensiveCallCount = expensiveCallCount + 1
local batchEnd = math.min(i + dataBatchSize - 1, #cardNumbers)
local pagesToQuery = {}
for j = i, batchEnd do
table.insert(pagesToQuery, cardNumbers[j] .. '/204·EN·' .. setId)
end
table.insert(reportLines, string.format("\nBatch %d (Data): Issuing call #%d...", (i - 1) / dataBatchSize + 1, expensiveCallCount))
-- THE CRITICAL FIX: Define explicit aliases for each property.
local askResult = frame:callParserFunction('#ask',
'[[' .. table.concat(pagesToQuery, '||') .. ']]',
'?#=Card Name',
'?CardSet=The Set',
'?SetN=The Number',
'format=template',
'template=CardDataRow',
'link=none',
'sep=|||'
)
if askResult and askResult ~= '' then
local results = mw.text.split(askResult, '|||')
table.insert(reportLines, string.format("* '''Result:''' <span style='color:green;'>SUCCESS</span>. Found %d raw data record(s).", #results))
table.insert(reportLines, "* '''Micro-Log:''' Processing each record...")
for idx, recordStr in ipairs(results) do
table.insert(reportLines, string.format("** Record #%d Raw String: <code>%s</code>", idx, recordStr))
local props = mw.text.split(recordStr, '!!|!!')
local pageName = mw.text.trim(props[1] or '')
table.insert(reportLines, string.format("*** Step A (Page Name): Extracted <code>%s</code>", pageName))
if pageName ~= '' and not pageName:find('Template:CardDataRow') then
local cardNumStr = pageName:match('^([^/]+)/')
table.insert(reportLines, string.format("*** Step B (Card Number): Matched <code>%s</code> from page name.", cardNumStr or 'nil'))
if cardNumStr then
totalCardsFound = totalCardsFound + 1
cardData[cardNumStr] = {
mainValue = mw.text.trim(props[2] or ''),
cardSet = mw.text.trim(props[3] or ''),
setN = mw.text.trim(props[4] or '')
}
table.insert(reportLines, string.format("*** <span style='color:green;'>Stored Data:</span> Name=''%s'', CardSet=''%s'', SetN=''%s''",
cardData[cardNumStr].mainValue, cardData[cardNumStr].cardSet, cardData[cardNumStr].setN))
else
table.insert(reportLines, string.format("*** <span style='color:orange;'>SKIPPED:</span> Could not extract a card number from the page name to use as a key."))
end
else
table.insert(reportLines, string.format("*** <span style='color:red;'>PARSING FAILED:</span> Template is not being processed correctly or page name is empty."))
end
end
else
table.insert(reportLines, "* '''Result:''' <span style='color:red;'>FAILURE</span>. Query returned no data for this batch.")
end
end
-- Stage 2: Verify Correct Ordering
table.insert(reportLines, "\n--- Stage 2: Verifying Correct Order and Data Integrity ---")
table.insert(reportLines, "Looping through the *original requested numbers* to prove data can be accessed in the correct sequence.")
for _, cardNum in ipairs(cardNumbers) do
local numStr = tostring(cardNum)
local data = cardData[numStr]
if data then
table.insert(reportLines, string.format("* Card #%s: [✓] Data found. Name=''%s'', SetN=''%s''", numStr, data.mainValue, data.setN))
else
table.insert(reportLines, string.format("* Card #%s: [✗] <span style='color:red;'>No data was stored for this card number.</span>", numStr))
end
end
table.insert(reportLines, "\n--- Final Report ---")
table.insert(reportLines, string.format("* Total Card Data Records Stored: %d", totalCardsFound))
table.insert(reportLines, string.format("* '''Total Expensive Parser Function Calls: %d'''", expensiveCallCount))
return table.concat(reportLines, '\n')
end
return p