Modul:Wikizprávy:Hlavní strana/Poslední zprávy

Dokumentaci tohoto modulu lze vytvořit na stránce Nápověda:Modul:Wikizprávy:Hlavní strana/Poslední zprávy

-- @brief
--  Backend for [[Wikizprávy:Hlavní strana/Poslední zprávy]].
-- 
-- @author
--  [[meta:User:Danny B.]]
local _module = {}
----------------------------------------


local Collation = require( "Module:Collation" )
local DateTime = require( "Module:DateTime" )
local kpairs = require( "Module:Iterators" ).kpairs


_module.sections = {
	{
		displayTitle = "Dnes"
	}, {
		displayTitle = "Včera"
	}, {}, {}, {}, {}, {}
}


-- @brief
--  Generates the content of [[Wikizprávy:Hlavní strana/Poslední zprávy]].
-- 
-- @return
--  Preprocessed wikitext Generated content of [[Wikizprávy:Hlavní strana/Poslední zprávy]]
function _module.print( frame )
	
	local output = ""
	local sectionDate = DateTime.getLocalDate()
	local sumPagesInCategories = 0
	
	
	for i = 1, 7 do
		
		_module.sections[i].dateNom = sectionDate.day .. ". " .. DateTime.months[sectionDate.month].name .. " " .. sectionDate.year
		_module.sections[i].dateGen = sectionDate.day .. ". " .. DateTime.months[sectionDate.month].genitive .. " " .. sectionDate.year
		
		_module.sections[i].title = mw.title.new( _module.sections[i].dateNom )
		-- expensive++
		
		_module.sections[i].pagesInCategory = mw.site.stats.pagesInCategory( _module.sections[i].dateNom, "pages" )
		-- expensive++
		if _module.sections[i].title.exists then
			_module.sections[i].pagesInCategory = _module.sections[i].pagesInCategory - 1
		end
		
		sumPagesInCategories = sumPagesInCategories + _module.sections[i].pagesInCategory
		_module.sections[i].sumPagesInCategories = sumPagesInCategories
		
		output = output .. "=== "
		if _module.sections[i].title.exists then
			output = output .. "[[" .. _module.sections[i].title.text
			if _module.sections[i].displayTitle then
				output = output .. "|" .. _module.sections[i].displayTitle
			end
			output = output .. "]]"
		else
			output = output .. ( _module.sections[i].displayTitle or _module.sections[i].title.text ) ..
				" <span title=\"Založit denní přehled pro " .. _module.sections[i].title.text .. "\" class=\"plainlinks\">[" ..
				_module.sections[i].title:fullUrl({
					action = "edit",
					editintro = "Wikizprávy:Kostry stránek/Denní přehled/návod",
					preload = "Wikizprávy:Kostry stránek/Denní přehled",
					summary = _module.sections[i].title.text
				}) ..
				" ✍]</span>"
		end
		output = output .. " ===\n"
		
		if _module.sections[i].pagesInCategory > 0 then
			if _module.sections[i].sumPagesInCategories <= 10 then
				output = output .. [[
<dynamicpagelist>
  category = ]] .. _module.sections[i].dateNom .. [[

  count = 10
  namespace = 0
  notcategory = Denní přehledy
  notcategory = Údržba:Dodělat
  notcategory = Údržba:Smazat
  notcategory = Údržba:Porušení autorských práv
  notcategory = Údržba:Upravit
  order = descending
  suppresserrors = true
</dynamicpagelist>
]]
			else
				output = output .. "''[[:Kategorie:" .. _module.sections[i].dateNom .. "|(všechny zprávy z&nbsp;" .. _module.sections[i].dateGen .. ")]]''\n\n"
			end
		end
		
		if i <= 2 then
			output = output .. "<small>''[[:Kategorie:" .. sectionDate.day .. ". " .. DateTime.months[sectionDate.month].name .. "|(" .. sectionDate.day .. ". " .. DateTime.months[sectionDate.month].name .. " v&nbsp;minulosti)]]''</small>\n"
		end
	
		sectionDate.day = sectionDate.day - 1
		sectionDate = os.date( "*t", os.time( sectionDate ) )
		
	end
	
	output = frame:preprocess( output )
	
	return output
	
end


