模組:存檔至/sandbox

local p = {}

local showError = require('Module:Error').error

function checkIsTalkPage(page)
	if page.isRedirect then
		return showError({[1]= "(錯誤:<code>".. page.prefixedText .."</code>是-{zh-hans:重定向;zh-hant:重新導向;}-頁面)" })
			.. '<span class="abusefilter-template-error"></span>[[Category:存檔目標錯誤的頁面|R]]'
	end
	if page.contentModel == 'flow-board' then
		return showError({[1]= "(錯誤:<code>".. page.prefixedText .."</code>是結構式討論頁面)" })
			.. '<span class="abusefilter-template-error"></span>[[Category:存檔目標錯誤的頁面|S]]'
	end
	if page.namespace % 2 == 1 then
		if page.namespace == 1 and page.subjectPageTitle.exists == false then
			return showError({[1]= "(錯誤:<code>".. page.prefixedText .."</code>是[[Wikipedia:快速删除方针#G15|孤立頁面]],可能是繁簡用字不同)" })
				.. '<span class="abusefilter-template-error"></span>[[Category:存檔目標錯誤的頁面|O]]' -- Orphan
		end
		return ''
	end
	if page.namespace == 4 then
		if page.isSubpage then
			if page.rootText == '格式手冊' or page.rootText == '格式手册' then
				return showError({[1]= "(錯誤:<code>".. page.prefixedText .."</code>不是應存檔的討論頁)" })
					.. '<span class="abusefilter-template-error"></span>[[Category:存檔目標錯誤的頁面|W]]'
			end
			if page.subpageText == '条目指引' or page.subpageText == '條目指引' then
				return showError({[1]= "(錯誤:<code>".. page.prefixedText .."</code>不是應存檔的討論頁)" })
					.. '<span class="abusefilter-template-error"></span>[[Category:存檔目標錯誤的頁面|W]]'
			end
		else
			return showError({[1]= "(錯誤:<code>".. page.prefixedText .."</code>不是應存檔的討論頁)" })
				.. '<span class="abusefilter-template-error"></span>[[Category:存檔目標錯誤的頁面|W]]'
		end
	else
		return showError({[1]= "(錯誤:<code>".. page.prefixedText .."</code>不是討論頁)" })
			.. '<span class="abusefilter-template-error"></span>[[Category:存檔目標錯誤的頁面|'..page.namespace..']]'
	end
	return ''
end

function p._core(title)
	local page = mw.title.new( title )
	local code = ''
	if page ~= nil then
		local errMsg = checkIsTalkPage(page)
		code = "<b>[[:".. page.prefixedText .."]]</b>"
			.. mw.getCurrentFrame():expandTemplate{title='Bot-directive-archiver', args={'archive-to', page.prefixedText}}
		if errMsg ~= '' then
			code = code .. errMsg .. mw.getCurrentFrame():expandTemplate{title='Bot-directive-archiver', args={'keep'}}
		else
		end
	else
		return nil
	end
	return code
end

function p._main(args)
	local targets = {}
	local i = 1
	while i <= 10 do
		if args[i] ~= nil then
			table.insert(targets, p._core(args[i]))
		end
		i = i + 1
	end

	local code = mw.getCurrentFrame():expandTemplate{title='Bot-directive-archiver', args={'no-archive-begin'}}
		.. '<p class="notice metadata" id="spoiler" style="font-size: small">本主題或以下段落文字,在討論結束後'
	if #targets > 0 then
		code = code .. "應<b>存檔</b>至" .. mw.text.listToText(targets, '、', '、')
	else
		code = code .. "<b>不存檔</b>"
	end
	code = code .. '。</p>' .. mw.getCurrentFrame():expandTemplate{title='Bot-directive-archiver', args={'no-archive-end'}}

	return code
end

function p.main(frame)
    return p._main( frame:getParent().args )
end

return p