Modul:RecentChangesTemplate

Dokumentaci tohoto modulu lze vytvořit na stránce Nápověda:Modul:RecentChangesTemplate

p = {}
local function ZiskejKodStranky (link)
	local stranka = link
	local title = mw.title.new(stranka)
	local content = title:getContent() 

	local KodStranky = {}
	for line in mw.text.gsplit( content, '\n' ) do
		table.insert(KodStranky, line)
	end
	return KodStranky
end

local function zadosti_o_overeni ()
	KodStranky = ZiskejKodStranky ("Wikislovník:Žádost o ověření")
	
	local Overovany = {}
	local Preskrtle = {}
	for line in pairs (KodStranky) do
		local headline = mw.ustring.match( KodStranky[line], '^%s*==%s*([^=]+)==%s*$' )
		if headline then
			local headline = mw.ustring.gsub (headline, "%s*$", "")
			if not mw.ustring.match(headline, "Zpochybnění hesla")  and not mw.ustring.match(headline,"Ověření hesla") and not mw.ustring.match(headline,"Neúspěšné ověření hesla") and not mw.ustring.match(headline,"Jak lze ověřit význam?") then
				headline = mw.ustring.gsub(headline, "[[]*","")
				headline = mw.ustring.gsub(headline, "[]]*","")
				headline = mw.ustring.gsub(headline, ":Kategorie","Kategorie")
				local bezskrt = mw.ustring.match (headline, "<s>(.*)</s>")
				if not bezskrt then
					table.insert (Overovany, headline)
				else
					table.insert(Preskrtle, bezskrt)
				end
			end
		end
	end
	return Overovany, Preskrtle
end

local function probihajici_hlasovani ()
	KodStranky = ZiskejKodStranky ("Wikislovník:Hlasování")
	local line_nadpis = 0
	local line_nasledujici_nadpis = 0
	
	local textOdkazu = false
	
	for a in pairs (KodStranky) do
		if mw.ustring.match (KodStranky[a], "^%s*==%s*Aktuální hlasování%s*==%s*$") then
			line_nadpis = a
		end
		if mw.ustring.match (KodStranky[a], "^%s*==%s*Související stránky%s*==%s*$") then
			line_nasledujici_nadpis = a
		end
		
	end
	local pocet_radku = line_nasledujici_nadpis - line_nadpis - 1
	local a = 1
	local Hlasovani = {}
	while a < pocet_radku do
		if not mw.ustring.match (KodStranky[line_nadpis + a], "^%s*$") then	
			local odkaz = mw.ustring.gsub (KodStranky[line_nadpis + a], "^[*#]%s*", "")
			local _,_,stranka = mw.ustring.find(odkaz, "^[[][[]([^]|]*)")
			if mw.ustring.sub (stranka, 1, 1) == "/" then
				if mw.ustring.sub (stranka, -1) ~= "/" then
					stranka = "Wikislovník:Hlasování" .. stranka
				else
					textOdkazu = mw.ustring.sub (stranka, 2, -2)
					stranka = "Wikislovník:Hlasování" .. mw.ustring.sub (stranka, 1, -2)
				end
			end

			if not textOdkazu then
				_,_,textOdkazu = mw.ustring.find(odkaz, "^[[][[][^]|]*[|]*([^]]*)[]][]]$")
				if not textOdkazu or textOdkazu == "" then
					textOdkazu = stranka
				end
			end
			
			if stranka then
				local stranka_s_hlasovanim = mw.title.new(stranka)
				if stranka_s_hlasovanim.id == 0 then
					table.insert(Hlasovani, {stranka, textOdkazu, false})
				else
					table.insert(Hlasovani, {stranka, textOdkazu, true})
				end
			end
		end
		a = a + 1
	end
	
	return Hlasovani
end


