Module:Effective protection level

From KitwarePublic
Revision as of 23:19, 12 July 2016 by WikiSysop (talk | contribs) (1 revision)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Documentation for this module may be created at Module:Effective protection level/doc

 1 local p = {}
 2 
 3 -- Returns the permission required to perform a given action on a given title.
 4 -- If no title is specified, the title of the page being displayed is used.
 5 function p._main(action, pagename)
 6 	local title
 7 	if type(pagename) == 'table' and pagename.prefixedText then
 8 		title = pagename
 9 	elseif pagename then
10 		title = mw.title.new(pagename)
11 	else
12 		title = mw.title.getCurrentTitle()
13 	end
14 	pagename = title.prefixedText
15 	if action == 'autoreview' then
16 		local level = mw.ext.FlaggedRevs.getStabilitySettings(title)
17 		level = level and level.autoreview
18 		if level == 'review' then
19 			return 'reviewer'
20 		elseif level ~= '' then
21 			return level
22 		else
23 			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
24 		end
25 	elseif action ~= 'edit' and action ~= 'move' and action ~= 'create' and action ~= 'upload' then
26 		error( 'First parameter must be one of edit, move, create, upload, autoreview', 2 )
27 	end
28 	if title.namespace == 8 then -- MediaWiki namespace
29 		return 'sysop'
30 	elseif title.namespace == 2 and title.isSubpage and ( title.contentModel == 'javascript' or title.contentModel == 'css' ) then -- user JS or CSS page
31 		return 'sysop'
32 	end
33 	local level = title.protectionLevels[action] and title.protectionLevels[action][1]
34 	if level == 'sysop' or level == 'editprotected' then
35 		return 'sysop'
36 	elseif title.cascadingProtection.restrictions[action] and title.cascadingProtection.restrictions[action][1] then -- used by a cascading-protected page
37 		return 'sysop'
38 	elseif level == 'templateeditor' then
39 		return 'templateeditor'
40 	elseif action == 'move' then
41 		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.
42 		if blacklistentry and not blacklistentry.params.autoconfirmed then
43 			return 'templateeditor'
44 		elseif title.namespace == 6 then
45 			return 'filemover'
46 		elseif level == 'extendedconfirmed' then
47 			return 'extendedconfirmed'
48 		else
49 			return 'autoconfirmed'
50 		end
51 	end
52 	local blacklistentry = mw.ext.TitleBlacklist.test(action, pagename)
53 	if blacklistentry then
54 		if not blacklistentry.params.autoconfirmed then
55 			return 'templateeditor'
56 		elseif level == 'extendedconfirmed' then
57 			return 'extendedconfirmed'
58 		else
59 			return 'autoconfirmed'
60 		end
61 	elseif level == 'editsemiprotected' then -- create-semiprotected pages return this for some reason
62 		return 'autoconfirmed'
63 	elseif level then
64 		return level
65 	elseif action == 'upload' then
66 		return 'autoconfirmed'
67 	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
68 		return 'user'
69 	else
70 		return '*'
71 	end
72 end
73 
74 setmetatable(p, { __index = function(t, k)
75 	return function(frame)
76 		return t._main(k, frame.args[1])
77 	end
78 end })
79 
80 return p