Module:RelatedNews: Difference between revisions

From TwogPedia
(Created page with "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...")
 
mNo edit summary
 
(2 intermediate revisions by the same user not shown)
Line 21: Line 21:
-- If less than 5 related tag news were found, then fill with news from categories
-- If less than 5 related tag news were found, then fill with news from categories
if #relatedTagsNews < 5 then
if #relatedTagsNews < 5 then
local relatedCatNews = getRelatedCatNews(args.categories, mw.title.getCurrentTitle().text, 5 - #relatedTagsNews)
local relatedCatNews = getRelatedCatNews(args.categories, mw.title.getCurrentTitle().text, relatedTagsNews, 5 - #relatedTagsNews)
for i = 1, #relatedCatNews do
for i = 1, #relatedCatNews do
addArticleToContainer(relatedCatNews[i], newsItemContainer)
addArticleToContainer(relatedCatNews[i], newsItemContainer)
Line 32: Line 32:
function addArticleToContainer(result, container)
function addArticleToContainer(result, container)
local image = result.image or 'News placeholder.png'
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 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=' .. result._pageName .. '|800x450px]]'):node(title)
 
local newsItem = mw.html.create('div'):addClass('related__article'):wikitext('[[File:'.. image .. '|link=' .. link .. '|800x450px]]'):node(title)
container:node(newsItem)
container:node(newsItem)
end
end
Line 41: Line 43:
if not tags then return {} end
if not tags then return {} end
local whereStr = '_pageName != "' .. pageName .. '" AND ('  
local whereStr = '_pageName != "' .. pageName:gsub('"', '""') .. '" AND ('  
local tagsSplit = mw.text.split(tags, ',')
local tagsSplit = mw.text.split(tags, ',')
for i = 1, #tagsSplit do
for i = 1, #tagsSplit do
Line 60: Line 62:
end
end


function getRelatedCatNews(categories, pageName, missingAmount)
function getRelatedCatNews(categories, pageName, tagNews, missingAmount)
if not categories then return {} end
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 = '_pageName != "' .. pageName .. '" AND ('  
local whereStr = whereStr .. '_pageName != "' .. pageName:gsub('"', '""') .. '" AND ('  
local catSplit = mw.text.split(categories, ',')
local catSplit = mw.text.split(categories, ',')
for i = 1, #catSplit do
for i = 1, #catSplit do

Latest revision as of 14:37, 22 April 2024

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