Sari la conținut

Modul:Protejat

De la Wikipedia, enciclopedia liberă
(dif) ← Versiunea anterioară | afișează versiunea curentă (dif) | Versiunea următoare → (dif)
-- Acest modul implementează {{Protejat}} --
local p = {}
local getArgs = require('Modul:Arguments').getArgs
local CONFIG_MODULE = 'Modul:Protejat/config'

local function validateDate(dateString, dateType)
	if not lang then
		lang = mw.language.getContentLanguage()
	end
	local success, result = pcall(lang.formatDate, lang, 'U', dateString)
	if success then
		result = tonumber(result)
		if result then
			return result
		end
	end
	error(string.format(
		'invalid %s: %s',
		dateType,
		tostring(dateString)
	), 4)
end

local function makeFullUrl(page, query, display)
	return string.format(
		'[%s %s]',
		tostring(mw.uri.fullUrl(page, query)),
		display
	)
end

local function effectiveProtectionLevel(pagename)
	local title
	if type(pagename) == 'table' and pagename.prefixedText then
		title = pagename
	elseif pagename then
		title = mw.title.new(pagename)
	else
		title = mw.title.getCurrentTitle()
	end
	pagename = title.prefixedText
	if title.namespace == 8 then -- MediaWiki namespace
		if title.text:sub(-3) == '.js' or title.text:sub(-4) == '.css' or title.contentModel == 'javascript' or title.contentModel == 'css' then -- site JS or CSS page
			return 'interfaceadmin'
		else -- any non-JS/CSS MediaWiki page
			return 'sysop'
		end
	elseif title.namespace == 2 and title.isSubpage then
		if title.contentModel == 'javascript' or title.contentModel == 'css' then -- user JS or CSS page
			return 'interfaceadmin'
		elseif title.contentModel == 'json' then -- user JSON page
			return 'sysop'
		end
	end
	local level = title.protectionLevels['edit'] and title.protectionLevels['edit'][1]
	return level
end

local function effectiveProtectionExpiry(pagename)
	local title
	if type(pagename) == 'table' and pagename.prefixedText then
		title = pagename
	elseif pagename then
		title = mw.title.new(pagename)
	else
		title = mw.title.getCurrentTitle()
	end
	pagename = title.prefixedText
	local rawExpiry = mw.getCurrentFrame():callParserFunction('PROTECTIONEXPIRY', 'edit', pagename)
	if rawExpiry == 'infinity' then
		return 'infinity'
	elseif rawExpiry == '' then
		return 'unknown'
	else
		local year, month, day, hour, minute, second = rawExpiry:match(
			'^(%d%d%d%d)(%d%d)(%d%d)(%d%d)(%d%d)(%d%d)$'
		)
		if year then
			return string.format(
				'%s-%s-%sT%s:%s:%s',
				year, month, day, hour, minute, second
			)
		else
			error('eroare internă în Modul')
		end
	end
end

function p._protection(args, cfg)
	local obj = {}
	obj._cfg = cfg
	obj.title = mw.title.getCurrentTitle()
	
	-- Set level
	obj.level = effectiveProtectionLevel(obj.title)
	if obj.level == nil then
		return '[[Categorie:Pagini neprotejate ce conțin formate de protejare]]'
	end
	-- Set expiry
	local effectiveExpiry = effectiveProtectionExpiry(obj.title)
	if effectiveExpiry == 'infinity' then
		obj.expiry = ' pe termen nelimitat'
	elseif effectiveExpiry ~= 'unknown' then
		obj.expiry = validateDate(effectiveExpiry, 'expiry date')
		obj.expiry = obj.expiry + 10800
		local lang = mw.language.getContentLanguage()
		local success, date = pcall(
			lang.formatDate,
			lang,
			'j F Y',
			'@' .. tostring(obj.expiry)
		)
		if success then
			obj.expiry = ' până pe ' .. date
		end
	end
	
	-- Set reason
	if args[1] or args.motiv then
		obj.reason = ' ' .. mw.ustring.lower(args[1] or args.motiv)
		if obj.reason:find('|') then
			error('parametrul „motiv” nu poate conține caracterul pipe ("|")', 3)
		end
	end
	
	if (args.dispute or args.disputa) and (args.dispute == 'yes' or args.dispute == 'y' or args.dispute == 'da') then
		obj.expiry = obj.expiry .. ' sau'
		obj.reason = ' până când disputa se va rezolva'
	end
	
	if args.sock and (args.sock == 'yes' or args.sock == 'y' or args.sock == 'da') then
		obj.reason = ' pentru a preveni [[Wikipedia:Clone|clonele]] unor [[Wikipedia:Blocare|utilizatori blocați]]'
		.. ' sau [[Wikipedia:Listă a utilizatorilor interziși|interziși]] să o modifice'
	end
	
	if args.small then
		if args.small == 'yes' or args.small == 'y'or args.small == 'da' then
			return p.build_icon(obj)
		else
			return p.build_banner(obj)
		end
	else return p.build_banner(obj)
	end
