Module:RelatedNews

From TwogPedia
Revision as of 14:37, 22 April 2024 by Couchor (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

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

local p = {}

function p.main(frame) 
	local args = getArgs(frame)
	
	local relatedTagsNews = getRelatedTagNews(args.tags, mw.title.getCurrentTitle().text)
	
	local newsItemContainer = mw.html.create('div'):addClass('related__news')
	local container = mw.html.create('div'):addClass('related__news__container '):node(mw.html.create('h3'):wikitext('Related news')):node(newsItemContainer)
   
	if #relatedTagsNews > 0 then
		for i = 1, #relatedTagsNews do
			addArticleToContainer(relatedTagsNews[i], newsItemContainer)
		end
	end
	
	
	-- If less than 5 related tag news were found, then fill with news from categories
	if #relatedTagsNews < 5 then
		local relatedCatNews = getRelatedCatNews(args.categories, mw.title.getCurrentTitle().text, relatedTagsNews, 5 - #relatedTagsNews)
		for i = 1, #relatedCatNews do
			addArticleToContainer(relatedCatNews[i], newsItemContainer)
		end
	end

	return container
end

function addArticleToContainer(result, container)
	local image = result.image or 'News placeholder.png'
	local link = mw.text.encode(result._pageName)	

	local title = mw.html.create('div'):wikitext(mw.ext.displaytitle.get(result._pageName))

	local newsItem = mw.html.create('div'):addClass('related__article'):wikitext('[[File:'.. image .. '|link=' .. link .. '|800x450px]]'):node(title)
	container:node(newsItem)
end

function getRelatedTagNews(tags, pageName)
	if not tags then return {} end
	
	local whereStr = '_pageName != "' .. pageName:gsub('"', '""') .. '" AND (' 
	local tagsSplit = mw.text.split(tags, ',')
	for i = 1, #tagsSplit do
		if i > 1 then whereStr = whereStr .. ' OR ' end
		whereStr = whereStr .. 'tags HOLDS "' .. mw.text.trim(tagsSplit[i]) .. '"'
	end
	whereStr = whereStr .. ')'

	local tables = 'News'
	local fields = '_pageName, date, tags, image'
	local cargoArgs = {
		where = whereStr,
		orderBy = 'date DESC',
		limit = 5
	}
	local results = cargo.query(tables, fields, cargoArgs)
	return results
end

function getRelatedCatNews(categories, pageName, tagNews, missingAmount)
	if not categories then return {} end
	local whereStr = ''
	
	if #tagNews > 0 then
		for i = 1, #tagNews do
			whereStr = '_pageName != "' .. tagNews[i]._pageName:gsub('"', '""') .. '" AND '
		end
	end
	
	local whereStr = whereStr .. '_pageName != "' .. pageName:gsub('"', '""') .. '" AND (' 
	local catSplit = mw.text.split(categories, ',')
	for i = 1, #catSplit do
		if i > 1 then whereStr = whereStr .. ' OR ' end
		whereStr = whereStr .. 'category HOLDS "' .. mw.text.trim(catSplit[i]) .. '"'
	end
	whereStr = whereStr .. ')'

	local tables = 'News'
	local fields = '_pageName, date, tags, image'
	local cargoArgs = {
		where = whereStr,
		orderBy = 'date DESC',
		limit = missingAmount
	}
	local results = cargo.query(tables, fields, cargoArgs)
	return results
end

return p