Module:VersionMessageBox: Difference between revisions

From Against the Storm Official Wiki
m modified the styling some more
Add support for lazy versioning (without the patch number)
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
-------------------------------------------------------------------------------
--- @module VersionMessageBox
-- This module renders the {{Version}} template.
-- https://hoodedhorse.com/wiki/Against_the_Storm/Template:Version
--
-- This template #invokes VersionMessageBox.checkVersion(frame), below.
--
-- The template requires one argument provided by wiki page writers using the
-- template, which is the version of the game when the wiki page was last
-- updated. There is a second argument provided by the template itself that the
-- page writers canont overwrite, which always refers to the current version of
-- the game. The current version is identified by the wiki as
-- {{CurrentVersion}}.
--
-- Depending on the arguments, this module creates a message box that displays
-- when a reader opens a page that hasn't been reviewed and updated since the
-- version changed. The message box tells the reader what version of the game
-- the page was udpated with, the current version, and an encouraging note to
-- help keep the wiki up to date.
-- @module VersionMessageBox
local VersionMessageBox = {}
local VersionMessageBox = {}


--
-- Constants
--
-- specialization names
local CSS_VERSION_MESSAGE_BOX = "max-width: 500px; margin-left: auto; padding: 15px; border: 1px solid #8ab3ff; border-radius: 12px; background: #dbe8ff; font-size: 10px"
local CSS_CLASS_VERSION_MESSAGE_BOX = "ATS-version-message-box"
local CSS_COLOR_VERSION_BACKGROUND = "#dbe8ff"
local CSS_COLOR_VERSION_BORDER = "#8ab3ff"
local MESSAGE_BOX_ICON = "Hourglass_Icon_.png"




--region Dependencies


--
local VIEW_TEMPLATE = "Version/view"
-- Main rendering function
--


-------------------------------------------------------------------------------
--endregion
-- Checks whether a message box is necessary.
 
--
 
-- If the page is outdated, calls the method to draw the message box and adds
 
-- the right category to the page.
--region Private constants
-- @param frame a table describing the MediaWiki frame; frame['args'] is a
 
-- table containg the template arguments; frame['args']['updated'] is the first
local PATTERN_CAPTURE_MAJOR_VERSION = "(%d+%p)"
-- argument; frame['args']['currentVersion'] is the second argument.
local PATTERN_CAPTURE_MINOR_VERSION = "(%p%d+)"
-- @return a string containing the wiki markup for a message box, or an empty
 
-- string if the page is updated
--endregion
function VersionMessageBox.checkVersion(frame)
 
 
-- extract the arguments we care about from the frame
 
local argLastUpdated = frame.args.updated
--region Private methods
local argCurrentVersion = frame.args.currentVersion
 
---
-- validate that there are version values to use
--- Extracts just the major version parts of the numbers and returns whether
if not argLastUpdated or "" == argLastUpdated then
--- the content is out of date.
return "Version error: please provide the game version with which you updated this page"
---
end
---@param versionWhenLastUpdated string the version it was last updated
if not argCurrentVersion or "" == argCurrentVersion then
---@param currentVersion string the current version
return "Version error: template did not identify current version correctly"
---@return boolean true if the last updated is before the current major version
end
local function isOutOfDate(versionWhenLastUpdated, currentVersion)
 
-- let's try it for a while just checking whether they are different. In the
local lastUpdatedMajor = tonumber(versionWhenLastUpdated:match(PATTERN_CAPTURE_MAJOR_VERSION) or 0)
-- future, we may need to add actual numerical-comparison checks.
local lastUpdatedMinor = tonumber(string.sub(versionWhenLastUpdated:match(PATTERN_CAPTURE_MINOR_VERSION), 2, -1) or 0)
if argLastUpdated == argCurrentVersion then
 
-- the page is up to date, nothing else needed
local currentMajor = tonumber(currentVersion:match(PATTERN_CAPTURE_MAJOR_VERSION) or 0)
return ""
local currentMinor = tonumber(string.sub(currentVersion:match(PATTERN_CAPTURE_MINOR_VERSION), 2, -1) or 0)
 
