Module:ImportPerformances

From Angelina Jordan Wiki
Revision as of 07:46, 3 October 2025 by Most2dot0 (talk | contribs) (Let them be expanded on the page. Will that work?)

Documentation for this module may be created at Module:ImportPerformances/doc

-- Module:ImportPerformances
-- Reads a wiki page containing JSON (an array of objects) and emits literal transclusion calls
-- of Performance-devel and Video-devel for each object.
-- Usage: {{#invoke:ImportPerformances|import|source=PageWithJSON}}

local p = {}

-- Helper: safe tostring
local function s(v)
  if v == nil then return nil end
  return tostring(v)
end

-- Build a template call as literal text
local function makeTemplate(name, args)
  local parts = {}
  for k, v in pairs(args) do
    if v ~= nil and v ~= '' then
      table.insert(parts, '|' .. k .. '=' .. v)
    end
  end
  return '{{' .. name .. table.concat(parts, '') .. '}}'
end

function p.import(frame)
  local args = frame.args or {}
  local source = args.source or args[1]
  if not source or source == "" then
    return "Error: supply a 'source' page name containing JSON, e.g. |source=Module:MyData"
  end

  local titleObj = mw.title.new(source)
  if not titleObj then
    return "Error: invalid page name: " .. source
  end

  local raw = titleObj:getContent()
  if not raw then
    return "Error: could not read page content: " .. source
  end

  -- JSON decode function: prefer mw.text.jsonDecode if available, else fall back to Module:JSON
  local decode
  if mw.text and mw.text.jsonDecode then
    decode = mw.text.jsonDecode
  else
    local ok, json = pcall(require, 'Module:JSON')
    if ok and json and type(json.decode) == 'function' then
      decode = json.decode
    else
      return "Error: No JSON decoder available (need mw.text.jsonDecode or Module:JSON)."
    end
  end

  local ok2, data = pcall(decode, raw)
  if not ok2 then
    return "Error decoding JSON: " .. tostring(data)
  end

  if type(data) ~= 'table' then
    return "Error: JSON must be an array of objects"
  end

  local out = {}
  local id = 0

  for _, obj in ipairs(data) do
    id = id + 1

    -- Build args for Performance-devel. Only include keys that exist in JSON.
    local perfArgs = {}
    if obj.song ~= nil then perfArgs.song = s(obj.song) end
    if obj.event ~= nil then perfArgs.event = s(obj.event) end

    if obj.context ~= nil then
      if type(obj.context) == 'table' then
        local ctx = {}
        for i, e in ipairs(obj.context) do ctx[i] = (e == nil and '' or s(e)) end
        perfArgs.context = table.concat(ctx, '#')
      else
        perfArgs.context = s(obj.context)
      end
    end

    if obj.date ~= nil then perfArgs.date = s(obj.date) end
    if obj.type ~= nil then perfArgs["type"] = s(obj.type) end
    if obj.pos ~= nil then perfArgs.pos = s(obj.pos) end
    if obj["with"] ~= nil then perfArgs["with"] = s(obj["with"]) end
    if obj.comment ~= nil then perfArgs.comment = s(obj.comment) end

    perfArgs.id = tostring(id)

    local perfCall = makeTemplate('Performance-devel', perfArgs)
    table.insert(out, perfCall)

    local function addVideo(url, duration)
      if not url or url == '' then return end
      local vArgs = { id = tostring(id), url = s(url) }
      if duration ~= nil and duration ~= '' then vArgs.duration = s(duration) end
      local vCall = makeTemplate('Video-devel', vArgs)
      table.insert(out, vCall)
    end

    if obj.url ~= nil and obj.url ~= '' then
      addVideo(obj.url, obj.duration)
    end

    if obj.videos ~= nil and type(obj.videos) == 'table' then
      for _, v in ipairs(obj.videos) do
        if v ~= nil then
          local vurl = v.url or v["url"]
          local vdur = v.duration or obj.duration
          addVideo(vurl, vdur)
        end
      end
    end

    table.insert(out, "")
  end

  return '' .. table.concat(out, "\n\n") .. ''
end

return p