Module:Request for permission links

Permanently protected module
From Wikipedia, the free encyclopedia

local p = {}
local getArgs = require('Module:Arguments').getArgs

function p.links(permission, parenthetical, username, userURL)
	local l = {}
	----------------------------------------------------------------------------
	----------------------------------------------------------------------------
	-- To add a new link, find an empty line in the section below
	-- and add either a link or a wikilink, like so:
	--
	--		l[#l+1] = '[URL external link]'
	--		l[#l+1] = '[[Page|wikilink]]' 
	--
	-- However, replace any instance of a username with:
	--
	--		'..userURL..'		-- For a  [ ]  URL/external link
	--		'..username..'		-- For a [[ ]] wikilink
	--
	-- Each new "l" (link string) will automatically have a spaced dot in
	-- the final output. To skip the spaced dot, add a line before with:
	--
	--		before('')
	--
	-- See the existing links below for examples.
	----------------------------------------------------------------------------
	----------------------------------------------------------------------------
	local Before = {'', '', ''} -- Skip dots before 1, 2, 3.
	function before( replacement )
		Before[#l+1] = replacement
	end
    -- First links: 1. Userpage, 2. parenthetical (defined below in p.main)
    -- 3. User talk page, 4. User talk page history, 5. User contributions.
	l[1] = '[[User:'..username..'|'..username..']]'
	l[2] = parenthetical
	l[3] = '([[User talk:'..username..'|t]]'
	l[4] = '[[Special:History/User talk:'..username..'|th]]'
	l[5] = '[[Special:Contribs/'..username..'|c]]'
	-- Permission-specific contributions links
	if permission == 'Template editor' then
		before(' <sup>(')
		l[#l+1] = '[https://en.wikipedia.org/w/index.php?title=Special:Contributions&target='..userURL..'&namespace=10 template space]'
		l[#l+1] = '[https://en.wikipedia.org/w/index.php?title=Special:Contributions&target='..userURL..'&namespace=11 edit requests])</sup>'
	elseif permission == 'Page mover' then
		before(' <sup>(')
		l[#l+1] = '[https://sigma.toolforge.org/usersearch.py?name='..userURL..'&page=Wikipedia%3ARequested+moves%2FTechnical+requests&max=500&server=enwiki RM/TR]'
		l[#l+1] = '[https://sigma.toolforge.org/summary.py?name='..userURL..'&search=requested+move&server=enwiki RMs])</sup>'
	elseif permission == 'File mover' then
		before(' <sup>(')
		l[#l+1] = '[https://en.wikipedia.org/w/index.php?title=Special:Contributions&target='..userURL..'&namespace=6 file space])</sup>'
	end
	
	l[#l+1] = '<span class="sysop-show">[[Special:DeletedContributions/'..username..'|del]]'
	l[#l+1] = '</span>[[:luxo:'..username..'|cross-wiki]]'
	l[#l+1] = '[[Special:CentralAuth/'..username..'|SUL]]'
	l[#l+1] = '[https://xtools.wmcloud.org/ec/en.wikipedia.org/'..userURL..' edit counter]'

	if permission == 'Event coordinator' then
		l[#l+1] = '[https://en.wikipedia.org/w/index.php?title=Special:Log&type=newusers&user='..userURL..'&limit=5000 accounts created]'
	elseif permission == 'Template editor' then
		l[#l+1] = '[https://xtools.wmflabs.org/pages?project=en.wikipedia.org&username='..userURL..'&namespace=10 templates created]'
	else 
		l[#l+1] = 'pages created ([https://xtools.wmflabs.org/pages?user='..userURL..'&project=en.wikipedia.org&namespace=all&redirects=noredirects xtools]'
		l[#l+1] = '[https://sigma.toolforge.org/created.py?name='..userURL..'&server=enwiki&ns=,,&redirects=none sigma])'
	end

	l[#l+1] = '[https://xtools.wmflabs.org/autoedits/en.wikipedia.org/'..userURL..' non-automated edits]'
	l[#l+1] = '[https://xtools.wmflabs.org/categoryedits/en.wikipedia.org/'..userURL..'/Living_people BLP edits]'
	l[#l+1] = '[https://en.wikipedia.org/w/index.php?title=Special%3AContributions&target='..userURL..'&namespace=all&tagfilter=mw-undo undos]'
	l[#l+1] = '[https://en.wikipedia.org/w/index.php?title=Special%3AContributions&target='..userURL..'&namespace=all&tagfilter=mw-manual-revert manual reverts]'
	l[#l+1] = '[https://en.wikipedia.org/w/index.php?title=Special%3AContributions&target='..userURL..'&namespace=all&tagfilter=mw-rollback rollbacks]'
	l[#l+1] = '[https://en.wikipedia.org/w/index.php?title=Special:Log&user='..userURL..' logs]'
	before(' <sup>(')
	l[#l+1] = '[https://en.wikipedia.org/w/index.php?title=Special:Log/block&page=User:'..userURL..' blocks]'
	l[#l+1] = '[https://en.wikipedia.org/w/index.php?title=Special:Log/rights&page=User:'..userURL..' rights]'
	l[#l+1] = '[https://en.wikipedia.org/w/index.php?title=Special:Log/move&user='..userURL..' moves])</sup>'
	
	if permission == 'New page reviewer' then
		l[#l+1] = '[https://en.wikipedia.org/w/index.php?title=Special:Log/pagetriage-curation&user='..userURL..' curation log]'
		l[#l+1] = '[https://en.wikipedia.org/w/index.php?title=Special:Log/patrol&user='..userURL..'&subtype=patrol patrol log]'
		l[#l+1] = '[https://afdstats.toolforge.org/afdstats.py?name='..userURL..'&max=&startdate=&altname= AFD stats]'
		l[#l+1] = '[https://apersonbot.toolforge.org/afchistory/?user='..userURL..' AFC stats]'
		l[#l+1] = '[[User:'..username..'/CSD log|CSD log]]'
		l[#l+1] = '[[User:'..username..'/PROD log|PROD log]]'
		l[#l+1] = '[[User:'..username..'/Draftify log|Draftify log]]'
		l[#l+1] = '[https://xtools.wmcloud.org/ec-namespacetotals/en.wikipedia.org/'..userURL..' Mainspace edits]'
	end

	l[#l+1] = '[[WP:Arbitration/Requests/Case/'..username..'|rfar]]'
	l[#l+1] = '[[WP:Sockpuppet investigations/'..username..'|spi]]'
	l[#l+1] = '[[WP:Contributor copyright investigations/'..username..'|cci]])'
	
	----------------------------------------------------------------------------
	----------------------------------------------------------------------------
	-- New links should be somewhere above this line.
	-- Next, links for assigning permissions and notifying the user.
	----------------------------------------------------------------------------
	----------------------------------------------------------------------------
	
	before(' <span class="sysop-show perm-assign-permissions"><span class="nowrap">(')
	if permission == 'AutoWikiBrowser' then
		l[#l+1] = "[https://en.wikipedia.org/w/index.php?title=Wikipedia:AutoWikiBrowser/CheckPageJSON&action=edit edit CheckPage])"
	else
		l[#l+1] = "[[Special:UserRights/"..username.."|<span style='color: #03B;'>'''assign permissions'''</span>]])"
	end
	
	local subpages = {'Account creator', 'Autopatrolled', 'AutoWikiBrowser', 'Confirmed', 'Event coordinator', 'Extended confirmed', 'File mover', 'Mass message sender', 'New page reviewer', 'Page mover', 'Pending changes reviewer', 'Rollback', 'Template editor'}
	local templates = {['Account creator']='acc', ['Autopatrolled']='ap', ['AutoWikiBrowser']=false, ['Confirmed']=false, ['Event coordinator']='ev', ['Extended confirmed']=false, ['File mover']='fm', ['Mass message sender']='mms', ['New page reviewer']='npr', ['Page mover']='pm', ['Pending changes reviewer']='pc', ['Rollback']='rb', ['Template editor']='te'}
	
	if permission then
		if templates[permission] then
			local templateURL = mw.ustring.gsub(permission..' granted', '%s', '%%20')
			local headingURL = templateURL
			if permission == 'Rollback' then
				templateURL = templateURL..'%20'..'2' -- [[Template:Rollback granted 2]]
			end
			before('<sup>(')
			l[#l+1] = '[https://en.wikipedia.org/w/index.php?title=User_talk:'..userURL..'&action=edit&section=new&preloadtitle='
			..headingURL..'&preload=Template:'..templateURL..' notify])</sup></span>'
		else
			l[#l] = l[#l]..'</span>'
		end
	else
		before('</span><sup>(')
		for i=1,#subpages do
			if templates[subpages[i]] then
				local templateURL = mw.ustring.gsub(subpages[i]..' granted', '%s', '%%20')
				l[#l+1] = '[https://en.wikipedia.org/w/index.php?title=User_talk:'..userURL..'&action=edit&section=new&preloadtitle='
				..templateURL..'&preload=Template:'..templateURL..' '..templates[subpages[i]]..']'
			end
		end
		l[#l] = l[#l]..')</sup>'
	end
	
	-- All links should be within a plainlinks span
	Before[1] = '<span class="plainlinks">'
	l[#l] = l[#l]..'</span></span>'
	
	local ret = ''
	
	for n=1,#l do
		ret = ret .. (Before[n] or " '''&middot;''' ") .. l[n]
	end
	
	return ret
end
	----------------------------------------------------------------------------
	----------------------------------------------------------------------------
	-- This is the main function (the one directly called by the template).
	-- It determines the permission (if uniquely determined), the username, 
	-- and the URL encoding of the username, then creates a parenthetical 
	-- listing any permission(s) sought at subpages of WP:PERM (if viewing the 
	-- main WP:PERM page rather than a subpage WP:PERM/[...]).
	-- It then uses this information to call the p.links function above.
	----------------------------------------------------------------------------
	----------------------------------------------------------------------------
function p.main(frame)
	local args = getArgs(frame)
	local username = args['User'] or args['1'] or 'Example' -- {{{User|{{{1|Example}}}}}}
	local userURL = frame:callParserFunction('urlencode', username, 'PATH') -- {{urlencode:{{{User|{{{1|Example}}}}}}|PATH}}
	local permission = args['permission'] -- {{{permission|nil}}}
	local base = 'Wikipedia:Requests for permissions' -- A/K/A [[WP:PERM]].
	local current = mw.allToString(mw.title.getCurrentTitle()) -- {{FULLPAGENAME}}
	local shortened = mw.ustring.gsub(current, base..'/', '') -- Subtitle, if the current title is a subpage of [[WP:PERM]].
	-- The 13 subpages of WP:PERM currently used for requesting permissions: 
	local subpages = {'Account creator', 'Autopatrolled', 'AutoWikiBrowser', 'Confirmed', 'Event coordinator', 'Extended confirmed', 'File mover', 'Mass message sender', 'New page reviewer', 'Page mover', 'Pending changes reviewer', 'Rollback', 'Template editor'}
	local check = {['Account creator']=true, ['Autopatrolled']=true, ['AutoWikiBrowser']=true, ['Confirmed']=true, ['Event coordinator']=true, ['Extended confirmed']=true, ['File mover']=true, ['Mass message sender']=true, ['New page reviewer']=true, ['Page mover']=true, ['Pending changes reviewer']=true, ['Rollback']=true, ['Template editor']=true} -- Use check[x] to check whether x is one of the subpages.
	
	-- Ignore any provided args.permission if not valid
	if not check[permission] then
		permission = nil
	end
	
	local intext = {}
	local count = 0
	local tentative = ''
	
	-- If viewing [[WP:PERM]] or its subpages, then check each subpage for a request for this user.
	if current == base or check[shortened] then
		local text = {}
		local pattern = mw.ustring.gsub('{{rfplinks|1='..username..'}}', "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" ) -- Copied from _escapePattern in [[Module:String]]
		for i=1,#subpages do
			text[i] = mw.title.new( base..'/'..subpages[i] ):getContent() or ''
			if text[i] ~= mw.ustring.gsub(text[i], pattern, '') then
				intext[i] = true
				tentative = subpages[i]
				count = count + 1
			end
		end
		-- If exactly one subpage with a request, then use that as the permission.
		if count == 1 then
			permission = tentative
		end
		-- If the page is one one of the subpages, then use that as the permission.
		if check[shortened] then
			permission = shortened
		end
	end
	
	-- Now to form the parenthetical. 
	local parenthetical = ' '
	-- If requesting multiple perms at [[WP:PERM]], show each one requested.
	if count > 1 then
		parenthetical = ' (<small>requesting '
		local first = true
		for i=1,#subpages do
			if intext[i] then
				if not first then
					parenthetical = parenthetical .. ', '
				else
					first = false
				end
				parenthetical = parenthetical..'[['..base..'/'..subpages[i]
				-- Unless the link is to the current page, add a section link.
				if subpages[i] ~= shortened then 
					parenthetical = parenthetical..'#User:'..username
				end
				parenthetical = parenthetical..'|'..subpages[i]..']]'
			end
		end
		parenthetical = parenthetical..'</small>) '
	-- If requesting one perm, but viewed on a page other than that 
	-- perm's subpage of [[WP:PERM]], then show the permission requested.
	elseif permission then
		if current ~= base..'/'..permission then
			parenthetical = ' (<small>requesting  [['..base..'/'..permission..'#User:'..username..'|'..permission..']]</small>) '
		end
	end
	
	return p.links(permission, parenthetical, username, userURL)
end

return p