Module:PersonRelatedNews: Difference between revisions
From TwogPedia
No edit summary Tag: Manual revert |
No edit summary |
||
| Line 94: | Line 94: | ||
return os.date("%b %d, %Y", time) | return os.date("%b %d, %Y", time) | ||
end | end | ||
-- function getRelatedNews(limit, title) | |||
-- local tables = 'News' | |||
-- local fields = '_pageName, date, tags, image, category, content' | |||
-- local words = mw.text.split(title, ' ') | |||
-- local conditions = {} | |||
-- for _, word in ipairs(words) do | |||
-- if #word > 2 then | |||
-- table.insert(conditions, '_pageName LIKE "%' .. word .. '%"') | |||
-- end | |||
-- end | |||
-- local whereClause = table.concat(conditions, ' OR ') | |||
-- local cargoArgs = { | |||
-- where = whereClause, | |||
-- orderBy = 'date DESC', | |||
-- limit = limit | |||
-- } | |||
-- local results = cargo.query(tables, fields, cargoArgs) | |||
-- return results or {} | |||
-- end | |||
function getRelatedNews(limit, title) | function getRelatedNews(limit, title) | ||
local tables = 'News' | local tables = 'News' | ||
local fields = '_pageName, date, tags, image, category, content' | local fields = '_pageName, date, tags, image, category, content' | ||
limit = tonumber(limit) or 4 | |||
local | -- Local helpers (scoped to this function) | ||
local | local STOP = { | ||
["the"]=true,["and"]=true,["for"]=true,["with"]=true,["from"]=true,["into"]=true, | |||
["of"]=true,["in"]=true,["on"]=true,["at"]=true,["to"]=true,["by"]=true,["is"]=true,["are"]=true, | |||
["a"]=true,["an"]=true,["as"]=true,["or"]=true,["if"]=true,["but"]=true,["not"]=true | |||
} | |||
local function escape_like(s) | |||
-- Escape %, _ and " for LIKE / SQL fragments | |||
return (s or ""):gsub("%%","%%%%"):gsub("_","__"):gsub('"','\\"') | |||
end | |||
local function normalize(s) | |||
s = mw.ustring.lower(s or "") | |||
s = s:gsub("^[^:]+:", "") -- strip namespace if present | |||
s = s:gsub("[%p%c]", " ") -- punctuation/control -> space | |||
s = s:gsub("%s+", " ") | |||
return mw.text.trim(s) | |||
end | |||
for | -- Extract meaningful terms from the title | ||
if # | local terms, seen = {}, {} | ||
table.insert( | for w in mw.ustring.gmatch(normalize(title), "[%w%d]+") do | ||
if #w >= 3 and not STOP[w] and not seen[w] then | |||
seen[w] = true | |||
table.insert(terms, w) | |||
end | end | ||
end | end | ||
local whereClause = table.concat(conditions, ' OR ') | -- If there are no useful terms, show nothing | ||
if #terms == 0 then | |||
return {} | |||
end | |||
-- Build WHERE combining TAGS (HOLDS LIKE) and title (_pageName LIKE), excluding current page | |||
local current = mw.title.getCurrentTitle().fullText | |||
local conditions = {} | |||
for _, t in ipairs(terms) do | |||
local esc = escape_like(t) | |||
table.insert(conditions, string.format('_pageName LIKE "%%%s%%"', esc)) | |||
table.insert(conditions, string.format('tags HOLDS LIKE "%%%s%%"', esc)) | |||
end | |||
local whereClause = '(' .. table.concat(conditions, ' OR ') .. | |||
string.format(') AND _pageName != "%s"', escape_like(current)) | |||
local cargoArgs = { | local cargoArgs = { | ||
| Line 116: | Line 174: | ||
} | } | ||
local results = cargo.query(tables, fields, cargoArgs) | local results = mw.ext.cargo.query(tables, fields, cargoArgs) or {} | ||
return results | |||
-- If no matches, return empty to avoid rendering a block | |||
if #results == 0 then | |||
return {} | |||
end | |||
return results | |||
end | end | ||
return p | return p | ||
Revision as of 08:14, 23 October 2025
Documentation for this module may be created at Module:PersonRelatedNews/doc
local cargo = mw.ext.cargo
local p = {}
function p.main(frame)
local newsItemContainer = mw.html.create('div'):addClass('related__news')
local title = mw.title.getCurrentTitle().fullText
local newsTitle = "Related news"
-- if title:match("^Companies/") then
-- newsTitle = "Team News"
-- end
local iconHtml = '[[File:News-icon.png|30px|alt=' .. newsTitle .. ' Icon]]'
local iconElement = mw.html.create('h3'):addClass('news-icon'):wikitext(iconHtml .. ' ' .. newsTitle)
local titleContainer = mw.html.create('div'):addClass('related__news__title-container'):attr('style', 'display: flex; align-items: center;')
titleContainer:node(iconElement)
-- local viewAllElement = mw.html.create('div'):addClass('related__news__view-all'):attr('id', 'view-all-button'):wikitext('View All')
local viewAllElement = mw.html.create('span')
:addClass('related__news__view-all')
:attr('id', 'view-all-button')
:wikitext('[[News|View All]]')
titleContainer:node(viewAllElement)
local container = mw.html.create('div'):addClass('related__news__container'):node(titleContainer):node(newsItemContainer)
local relatedNews = getRelatedNews(4, title)
if #relatedNews > 0 then
for i = 1, #relatedNews do
addArticleToContainer(relatedNews[i], newsItemContainer)
end
end
return container
end
function addArticleToContainer(result, container)
local image = result.image or 'News placeholder.png'
local link = result._pageName
local category = result.category and mw.text.split(result.category, ',')[1] or 'Uncategorized'
local categoryClassMap = {
['News'] = 'news-category-news',
['Transfer Market'] = 'news-category-transfer',
['Drama'] = 'news-category-drama',
['Business'] = 'news-category-business',
['Sponsorships'] = 'news-category-sponsorships'
}
local categoryClass = categoryClassMap[category] or 'default-category-class'
local date = result.date and formatDate(result.date) or 'Unknown date'
local title = mw.html.create('div')
:addClass('news-title-twogpedia')
:wikitext('[[' .. result._pageName .. '|' .. mw.ext.displaytitle.get(result._pageName) .. ']]')
local metaContainer = mw.html.create('div'):addClass('news-meta-container')
local categoryElement = mw.html.create('p'):addClass('news-meta news-category-meta ' .. categoryClass):wikitext(category)
local dateElement = mw.html.create('p'):addClass('news-meta news-date'):wikitext(date)
metaContainer:node(categoryElement):node(dateElement)
local contentSnippet = getContentSnippet(result.content)
local contentElement = mw.html.create('p'):addClass('news-content'):wikitext(contentSnippet)
local imageHtml = '[[File:' .. image .. '|link=' .. link .. '|800px|class=news-image|alt=News Image]]'
local imageContainer = mw.html.create('div'):addClass('news-image-container'):wikitext(imageHtml)
local newsItem = mw.html.create('div')
:addClass('related__article')
:node(imageContainer)
:node(title)
:node(contentElement)
:node(metaContainer)
container:node(newsItem)
end
function getContentSnippet(content)
local words = mw.text.split(content, ' ')
local snippet = table.concat(words, ' ', 1, math.min(20, #words))
return snippet .. ( #words > 20 and '...' or '' )
end
function formatDate(dateStr)
local year, month, day = dateStr:match("(%d+)%-(%d+)%-(%d+)")
local time = os.time({year = year, month = month, day = day})
return os.date("%b %d, %Y", time)
end
-- function getRelatedNews(limit, title)
-- local tables = 'News'
-- local fields = '_pageName, date, tags, image, category, content'
-- local words = mw.text.split(title, ' ')
-- local conditions = {}
-- for _, word in ipairs(words) do
-- if #word > 2 then
-- table.insert(conditions, '_pageName LIKE "%' .. word .. '%"')
-- end
-- end
-- local whereClause = table.concat(conditions, ' OR ')
-- local cargoArgs = {
-- where = whereClause,
-- orderBy = 'date DESC',
-- limit = limit
-- }
-- local results = cargo.query(tables, fields, cargoArgs)
-- return results or {}
-- end
function getRelatedNews(limit, title)
local tables = 'News'
local fields = '_pageName, date, tags, image, category, content'
limit = tonumber(limit) or 4
-- Local helpers (scoped to this function)
local STOP = {
["the"]=true,["and"]=true,["for"]=true,["with"]=true,["from"]=true,["into"]=true,
["of"]=true,["in"]=true,["on"]=true,["at"]=true,["to"]=true,["by"]=true,["is"]=true,["are"]=true,
["a"]=true,["an"]=true,["as"]=true,["or"]=true,["if"]=true,["but"]=true,["not"]=true
}
local function escape_like(s)
-- Escape %, _ and " for LIKE / SQL fragments
return (s or ""):gsub("%%","%%%%"):gsub("_","__"):gsub('"','\\"')
end
local function normalize(s)
s = mw.ustring.lower(s or "")
s = s:gsub("^[^:]+:", "") -- strip namespace if present
s = s:gsub("[%p%c]", " ") -- punctuation/control -> space
s = s:gsub("%s+", " ")
return mw.text.trim(s)
end
-- Extract meaningful terms from the title
local terms, seen = {}, {}
for w in mw.ustring.gmatch(normalize(title), "[%w%d]+") do
if #w >= 3 and not STOP[w] and not seen[w] then
seen[w] = true
table.insert(terms, w)
end
end
-- If there are no useful terms, show nothing
if #terms == 0 then
return {}
end
-- Build WHERE combining TAGS (HOLDS LIKE) and title (_pageName LIKE), excluding current page
local current = mw.title.getCurrentTitle().fullText
local conditions = {}
for _, t in ipairs(terms) do
local esc = escape_like(t)
table.insert(conditions, string.format('_pageName LIKE "%%%s%%"', esc))
table.insert(conditions, string.format('tags HOLDS LIKE "%%%s%%"', esc))
end
local whereClause = '(' .. table.concat(conditions, ' OR ') ..
string.format(') AND _pageName != "%s"', escape_like(current))
local cargoArgs = {
where = whereClause,
orderBy = 'date DESC',
limit = limit
}
local results = mw.ext.cargo.query(tables, fields, cargoArgs) or {}
-- If no matches, return empty to avoid rendering a block
if #results == 0 then
return {}
end
return results
end
return p
No categories