Prijeđi na sadržaj

Modul:Rasprava o brisanju

Izvor: Wikipedija


local p = {}
	
	local MIN = -3
	local MAX = 3
	
	local MIN_GOD = -2
	local MAX_GOD = 2
	
	local separator = " · "
	local now = os.date("*t")
	
function mapNumberToText(mjesecNumber)
	local mjesec = "00"
		
		if mjesecNumber == 1 then mjesec = "siječanj"
		elseif mjesecNumber == 2 then mjesec = "veljača"
		elseif mjesecNumber == 3 then mjesec = "ožujak"
		elseif mjesecNumber == 4 then mjesec = "travanj"
		elseif mjesecNumber == 5 then mjesec = "svibanj"
		elseif mjesecNumber == 6 then mjesec = "lipanj"
		elseif mjesecNumber == 7 then mjesec = "srpanj"
		elseif mjesecNumber == 8 then mjesec = "kolovoz"
		elseif mjesecNumber == 09 then mjesec = "rujan"
		elseif mjesecNumber == 10 then mjesec = "listopad"
		elseif mjesecNumber == 11 then mjesec = "studeni"
		elseif mjesecNumber == 12 then mjesec = "prosinac"
		end
	return mw.language.getContentLanguage():ucfirst(mjesec)
end

function mapTextToNumber(mjesecText)
	local mjesec = "00"
		
		if mjesecText == "siječanj" then mjesec = "01"
		elseif mjesecText == "veljača" then mjesec = "02"
		elseif mjesecText == "ožujak" then mjesec = "03"
		elseif mjesecText == "travanj" then mjesec = "04"
		elseif mjesecText == "svibanj" then mjesec = "05"
		elseif mjesecText == "lipanj" then mjesec = "06"
		elseif mjesecText == "srpanj" then mjesec = "07"
		elseif mjesecText == "kolovoz" then mjesec = "08"
		elseif mjesecText == "rujan" then mjesec = "09"
		elseif mjesecText == "listopad" then mjesec = "10"
		elseif mjesecText == "studeni" then mjesec = "11"
		elseif mjesecText == "prosinac" then mjesec = "12"
		end
	return mjesec
end

function ispisiMjesec(mjesec, godina, i, godinaOnly, frame)
	local vrijeme = os.date( "*t", os.time({year=godina, month=mjesec, day="15"}) )
	local datum = mapNumberToText(vrijeme.month) .. " " .. vrijeme.year .. "."
	local tekst = datum
	
	-- Calculate the difference in months
    local year_diff = now.year - vrijeme.year
    local month_diff = now.month - vrijeme.month
    local total_month_diff = year_diff * 12 + month_diff

    -- Check if the difference is more than a month
    local plaintext = math.abs(total_month_diff) > 1

	if godinaOnly == true then tekst = vrijeme.year .. "." end
	
	local link = ''
	if mw.title.new("Rasprava o brisanju/Rasprave/" .. datum, 'Wikipedija').exists then
		link = "[[Wikipedija:Rasprava o brisanju/Rasprave/" .. datum .. "|" .. tekst .. "]]"
		else
			if plaintext == false and (vrijeme.year > 2021 or (vrijeme.year == 2021 and vrijeme.month == 12)) then
				-- zapriječi otvaranje novih rasprava za više od mjesec dana unaprijed ili prije uvođenja WP:ROB  (pros. 2021)
				link = mw.getCurrentFrame():preprocess('<span class="plainlinks rasprava-o-brisanju-novo">['
				.. '{{fullurl:Wikipedija:Rasprava o brisanju/Rasprave/{{urlencode:' .. datum..'|WIKI}}'
				.. '|action=edit'
				.. '&preload=Wikipedija:Rasprava_o_brisanju/Rasprave/bazna'
				.. '&summary={{urlencode:Otvaranje rasprave u novom mjesecu}}}}<span style="color:#ba0000;">'.. tekst .. '</span>]</span>')
			else link = tekst
			end
	end
	
	-- zadnji mjesec nema povlaku
	if i == MAX and godinaOnly == false then return link
		elseif i ==MAX_GOD and godinaOnly == true then return link
			else return link .. separator
	end
end

function ispisiZaglavlje(mjesecNominativ, godina, frame)
	local mjesec = mapTextToNumber(mjesecNominativ)
	ret = ''
	for i=MIN_GOD,MAX_GOD,1 do
		ret = ret .. ispisiMjesec(mjesec, godina+i, i, true)
	end
	ret = ret .. '<br>'
	for i=MIN,MAX,1 do
		ret = ret .. ispisiMjesec(mjesec+i, godina, i, false)
	end
	return ret
end


function p.zaglavlje(frame)
	local naslov = mw.ustring.lower(mw.title.getCurrentTitle().subpageText)
	local mjesecNominativ, godina = naslov:match("([A-ž]*) (%d%d%d%d)%.")
	if mjesecNominativ == nil then 
		return ''
	else 
		return ispisiZaglavlje(mjesecNominativ, godina)
	end
end

