Module:NewsLanding/MostViewed: Difference between revisions

From TwogPedia
(Created page with "local getArgs = require('Module:Arguments').getArgs local cargo = mw.ext.cargo local p = {} function p.main(frame) local args = getArgs(frame) local pageTitles = {} for title in mw.ustring.gmatch(args.news, "%[%[([^\]]+)%]%]") do table.insert(pageTitles, mw.text.split(title, '|')[1]) 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'):wikitext(...")
 
mNo edit summary
 
Line 34: Line 34:


for j = 1, #categorySplit do
for j = 1, #categorySplit do
local span = mw.html.create('span'):wikitext('[[News/' .. categorySplit[j] .. '|' .. categorySplit[j] .. ']]')
local span = mw.html.create('span'):wikitext('[[News/' .. categorySplit[j] .. '|' .. categorySplit[j] .. ']] ')
category:node(span)
category:node(span)
end
end

Latest revision as of 07:00, 16 April 2024

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

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

local p = {}

function p.main(frame)
	local args = getArgs(frame)

	local pageTitles = {}
	
	for title in mw.ustring.gmatch(args.news, "%[%[([^\]]+)%]%]") do
        table.insert(pageTitles, mw.text.split(title, '|')[1])
    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'):wikitext('Hot')):node(hotItems)
	
	for i, pageTitle in ipairs(pageTitles) do
        local tables = 'News'
		local fields = '_pageName, date, category'
		local cargoArgs = {
			orderBy = 'date DESC',
			where = '_pageName = "' .. string.gsub(pageTitle, '"', '""') .. '"'
		}
		local results = cargo.query(tables, fields, cargoArgs)

		if #results > 0 then
			local result = results[1]

			
			local categorySplit = mw.text.split(result.category, ',')
			local category = mw.html.create('div'):wikitext('Categor' .. (#categorySplit > 1 and 'ies' or 'y') .. ': ')
				-- .. table.concat(categorySplit, ', '))

			for j = 1, #categorySplit do
				local span = mw.html.create('span'):wikitext('[[News/' .. categorySplit[j] .. '|' .. categorySplit[j] .. ']] ')
				category:node(span)
			end
			
			local dateString = mw.ustring.gsub(result.date, "%s(AM|PM)$", "")

			-- Use os.date to convert the string to a table of date and time components
			local dateTimeTable = os.date("*t", os.time({year=string.sub(dateString, 1, 4), month=string.sub(dateString, 6, 7), day=string.sub(dateString, 9, 10), hour=tonumber(string.sub(dateString, 12, 13)) + ((string.sub(dateString, 22, 22) == "PM") and 12 or 0), min=string.sub(dateString, 15, 16), sec=string.sub(dateString, 18, 19)}))
			
			-- Use os.date again to format the date and time components into a desired format
			local formatedDate = os.date("%d %b %Y %H:%M", os.time(dateTimeTable))
			
			local title = mw.html.create('div'):wikitext('[[' .. pageTitle .. '|' .. mw.ext.displaytitle.get(pageTitle) .. ']] - ' .. formatedDate)
			local newsItem = mw.html.create('div'):node(category):node(title)
			hotItems:node(newsItem)
		end
	end

	return container
end

return p