Module:NewsSearch: Difference between revisions

From TwogPedia
mNo edit summary
mNo edit summary
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local News = require('Module:NewsItem')
local cargo = mw.ext.cargo
local cargo = mw.ext.cargo


local limit = 25
local limit = 100000


local NewsSearch = {}
local NewsSearch = {}
Line 8: Line 9:
function NewsSearch.main(frame)
function NewsSearch.main(frame)
local args = getArgs(frame)
local args = getArgs(frame)
local results = NewsSearch.query( args)
if args.games == nil and args.categories == nil then return tableContainer:wikitext('At least one category or game needs to be chosen') end
if #results > 0 then
local container = mw.html.create('div'):attr('id', 'news-search-container')
if args.more then return NewsSearch.html(args.page or 1, args) end
local ul = mw.html.create('ul')
local tableContainer = mw.html.create('div')
    local listContainer = mw.html.create('div'):attr('id', 'list__container')
for r = 1, #results do
tableContainer:node(listContainer)
ul:node(mw.html.create('li'):node(News.main(newsObject(results[r]))))
local html = NewsSearch.html(args.page or 1, args)
end
 
if html == nil or html == '' then
container:node(ul)
return listContainer:wikitext('No news found')  
else
return container
  listContainer:node(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
return tableContainer
end
end


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


Line 55: Line 52:
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
    if args.title then
args.title = string.gsub(args.title, '_', ' ')
if whereStr ~= '' then
whereStr = whereStr .. ' AND _pageName LIKE "%' .. args.title.. '%"'
else
whereStr = whereStr .. ' _pageName LIKE "%' .. args.title.. '%"'
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
}
}
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 newsObject(result)  
local results = NewsSearch.query(page, args)
local object = {
local list = ''
date = result.date,
_pageName  = result._pageName,
category = result.category,
game = result.game,
content = result.content,
author = result.author
}
if #results > 0 then
return object
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 .. ']]')
list = list .. tostring(news)
end
else
return nil
end
return list
end
end


return NewsSearch
return NewsSearch

Latest revision as of 11:59, 19 April 2024

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

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

local limit = 100000

local NewsSearch = {}

function NewsSearch.main(frame)
	local args = getArgs(frame)
	local results = NewsSearch.query( args)
	
	if #results > 0 then
		local container = mw.html.create('div'):attr('id', 'news-search-container')
		
		local ul = mw.html.create('ul')
		
		for r = 1, #results do
			ul:node(mw.html.create('li'):node(News.main(newsObject(results[r]))))
		end

		container:node(ul)
		
		return container
	end
end

function NewsSearch.query(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

    if args.title then
		args.title = string.gsub(args.title, '_', ' ')
		if whereStr ~= '' then
			whereStr = whereStr .. ' AND _pageName LIKE "%' .. args.title.. '%"'
		else
			whereStr = whereStr .. ' _pageName LIKE "%' .. args.title.. '%"'
		end
	end
	
	local tables = 'News'
	local fields = '_pageName, date, category, game, image, content'
	local cargoArgs = {
		where = whereStr,
		orderBy = 'date DESC',
		limit = limit
	}
	local results = cargo.query(tables, fields, cargoArgs)
	
    return results
end

function newsObject(result) 
	local object = {
		date = result.date,
		_pageName  = result._pageName,
		category = result.category,
		game = result.game,
		content = result.content,
		author = result.author
	}
	
	return object
end

return NewsSearch