Module:NewsLanding/LatestMore

From TwogPedia
< Module:NewsLanding
Revision as of 15:37, 12 February 2024 by Couchor (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local getArgs = require('Module:Arguments').getArgs
local cargo = mw.ext.cargo

local p = {}

function p.main(frame)
	local args = getArgs(frame)
	local pages = {}
 
	local pattern = "(%d%d%d%d%-%d%d%-%d%d %d%d:%d%d:%d%d) %[%[(.-)|(.-)%]%]"

	for date, title in args.companies:gmatch(pattern) do
		table.insert(pages, {_pageName = title, date = date})
	end
	
	local hotItems = mw.html.create('div'):addClass('news-hot-items')
	local container = mw.html.create('div'):addClass('news-hot'):node(mw.html.create('h3'):addClass('tc'):wikitext('Latest Edited Pages')):node(hotItems)

	for i, page in ipairs(pages) do
		local title = mw.html.create('div'):wikitext('[[' .. page._pageName .. '|' .. mw.ext.displaytitle.get(page._pageName) .. ']]')
		local date = mw.html.create('div'):wikitext('Last edit: ' .. page.date)
		-- local textDiv = mw.html.create('div'):node(title):node(date)
		local newsItem = mw.html.create('div'):attr('style', 'display: flex; flex-direction: column; align-items: center; gap: 0.5rem;'):node(title):node(date)
		
		local logoDiv = mw.html.create('div')
		
		local title = mw.html.create('div'):wikitext('[[' .. page._pageName .. '|' .. mw.ext.displaytitle.get(page._pageName) .. ']]')
		local date = mw.html.create('div'):wikitext('Last edit: ' .. page.date)
		-- local textDiv = mw.html.create('div'):node(title):node(date)
		local newsItem = mw.html.create('div'):attr('style', 'display: flex; flex-direction: column; align-items: center; gap: 0.5rem;'):node(title):node(logoDiv):node(date)
		
		hotItems:node(newsItem)

		local split = string.find(page._pageName, '/') and mw.text.split(page._pageName, '/')[2] or ''
		local tables = 'Logos'
		local fields = '_pageName, logoAll, logoDark, logoLight, iconAll, iconLight, iconDark'
		local cargoArgs = {
			where = '_pageName = "' .. page._pageName .. '" OR _pageName= "Companies/' .. split .. '"'
		}
		local results = cargo.query(tables, fields, cargoArgs)

		if #results > 0 then
			local result = results[1]
			if results[1].iconAll then
				logoDiv:node(createLogo(result._pageName, results[1].iconAll))
			elseif results[1].iconLight and results[1].iconDark then
				logoDiv:node(createLogo(result._pageName, results[1].iconLight, 'light')):node(createLogo(result._pageName, results[1].iconDark, 'dark'))
			elseif results[1].logoAll then
				logoDiv:node(createLogo(result._pageName, results[1].logoAll))
			elseif results[1].logoLight and results[1].logoDark then
				logoDiv:node(createLogo(result._pageName, results[1].logoLight, 'light')):node(createLogo(result._pageName, results[1].logoDark, 'dark'))
			else  
				local logo = results[1].iconLight or results[1].iconDark or results[1].logoLight or results[1].logoDark
				logoDiv:node(createLogo(result._pageName, logo))
			end
		else
    		local pageContent = mw.title.new(page._pageName):getContent()
		
		    local img = getFirstImage(pageContent)
    		if img then
		        logoDiv:node(createLogo(page._pageName, img))
		    else
		        logoDiv:node(createLogo(page._pageName, 'Team_placeholder_dark.png', 'dark')):node(createLogo(page._pageName, 'Team_placeholder_light.png', 'light'))
		    end
		end
	end

	return container
end

function createLogo(page, url, class)
	local className = class and class .. ' h-100' or 'h-100'
	local logoURL = url and url or 'Team_placeholder_dark.png'
	local logoContainer = mw.html.create('div')
		:addClass(className)
	local logo = mw.html.create('div')
		:addClass('team-list-logo')
		:node('[[File:' .. logoURL .. '|80x80px|link=' .. page .. ']]')
	logoContainer:node(logo)
	return logoContainer
end

function getFirstImage(pageContent)
    -- List of parameters to check for image names
    local imageParameters = {"image", "logoAll", "logoLight", "logoDark"}
    -- Iterate through the parameters and find the first one with a value
    for _, paramName in ipairs(imageParameters) do
        local imageValue = string.match(pageContent, "|%s*" .. paramName .. "%s*=%s*([^|\n}]+)")

        if imageValue then
            -- Return the extracted image file name
            return imageValue
        end
    end

    return nil
end

return p