Module:Sandbox/Most2dot0/lua

-- Introduction to Lua in Wikipedia

local p = {}

-- Function to convert JSON data to a Lua table
function p.jsonToTable(jsonPage)
    -- Check if jsonPage is a valid string
    if type(jsonPage) ~= 'string' then
        return nil, "Invalid argument: JSON page name must be a string."
    end
    
    -- Try to load the page content
    local title = mw.title.new(jsonPage)
    if not title then
        return nil, "Failed to create title object for page: " .. jsonPage
    end

    local content = title:getContent()
    if not content then
        return nil, "Failed to load content for page: " .. jsonPage
    end

    -- Try to decode the JSON content
    local data = mw.text.jsonDecode(content)
    if type(data) ~= 'table' then
        return nil, "Invalid JSON data in page: " .. jsonPage
    end

    -- Initialize a Lua table to store the processed data
    local videosTable = {}

    -- Loop over the JSON data and copy the entries to the Lua table
    for i, video in ipairs(data) do
        table.insert(videosTable, {
            url = video.Url,
            title = video.Metadata["title"],
            description = video.Metadata["description"]
        })
    end

    return videosTable, nil
end

-- Function to generate a wikitable from the filtered content
function p.generateWikitable(frame)
    local jsonPage = frame.args[1]
    if not jsonPage then
        return "Error: No JSON page specified."
    end

    local videosTable, err = p.jsonToTable(jsonPage)

    if not videosTable then
        return "Wikitable from JSON data error: " .. err
    end

    local result = {"{| class=\"wikitable\"\n! URL\n! Title\n! Description\n"}
    
    for i, video in ipairs(videosTable) do
        table.insert(result, string.format("|-\n| %s\n| %s\n| %s\n", video.url, video.title, video.description))
    end
    
    table.insert(result, "|}")
    return table.concat(result, "\n")
end

-- start Gemini Flash

function p.filterSongs(frame)
  local filters = frame.args
  local results = {}
  local index = 1
  local maxEntries = 1000

  while index <= maxEntries do
    local songData = frame:expandTemplate{ title = "User:Most2dot0/sandbox/Template:SongData", args = { index } }
    if not songData or songData == "none" then
      break
    end

    local dataParts = mw.text.split(songData, ";")
    local songInfo = {
      Title = dataParts[2],
      Artist = dataParts[3],
      Year = dataParts[4],
      Album = dataParts[5]
    }

    local match = true
    for key, value in pairs(filters) do
      if value ~= "" and songInfo[key] ~= value then
        match = false
        break
      end
    end

    if match then
      table.insert(results, songInfo)
    end

    index = index + 1
  end

  return results
end

function p.displayWikitable(frame)
  local results = p.filterSongs(frame)
  local tableContent = ""

  if #results > 0 then
    tableContent = "{| class='wikitable'\n"
    tableContent = tableContent .. "! Title !! Artist !! Year !! Album\n"

    for _, song in ipairs(results) do
      tableContent = tableContent .. "|-\n"  -- Add a new row
      tableContent = tableContent .. "| " .. song.Title .. " \n| " .. song.Artist .. " \n| " .. song.Year .. " \n| " .. song.Album .. "\n"
    end

    tableContent = tableContent .. "|}\n"
  else
    tableContent = "No matching songs found."
  end

  return tableContent
end



-- end Gemini Flash

function p.filterData(frame)
    local filters = frame.args
    local filteredResults = {}
    local index = 1
    local maxEntries = 1000  -- Safety limit to prevent infinite loops

    while index <= maxEntries do
        local songData = frame:expandTemplate{ title = "User:Most2dot0/sandbox/Template:SongData", args = { index } }
        if not songData or songData == "none" then
            break
        end

        local dataParts = mw.text.split(songData, ";")
        local songInfo = {
            Title = dataParts[1],
            Artist = dataParts[2],
            Year = dataParts[3],
            Event = dataParts[4]
        }
        
        local match = true
        for key, value in pairs(filters) do
            if value ~= "" and songInfo[key] ~= value then
                match = false
                break
            end
        end

        if match then
            table.insert(filteredResults, songInfo)
        end
        
        index = index + 1
    end
    
    return filteredResults
end

function p.filterVideoData(frame)
    local filteredResults = {}
    local index = 1
    local maxEntries = 1000  -- Safety limit to prevent infinite loops

    while index <= maxEntries do
        local videoData = frame:expandTemplate{ title = "User:Most2dot0/sandbox/Template:VideoData", args = { index } }
        if not videoData or videoData == "none" then
            break
        end

        local dataParts = mw.text.split(videoData, ";")
        local videoInfo = {
            Title = dataParts[1],
            Event = dataParts[2],
            URL = dataParts[3],
            Channel = dataParts[4],
            Comment = dataParts[5]
        }
        
        table.insert(filteredResults, videoInfo)
        
        index = index + 1
    end
    
    return filteredResults
end

function p.display(frame)
    local filters = frame.args
    local result = ""
    local index = 1
    local maxEntries = 1000  -- Safety limit to prevent infinite loops

    while index <= maxEntries do
        local songData = frame:expandTemplate{ title = "User:Most2dot0/sandbox/Template:SongData", args = { index } }
        if not songData or songData == "none" then
            break
        end

        local dataParts = mw.text.split(songData, ";")
        local songInfo = {
	      Title = dataParts[2],
	      Artist = dataParts[3],
	      Year = dataParts[4],
	      Album = dataParts[5]
       }
        
        local match = true
        for key, value in pairs(filters) do
            if value ~= "" and songInfo[key] ~= value then
                match = false
                break
            end
        end

        if match then
            result = result .. frame:expandTemplate{ 
                title = "User:Most2dot0/sandbox/Template:SongDataDisplay", 
                args = songInfo
            } .. "<br>"
        end
        
        index = index + 1
    end
    
    return result
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.