Module:VersionMessageBox: Difference between revisions
From Against the Storm Official Wiki
(created) |
(updated to externalize the view; makes this logic simpler) |
||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
- | --- @module VersionMessageBox | ||
-- @module VersionMessageBox | |||
local VersionMessageBox = {} | local VersionMessageBox = {} | ||
--region Dependencies | |||
-- | local VIEW_TEMPLATE = "Version/view" | ||
-- | |||
-- | --endregion | ||
--region Private constants | |||
local PATTERN_CAPTURE_MAJOR_VERSION = "(%d+%.%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 lastUpdated = tonumber(versionWhenLastUpdated:match(PATTERN_CAPTURE_MAJOR_VERSION) or 0) | |||
local current = tonumber(currentVersion:match(PATTERN_CAPTURE_MAJOR_VERSION) or 0) | |||
return current > lastUpdated | |||
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 | -- 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(lastUpdated, currentVersion) then | |||
return frame:expandTemplate{ | |||
title = VIEW_TEMPLATE, | |||
args = { | |||
["lastupdated"] = lastUpdated, | |||
} | |||
} | |||
else | |||
-- the page is up to date, nothing else needed | -- the page is up to date, nothing else needed | ||
return "" | return "" | ||
end | end | ||
end | end | ||
--endregion | |||
return VersionMessageBox | return VersionMessageBox |
Latest revision as of 18:00, 15 October 2024
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+%.%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 lastUpdated = tonumber(versionWhenLastUpdated:match(PATTERN_CAPTURE_MAJOR_VERSION) or 0) local current = tonumber(currentVersion:match(PATTERN_CAPTURE_MAJOR_VERSION) or 0) return current > lastUpdated 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