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 } .. " "       end index = index + 1 end return result end

return p