local function UdajeHlasovani (Hlasovani)
	KodStranky = ZiskejKodStranky (Hlasovani)
	
	local sekce_pro = false
	local sekce_proti = false
	local sekce_zdrzujise = false
	
	local Udaje = {}
	Udaje["pro"] = 0
	Udaje["proti"] = 0
	Udaje["zdrzuji_se"] = 0

	for a in pairs (KodStranky) do
		if mw.ustring.match (KodStranky[a], "^%s*===%s*Pro%s*===%s*$") then
			sekce_pro = true
		end
		if mw.ustring.match (KodStranky[a], "^%s*===%s*Proti%s*===%s*$") then
			sekce_pro = false
			sekce_proti = true
		end
		if mw.ustring.match (KodStranky[a], "^%s*===%s*Zdržují se%s*===%s*$") then
			sekce_proti = false
			sekce_zdrzujise = true
		end
		if mw.ustring.match (KodStranky[a], "^%s*==%s*Komentáře%s*==%s*$") then
			sekce_zdrzujise = false
		end
		
		if sekce_pro and mw.ustring.match (KodStranky[a], "^#[^:]") then
			Udaje["pro"] = 	Udaje["pro"] + 1
		end
		if sekce_proti and mw.ustring.match (KodStranky[a], "^#[^:]") then
			Udaje["proti"] = Udaje["proti"] + 1
		end
		if sekce_zdrzujise and mw.ustring.match (KodStranky[a], "^#[^:]") then
			Udaje["zdrzuji_se"] = Udaje["zdrzuji_se"] + 1
		end
		
		temp = mw.ustring.match (KodStranky[a], "^[*]*%s*Hlasování začíná[:]*%s*[']*([0-9]+%s*[.]%s*[0-9]*[.]%s*[0-9]*%s*[0-9]+[:.][0-9]+%s*SELČ)[']*")
		if temp then
			Udaje["zacatek"] = temp
		end
		temp = mw.ustring.match (KodStranky[a], "^[*]*%s*Hlasování končí[:]*%s*[']*([0-9]+%s*[.]%s*[0-9]*[.]%s*[0-9]*%s*[0-9]+[:.][0-9]+%s*SELČ)[']*")
		if temp then
			Udaje["konec"] = temp
		end
		temp = mw.ustring.match (KodStranky[a], "^[*]*%s*Hlasování prodlouženo do[:]*%s*[']*%s*([0-9]+%s*[.]%s*[0-9]*[.]%s*[0-9]*%s*[0-9]+[:.][0-9]+%s*SELČ)[']*")
		if temp then
			Udaje["prodlouzeno_do"] = temp
		end
	end

	return Udaje
end
function CheckIfExists (page)
	if page and page ~= "" then
		local stranka = mw.title.new(page)
		if stranka.id == 0 then
			return false
		else
			return true
		end
	else
		return nil
	end

end
	
