Module:Protection banner: Difference between revisions

add namespace parser
Enwikipedia>Mr. Stradivarius
(convert categories back to a flat table scheme)
Enwikipedia>Mr. Stradivarius
(add namespace parser)
Line 43:
['pc-autoconfirmed-all-all-all'] = 'Wikipedia pending changes protected pages (level 1)',
['pc-reviewer-all-all-all'] = 'Wikipedia pending changes protected pages (level 2)',
}
 
local sortCatsnskeys = {}
[2] = 'user',
[3] = 'user',
[4] = 'project',
[6] = 'file',
[10] = 'template',
[12] = 'project',
[14] = 'category',
[100] = 'portal',
}
 
Line 95 ⟶ 106:
end
 
function p.parseNamespace(ns)
function p.matchCategory(cats, protType, protLevel, namespace, expiry, reason)
-- The ns variable is the namespace number.
local function isValidCat(t)
if not ns or type(ns) ~= 'number' then
--[[
return true'all'
-- Does a first parse over the categories, removing any that explicitly
-- name a wrong parameter. This accounts for the majority of all of the
-- categories.
--
-- We check the parameters most likely to be wrong first. This saves on
-- table lookups, which shoud make the function faster.
--]]
local testType, testLevel, testNs, testExpiry, testReason
testReason = t.reason
if testReason and testReason ~= reason then
return false
end
testNs = t.namespace
if testNs and not testNs[namespace] then
return false
end
testExpiry = t.expiry
if testExpiry and testExpiry ~= expiry then
return false
end
testLevel = t.level
if testLevel and testLevel ~= protLevel then
return false
end
testType = t.type
if testType and testType ~= protType then
return false
end
return true
end
local nskey = nskeys[ns]
 
if not nskey and ns % 2 == 1 then
-- Remove invalid cats.
nskey = 'talk'
local sortCats = {}
else
for i, t in ipairs(cats) do
nskey = 'all'
if isValidCat(t) then
sortCats[#sortCats + 1] = t
end
end
return falsenskey
end
 
function p.matchCategory(cats, protType, protLevel, namespace, expiry, reason)
-- Sort the remaining cats.
local ns1nskey = t1p.parseNamespace(namespace)
-- The categories which are left either explicitly name the right parameter,
-- or implicitly allow all values for that parameter.
table.sort(sortCats, function (t1, t2)
local type1 = t1.type
local type2 = t2.type
local level1 = t1.level
local level2 = t2.level
local ns1 = t1.namespace
local ns2 = t2.namespace
local exp1 = t1.expiry
local exp2 = t2.expiry
local reason1 = t1.reason
local reason2 = t2.reason
 
local score1 = 0
local score2 = 0
end)
end