end

local function getCat(obj)
	local cat = '[[Categorie:'
	local expiry = ''
	
	if obj.expiry ~= ' pe termen nelimitat' then
		expiry = ' temporar'
	end
	
	if obj.level == 'templateeditor' and obj.title.namespace ~= 10 then cat = cat .. 'Pagini non-format protejate ca format'
	elseif obj.level == 'autoconfirmed' and obj.title.namespace == 10 then cat = cat .. 'Formate semiprotejate'
	elseif obj.level == 'autoconfirmed' and obj.title.namespace == 3 then cat = cat .. 'Utilizatori care au pagina de discuție semiprotejată'
	else cat = cat .. obj._cfg.cats[obj.level]
	end
	
	cat = cat .. expiry .. ']]'
	return cat
end

local function buildTextIcon (obj)
	local text = (obj._cfg.pageNamespace[obj.title.namespace] or obj._cfg.pageNamespace['default']) .. ' este '
	local protectionLevel
	
	if obj.level == 'autoconfirmed' then text = text .. 'semi' end
	text = text .. 'protejat' .. (obj._cfg.pageNamespaceA[obj.title.namespace] or obj._cfg.pageNamespaceA['default'])
	
	if obj.level == 'autoconfirmed' then text = text .. obj.expiry .. ' pentru a preveni vandalismul.'
	else text = text .. (obj._cfg.levels[obj.level] or '') .. obj.expiry .. ' pentru a preveni vandalismul.' end
	
	return text
end

function p.build_icon (obj)
	local image = obj._cfg.images[obj.level]
	local imageLink = obj._cfg.imageLinks[obj.level]
	local text = buildTextIcon(obj)
	local frame = mw.getCurrentFrame()
	local icon = frame:extensionTag{name = 'nowiki'} .. frame:extensionTag{
		name = 'indicator',
		args = {name = 'protected'},
		content = '<span style="white-space: nowrap">&nbsp;[[Fișier:'..image..'|20px|link='..imageLink..'|'..text..']]</span>'
	}
	return icon .. getCat(obj)
end

function p.build_banner (obj)
	local text = '<span style="font-weight:bold">' .. (obj._cfg.pageNamespace[obj.title.namespace] or obj._cfg.pageNamespace['default']) .. ' este '
	.. '[[' .. obj._cfg.imageLinks[obj.level] .. '|protejat'
	.. (obj._cfg.pageNamespaceA[obj.title.namespace] or obj._cfg.pageNamespaceA['default'])
	.. ' la modificare]]' .. obj.expiry .. (obj.reason or ' pentru a preveni vandalismul')
	.. ', astfel încât doar ' .. obj._cfg.text[obj.level] .. (obj._cfg.pageNamespaceO[obj.title.namespace] or obj._cfg.pageNamespaceO['default']) .. ' pot modifica.</span>'
	local image = '[[Fișier:' .. obj._cfg.images[obj.level] .. '|40px]]'
	
	local logpage = ('[https://ro.wikipedia.org/w/index.php?title=Special:Jurnal&page=%s&type=protect jurnalul protejărilor]'):format(mw.uri.encode(tostring(obj.title)))
	local talkpage = string.format(
		'[[%s:%s#%s|%s]]',
		mw.site.namespaces[obj.title.namespace].talk.name,
		obj.title.text,
		'top',
		'discuta pe pagina de discuții'
	)
	
	local text_info = '<br>Citiți [[Wikipedia:Pagină protejată|politica de protejare]] și ' .. logpage
			.. ' pentru mai multe detalii. Dacă nu puteți modifica pagina și doriți să faceți o schimbare, puteți ' .. talkpage
			.. ', [[Wikipedia:Cereri pentru protejarea paginilor#Cereri pentru deprotejarea paginilor|cere deprotejarea paginii]], vă puteți [[Special:Userlogin|autentifica]] sau [[Special:Înregistrare|înregistra]].'
	
	text = text .. text_info
	
	return require('Module:Message box').main('ombox', {
		type = 'protection',
		image = image,
		text = text,
		class = 'def'
	}) .. getCat(obj) .. mw.getCurrentFrame():extensionTag{ name = 'templatestyles', args = { src = 'Meta-casetămap/styles.css'} }
end

function p._main(args, cfg)
	cfg = cfg or require(CONFIG_MODULE)
	local result = p._protection(args, cfg)
	return result
end

function p.main(frame)
	local cfg = require (CONFIG_MODULE)
	return p._main(getArgs(frame), cfg)
end

return p