if currentMajor > lastUpdatedMajor then
return true
elseif currentMajor == lastUpdatedMajor then
if currentMinor > lastUpdatedMinor then
return true
end
else
else
-- render the message box
return false
local html = VersionMessageBox.renderMessageBox(argLastUpdated, argCurrentVersion)
-- add the category at the bottom.
--[[ mw:addCategory("Pages needing version review") this doesn't work --]]
return tostring(html)
end
end
end
end
--endregion




--region Public methods


---main
--- Checks whether a message box is necessary. If the page is outdated, sends the information to the view template.
---
---
-- Renders the message box.
---@param frame table the template's calling context
--
---@return string wiki markup for a message box, or a blank string if it's up-to-date
-- Renders a div with the appropriate CSS class for correct display. The div
function VersionMessageBox.main(frame)
-- contains the message about the version of the page and the current version of
 
-- the game. And it includes a statement encouraging people to keep the wiki up
local lastUpdated = frame.args.lastupdated
-- to date.
local currentVersion = frame.args.currentversion
-- @param argLastUpdated the version of the game when the page was updated
-- @param argCurrentVersion the current version of the game
-- @return the HTML markup for a message box
function VersionMessageBox.renderMessageBox(argLastUpdated, argCurrentVersion)


local div = mw.html.create('div')
-- validate that there are version values to use
if not lastUpdated or "" == lastUpdated then
div:addClass(CSS_CLASS_VERSION_MESSAGE_BOX)
error("You must specify the version with which you updated the page. Please see the template documentation for how to use the parameters")
div:cssText(CSS_VERSION_MESSAGE_BOX)
end
if not currentVersion or "" == currentVersion then
messageText = "This page was last updated during version '''" .. argLastUpdated .. "'''." ..
error("The current version was not retrieved correctly. Please reach out on the community discord for assistance")
"The game is now in version '''" .. argCurrentVersion .. "'''.<br />" ..
end
"Some information may be out of date. " ..
 
"Help us keep the wiki up to date by comparing this article against the patch notes and updating this page."
if isOutOfDate(lastUpdated, currentVersion) then
return frame:expandTemplate{
div:wikitext("[[File:" .. MESSAGE_BOX_ICON .. "|left|x48px|link=|Out of date]] ")
title = VIEW_TEMPLATE,
:tag('div'):cssText("margin-left: 60px")
args = {
:wikitext(messageText)
["lastupdated"] = lastUpdated,
}
return div
}
else
-- the page is up to date, nothing else needed
return ""
end
end
end


-- return when required into another module
--endregion
 
return VersionMessageBox
return VersionMessageBox

Latest revision as of 08:03, 7 August 2026

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

--- @module VersionMessageBox
local VersionMessageBox = {}



--region Dependencies

local VIEW_TEMPLATE = "Version/view"

--endregion



--region Private constants

local PATTERN_CAPTURE_MAJOR_VERSION = "(%d+%p)"
local PATTERN_CAPTURE_MINOR_VERSION = "(%p%d+)"

--endregion



--region Private methods

---
--- Extracts just the major version parts of the numbers and returns whether
--- the content is out of date.
---
---@param versionWhenLastUpdated string the version it was last updated
---@param currentVersion string the current version
---@return boolean true if the last updated is before the current major version
local function isOutOfDate(versionWhenLastUpdated, currentVersion)

	local lastUpdatedMajor = tonumber(versionWhenLastUpdated:match(PATTERN_CAPTURE_MAJOR_VERSION) or 0)
	local lastUpdatedMinor = tonumber(string.sub(versionWhenLastUpdated:match(PATTERN_CAPTURE_MINOR_VERSION), 2, -1) or 0)

	local currentMajor = tonumber(currentVersion:match(PATTERN_CAPTURE_MAJOR_VERSION) or 0)
	local currentMinor = tonumber(string.sub(currentVersion:match(PATTERN_CAPTURE_MINOR_VERSION), 2, -1) or 0)

	if currentMajor > lastUpdatedMajor then
		return true
	elseif currentMajor == lastUpdatedMajor then
		if currentMinor > lastUpdatedMinor then
			return true
		end
	else
		return false
	end
end

--endregion



--region Public methods

---main
--- Checks whether a message box is necessary. If the page is outdated, sends the information to the view template.
---
---@param frame table the template's calling context
---@return string wiki markup for a message box, or a blank string if it's up-to-date
function VersionMessageBox.main(frame)

	local lastUpdated = frame.args.lastupdated
	local currentVersion = frame.args.currentversion

	-- validate that there are version values to use
	if not lastUpdated or "" == lastUpdated then
		error("You must specify the version with which you updated the page. Please see the template documentation for how to use the parameters")
	end
	if not currentVersion or "" == currentVersion then
		error("The current version was not retrieved correctly. Please reach out on the community discord for assistance")
	end

	if isOutOfDate(lastUpdated, currentVersion) then
		return frame:expandTemplate{
			title = VIEW_TEMPLATE,
			args = {
				["lastupdated"] = lastUpdated,
			}
		}
	else
		-- the page is up to date, nothing else needed
		return ""
	end
end

--endregion

return VersionMessageBox