Shko te përmbajtja

Moduli:Request counter

Page protected
Nga Wikipedia, enciklopedia e lirë

local p = {}

-- Helper to escape special characters for Lua patterns
local function escape_pattern(s)
	return string.gsub(s, '([%(%)%.%%%+%-%*%?%[%]%^%$])', '%%%1')
end

function p.fetch(frame)
	-- 1. Retrieve Arguments
	-- We look at the parent frame (Template args) first, then direct invoke args
	local args = frame.args
	local target_page = args[1] or (frame:getParent() and frame:getParent().args[1])

	-- 2. Validate Input (Strict Mode)
	-- If no page is provided, we do NOT auto-detect. We return an error or empty string.
	-- This prevents the "works here, breaks there" confusion.
	if not target_page or mw.text.trim(target_page) == '' then
		return '<span class="error">Gabim: Mungon titulli i faqes burimore.</span>'
	end
	
	target_page = mw.text.trim(target_page)
	
	-- 3. Check Cache
	-- (Optional for this scale, but good practice)
	if p.cache and p.cache[target_page] then return p.cache[target_page] end

	-- 4. The Counting Logic
	local title = mw.title.new(target_page)
	local count = 0

	if title and title.exists then
		local content = title:getContent() or ''
		
		-- Logic: We are looking for transclusions of subpages of the target.
		-- Pattern: {{TargetPage/Subpage}}
		-- We use the full page name to be precise.
		local prefix_pattern = escape_pattern(target_page .. '/')
		
		-- Regex Breakdown:
		-- ^%s* Start of line (ignoring indentation)
		-- %{%{%s* Opening braces {{
		-- prefix...    The specific page path
		-- [^|}]* The request name (stop before | parameters or closing })
		for line in string.gmatch(content, "[^\r\n]+") do
			-- We check if the line STARTS with the transclusion to avoid false positives in comments
			if string.match(line, '^%s*%{%{%s*' .. prefix_pattern .. '[^|}]*') then
				count = count + 1
			end
		end
	end

	-- 5. Output Formatting
	local result
	if count == 0 then
		result = 'Aktualisht nuk gjendet asnjë kërkesë e hapur.'
	elseif count == 1 then
		result = 'Aktualisht gjendet 1 kërkesë e hapur.'
	else
		result = 'Aktualisht gjenden ' .. count .. ' kërkesa të hapura.'
	end
	
	return result
end

return p