Module:NewsSearch: Difference between revisions

From TwogPedia
mNo edit summary
Tag: Manual revert
mNo edit summary
Line 2: Line 2:
local cargo = mw.ext.cargo
local cargo = mw.ext.cargo


local limit = 25
local limit = 10


local NewsSearch = {}
local NewsSearch = {}
Line 9: Line 9:
local args = getArgs(frame)
local args = getArgs(frame)
if args.games == nil and args.categories == nil then return tableContainer:wikitext('At least one category or game needs to be chosen') end
if args.games == nil and args.categories == nil and args.tag == nil then
-- If initial load, then display nothing
if args.initial then return
-- else return tableContainer:wikitext(table.concat(args, ', ')) end
else return mw.html.create('div'):wikitext('At least one category or game needs to be chosen.') end
end
if args.more then return NewsSearch.html(args.offset or 0, args) end
if args.more then return NewsSearch.html(args.page or 1, args) end
local tableContainer = mw.html.create('div')
--  local listContainer = mw.html.create('div'):attr('id', 'list__container')
    local listContainer = mw.html.create('div'):attr('id', 'list__container')
-- tableContainer:node(listContainer)
tableContainer:node(listContainer)
local html = NewsSearch.html(args.offset or 0, args)
local html = NewsSearch.html(args.page or 1, args)
if html == nil or html == '' then
if html == nil or html == '' then
return listContainer:wikitext('No news found')
return '<div>No news found</div>'
else
else
  listContainer:node(html)
  return html
local loadMore = frame:callParserFunction{ name = '#widget', args = { 'Pagination', id = 'list__container', template = 'NewsSearch', arg = mw.text.jsonEncode({categories = args.categories}) } }
-- local loadMore = frame:callParserFunction{ name = '#widget', args = { 'Pagination', id = 'list__container', template = 'NewsSearch', arg = mw.text.jsonEncode({categories = args.categories}) } }
tableContainer:node(loadMore)
-- tableContainer:node(loadMore)
end
end
return tableContainer
end
end


function NewsSearch.query(page, args)
function NewsSearch.query(offset, args)
local whereStr = ''
local whereStr = ''


Line 55: Line 58:
end
end
whereStr = whereStr .. ')'
whereStr = whereStr .. ')'
end
if args.tag then
args.tag = string.gsub(args.tag, '_', ' ')
if args.categories or args.games then
whereStr = whereStr .. ' AND tags HOLDS LIKE "%' .. args.tag .. '%"'
else
whereStr = whereStr .. ' tags HOLDS LIKE "%' .. args.tag .. '%"'
end
end
end
local tables = 'News'
local tables = 'News'
local fields = '_pageName, date, category, game'
local fields = '_pageName, date, category, game, image, content'
local cargoArgs = {
local cargoArgs = {
where = whereStr,
where = whereStr,
orderBy = 'date DESC',
orderBy = 'date DESC',
limit = limit,
limit = limit,
offset = (page - 1) * limit
offset = offset
}
}
local results = cargo.query(tables, fields, cargoArgs)
local results = cargo.query(tables, fields, cargoArgs)
     return results
     return results
end
end


function NewsSearch.html(page, args)
function NewsSearch.html(offset, args)
local results = NewsSearch.query(page, args)
local results = NewsSearch.query(offset, args)
local list = ''
local list = ''
Line 77: Line 89:
for i = 1, #results do
for i = 1, #results do
local result = results[i]
local result = results[i]
local image = mw.html.create('div'):wikitext('[[File:' .. result.image .. '|300px]]')
local title = mw.html.create('div'):addClass('news-latest-item-title'):wikitext(mw.ext.displaytitle.get(result._pageName))
local description = getExcerpt(removeLinks(result.content))
local dateString = mw.ustring.gsub(result.date, "%s(AM|PM)$", "")
local dateString = mw.ustring.gsub(result.date, "%s(AM|PM)$", "")


Line 83: Line 99:
-- Use os.date again to format the date and time components into a desired format
-- 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 formatedDate = os.date("%d %b %Y %H:%M", os.time(dateTimeTable))
 
local news = mw.html.create('div'):wikitext(formattedDate .. ' - [[' .. result._pageName .. ']]')
local date = mw.html.create('div'):wikitext(formatedDate)
list = list .. tostring(news)
local text = mw.html.create('div'):addClass('news-latest-item-text'):node(title):node(description):node(date)
local div = mw.html.create('div'):addClass('news-latest-item'):node(image):node(text):node(mw.html.create('div'):addClass('link-overlay'):wikitext('[[File:Team_placeholder_light.png|1x1px|link=' .. result._pageName .. ']]'))
list = list .. tostring(div)
end
end
else
else
Line 92: Line 110:
end
end
return list
return list
end
function getExcerpt(text)
    return text:sub(1, 100)
