モジュール:Protected edit request/active

require('strict')
local yesno, makeMessageBox -- passed in from Module:Protected edit request
local makeToolbar = require('Module:Toolbar')._main
local getPagetype = require('Module:Pagetype')._main
local effectiveProtectionLevel = require('Module:Effective protection level')._main
----------------------------------------------------------------------
-- Helper functions
----------------------------------------------------------------------
local function makeWikilink(page, display)
	if display then
		return mw.ustring.format('%s', page, display)
	else
		return mw.ustring.format('%s', page)
	end
end
----------------------------------------------------------------------
-- Title class
----------------------------------------------------------------------
-- This is basically the mw.title class with some extras thrown in.
local title = {}
title.__index = title
function title.getProtectionLevelText(protectionLevel)
	-- Gets the text to use in anchors and urn links.
	local levels = {unprotected = 'editunprotected', autoconfirmed = 'editsemiprotected', extendedconfirmed = 'editextendedprotected', templateeditor = 'edittemplateprotected', sysop = 'editprotected', interfaceadmin = 'editinterfaceprotected'}
	return levelsprotectionLevel
end
function title.new(...)
	local success, obj = pcall(mw.title.new, ...)
	if not (success and obj) then return end
	title.init(obj)
	return obj
end
function title.init(obj)
	-- Add a protectionLevel property.
	obj.protectionLevel = effectiveProtectionLevel(obj.exists and 'edit' or 'create', obj)
	if obj.protectionLevel == '*' then
		-- Make unprotected pages return "unprotected".
		obj.protectionLevel = 'unprotected'
	elseif obj.protectionLevel == 'user' then
		-- If we just need to be registered, pretend we need to be autoconfirmed, since it's the closest thing we have.
		obj.protectionLevel = 'autoconfirmed'
	end
	-- Add a pagetype property.
	obj.pagetype = getPagetype{page = obj.prefixedText, defaultns = 'all'}
	
	-- Add link-making methods.
	function obj:makeUrlLink(query, display)
		return mw.ustring.format('%s %s', self:fullUrl(query), display)
	end
	function obj:makeViewLink(display)
		return self:makeUrlLink({redirect = 'no'}, display)
	end
	function obj:makeEditLink(display)
		return self:makeUrlLink({action = 'edit'}, display)
	end
	function obj:makeHistoryLink(display)
		return self:makeUrlLink({action = 'history'}, display)
	end
	function obj:makeLastEditLink(display)
		return self:makeUrlLink({diff = 'cur', oldid = 'prev'}, display)
	end
	function obj:makeWhatLinksHereLink(display)
		return makeWikilink('Special:WhatLinksHere/' .. self.prefixedText, display)
	end
	function obj:makeCompareLink(otherTitle, display)
		display = display or 'diff'
		local comparePagesTitle = title.new('Special:ComparePages')
		return comparePagesTitle:makeUrlLink({page1 = self.prefixedText, page2 = otherTitle.prefixedText}, display)
	end
	function obj:makeLogLink(logType, display)
		local logTitle = title.new('Special:Log')
		return logTitle:makeUrlLink({type = logType, page = self.prefixedText}, display)
	end
	function obj:urlEncode()
		return mw.uri.encode(self.prefixedText, 'WIKI')
	end
	function obj:makeUrnLink(boxProtectionLevel)
		-- Outputs a urn link. The protection level is taken from the template, rather than detected from page itself,
		-- as the detection may be inaccurate for cascade-protected and title-blacklisted pages as of Nov 2013.
		local protectionLinkText = title.getProtectionLevelText(boxProtectionLevel)
		return mw.ustring.format('urn:x-wp-%s:%s ', protectionLinkText, self:urlEncode())
	end
	-- Get a subpage title object, but go through pcall rather than use the unprotected mw.title:subPageTitle.
	function obj:getSubpageTitle(subpage)
		return title.new(self.prefixedText .. '/' .. subpage)
	end
	function obj:getSandboxTitle()
		if self.isSubpage and self.contentModel == 'sanitized-css' then
			local success2, obj2 = pcall(mw.title.makeTitle, self.namespace, self.baseText .. '/sandbox/' .. self.subpageText)
			if success2 and obj2 then
				title.init(obj2)
				return obj2
			end
		end
		return self:getSubpageTitle('sandbox')
	end
