Module:Lorebook/Debug: Difference between revisions

No edit summary
Escape curly braces ({/}) in debug output to prevent template invocation
 
(6 intermediate revisions by the same user not shown)
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


local function getAllConcepts(world)
local function getAllConcepts(world)
   local query = string.format('[[Category:Concept]][[Belongs to world::%s]]', world)
   local query = {
  local res = mw.smw.ask(query, {
    '[[Category:Concept]][[Belongs to world::' .. world .. ']]',
     ['?Plist'] = 'plist',
     '?Plist=plist',
     ['?AliChat'] = 'alichat',
     '?AliChat=alichat',
     ['?Primary keys'] = 'primary',
     '?Primary keys=primary',
     ['?Secondary keys'] = 'secondary',
     '?Secondary keys=secondary',
     ['?Logic'] = 'logic',
     '?Logic=logic',
     ['?Key mode'] = 'mode',
     '?Key mode=mode',
     ['?Parent concept'] = 'parent',
     '?Parent concept=parent',
     ['?Non-recursable'] = 'nonrec',
     '?Non-recursable=nonrec',
     ['?Placement'] = 'placement',
     '?Placement=placement',
     limit = 999
     limit = 999
   })
   }
 
  local res = mw.smw.ask(query)
    
    
   if not res then return {} end
   if not res then return {} end
Line 99: Line 104:
   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"
     
      -- List all keys in the row table
      debug_info = debug_info .. "\nAll keys in row:\n"
      for k, v in pairs(row) do
        debug_info = debug_info .. "  [" .. tostring(k) .. "] = " .. tostring(v) .. " (type: " .. type(v) .. ")\n"
      end
     
      -- 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
       plist = normalizeValue(row.plist),
       plist = normalizeValue(row.plist),
       alichat = normalizeValue(row.alichat),
       alichat = normalizeValue(row.alichat),
Line 183: Line 204:
   end
   end
   if #chatChunks > 0 then
   if #chatChunks > 0 then
    if #plistChunks > 0 then
      text = text .. '\n'  -- Add extra newline between plist and chat sections
    end
     text = text .. table.concat(chatChunks, '\n')
     text = text .. table.concat(chatChunks, '\n')
   end
   end
Line 198: Line 222:
   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 244:
     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"
Line 257: Line 291:
   end
   end
    
    
   output = output .. result
   -- Escape curly braces and use <pre> tag to preserve formatting
    
  if result and result ~= '' then
  if DEBUG then
    result = result:gsub('{', '&#123;'):gsub('}', '&#125;')
     output = output .. "\n</pre>\n"
    output = output .. result .. '\n</pre>\n'
   else
     output = output .. "(empty output)\n</pre>\n"
   end
   end