Module:Daily: Difference between revisions
Jump to navigation
Jump to search
TeamSpecBot (talk | contribs) Daily rotation from a list page |
TeamSpecBot (talk | contribs) Daily rotation from a list page |
||
| Line 28: | Line 28: | ||
local entry = entries[ ( day + ( tonumber( args.offset ) or 0 ) ) % #entries + 1 ] | local entry = entries[ ( day + ( tonumber( args.offset ) or 0 ) ) % #entries + 1 ] | ||
local field = tonumber( args.field ) | local field = tonumber( args.field ) | ||
if | if field then | ||
entry = mw.text.trim( mw.text.split( entry, '|', true )[ field ] or '' ) | |||
end | end | ||
return frame:preprocess( entry ) | |||
end | end | ||
return p | return p | ||
Latest revision as of 22:38, 20 October 2026
Documentation for this module may be created at Module:Daily/doc
-- Picks one line of a list page per day, the same line for every visitor.
-- {{#invoke:Daily|pick|list=Template:Did you know/list}} returns the whole line;
-- add |field=2 to return the second part of a line split on "|".
local p = {}
local SECONDS_PER_DAY = 86400
local function readList( listTitle )
local title = mw.title.new( listTitle )
local text = title and title:getContent() or ''
local entries = {}
for line in mw.text.gsplit( text, '\n', true ) do
line = mw.text.trim( ( line:gsub( '^%*+', '' ) ) )
if line ~= '' then
entries[ #entries + 1 ] = line
end
end
return entries
end
function p.pick( frame )
local args = frame.args
local entries = readList( args.list or '' )
if #entries == 0 then
return ''
end
local day = math.floor( tonumber( mw.getContentLanguage():formatDate( 'U' ) ) / SECONDS_PER_DAY )
local entry = entries[ ( day + ( tonumber( args.offset ) or 0 ) ) % #entries + 1 ]
local field = tonumber( args.field )
if field then
entry = mw.text.trim( mw.text.split( entry, '|', true )[ field ] or '' )
end
return frame:preprocess( entry )
end
return p