end
----------------------------------------------------------------------
-- TitleTable class
----------------------------------------------------------------------
local titleTable = {}
titleTable.__index = titleTable
function titleTable.new(args)
	-- Get numerical arguments and make title objects for each of them. 
	local nums = {}
	for k, v in pairs(args) do
		if type(k) == 'number' then
			table.insert(nums, k)
		end
	end
	table.sort(nums)
	local titles = {}
	for _, num in ipairs(nums) do
		local title = title.new(argsnum)
		table.insert(titles, title)
	end
	-- Get the current title, and get the subject title if no titles were specified.
	titles.currentTitle = mw.title.getCurrentTitle()
	if #titles %s%s%s%s', pipe, name, equals, val)
end
function blurb.makeTemplateLink(s)
	return mw.ustring.format('%s%s%s', mw.text.nowiki())
end
function blurb:makeProtectionText()
	local boxProtectionLevel = self.boxProtectionLevel
	local levels = {'*' = '保護されていない', autoconfirmed = '半保護された', extendedconfirmed = '拡張半保護された', templateeditor = 'template-protected', sysop = '全保護された', interfaceadmin = 'interface-protected'}
	for level, protectionText in pairs(levels) do
		if level == boxProtectionLevel then
			return mw.ustring.format('%s', protectionText)
		end
	end
	error('Unknown protection level ' .. boxProtectionLevel)
end
function blurb.getPagetypePlural(title)
	local pagetype = title.pagetype
	return pagetype
	--if pagetype == 'category' then
		return 'categories'
	else
		return pagetype .. 's'
	end--
end
-- Normal methods --
function blurb:makeLinkList(title)
	local tbargs = {} -- The argument list to pass to Module:Toolbar
	tbargs.style = 'font-size: smaller;'
	tbargs.separator = 'dot'
	-- Page links.
	table.insert(tbargs, title:makeEditLink('編集'))
	table.insert(tbargs, title:makeHistoryLink('履歴'))
	table.insert(tbargs, title:makeLastEditLink('最新版'))
	table.insert(tbargs, title:makeWhatLinksHereLink('リンク元'))
	-- Sandbox links.
	local sandboxTitle = title:getSandboxTitle()
	if sandboxTitle and sandboxTitle.exists then
		table.insert(tbargs, sandboxTitle:makeViewLink('サンドボックス'))
		table.insert(tbargs, sandboxTitle:makeEditLink('サンドボックスを編集'))
		table.insert(tbargs, sandboxTitle:makeHistoryLink('サンドボックスの履歴'))
		table.insert(tbargs, sandboxTitle:makeLastEditLink('サンドボックスの最新版'))
		table.insert(tbargs, title:makeCompareLink(sandboxTitle, 'サンドボックスとの差分'))
	end
	-- Test cases links.
	local testcasesTitle = title:getSubpageTitle('testcases')
	if testcasesTitle and testcasesTitle.exists then
		table.insert(tbargs, testcasesTitle:makeViewLink('テストケース'))
	end
	-- Transclusion count link.
	if title.namespace == 10 or title.namespace == 828 then -- Only add the transclusion count link for templates and modules.
		local tclink = mw.uri.new{
			host = 'templatecount.toolforge.org',
			path = '/index.php',
			query = {
				lang = 'ja',
				name = title.text,
				namespace = title.namespace,
			},
			fragment = 'bottom'
		}
		tclink = string.format('%s 使用数', tostring(tclink))
		table.insert(tbargs, tclink)
	end
	-- Protection log link.
	if title.namespace ~= 8 then -- MediaWiki pages don't have protection log entries.
		table.insert(tbargs, title:makeLogLink('protect', '保護記録'))
	end
	self.linkCount = self.linkCount + #tbargs -- Keep track of the number of total links created by the object.
	return makeToolbar(tbargs)
end
function blurb:makeLinkLists()
	local titles = self.titles
	if #titles == 1 then
		return self:makeLinkList(titles1)
	else
		local ret = {}
		table.insert(ret, )
		for i, titleObj in ipairs(titles) do
			table.insert(ret, mw.ustring.format('%s %s', titleObj:makeViewLink(titleObj.prefixedText), self:makeLinkList(titleObj)))
		end
		table.insert(ret, )
		return table.concat(ret)
	end
end
function blurb:makeIntro()
	local titles = self.titles
	local requested = '依頼が出されています'
	local protectionText
	if titles:hasNonInterfacePage() then
		protectionText = self:makeProtectionText()
	else
		protectionText =  -- Interface pages cannot be unprotected, so we don't need to explicitly say they are protected.
	end
	-- Deal with cases where we are passed multiple titles.
	if #titles > 1 then
		local pagetype
		if titles:hasSamePagetype() then
			pagetype = blurb.getPagetypePlural(titles1)
		else
			pagetype = 'ページ'
		end
		return mw.ustring.format("以下に示す%s%sに編集%s。", protectionText, pagetype, requested)
	end
	-- Deal with cases where we are passed only one title.
	local title = titles1
	local stringToFormat
	if title.exists then
		stringToFormat = '%s%s「%s」に編集%s。'
	else
		stringToFormat = '%s%s「%s」に作成%s。'
	end
	stringToFormat = "" .. stringToFormat .. ""
	return mw.ustring.format(stringToFormat, protectionText, title.pagetype, title:makeViewLink(title.prefixedText), requested)
