Module:Page/NewsList

local p = {}

function p.getNewsList(args) local cargo_query = mw.ext.cargo.query local whereQuery = '' if args.days and args.days ~= nil and args.days > 0 then local currentDate = os.date("!%Y-%m-%d") whereQuery = 'DATE_SUB("' .. currentDate .. '", INTERVAL 14 DAY) <= post_date' end local whereParams = { where = whereQuery, orderBy = 'post_date DESC', limit = 500, }   if args.limit and args.limit ~= nil and args.limit > 0 then whereParams.limit = args.limit end local newsList = cargo_query('News', '_pageName,title,category,DATE_FORMAT(post_date, "%Y-%m-%d %H:%i") = post_date', whereParams) or {} return newsList end

function p.formatNewsListTable(newsList) if newsList == nil or #newsList == 0 then return '' end local root = mw.html.create local tbl = root:tag('table'):addClass('wikitable'):addClass('sortable'):addClass('frozen-header') local tr = tbl:tag('tr') tr:tag('th'):wikitext('Title') tr:tag('th'):wikitext('Date') for ignore, newsItem in ipairs(newsList) do       local tr = tbl:tag('tr') tr:tag('td'):wikitext(..newsItem['title']..) tr:tag('td'):wikitext(newsItem['post_date']) end return tostring(root) end

function p.formatNewsList(newsList) if newsList == nil or #newsList == 0 then return '' end local root = mw.html.create local ul = root:tag('ul') for ignore, newsItem in ipairs(newsList) do       ul:tag('li'):wikitext(''..newsItem['title']..' ('..(newsItem['post_date'])..")\n") end return tostring(root) end

function p.printNewsList(frame) local args = require('Module:Arguments').getArgs(frame, {       parentFirst = true,    }) local newsList = p.getNewsList(args) if newsList and #newsList > 0 then return p.formatNewsListTable(newsList) else return 'No news found.' end end

return p