Module:NewsLanding: Difference between revisions
From TwogPedia
mNo edit summary |
mNo edit summary |
||
Line 8: | Line 8: | ||
local tableContainer = mw.html.create('div'):node(mw.html.create('h2'):wikitext('Latest news')) | local tableContainer = mw.html.create('div'):node(mw.html.create('h2'):wikitext('Latest news')) | ||
local tables = 'News' | local tables = 'News' | ||
local fields = '_pageName, date' | local fields = '_pageName, date' | ||
local cargoArgs = { | local cargoArgs = { | ||
orderBy = 'date DESC', | orderBy = 'date DESC', | ||
limit = | limit = 50, | ||
offset = (args.page - 1) * 50 | |||
} | } | ||
local results = cargo.query(tables, fields, cargoArgs) | local results = cargo.query(tables, fields, cargoArgs) | ||
Line 20: | Line 21: | ||
for i = 1, #results do | for i = 1, #results do | ||
local result = results[i] | local result = results[i] | ||
dateString = mw.ustring.gsub(result.date, "%s(AM|PM)$", "") | 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 | -- Use os.date to convert the string to a table of date and time components |
Revision as of 07:26, 27 July 2023
Documentation for this module may be created at Module:NewsLanding/doc
local getArgs = require('Module:Arguments').getArgs
local cargo = mw.ext.cargo
local NewsLanding = {}
function NewsLanding.main(frame)
local args = getArgs(frame)
local tableContainer = mw.html.create('div'):node(mw.html.create('h2'):wikitext('Latest news'))
local tables = 'News'
local fields = '_pageName, date'
local cargoArgs = {
orderBy = 'date DESC',
limit = 50,
offset = (args.page - 1) * 50
}
local results = cargo.query(tables, fields, cargoArgs)
if #results > 0 then
for i = 1, #results do
local result = results[i]
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 formattedDate = os.date("%d %b %Y %H:%M", os.time(dateTimeTable))
local news = mw.html.create('div'):wikitext(formattedDate .. ' - [[' .. result._pageName .. ']]')
tableContainer:node(news)
end
else
return tableContainer:wikitext('No news in the database')
end
return tableContainer
end
return NewsLanding
No categories