----- Traženje sekcije gdje je brisanje osporeno -----
function p.postojiRasprava()
	local frame = mw.getCurrentFrame():getParent()
	local datumBrisanja = frame.args['datum']
	if datumBrisanja == nil then
		return ''
	end
	
	local stranicaKojuTrazimo = ''
	if mw.title.getCurrentTitle().nsText == '' then
		stranicaKojuTrazimo = mw.title.getCurrentTitle().text
	else
			stranicaKojuTrazimo = mw.title.getCurrentTitle().nsText .. ":" .. mw.title.getCurrentTitle().text
	end

	mw.log("stranica", stranicaKojuTrazimo)
	return p.pretraziArhivu(stranicaKojuTrazimo, datumBrisanja)
end



function p.sortirajKategoriju()
	local naslov = mw.ustring.lower(mw.title.getCurrentTitle().subpageText)
	local mjesecNominativ, godina = naslov:match("([A-ž]*) (%d%d%d%d)%.")
	local  mjesec = mapTextToNumber(mjesecNominativ) or "00"
	if mjesecNominativ == nil then 
		return ''
	else 
		return godina .. mjesec
	end
end

function p.pretraziArhivu(naslov, datum)
	local sadrzaj = mw.title.new('Rasprava o brisanju/Rasprave/'.. datum, 'Wikipedija' )
	local retval = -5
	if sadrzaj.exists then
		local textContent = mw.text.decode(sadrzaj:getContent(), true)
		-- mw.log(mw.text.decode(sadrzaj:getContent(), true))
		naslov = naslov:gsub("([()-])", "%%%1")	-- escaping parens, hyphens for regex below
		local match_start, match_end = string.find(textContent, "==% *%[%[%:?"..naslov)
		if match_start == nil then
			retval = 0				-- rasprava nije pronađena
		else 
			-- provjeri je li rasprava već zatvorena
			local after_match = string.sub(textContent, match_end + 1, match_end + 50)
			mw.log(after_match)
			if string.find(after_match:lower(), "zadrzan") or
			string.find(after_match:lower(), "zadržan") or
			string.find(after_match:lower(), "obrisan") then
				retval = 22			-- rasprava pronađena, zatvorena
				else retval = 11		-- rasprava pronađena, otvorena
			end
		end
	else retval = -1		-- stranica za traženi datum ne postoji
	end
	mw.log("pretraziArhivu: " .. retval)
	return retval
end
	
function p.pretraziSveArhive(frame)
	local ret = ''
	local count = 0;
	local naslov = frame.args[1]:gsub("^%s*(.-)%s*$", "%1") --remove staring and ending spaces
	local excludeMonth = frame.args[2] or nil
	local samoIzbroji = frame.args[3] or nil
	
	local rob = require('Modul:Rasprava o brisanju/data')
	
	if rob[naslov] then
	    for k, v in ipairs(rob[naslov]) do
	    	if v.mjesec ~= excludeMonth then
		    	if count == 0 then
		    		ret = ret .. '[[Wikipedija:Rasprava o brisanju/Rasprave/' .. v.mjesec .. '#' .. naslov .. '|' .. v.mjesec ..']] (' .. v.status .. ')'
		    		count=count+1
		    	else
		    		ret = ret .. '; [[Wikipedija:Rasprava o brisanju/Rasprave/' .. v.mjesec .. '#' .. naslov .. '|' .. v.mjesec ..']] (' .. v.status .. ')'
		    		count=count+1
		    	end
	    	end
	    end

	end
	
	-- backup ako lista nije ažurna
	local sm, sy = rob['@UPDATED@']:match("(%d+)%s+(%d+)")
	local startMonth = tonumber(sm)
	local startYear = tonumber(sy)
	-- ne oslanjaj se na listu za zadnji mjesec 
	-- (ako su rasprave dodane nakon zadnjeg ažuriranja) 
	for year = startYear, 2050 do
    local beginMonth = 1

    if year == startYear then
        beginMonth = startMonth
    end

    for month = beginMonth, 12 do
		mw.log('Manual search: ' .. month .. ' - ' .. year)
		datum = mapNumberToText(month) .. ' ' .. year .. '.'
		local arhiva = p.pretraziArhivu(naslov, datum) 
		if tonumber(arhiva) > 10 then
			if datum ~= excludeMonth then
				if count == 0 then
		    		ret = ret .. '[[Wikipedija:Rasprava o brisanju/Rasprave/' .. datum .. '#' .. naslov .. '|' .. datum ..']]'
		    		count=count+1
		    	else
		    		ret = ret .. '; [[Wikipedija:Rasprava o brisanju/Rasprave/' .. datum .. '#' .. naslov .. '|' .. datum ..']]'
		    		count=count+1
				end
    		end
			elseif arhiva == -1 then
		 		mw.log('stop', datum)
		 		if samoIzbroji then
		 			return count
		 		else
			 		return ret		-- rasprava za datum nije otvorena, stani s pretragom
		 		end
			 end
		end
	end
	if samoIzbroji then
 		return count
 	else
 		return ret
 	end
end

return p