Модуль:Effective protection level

Матеріал з Вікіпедії — вільної енциклопедії.
Перейти до навігації Перейти до пошуку
{{i}} Документація модуля[перегляд] [редагувати] [історія] [очистити кеш]

Цей модуль надає можливість отримати групу, яка необхідна для виконання вибраної дії на сторінці. Зараз він проводить тести за наступними критеріями:

  • Сторінка захищена від неперевірених змін: autoconfirmed
  • Сторінка є підсторінкою JavaScript або CSS в просторі користувача або MediaWiki: interfaceadmin
  • Сторінка є в просторі MediaWiki: sysop
  • Сторінка є підсторінкою JSON в просторі користувача: sysop
  • Сторінка є повністю захищена: sysop, templateeditor, extendedconfirmed або autoconfirmed
  • Сторінка використовується в сторінці, що захищена каскадним методом: sysop
  • Назва сторінки відповідає titleblacklist: templateeditor або autoconfirmed
  • Файл перейменовують: filemover
  • Сторінку перейменовують або завантажують файл: autoconfirmed
  • Створюється сторінка, що не є обговоренням чи сторінкою в Інкубаторі: user
  • Будь-що інше: *

Використання

[ред. код]

Увага: Цей модуль використовує до 4 ресурсозатратних викликів парсерних функцій кожного разу при своїй роботі. Він повинен використовуватися лише, якщо необхідно встановити точний рівень чинного захисту. В іншому випадку, натомість розгляньте використання title.protectionLevels.

В інших модулях

[ред. код]

Щоб завантажити цей модуль:

local effectiveProtectionLevel = require('Module:Effective protection level')._main

Функція приймає два параметри. Першим є рядком, що містить дію, яку потрібно перевірити, що повинна бути однією з «edit», «create», «move», «upload», «undelete» або «autoreview». Другим є необов'язковим параметром та може бути або назвою сторінки для перевірки, або назвою, яку повернула функції mw.title. Якщо другий параметр опущено, то перевіряється сторінка, яка відображається користувачу. Значення, що повертається, є рядком, що містить назву групи, яка вимагається для виконання вибраної дії.

У вікітексті

[ред. код]

Параметри є такими самими як і при виклику напряму.

{{#invoke:Effective protection level|дія|назва}}

Див. також

[ред. код]
local p = {}

-- Returns the permission required to perform a given action on a given title.
-- If no title is specified, the title of the page being displayed is used.
function p._main(action, 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 action == 'autoreview' then
		local level = mw.ext.FlaggedRevs.getStabilitySettings(title)
		level = level and level.autoreview
		if level == 'review' then
			return 'reviewer'
		elseif level ~= '' then
			return level
		else
			return nil -- not '*'. a page not being PC-protected is distinct from it being PC-protected with anyone able to review. also not '', as that would mean PC-protected but nobody can review
		end
	elseif action ~= 'edit' and action ~= 'move' and action ~= 'create' and action ~= 'upload' then
		error( 'First parameter must be one of edit, move, create, upload, autoreview', 2 )
	end
	if title.namespace == 8 then -- MediaWiki namespace
		return 'sysop'
	elseif title.namespace == 2 and title.isSubpage and ( title.contentModel == 'javascript' or title.contentModel == 'css' or title.contentModel == 'json' ) then -- user JS, CSS or JSON page
		return 'sysop'
	end
	local level = title.protectionLevels[action] and title.protectionLevels[action][1]
	if level == 'sysop' or level == 'editprotected' then
		return 'sysop'
	elseif title.cascadingProtection.restrictions[action] and title.cascadingProtection.restrictions[action][1] then -- used by a cascading-protected page
		return 'sysop'
	elseif level == 'templateeditor' then
		return 'templateeditor'
	elseif action == 'move' then
		local blacklistentry = mw.ext.TitleBlacklist.test('edit', pagename) -- Testing action edit is correct, since this is for the source page. The target page name gets tested with action move.
		if blacklistentry and not blacklistentry.params.autoconfirmed then
			return 'templateeditor'
		elseif title.namespace == 6 then
			return 'filemover'
		elseif level == 'extendedconfirmed' then
			return 'extendedconfirmed'
		else
			return 'autoconfirmed'
		end
	end
	local blacklistentry = mw.ext.TitleBlacklist.test(action, pagename)
	if blacklistentry then
		if not blacklistentry.params.autoconfirmed then
			return 'templateeditor'
		elseif level == 'extendedconfirmed' then
			return 'extendedconfirmed'
		else
			return 'autoconfirmed'
		end
	elseif level == 'editsemiprotected' then -- create-semiprotected pages return this for some reason
		return 'autoconfirmed'
	elseif level then
		return level
	elseif action == 'upload' then
		return 'autoconfirmed'
	elseif action == 'create' and title.namespace % 2 == 0 and title.namespace ~= 118 then -- You need to be registered, but not autoconfirmed, to create non-talk pages other than drafts
		return 'user'
	else
		return '*'
	end
end

setmetatable(p, { __index = function(t, k)
	return function(frame)
		return t._main(k, frame.args[1])
	end
end })

return p