Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Revision as of 22:14, 6 January 2018 by commons>Tacsipacsi (add support for “lang” parameter – would be useful for testing, although license templates don’t support it)
Module documentation[ create · purge ]
This documentation is transcluded from Module:Self/doc.
local p = {}
local getArgs = require('Module:Arguments').getArgs
local frame = mw.getCurrentFrame()
local lang = mw.getLanguage( frame:preprocess( '{{int:lang}}' ) )
local licenses = {}

local function is_pd_expired()
	local pd_expired = {
		'pd-us',
		'pd-1923',
		'pd-old',
		'pd-old-70',
		'pd-old-70-1923',
		'pd-old-80',
		'pd-old-80-1923',
		'pd-old-100',
		'pd-old-100-1923',
		'pd-old-auto',
		'pd-old-auto-1923',
		'pd-anon-1923',
		'pd-anon-70',
		'pd-anon-70-EU',
		'pd-canada-anon',
		'anonymous-eu'
	}
	for _, v in ipairs(licenses) do
		for _, w in ipairs(pd_expired) do
			if string.lower(v) == w then
				return true
			end
		end
	end
	return false
end

local function license_migration_redundant(migration)
	if migration then
		return migration
	end
	for _, v in ipairs(licenses) do
		if frame:expandTemplate{ title = 'License migration is redundant', args = { v } } ~= '' then
			return 'redundant'
		end
	end
	return ''
end

local function process_license(name, attribution, migration)
	return frame:preprocess(string.format('{{%s|attribution=%s|migration=%s|lang=%s}}', name, attribution, migration, lang:getCode()))
end

local function in_language_header(style)
	local code = lang:getCode()
	local skeleton = '<div class="description %s" style="%s" lang="%s" xml:lang="%s">'
	return string.format(skeleton, code, style, code, code)
end

local function create_box(options)
	local css = 'clear:both; margin:0.5em auto; background-color:#f0f0f0; border:2px solid #e0e0e0; padding:8px; border-spacing:0; direction:'
	local box = {}
	table.insert(box, '{| class="licensetpl_wrapper" style="');
	table.insert(box, css);
	table.insert(box, lang:getDir())
	table.insert(box, ';"\n| ')
	if options.is_pd_expired then
		table.insert(box, '<div style="color:red; font-size:150%; text-align:center; font-weight:bold;">')
		table.insert(box, mw.message.new('wm-license-self-invalid-parameter'):inLanguage(lang):plain())
		table.insert(box, '</div>[[Category:Files with invalid parameter in Self template]]')
	end
	table.insert(box, in_language_header('text-align:center; font-weight:bold;'))
	table.insert(box, options.top_message)
	table.insert(box, '</div>')
	for _, v in ipairs(licenses) do
		table.insert(box, process_license(v, options.attribution, options.migration))
	end
	if options.footer then
		table.insert(box, in_language_header('text-align:center; font-style:italic;'))
		table.insert(box, options.footer)
		table.insert(box, '</div>')
	end
	table.insert(box, '\n|}')
	return table.concat(box)
end

function p.main(frame)
	local args = getArgs(frame)
	for _, v in ipairs(args) do
		table.insert(licenses, v)
	end
	if args.lang and mw.language.isSupportedLanguage(args.lang) then
		lang = mw.getLanguage(args.lang)
	end
	local options = {}
	options.is_pd_expired = is_pd_expired()
	local top_message
	if args.author then
		top_message = mw.message.new('wm-license-self-with-author', args.author, #licenses)
	else
		top_message = mw.message.new('wm-license-self', #licenses)
	end
	options.top_message = mw.getCurrentFrame():preprocess(top_message:inLanguage(lang):plain())
	options.attribution = args.attribution or args.author or ''
	options.migration = license_migration_redundant(args.migration or args.Migration)
	if #licenses > 1 then
		options.footer = mw.message.new('wm-license-self-multiple-licenses-select'):inLanguage(lang):plain()
	end
	local ret = create_box(options)
	if mw.title.getCurrentTitle().namespace == 6 then
		ret = ret .. (frame.args.category or frame:getParent().args.category or '[[Category:Self-published work]]')
	end
	return ret
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.