Aller au contenu

Module:Citation du jour

Une page de Wikiquote, le recueil des citations libres.

Ce module est appelé par {{Citations du mois}} pour lister toutes les citations du jour et les classer par mois dans les pages de Catégorie:Citations du mois.


local months = {'january','february','march','april','may long','june','july','august','september','october','november','december'}
local dayBase = 'Modèle:Citation du jour/'
local monthBase = 'Wikiquote:Citations du jour/'
local lang = mw.language.getContentLanguage()
local p = {}

function monthNumber(monthName)
	for i,x in pairs(months) do
		if lang:ucfirst(mw.message.new(x):plain())==lang:ucfirst(monthName) then
			return i
		end
	end
end

function splitMonthYear(s)
	return string.match(s,'^(%S+) (%d+)$')
end

function p.archives(frame)
	local fromMonth, fromYear = splitMonthYear(frame.args[1])
	local toMonth, toYear = splitMonthYear(frame.args[2])
	if fromMonth==nil or fromYear==nil or toMonth==nil or toYear==nil then
		return nil
	end
	fromYear = tonumber(fromYear)
	toYear = tonumber(toYear)
	fromMonth = monthNumber(fromMonth)
	toMonth = monthNumber(toMonth)
	local i18nMonths = {}
	for i, x in pairs(months) do
		i18nMonths[i] = frame:preprocess('{{int:'..x..'}}')
	end
	local output = {}
	for year = fromYear, toYear do
		local monthArchive = {}
		for month = 1, 12 do
			if (year~=fromYear or month>=fromMonth) and (year~=toYear or month<=toMonth) then
				table.insert(monthArchive, '[['..monthBase..mw.message.new(months[month]):plain()..' '..year..'|'..i18nMonths[month]..']]')
			elseif year==toYear then
				break
			end
		end
		table.insert(output, '*'..year..' : '..table.concat(monthArchive, ' - '))
	end
	return table.concat(output, '\n')
end

function p.mois(frame)
	local month = frame.args[1]
	local year = frame.args[2]
	if month==nil or year==nil or month=='' or year=='' then
		-- if no month or year are specified, try to autodetect them via the page title
		local s = mw.title.getCurrentTitle().subpageText
		month, year = splitMonthYear(s)
	end
	if month~=nil and year~=nil then
		local mn = monthNumber(month)
		if mn~=nil then
			local output = {}
			-- show Modèle:Citations du mois/Introduction
			table.insert(output,frame:expandTemplate{title='Citations du mois/Introduction',args={month,year}})
			-- localized message for the 'edit' link
			local edit = frame:preprocess('{{lcfirst:{{int:edit}}}}')
			for x = 1,tonumber(lang:formatDate('t',year..'-'..mn..'-01')) do
				local page = mw.title.new(dayBase..x..' '..month..' '..year)
				local t = '<span class="editsection">[<span class="plainlinks">['..page:fullUrl({action = 'edit'})..' '..edit..']</span>]</span>\'\'\''..page.subpageText..'\'\'\'\n----\n'
				if page.exists then
					-- try to show the quote, ...
					t = t..frame:preprocess(page:getContent())
				else
					-- ...otherwise display a link
					t = t..'[['..page.fullText..']]'
				end
				table.insert(output,t)
			end	
			-- categorization by year and month
			table.insert(output,'[['..mw.site.namespaces[14].name..':Citations du mois/'..year..'| '..string.format('%02d',mn)..']]') 
			return table.concat(output,'<br />\n')
		end
	end
end

return p