Module:VersionMessageBox: Difference between revisions
From Against the Storm Official Wiki
m Forgot to add back a space |
Anteprefix (talk | contribs) Add support for lazy versioning (without the patch number) |
||
| (6 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
--- @module VersionMessageBox | --- @module VersionMessageBox | ||
local VersionMessageBox = {} | local VersionMessageBox = {} | ||
| Line 22: | Line 4: | ||
local | --region Dependencies | ||
local VIEW_TEMPLATE = "Version/view" | |||
--endregion | |||
| Line 28: | 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 = "(%p%d+)" | |||
local | |||
--endregion | --endregion | ||
| Line 37: | Line 22: | ||
--region Private methods | --region Private methods | ||
--- | --- | ||
| Line 80: | Line 32: | ||
local function isOutOfDate(versionWhenLastUpdated, currentVersion) | local function isOutOfDate(versionWhenLastUpdated, currentVersion) | ||
local | local lastUpdatedMajor = tonumber(versionWhenLastUpdated:match(PATTERN_CAPTURE_MAJOR_VERSION) or 0) | ||
local | local lastUpdatedMinor = tonumber(string.sub(versionWhenLastUpdated:match(PATTERN_CAPTURE_MINOR_VERSION), 2, -1) or 0) | ||
return | 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 | end | ||
| Line 92: | Line 55: | ||
--region Public methods | --region Public methods | ||
--- | ---main | ||
--- Checks whether a message box is necessary. | --- 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 | ---@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 | ---@return string wiki markup for a message box, or a blank string if it's up-to-date | ||
function VersionMessageBox. | function VersionMessageBox.main(frame) | ||
local lastUpdated = frame.args.lastupdated | |||
local | local currentVersion = frame.args.currentversion | ||
local | |||
-- validate that there are version values to use | -- validate that there are version values to use | ||
if not | 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 | end | ||
if not | if not currentVersion or "" == currentVersion then | ||
error("The current version was not retrieved correctly. Please reach out on the community discord for assistance") | |||
end | end | ||
if isOutOfDate( | if isOutOfDate(lastUpdated, currentVersion) then | ||
return frame:expandTemplate{ | |||
title = VIEW_TEMPLATE, | |||
args = { | |||
["lastupdated"] = lastUpdated, | |||
} | |||
} | |||
else | else | ||
-- the page is up to date, nothing else needed | -- the page is up to date, nothing else needed | ||
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