function p.test (frame)
	local img_pro = "[[Soubor:Symbol support vote.svg|12px|link=|alt=počet hlasujících pro]]"
	local img_proti = "[[Soubor:Symbol oppose vote.svg|12px|link=|alt=počet hlasujících proti]]"
	local img_zdrzujise = "[[Soubor:Symbol abstain vote.svg|12px|link=|alt=počet zdržujících se hlasování]]"
	
	local seznamy = require ("Modul:RecentChangesTemplate/AutoPages")
	local sekce = require ("Modul:RecentChangesTemplate/Content")
	local MostWanted = {}
	local Wanted = {}
	local Orphaned = {}
	
	local n = 5

	for i=1,n do
		if #MostWanted < 6 then
			if WantedPages[i] and CheckIfExists (WantedPages[i][1]) == false then
				table.insert(MostWanted, {WantedPages[i][1], WantedPages[i][2]})
			else
				n = n + 1
			end
		end
	
		math.randomseed( os.time())
		local rand
		if i < 6 then
			rand = math.random ( (i*1000)-999, i*1000)
		else
			rand = math.random ( n, 5000)
		end

		if #Wanted < 6 then
			if WantedPages[rand] and CheckIfExists (WantedPages[rand][1]) == false then
				table.insert(Wanted, WantedPages[rand][1])
			else
				n = n + 1
			end
		end
		if #Orphaned < 6 then
			if OrphanedPages[rand] then
				table.insert(Orphaned, OrphanedPages[rand])
			else
				n = n + 1
			end
		end
			
	end
	
	local out = ""
	local showTable = false
	local showZoO = false
	local showZoO_old = false
	local showVotes = false
	local showOtherSections = false
	
	local ZoO_Probihajici, ZoO_Preskrtle = zadosti_o_overeni()
	if #ZoO_Probihajici > 0 then
		showZoO = true
	end
	
	if #ZoO_Preskrtle > 0 then
		showZoO_old = true
		showZoO = true
	end
	
	local Hlasovani = probihajici_hlasovani()
	if #Hlasovani > 0 then
		showVotes = true
	end

	if #sekce > 0 then
		showOtherSections = true
	end
	
	if showZoO or showZoO_old or showVotes or showOtherSections then
		showTable = true
	end
	
	if showTable then
		out = "<div class=\"breaking-news\">"
		out = out .. "<table>"
		if showOtherSections then
			for line in pairs (sekce) do
				out = out .. "<tr><td>" .. sekce[line]["section"] .. "</td><td class=\"hlist\"><ul><li>" .. sekce[line]["content"] .. "</li></ul></td></tr>"
			end
		end
				
		if showZoO or showZoO_old then
			out = out .. "<tr><th>[[Wikislovník:Žádost o ověření|žádosti o ověření]]"
		
			if showZoO then
				out = out .. " — probíhající</th>"
				out = out ..  "<td class=\"hlist\"><ul>"
				for a in pairs (ZoO_Probihajici) do
					out = out .. "<li>[[Wikislovník:Žádost o ověření#" .. ZoO_Probihajici[a] .. "|" .. ZoO_Probihajici[a] .. "]]</li>"
				end
				out = out .. "</ul><small>Viz též [[:Kategorie:Údržba:Ověřit]]</small></td></tr>"
			else
				out = out .. " — nedávno skončené</th>"
				out = out .. "<td class=\"hlist\"><ul>"
				for a in pairs (ZoO_Preskrtle) do
					out = out .. "<li>[[Wikislovník:Žádost o ověření#" .. ZoO_Preskrtle[a] .. "|" .. ZoO_Preskrtle[a] .. "]]</li>"
				end
				out = out .. "</ul></td></tr>"
			end
			if showZoO and showZoO_old then
				out = out .. "<tr><th>— nedávno skončené</th>"
				out = out ..  "<td class=\"hlist\"><ul>"
				for a in pairs (ZoO_Preskrtle) do
					out = out .. "<li>[[Wikislovník:Žádost o ověření#" .. ZoO_Preskrtle[a] .. "|" .. ZoO_Preskrtle[a] .. "]]</li>"
				end
				out = out .. "</ul></td></tr>"
			end
	end
	
		if showVotes then
			out = out .. "<tr><th>probíhající [[Wikislovník:Hlasování|hlasování]]</th><td><ul>"
			for a in pairs (Hlasovani) do
				out = out .. "<li>[[" .. Hlasovani[a][1] .. "|" .. Hlasovani[a][2] .. "]]"
				if Hlasovani[a][3] then
					Udaje_o_hlasovani = UdajeHlasovani(Hlasovani[a][1])
					if Udaje_o_hlasovani["zacatek"] ~= nil and Udaje_o_hlasovani["zacatek"] ~= "" and Udaje_o_hlasovani["konec"] ~= nil and Udaje_o_hlasovani["konec"] ~= "" then
						out = out .. " (od " .. Udaje_o_hlasovani["zacatek"] .. " do " .. Udaje_o_hlasovani["konec"] .. ") " 
					end
					out = out .. img_pro .. " " .. Udaje_o_hlasovani["pro"] .. " " .. img_proti .. " " .. Udaje_o_hlasovani["proti"] .. " " .. img_zdrzujise .. " " .. Udaje_o_hlasovani["zdrzuji_se"]
				end	
				out = out .. "</li>"
			end
			out = out .. "</ul></td></tr>"
		end
			if ( MostWanted and #MostWanted > 0 ) or ( Wanted and #Wanted > 0 ) or ( Orphaned and #Orphaned > 0 ) then
				out = out .. "<tr><th>přidejte se</th><td class=\"hlist\">"
			
				if MostWanted and #MostWanted > 0 then
					out = out .. "nejvíce chybějící stránky: <ul>"
					for i=1,5 do
						if MostWanted[i] then
							out = out .. "<li>[[" .. MostWanted[i][1] .. "]]" .. " (" .. MostWanted[i][2] .. ")</li>"
						end
					end
				end
				if Wanted and #Wanted > 0 then
					out = out .. "</ul>z dalších chybějících: <ul>"
					for i=1,5 do
						if Wanted[i] then
							out = out .. "<li>[[" .. Wanted[i] .. "]]</li>"
						end
					end
				end
				if Orphaned and #Orphaned > 0 then
					out = out .. "</ul>ze sirotčích stránek:<ul>"
					for i=1,5 do
						if Orphaned[i] then
							out = out .. "<li>[[" .. Orphaned[i] .. "]]</li>"
						end
					end
					out = out .. "</ul>"
				end
				out = out .. "</td></tr>"
			end


		out = out .. "</table></div>"		
	end		
		
	
	return out
end

return p