Jump to content

Module:Backwards copy

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Chlod (talk | contribs) at 11:19, 17 August 2022 (basic args logic). 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 yesno = require("Module:Yesno")

local p = {}

local function demoCheck(args)
	return yesno(args["demo"]) or yesno(args["nocat"])
end

local function showWarning(text, nocat)
	mw.addWarning(text)
	return nocat and "" or "[[Category:Pages with backwards copy warnings]]"
end

local function delink(text)
	if text == nil then
		return nil
	end
	mw.logObject(require("Module:Delink")._delink{text})
	return string.gsub(string.gsub(require("Module:Delink")._delink{text}, "%[%[", ""), "%]%]", "")
end

local function showError(text, nocat)
	return string.format(
		"%s%s %s",
		nocat and "" or "[[Category:Pages with backwards copy errors]] ",
		tostring(
			mw.html.create("span")
				:css("color", "red")
				:css("font-weight", "bold")
				:wikitext("Error:")
		),
		text
	)
end

local function row(args, i)
	return mw.html.create("li"):wikitext(rowText)
end

local function bannerText(args)
	local text = ""
	
	local id = args["id"] or args["revid"]
	if id ~= nil then
		text = string.format("Revisions succeeding [[%s|this version]] of this", id)
	else
		text = "This"
	end
	
	local citations = {}
	local i = 1
	while args[tostring(i)] or args["articlename" .. i] or args["title" .. i] or (i == 1 and (args["articlename"] or args["title"])) do
		if args[tostring(i)] then
			citations.insert(i)
		else
			local date = (args["date" .. i] or (i == 1 and args["date"])) or
				-- legacy way of providing dates
				string.format(
					"%s %s",
					args["monthday" .. i] or (i == 1 and args["monthday"]),
					args["year" .. i] or (i == 1 and args["year"])
				)
			
			citations.insert(frame:expandTemplate{ title = "Citation", args = {
				ref = "none",
				author = args["author" .. i] or (i == 1 and args["author"]),
				authorlist = args["authorlist" .. i] or (i == 1 and args["authorlist"]),
				["display-authors"] = args["display-authors" .. i] or (i == 1 and args["display-authors"]),
				date = date,
				url = args["url" .. i] or (i == 1 and args["url"]),
				title = (args["articlename" .. i] or (i == 1 and args["articlename"]))
					or args["title" .. i] or (i == 1 and args["title"]),
				publisher = (args["title" .. i] or (i == 1 and args["title"]))
					or args["publisher" .. i] or (i == 1 and args["publisher"])
			} })
		end
		
		i = i + 1
	end

	text = string.format(
		"%s article is substantially duplicated by a piece in an external publication. " ..
		"Please do not flag this article as a copyright violation of the following source",
		text
	)
	if citations.getn() > 0 then
		return showError(
			"No citations were provided. Provide at least one citation using <code>title</code>.", 
			demoCheck(args)
		)
	elseif citations.getn() > 1 then
		text = string.format("%ss:", text)
	else
		text = string.format("%s:", text)
	end
	
	local ul = mw.html.create("ul")
	for k, v in pairs(citations) do
		ul:node(mw.html.create("li"):wikitext(v))
	end
	
	text = text .. " " .. tostring(ul)
	
	return text
end

function p.renderBanner(args)
	return MessageBox.main('tmbox', {
		name = "backwards-copy",
		small = args["small"],
		image = '[[File:Newspaper Cover.svg|50px]]',
		text = bannerText(args)
	}) .. (demoCheck(args) and "[[Category:Wikipedia article talk pages incorporating the backwardscopy template]]" or "")
end

function p.main(frame)
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame, {
		trim = true,
		removeBlanks = true
	})
	return p.renderBanner(args)
end

return p