function _module.printList( frame )
	
	local output
	local html = mw.html.create()
	local dateObject = DateTime.getLocalDate()
	local dailyOverview = {
		editintro = "Wikizprávy:Kostry stránek/Denní přehled/návod",
		preload = "Wikizprávy:Kostry stránek/Denní přehled"
	}
	local stateHolidays = {
		"01-01",
		"04-07",
		"04-10",
		"05-01",
		"05-08",
		"07-05",
		"07-06",
		"09-28",
		"10-28",
		"11-17",
		"12-24",
		"12-25",
		"12-26"
	}
	
	for i = 1, 7 do
		
		local newsList
		local ribbon
		local dateDay = mw.ustring.format( "%d. %s", dateObject.day, DateTime.months[dateObject.month].name )
		local dateDaystamp = mw.ustring.format( "%02d-%02d", dateObject.month, dateObject.day )
		local dateFull = mw.ustring.format( "%d. %s %d", dateObject.day, DateTime.months[dateObject.month].name, dateObject.year )
		local stateHoliday = false
		local titleObject = mw.title.new( dateFull )
		-- expensive++
		
		if titleObject.exists then
			
			local deathPrefix = "Úmrtí:"
			local dailyNews = {}
			local content = titleObject:getContent()
				:gsub( "<ref[^>]-/>", "" )
				:gsub( "<ref[^>]->.-</ref>", "" )
			for news in content:gmatch( '%*.-\n' ) do
				news = mw.text.trim( news )
					:gsub( "^%* *", "" )
					:gsub( "{{Podrobná zpráva%|.-}}$", function( detailNewsTemplate )
						return frame:preprocess( detailNewsTemplate )
					end )
					:gsub( "(.-%(%*[^%)]+%d%d%d%d%))$", deathPrefix .. "%1" )
					:gsub( "%[%[[wW]:.-%|(.-)%]%]", "%1" )
				local key = news
					:gsub( "%[%[.-%|(.-)%]%]", "%1" )
					:gsub( "&nbsp;", " " )
				dailyNews[key] = news
			end
			
			if next( dailyNews ) ~= nil then
				
				local news = {}
				local deaths = {}
				
				for _, item in kpairs( dailyNews, Collation.sortCompare ) do
					table.insert( item:match( "^" .. deathPrefix ) and deaths or news, item )
				end
				
				if #news + #deaths > 0 then
					newsList = mw.html.create( "ul" )
					for _, newsItem in pairs( news ) do
						newsList = newsList
							:tag( "li")
								:wikitext( newsItem )
								:done()
							:newline()
					end
					if #deaths > 0 then
						newsList = newsList
							:tag( "li")
								:wikitext( deathPrefix )
								:newline()
								:tag( "ul" )
									:addClass( "deaths" )
						for _, item in pairs( deaths ) do
							item = item:gsub( "^" .. deathPrefix, "" )
							newsList = newsList
								:tag( "li")
								:wikitext( item )
								:done()
								:newline()
						end
						newsList = newsList
								:done()
								:newline()
							:done()
					end
					newsList = newsList
							:done()
							:newline()
						:allDone()
				end
				
			else
				
				newsList = mw.html.create()
					:tag( "div" )
						:addClass( "mainpage-addNews" )
						:wikitext( mw.ustring.format( "[%s přidejte krátkou zprávu do denního přehledu pro %s]",
							titleObject:fullUrl({
								action = "edit",
								editintro = dailyOverview.editintro
							}),
							titleObject.text
						))
						:done()
					:newline()
					:allDone()
				
			end
			
			ribbon = nil
			
		else
			
			ribbon = mw.html.create()
				:wikitext( " " )
				:tag( "span" )
					:addClass( "ribbon" )
					:wikitext(
						mw.ustring.format( "[%s založit]",
							titleObject:fullUrl({
								action = "edit",
								editintro = dailyOverview.editintro,
								preload = dailyOverview.preload,
								summary = dateFull
							})
						)
					)
					:done()
				:allDone()
			
		end
		
		for _, daystamp in ipairs( stateHolidays ) do
			if daystamp == dateDaystamp then
				stateHoliday = true
			end
		end
		
		html = html
			:tag( "div" )
			:addClass( "mainpage-dayList" )
				:tag( "h3" )
					:addClass( "calendarSheet" )
					:addClass( ( dateObject.wday == 1 or stateHoliday ) and "calendarSheetHoliday" or nil )
					:tag( "time" )
						:attr( "datetime", string.format( "%d-%02d-%02d", dateObject.year, dateObject.month, dateObject.day ) )
						:tag( "span" )
							:addClass( "calendarSheet-dayName" )
							:wikitext( DateTime.days[dateObject.wday].name )
							:done()
						:wikitext( " " )
						:tag( "span" )
							:addClass( "calendarSheet-day" )
							:wikitext( titleObject.exists and mw.ustring.format( "[[%s|%s]]", dateFull, dateObject.day ) or dateObject.day )
							:done()
						:tag( "span" )
							:addClass( "calendarSheet-dmSeparator" )
							:wikitext( "." )
							:done()
						:wikitext( " " )
						:tag( "span" )
							:addClass( "calendarSheet-monthName" )
							:wikitext( DateTime.months[dateObject.month].name )
							:done()
						:wikitext( " " )
						:tag( "span" )
							:addClass( "calendarSheet-year" )
							:wikitext( dateObject.year )
							:done()
						:done()
					:node( ribbon )
					:done()
				:newline()
				:tag( "div" )
					:addClass( "mainpage-newsList" )
					:newline()
					:node( newsList )
					:tag( "small" )
						:wikitext( mw.ustring.format( "[[:Kategorie:%s|<time datetime=\"%02d-%02d\">%s</time> v&nbsp;minulosti]]", dateDay, dateObject.month, dateObject.day, dateDay ) )
						:done()
					:newline()
					:done()
				:newline()
				:done()
			:newline()
			:allDone()
		
		dateObject.day = dateObject.day - 1
		dateObject = os.date( "*t", os.time( dateObject ) )
		
	end
	
	output = frame:preprocess( tostring( html ) )
	
	return output
	
end


----------------------------------------
return _module