end
function removeLinks(text)
    local result = text:gsub("%[%[(.-)|.-%]%]", "%1"):gsub("%[%[(.-)%]%]", "%1")
    return result
end
end


return NewsSearch
return NewsSearch

Revision as of 14:40, 12 February 2024

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

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

local limit = 10

local NewsSearch = {}

function NewsSearch.main(frame)
	local args = getArgs(frame)
	
	if args.games == nil and args.categories == nil and args.tag == nil then 
		-- If initial load, then display nothing
		if args.initial then return
		-- else return tableContainer:wikitext(table.concat(args, ', ')) end
		else return mw.html.create('div'):wikitext('At least one category or game needs to be chosen.') end
	end
	
	if args.more then return NewsSearch.html(args.offset or 0, args) end
	
	
 --   local listContainer = mw.html.create('div'):attr('id', 'list__container')
	-- tableContainer:node(listContainer)
	local html = NewsSearch.html(args.offset or 0, args)
	
	if html == nil or html == '' then
		return '<div>No news found</div>'
	else
	   	return html
		-- local loadMore = frame:callParserFunction{ name = '#widget', args = { 'Pagination', id = 'list__container', template = 'NewsSearch', arg = mw.text.jsonEncode({categories = args.categories}) } }
		-- tableContainer:node(loadMore)
	end
end

function NewsSearch.query(offset, args)
	local whereStr = ''

	if args.categories then
		local categories = mw.text.split(args.categories, ',')
		whereStr = whereStr .. '('
		for i = 1, #categories do
			if i ~= 1 then whereStr = whereStr .. ' OR ' end
			whereStr = whereStr .. ' category HOLDS WITHIN "' .. categories[i] .. '"'
		end
		whereStr = whereStr .. ')'
	end
	
	if args.games then
		if args.categories then 
			whereStr = whereStr .. ' AND (' 
		else 
			whereStr = whereStr .. '(' 	
		end
		
		local games = mw.text.split(args.games, ',')
		for i = 1, #games do
			if i ~= 1 then whereStr = whereStr .. ' OR ' end
			whereStr = whereStr .. ' game HOLDS WITHIN "' .. games[i] .. '"'
		end
		whereStr = whereStr .. ')'
	end
	
	if args.tag then
		args.tag = string.gsub(args.tag, '_', ' ')
		if args.categories or args.games then
			whereStr = whereStr .. ' AND tags HOLDS LIKE "%' .. args.tag .. '%"'
		else
			whereStr = whereStr .. ' tags HOLDS LIKE "%' .. args.tag .. '%"'
		end
	end
	
	local tables = 'News'
	local fields = '_pageName, date, category, game, image, content'
	local cargoArgs = {
		where = whereStr,
		orderBy = 'date DESC',
		limit = limit,
		offset = offset
	}
	local results = cargo.query(tables, fields, cargoArgs)
	
    return results
end

function NewsSearch.html(offset, args)
	local results = NewsSearch.query(offset, args)
	local list = ''
	
	if #results > 0 then
		for i = 1, #results do
			local result = results[i]
			local image = mw.html.create('div'):wikitext('[[File:' .. result.image .. '|300px]]')
			local title = mw.html.create('div'):addClass('news-latest-item-title'):wikitext(mw.ext.displaytitle.get(result._pageName))
			local description = getExcerpt(removeLinks(result.content))
			
			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 date = mw.html.create('div'):wikitext(formatedDate)
			local text = mw.html.create('div'):addClass('news-latest-item-text'):node(title):node(description):node(date)
			local div = mw.html.create('div'):addClass('news-latest-item'):node(image):node(text):node(mw.html.create('div'):addClass('link-overlay'):wikitext('[[File:Team_placeholder_light.png|1x1px|link=' .. result._pageName .. ']]'))
			list = list .. tostring(div)
		end
	else
		return nil
	end
	return list
end

function getExcerpt(text)
    return text:sub(1, 100)
end

function removeLinks(text)
    local result = text:gsub("%[%[(.-)|.-%]%]", "%1"):gsub("%[%[(.-)%]%]", "%1")
    return result
end

return NewsSearch