Vai al contenuto

Modulo:POTD

Da Wikipedia

La documentazione per questo modulo può essere creata in Modulo:POTD/man

local p = {}

-- Loading modules of pictures by year
local absolutetime = os.time() -- now
local current_year = os.date("%Y", absolutetime)

local exist
local pictures = {}

local function tryLoad(m)
	return mw.loadData(m)
end

for first_year = 0, -10, -1 do
	current_year = current_year + first_year
	exist, pictures["0"] = pcall(tryLoad, "Module:POTD/" .. current_year)
	if exist then
		for prev_year = -1, -5, -1 do
			exist, pictures[tostring(prev_year)] = pcall(tryLoad, "Module:POTD/" .. current_year + prev_year)
			if not exist then
				break
			end
		end
		break
	end
end

local function getFile(daymonth)
	local filename, description
	for years = 0, -5, -1 do
		local picts = pictures[tostring(years)]
		if picts then
			if picts.file[daymonth] then
				filename = picts.file[daymonth]
				description = picts.footer[daymonth] or ''
				break
			end
		else
			return
		end
	end
	return filename, description
end

-- Building a gallery for requested number of pictures --
local function gallerator(scope)
	local buildgallery = ""
	local range = 0
	local iter = 1
	if scope == "all" then
		range = 365
	elseif tonumber(scope) then
		range = tonumber(scope)
		iter = range < 0 and -1 or 1
	end
	
	absolutetime = os.time() -- now
	current_year = os.date("%Y", absolutetime)
	local daymonth = ""
	local exist, pictures_next = pcall(tryLoad, "Module:POTD/" .. os.date("%Y", absolutetime) + 1)
	if not exist then
		pictures_next = {}
	end
	local filename, description
	
	for days = iter, range, iter do
		absolutetime = absolutetime + (86400 * iter) -- one day
		daymonth = os.date( "%d/%m" , absolutetime )
		if os.date("%Y", absolutetime) > current_year then
			if pictures_next.file and pictures_next.file[daymonth] then
				buildgallery = buildgallery .. '\n' .. 'File:' .. pictures_next.file[daymonth] .. '|' .. daymonth .. ': ' .. (pictures_next.footer[daymonth] or '')
			else
				filename, description = getFile(daymonth)
				if filename then
					buildgallery = buildgallery .. '\n' .. 'File:' .. filename .. '|' .. daymonth .. ': ' .. description
				else
					buildgallery = buildgallery .. '\n' .. 'File:Empty set.svg' .. '|' .. daymonth
				end
			end
		else
			filename, description = getFile(daymonth)
			if filename then
				buildgallery = buildgallery .. '\n' .. 'File:' .. filename .. '|' .. daymonth .. ': ' .. description
			else
				buildgallery = buildgallery .. '\n' .. 'File:Empty set.svg' .. '|' .. daymonth
			end
		end
	end
	return buildgallery
end


-- Matching pictures with day of year --
function p.todayspics(frame)
	local args = frame:getParent().args
	local option = args[1] or ""
	
	-- Which date is today? --
	local daymonth = os.date("%d/%m", absolutetime)
	
	-- Which picture and description should we use today? --
	if option:find("%d%d/%d%d") then
		daymonth = option
	end
	
	local filename, description = getFile(daymonth)
	
	while not filename do
		absolutetime = absolutetime - 86400 -- previous day
		daymonth = os.date("%d/%m", absolutetime)
		filename, description = getFile(daymonth)
	end
	
	-- Any template in footer? Expand it
	if string.match(description, '%b{}') then
		description = frame:preprocess(description)
	end
	
	-- Which output is desired, text or description? --
	local show = ""
	if option == "filename" then
		show = filename
	elseif option == "description" then
		show = description
	else
		show = '[[File:' .. filename .. '|350x350px]]<br />' .. description
	end
	
	-- Which pictures should we display in addition of today's? --
	local gallery = ""
	if option == "all" or tonumber(option) then
		gallery = frame:extensionTag{name = 'gallery', content = gallerator(option), args = {mode='packed-hover', heights='150px'}}
	end
	
	return show .. gallery
end

-- Generates an archive of a year. Use it {{subst:#invoke:POTD|archive|<YYYY>}}
function p.archive(frame)
	local year = frame.args[1]
	if mw.title.new("Module:POTD/" .. year).exists then
		local pictures = mw.loadData("Module:POTD/" .. year)
		local buildgallery = {}
		local absolutetime = os.time({year=year, month=1, day=1})
		for iter = 0, 365 do
			day = absolutetime + (86400 * iter)
			daymonth = os.date("%d/%m", day)
			if pictures.file[daymonth] ~= nil then
				table.insert(buildgallery, 'File:' .. pictures.file[daymonth] .. '|' .. daymonth .. ': ' .. (pictures.footer[daymonth] or ''))
			end
		end
		return frame:extensionTag{name = 'gallery', content = table.concat(buildgallery, '\n'), args = {mode='packed', heights='150px'}}
	end
	return
end
	
return p