end
function blurb:makeBody()
	local titles = self.titles
	local protectionLevels = titles:getProtectionLevels()
	local boxProtectionLevel = self.boxProtectionLevel
	local hasNonInterfacePage = titles:hasNonInterfacePage()
	local isPlural = false
	if #titles > 1 then
		isPlural = true
	end
	
	local userText
	local responseTemplate
	if boxProtectionLevel == 'extendedconfirmed' then
		userText = '拡張承認された利用者'
		responseTemplate = blurb.makeTemplateLink('EEp')
	elseif boxProtectionLevel == 'autoconfirmed' then
		userText = '自動承認された利用者'
		responseTemplate = blurb.makeTemplateLink('ESp')
	elseif boxProtectionLevel == 'interfaceadmin' then
		userText = 'インターフェース管理者'
		responseTemplate = blurb.makeTemplateLink('EIp')
	elseif boxProtectionLevel == 'sysop' then
		userText = '管理者'
	else
		userText = '利用者'
		responseTemplate = blurb.makeTemplateLink('ESp')
	end
	local descriptionText = "このテンプレートの後には、依頼する編集内容を具体的に提示してください。"
	if boxProtectionLevel == 'sysop' or boxProtectionLevel == 'templateeditor' then
		descriptionText = mw.ustring.format('ページの内容に詳しくない%sでも依頼に対処できるよう、%s', userText, descriptionText)
	end
	descriptionText = descriptionText .. "テキストを置き換える場合は「○○を××に変更してください」のように、"
		.. "除去するテキストと新たに挿入するテキストの両方を示す形で依頼を出してください。"
	local smallText = 
	if boxProtectionLevel == 'sysop' or boxProtectionLevel == 'templateeditor' then
		local templateFullText
		if boxProtectionLevel == 'sysop' then
			templateFullText = '全保護された'
		elseif boxProtectionLevel == 'templateeditor' then
			templateFullText = 'template-protected'
		end
		smallText =  templateFullText .. "ページの編集依頼は合意形成済みであるか、"
			.. "議論の余地がないような編集にのみ用いるべきです。"
			.. "そうでない場合は、このテンプレートを用いる前にノートページで議論を行ってください。"
	else
		smallText = userText .. 'であれば誰でも依頼のあった編集を行うことができます。'
	end
	if not isPlural then
		local title = titles1
		if title.namespace == 10 or title.namespace == 828 then
			local sandboxTitle = title:getSubpageTitle('sandbox')
			if sandboxTitle and sandboxTitle.exists then
				smallText = smallText .. '依頼のあったテンプレートを編集する前に、'
					.. sandboxTitle:makeViewLink(title.pagetype .. "のサンドボックス")
				local testcasesTitle = title:getSubpageTitle('testcases')
				if testcasesTitle and testcasesTitle.exists then
					smallText = smallText .. 'や' .. testcasesTitle:makeViewLink('テストケース')
				end
				smallText = smallText .. 'で試験することを心がけてください。'
			end
		end
	end
	smallText = smallText .. '依頼された編集を実施した場合、却下した場合、保留した場合はこのテンプレートに'
		.. blurb.makeParaText('answered', 'yes') .. 'もしくは' ..blurb.makeParaText('ans', 'yes') .. 'を追加してください。'
		.. "こうすることで対処済みの依頼が編集依頼用のカテゴリから除去されます。 "
		-- .. mw.ustring.format('You may also wish to use the %s template in the response.', responseTemplate)
	if hasNonInterfacePage then
		smallText = smallText
			.. 'ページの保護を依頼する場合はWikipedia:保護依頼を、'
			.. '保護の解除を依頼する場合はWikipedia:保護解除依頼を利用してください。'
	end
	return mw.ustring.format('%s\n\n%s\n', descriptionText, smallText)
end
function blurb:export()
	local intro = self:makeIntro()
	local linkLists = self:makeLinkLists()
	local body = self:makeBody()
	-- Start long links lists on a new line.
	local linkListSep = ' '
	if self.linkCount > 5 then
		linkListSep = 
	end
	return mw.ustring.format('%s%s%s\n\n%s', intro, linkListSep, linkLists, body)
