Module:VersionMessageBox: Difference between revisions
From Against the Storm Official Wiki
(Forgot to update the rendering) |
(updated to externalize the view; makes this logic simpler) |
||
(4 intermediate revisions by the same user 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 29: | Line 15: | ||
local PATTERN_CAPTURE_MAJOR_VERSION = "(%d+%.%d+)" | local PATTERN_CAPTURE_MAJOR_VERSION = "(%d+%.%d+)" | ||
--endregion | --endregion | ||
Line 37: | Line 21: | ||
--region Private methods | --region Private methods | ||
--- | --- | ||
Line 93: | Line 43: | ||
--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 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