Module:Lorebook/Debug: Difference between revisions

No edit summary
Add detailed raw SMW data debugging to diagnose primary keys issue
Line 77: Line 77:


local function normalizeValue(val)
local function normalizeValue(val)
   return val
  if type(val) == 'table' then
    return val[1] or ''
  end
   return val or ''
end
end


Line 99: Line 102:
   local normalized = {}
   local normalized = {}
   for i, row in ipairs(res) do
   for i, row in ipairs(res) do
    -- Debug: log raw row structure for first result
    if DEBUG and i == 1 then
      local debug_info = "Raw row[1]: " .. tostring(row[1]) .. " (type: " .. type(row[1]) .. ")\n"
      debug_info = debug_info .. "Raw row.primary: " .. tostring(row.primary) .. " (type: " .. type(row.primary) .. ")\n"
      debug_info = debug_info .. "Raw row['Primary keys']: " .. tostring(row['Primary keys']) .. " (type: " .. type(row['Primary keys']) .. ")\n"
      -- Store debug info in a global to access later
      _G._debug_row_info = debug_info
    end
   
     normalized[i] = {
     normalized[i] = {
       page = normalizeValue(row[1]),  -- First element is the page title when mainlabel is not '-'
       page = normalizeValue(row[1]),  -- First element is the page title when mainlabel is not '-'
Line 198: Line 210:
   local rows = getAllConcepts(world)
   local rows = getAllConcepts(world)
   output = output .. debug_log("Found " .. #rows .. " concepts")
   output = output .. debug_log("Found " .. #rows .. " concepts")
 
  -- Show raw row debug info
  if _G._debug_row_info then
    output = output .. "\n'''Raw SMW data (first row):'''\n<pre>\n" .. _G._debug_row_info .. "</pre>\n"
    _G._debug_row_info = nil  -- Clear after use
  end
    
    
   if #rows == 0 then
   if #rows == 0 then
Line 214: Line 232:
     output = output .. "\n'''Found concepts:'''\n"
     output = output .. "\n'''Found concepts:'''\n"
     for i, r in ipairs(rows) do
     for i, r in ipairs(rows) do
      local primary_debug = "none"
      if r.primary then
        primary_debug = tostring(r.primary) .. " (type: " .. type(r.primary) .. ")"
      end
       output = output .. "* " .. (r.page or "unnamed") ..  
       output = output .. "* " .. (r.page or "unnamed") ..  
                         " (mode: " .. (r.mode or "conditional") ..  
                         " (mode: " .. (r.mode or "conditional") ..  
                         ", primary: " .. (r.primary or "none") .. ")\n"
                         ", primary: " .. primary_debug .. ")\n"
     end
     end
     output = output .. "\n"
     output = output .. "\n"