Jump to content

Module:Copied

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gonnym (talk | contribs) at 09:07, 8 February 2020 (simply and clean up code). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local MessageBox = require('Module:Message box')


local p = {}

local function singleText(args)
	local from_oldid = args["from_oldid"] or args["from_oldid1"] or ""
	local from = args["from"] or args["from1"] or ""
	local to_diff = args["to_diff"] or args["to_diff1"] or ""
	local diff = args["diff"] or args["diff1"] or ""
	local to_oldid = args["to_oldid"] or args["to_oldid1"] or ""
	local to = args["to"] or args["to1"] or ""
	local date = args["date"] or args["date1"] or ""
	local afd = args["afd"] or args["afd1"] or ""
	local merge = args["merge"] or args["merge1"] or ""
	local text = ""
	text = text .. "Text and/or other creative content from " 
	if not (from_oldid == "") then
		text = text .. "[" .. tostring(mw.uri.fullUrl(from, {oldid=from_oldid} )) .. " this version] of "
	end
	text = text .. "[[" .. from .."]]" 
	if (merge == "yes") or (not afd == "") then
		text = text .. " was merged into "
	else 
		text = text .. " was copied or moved into " 
	end 
	text = text .. "[[" 
	if (merge == "yes") and to == "" then
		text = text .. mw.title.getCurrentTitle().nsText .. ":" .. mw.title.getCurrentTitle().text 
	else 
		text = text .. to
	end 
	text = text	.. "]]"
	if not (diff == "") then
		text = text .. " with [" .. diff .. " this edit]"
	elseif not (to_oldid == "") then
		text = text .. " with [" .. tostring(mw.uri.fullUrl(to,{diff=to_diff, oldid=to_oldid} )) .. " this edit]"
	end
	if not (date == "") then
		text = text .. " on " .. date
	end
	if not (afd == "") then
		text = text .. " after being [["
		if not mw.ustring.match(afd, "Wikipedia:", 1 ) then
			text = text .. "Wikipedia:Articles for deletion/"
		end
		text = text .. afd .. "|nominated for deletion]]"
	end
	text = text .. ". The former page's " 
	text = text .. "[" .. tostring(mw.uri.fullUrl(from,{action="history"}) or "") .. " history] "
	text = text .. "now serves to [[WP:Copying within Wikipedia|provide attribution]] for that content in the latter page, and it must not be deleted so long as the latter page exists."
	return text
end

local function row(args, i)
	local text = ""
	local afd = args["afd" .. i]
	if (afd and args["merge" .. i]) then
		text = text .. "\n*Merged"
	else 
		text = text .. "\n*Copied"
	end

	local from = args["from" .. i] or ""
	text = text .. " [" .. tostring(mw.uri.fullUrl(from, {redirect = "no"} )) .. " " .. from .. "] ("

	local from_oldid = args["from_oldid" .. i]
	if (from_oldid) then
		text = text .. "[" .. tostring(mw.uri.fullUrl(from, {oldid = from_oldid} )) .. " oldid], "
	end
	
	local to = args["to".. i] or ""
	text = text .. "[" .. tostring(mw.uri.fullUrl(from, {action = "history"} )) .. " history]) → [[" .. to .. "]]"

	local diff = args["diff" .. i]
	local to_oldid = args["to_oldid" .. i]
	if (diff) then
		text = text .. "([" .. diff .. " diff])"
	elseif (to_oldid) then
		local to_diff = args["to_diff".. i] or ""
		text = text .. " ([" .. tostring(mw.uri.fullUrl(to, {diff=to_diff, oldid = to_oldid} )) .. " diff])"
	end

	local date = args["date" .. i]
	if (date) then
		text = text .. " on " .. date
	end

	if (afd) then
		text = text .. " after being [["
		if (not mw.ustring.match(afd, "Wikipedia:", 1)) then
			text = text .. "Wikipedia:Articles for deletion/"
		end
		text = text .. afd .. "|after being nominated for deletion]]"
	end
	
	return text
end

local function list(args)
	local text = ""
	local from1 = args["from1"]
	if (from1) then
		text = text .. row(args, 1)
	else
		text = text .. row(args, "")
	end
	local i = 2
	while i > 0 do
		if (args["from" .. i]) then 
			text = text .. row(args, i)
			i = i + 1
		else
			i = - 1
		end
	end
	return text
end
	
local function multiText(args)
	local text = "Text has been copied to or from this %s see the list below. The source pages now serve to [[WP:Copying within Wikipedia|provide attribution]] for the content in the destination pages and must not be deleted so long as the copies exist. For attribution and to access older versions of the copied text, please see the history links below. %s"

	local pageType
	if (mw.title.getCurrentTitle():inNamespace(0)) then
		pageType = "article"
	else
		pageType = "page"
	end
	
	local historyList = list(args)
	if (args["collapse"] == 'yes') then
		local collapsedText = '<table style="width:100%%; background: transparent;" class="collapsible collapsed">\n<tr><th>Copied pages:</th></tr>\n<tr><td> %s </td></tr></table>'
		historyList = string.format(collapsedText, historyList)
	end

	text = string.format(text, pageType, historyList)
	return text
end

local function BannerText(args)
	local text
	local from2 = (args["from2"])
	if (from2) then
		text = multiText(args)
	elseif (not from2) then
		text = singleText(args)
	end
	return text
end

local function renderBanner(args)
	return MessageBox.main('tmbox', {
		small = args["small"],
		type = 'notice',
		image = '[[File:Splitsection.svg|50px]]',
		smallimage = 'none',
		text = BannerText(args)
	})
end

local function categories(args)
	local to_oldid = args["to_oldid"] or args["to_diff"] or args["diff"]
	local from_oldid = args["from_oldid"]
	local text = "[[Category:Wikipedia pages using copied template]]" 
	if ((not from_oldid) or (not to_oldid)) then
		text = text .. [[Category:Wikipedia pages using copied template without oldid]]
	end
	return text
end

function p.main(frame)
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame)
	return renderBanner(args) .. categories(args)
end

return p