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:
---
--- 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
--- @module VersionMessageBox
local VersionMessageBox = {}
local VersionMessageBox = {}
Line 22: Line 4:




local StyleUtils = require("Module:StyleUtils")
--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+)"
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"


--endregion
--endregion
Line 37: Line 21:


--region Private methods
--region Private methods
---
--- Renders the message box.
---
--- Renders a div with the appropriate CSS class for correct display. The div
--- 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 to date.
---
--- @param argLastUpdated string the version of the game when the page was updated
--- @param argCurrentVersion string the current version of the game
--- @return string the HTML markup for a message box
local function renderMessageBox(argLastUpdated, argCurrentVersion)
messageText = "This page was last updated during version " ..
StyleUtils.BOLD .. argLastUpdated .. StyleUtils.BOLD .. ". " ..
"The game is now in version " ..
StyleUtils.BOLD .. argCurrentVersion .. StyleUtils.BOLD .. "." ..
StyleUtils.BR ..
"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."
local div = mw.html.create("div")
div:addClass(StyleUtils.CLASS_VERSION_MESSAGE_BOX)
:cssText(CSS_VERSION_MESSAGE_BOX)
:newline()
div:tag("p"):wikitext(messageText)
:done():newline()
:done()
return div
end


---
---
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.
---
--- If the page is outdated, calls the method to draw the message box and adds
--- the right category to the page.
---
---
---@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.checkVersion(frame)
function VersionMessageBox.main(frame)


-- extract the arguments we care about from the frame
local lastUpdated = frame.args.lastupdated
local argLastUpdated = frame.args.updated
local currentVersion = frame.args.currentversion
local argCurrentVersion = frame.args.currentVersion


-- validate that there are version values to use
-- validate that there are version values to use
if not argLastUpdated or "" == argLastUpdated then
if not lastUpdated or "" == lastUpdated then
return "Version error: please provide the game version with which you updated this page"
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 argCurrentVersion or "" == argCurrentVersion then
if not currentVersion or "" == currentVersion then
return "Version error: template did not identify current version correctly"
error("The current version was not retrieved correctly. Please reach out on the community discord for assistance")
end
end


if isOutOfDate(argLastUpdated, argCurrentVersion) then
if isOutOfDate(lastUpdated, currentVersion) then
 
return frame:expandTemplate{
local html = renderMessageBox(argLastUpdated, argCurrentVersion)
title = VIEW_TEMPLATE,
return tostring(html)
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