end
----------------------------------------------------------------------
-- Subclass of Module:Protected edit request's box class for active boxes
----------------------------------------------------------------------
local box = {}
box.__index = box
function box.new(protectionType, args)
	-- In the inheritance system used here, an object's metatable is its class, and a class's metatable is its superclass
	local obj = getmetatable(box).new(protectionType, args)
	setmetatable(obj, box)
	local boxProtectionLevels = {semi = 'autoconfirmed', extended = 'extendedconfirmed', template = 'templateeditor', full = 'sysop', interface = 'interfaceadmin'}
	obj.boxProtectionLevel = boxProtectionLevelsprotectionType
	obj.demo = yesno(args.demo)
	-- Set dependent objects.
	obj.titles = titleTable.new(args)
	if not yesno(args.force) and obj.titles:hasSameProperty('sameProtectionStatus', function (title) return title.protectionLevel end) and obj.titles1.protectionLevel ~= 'unprotected' then
		obj.boxProtectionLevel = obj.titles1.protectionLevel
	end
	obj.blurb = blurb.new(obj.titles, obj.boxProtectionLevel)
	return obj
end
function box:setImage()
	local titles = self.titles
	local boxProtectionLevel = self.boxProtectionLevel
	local padlock
	if boxProtectionLevel == 'sysop' then
		padlock = 'Edit Protection.svg'
	elseif boxProtectionLevel == 'interfaceadmin' then
		padlock = 'Interface-protection-shackle.svg '
	elseif boxProtectionLevel == 'templateeditor' then
		padlock = 'Template-protection-shackle.svg'
	elseif boxProtectionLevel == 'autoconfirmed' then
		padlock = 'Edit Semi-protection.svg'
	elseif boxProtectionLevel == 'extendedconfirmed' then
		padlock = 'Edit Extended Semi-protection.svg'
	else
		padlock = 'Padlock-bronze-open.svg'
	end
	local stringToFormat = 'link='
	local smallPadlock = mw.ustring.format(stringToFormat, padlock, 25)
	local largePadlock = mw.ustring.format(stringToFormat, padlock, 60)
	self:setArg('smallimage', smallPadlock)
	self:setArg('image', largePadlock)
end
function box:buildUrnLinks()
	local ret = {}
	local boxProtectionLevel = self.boxProtectionLevel
	for titleObj in self.titles:titleIterator() do
		table.insert(ret, titleObj:makeUrnLink(boxProtectionLevel))
	end
	return mw.ustring.format('%s', table.concat(ret))
end
function box:setBlurbText()
	self:setArg('text', self.blurb:export() .. self:buildUrnLinks())
end
function box:exportRequestTmbox()
	self:setImage()
	self:setBlurbText()
	self:setArg('class', 'editrequest')
	self:setArg('id', title.getProtectionLevelText(self.boxProtectionLevel)) -- for anchor. yes, this leads to multiple elements with the same ID. we should probably fix this at some point
	return makeMessageBox('tmbox', self.tmboxArgs)
end
function box:exportRequestCategories()
	local cats = {}
	local boxProtectionLevel = self.boxProtectionLevel
	local function addCat(cat)
		table.insert(cats, mw.ustring.format(, cat))
	end
	local protectionCats = {
		autoconfirmed = '半保護されたページの編集依頼',
		extendedconfirmed = '拡張半保護されたページの編集依頼',
		templateeditor = 'Wikipedia template-protected edit requests',
		sysop = '保護編集依頼中のページ',
		interfaceadmin = '保護編集依頼中のページ'
	}
	addCat(protectionCatsboxProtectionLevel)
	if self.titles:hasOtherProtectionLevel(boxProtectionLevel) then
		addCat('依頼対象を間違えている可能性のある保護編集依頼')
	end
	return table.concat(cats)
end
function box:export()
	local title = self.titles.currentTitle
	if not title.isTalkPage and not self.demo and not yesno(self.args.skiptalk) then
		return 'エラー: 保護されたページの編集依頼は、ここではなくノートで出してください。'
	end
	local ret = {}
	table.insert(ret, self:exportRequestTmbox())
	if not self.demo then
		table.insert(ret, self:exportRequestCategories())
	end
	return table.concat(ret)
end
----------------------------------------------------------------------
-- Function exported to Module:Protected edit request
----------------------------------------------------------------------
return function(superclass, yn, mb)
	yesno = yn
	makeMessageBox = mb
	return setmetatable(box, superclass)
end