Page MenuHomePhabricator

os.date doesn't set TTL for cache entry correctly
Open, Needs TriagePublicBUG REPORT

Description

Steps to reproduce:

  1. Visit a page on which a Lua module with os.date('*t') is run and the current day/month/year is accessed.
  2. Open the HTML source code and examine NewPP limit report.

Actual Results:

  • Observed TTL ("Cache expiry") is too high.

Expected Results:

  • Observed TTL corresponds to the time when the HTML output might change.

Example:
Report for Josef Hron (2592000 = 30 * 86400, i.e. 30 days) after the page was purged:

NewPP limit report
Parsed by mw1362
Cached time: 20201217111322
Cache expiry: 2592000
Dynamic content: false
Complications: []
[...]

The infobox loads the birthdate from Wikidata and uses Module:Wikidata/Formatters/time with the following piece of code to compute the person's age:

local Time = require 'Modul:Time'
local osDate = os.date('*t')
local currentTime = Time.new{
	year = osDate.year,
	month = osDate.month,
	day = osDate.day,
	precision = Time.PRECISION.DAY,
	calendar = Time.CALENDAR.GREGORIAN,
}

The following live code in Scribunto is expected to adjust the TTL, but that doesn't happen:

extensions/Scribunto/includes/Engines/LuaCommon/lualib/mw.lua
--- Create a table like the one os.date() returns, but with a metatable that sets TTLs as the values are looked at.
local function wrapDateTable( now )
	return setmetatable( {}, {
		__index = function( t, k )
			if k == 'sec' then
				php.setTTL( 1 )
			elseif k == 'min' then
				php.setTTL( 60 - now.sec )
			elseif k == 'hour' then
				php.setTTL( 3600 - now.min * 60 - now.sec )
			elseif now[k] ~= nil then
				php.setTTL( 86400 - now.hour * 3600 - now.min * 60 - now.sec )
			end
			t[k] = now[k]
			return now[k]
		end
	} )
end

This code should have set the TTL to ca. 42398.

Event Timeline

Hmm, its because we are setting the TTL on the preprocessor frame, and not the ParserOutput. However, preprocessor frame TTLs are used for absolutely nothing, so its a bit pointless

Change 854490 had a related patch set uploaded (by Brian Wolff; author: Brian Wolff):

[mediawiki/extensions/Scribunto@master] Make lua time functions shorten cache like variables do

https://gerrit.wikimedia.org/r/854490