Module:VersionMessageBox: Difference between revisions

From Against the Storm Official Wiki
Fix issue with version message box not showing for version 1.10 and above
Add support for lazy versioning (without the patch number)
 
(2 intermediate revisions by the same user not shown)
Line 14: Line 14:
--region Private constants
--region Private constants


local PATTERN_CAPTURE_MAJOR_VERSION = "(%d+[.])"
local PATTERN_CAPTURE_MAJOR_VERSION = "(%d+%p)"
local PATTERN_CAPTURE_MINOR_VERSION = "([.]%d+[.])"
local PATTERN_CAPTURE_MINOR_VERSION = "(%p%d+)"


--endregion
--endregion
Line 33: Line 33:


local lastUpdatedMajor = tonumber(versionWhenLastUpdated:match(PATTERN_CAPTURE_MAJOR_VERSION) or 0)
local lastUpdatedMajor = tonumber(versionWhenLastUpdated:match(PATTERN_CAPTURE_MAJOR_VERSION) or 0)
local lastUpdatedMinor = tonumber(versionWhenLastUpdated:match(string.sub(PATTERN_CAPTURE_MINOR_VERSION, 2, -2)) 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 currentMajor = tonumber(currentVersion:match(PATTERN_CAPTURE_MAJOR_VERSION) or 0)
local currentMinor = tonumber(currentVersion:match(string.sub(PATTERN_CAPTURE_MINOR_VERSION, 2, -2)) or 0)
local currentMinor = tonumber(string.sub(currentVersion:match(PATTERN_CAPTURE_MINOR_VERSION), 2, -1) or 0)


if currentMajor > lastUpdatedMajor then
if currentMajor > lastUpdatedMajor then

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