Module:Bongolium500/main page modules: Difference between revisions

From Tardis Wiki, the free Doctor Who reference
No edit summary
No edit summary
 
(46 intermediate revisions by the same user not shown)
Line 5: Line 5:
local h = {}
local h = {}
local MONTHS = {"January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
local MONTHS = {"January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
function p.recentReleases(frame)
local args = frame.args
if not args["max"] or args["max"] == "" or not type(tonumber(args["max"])) == "number" then
args["max"] = 8
end
local outputText = "<div class=\"mainpage-box-recent\">\n"
local done = false
local i = 1
local side = "left"
while not done do
outputText = outputText .. "<div class=\"recent-release-box\">" .. frame:preprocess(args[i]:gsub("{!%-%-" .. side .. "%-.-%-align%-%-}", ""):gsub("{!%-%-left%-", ""):gsub("{!%-%-right%-", ""):gsub("%-align%-%-}", "")) .. "</div>\n"
if side == "left" then side = "right" else side = "left" end
i = i + 1
if i > tonumber(args["max"]) then
done = true
elseif args[i] == null or args[i] == "" then
done = true
end
end
outputText = outputText .. "</table></div>"
return outputText
end


function p.releaseModule(frame)
function p.releaseModule(frame)
local PREFIX = "MAINPAGERELEASE-"
local PREFIX = "MAINPAGERELEASE-"
local currentDate = os.date("%F")
local currentDate = frame.args[1]
local outputWidget = cache.get(PREFIX .. currentDate) or ""
local outputWidget = cache.get(PREFIX .. currentDate) or ""
Line 15: Line 39:
local monthName = MONTHS[tonumber(month)]
local monthName = MONTHS[tonumber(month)]
local pageName = day .. " " .. monthName .. " (releases)"
local pageName = day .. " " .. monthName .. " (releases)"
local outputWidget = "<div class=\"mainpage-releases tech\" id=\"releases\"><h2>[[" .. pageName .. "|On this day in...]]</h2> \n<h3>"
local outputWidget = "<div class=\"mainpage-releases tech\" id=\"releases\"><h2>[[" .. pageName .. "|On this day in...]]</h2>"
local pageText = frame:callParserFunction('#dpl:', {include='*', title=pageName}):gsub(".*-->", ""):gsub("<!--.*", "")
local pageText = frame:callParserFunction('#dpl:', {include='*', title=pageName}):gsub(".*-->", ""):gsub("<!--.*", "")
local releases = textUtil.split(pageText, "*")
local releases = textUtil.split(pageText, "*")
local randomRelease = releases[math.random(1, #releases)]
local numToReturn = 0
local temp = textUtil.split(randomRelease, ".* %- ", "")
if #releases < 5 then numToReturn = #releases else numToReturn = 5 end
local year = temp[1]
local usedNumbers = {}
local text = temp[2]
for count=1, numToReturn do
outputWidget = outputWidget .. "..." .. year .."</h3> \n<span>text</span></div>"
local ranNum = math.random(1, #releases)
while usedNumbers[ranNum] do
ranNum = math.random(1, #releases)
end
usedNumbers[ranNum] = true
local randomRelease = releases[ranNum]
local temp = textUtil.split(randomRelease, " %- ")
local year = temp[1]
if not string.find(year, "%[%[") then
year = "[[" .. year .. " (releases)|" .. year .. "]]"
end
local text = temp[2] or ""    --:gsub([['"`UNIQ--nowiki-00000001-QINU`"']], "\'")
outputWidget = outputWidget .. "\n<h3>..." .. year .."</h3>\n<span>" .. text .. "</span>"
end
cache.set(PREFIX .. currentDate, outputWidget)
outputWidget = outputWidget .. "</div>"
cache.set(PREFIX .. currentDate, outputWidget)--]]
end
end
return outputWidget
end
end


function p.birthdayModule(frame)
function p.birthdayModule(frame)
local PREFIX = "MAINPAGEBIRTHDAY-"
local PREFIX = "MAINPAGEBIRTHDAY-"
local currentDate = os.date("%F")
local currentDate = frame.args[1]
local outputWidget = cache.get(PREFIX .. currentDate) or ""
local outputWidget = cache.get(PREFIX .. currentDate) or ""

Latest revision as of 17:31, 18 January 2023

Documentation for this module may be created at Module:Bongolium500/main page modules/doc

local smwUtil = require('Module:SMW')
local textUtil = require('Module:TextUtil')
local cache = require('mw.ext.LuaCache')
local p = {}
local h = {}
local MONTHS = {"January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}

function p.recentReleases(frame)
	local args = frame.args
	if not args["max"] or args["max"] == "" or not type(tonumber(args["max"])) == "number" then
		args["max"] = 8
	end
	
	local outputText = "<div class=\"mainpage-box-recent\">\n"
	local done = false
	local i = 1
	local side = "left"
	while not done do
		outputText = outputText .. "<div class=\"recent-release-box\">" .. frame:preprocess(args[i]:gsub("{!%-%-" .. side .. "%-.-%-align%-%-}", ""):gsub("{!%-%-left%-", ""):gsub("{!%-%-right%-", ""):gsub("%-align%-%-}", "")) .. "</div>\n"
		if side == "left" then side = "right" else side = "left" end
		i = i + 1
		if i > tonumber(args["max"]) then 
			done = true
		elseif args[i] == null or args[i] == "" then
			done = true
		end
	end
	outputText = outputText .. "</table></div>"
	return outputText
end

function p.releaseModule(frame)
	local PREFIX = "MAINPAGERELEASE-"
	local currentDate = frame.args[1]
	
	local outputWidget = cache.get(PREFIX .. currentDate) or ""
	if outputWidget == "" then
		local year, month, day = string.match(currentDate, "(%d+)-(%d+)-(%d+)")
		local monthName = MONTHS[tonumber(month)]
		local pageName = day .. " " .. monthName .. " (releases)"
		local outputWidget = "<div class=\"mainpage-releases tech\" id=\"releases\"><h2>[[" .. pageName .. "|On this day in...]]</h2>"
		local pageText = frame:callParserFunction('#dpl:', {include='*', title=pageName}):gsub(".*-->", ""):gsub("<!--.*", "")
		local releases = textUtil.split(pageText, "*")
		
		local numToReturn = 0
		if #releases < 5 then numToReturn = #releases else numToReturn = 5 end
		local usedNumbers = {}
		for count=1, numToReturn do
			local ranNum = math.random(1, #releases)
			while usedNumbers[ranNum] do
				ranNum = math.random(1, #releases)
			end 
			usedNumbers[ranNum] = true
			local randomRelease = releases[ranNum]
			local temp = textUtil.split(randomRelease, " %- ")
			local year = temp[1]
			if not string.find(year, "%[%[") then
				year = "[[" .. year .. " (releases)|" .. year .. "]]"
			end
			local text = temp[2] or ""     --:gsub([['"`UNIQ--nowiki-00000001-QINU`"']], "\'")
			outputWidget = outputWidget .. "\n<h3>..." .. year .."</h3>\n<span>" .. text .. "</span>"
		end
		
		outputWidget = outputWidget .. "</div>"
		cache.set(PREFIX .. currentDate, outputWidget)--]]
	end
	return outputWidget
end

function p.birthdayModule(frame)
	local PREFIX = "MAINPAGEBIRTHDAY-"
	local currentDate = frame.args[1]
	
	local outputWidget = cache.get(PREFIX .. currentDate) or ""
	if outputWidget == "" then
		local year, month, day = string.match(currentDate, "(%d+)-(%d+)-(%d+)")
		local monthName = MONTHS[tonumber(month)]
		local pageName = day .. " " .. monthName .. " (people)"
		local outputWidget = "<div class=\"mainpage-birthdays tech\" id=\"birthdays\"><h2>[[" .. pageName .. "|Today's Birthdays!]]</h2> \n <table>"
		local pageText = frame:callParserFunction('#dpl:', {include='*', title=pageName}):gsub(".*-->", ""):gsub("<!--.*", "")
		local events = textUtil.split(pageText, "*")
		local birthdays = {}
		local position = "left"
		for _, event in pairs(events) do
			if string.find(event, "born") then
				local birthday = event:gsub(".* %- ", "")
				local temp = textUtil.split(birthday, "%.")
				local person = temp[1]:gsub(" was born.*", "")
				local source = temp[2]
				local pageName = person:gsub(".*%[%[", ""):gsub("|.*", "")
				local smwImage = mw.smw.ask( "[[" .. pageName .. "]]" .. "\n|?Has image")[1]["Has image"]
				local image = ""
				if smwImage then
					image = "<span style=\"float:" .. position .. ";padding:2px;\">[[" .. smwImage .. "|75px]]</span>"
					if position == "left" then
						position = "right"
					else
						position = "left"
					end
				end
				outputWidget = outputWidget .. "\n<tr><td>" .. image .. "Happy birthday to " .. person .. "! " .. source .. "</td></tr>"
				outputWidget = outputWidget:gsub("'\"`UNIQ.*", "")
			end
		end
		outputWidget = outputWidget .. "</table></div>"
		cache.set(PREFIX .. currentDate, outputWidget)
	end
	return outputWidget
end

return p