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.

Module:Self: Difference between revisions

From Neodyland Wiki
No edit summary
 
(23 intermediate revisions by 6 users not shown)
Line 1: Line 1:
-- .___  ___.  ______    _______  __    __  __      _______        _______. _______  __      _______
-- |  \/  |  /  __  \  |      \ |  |  |  | |  |    |  ____| _    /      ||  ____||  |    |  ____|
-- |  \  /  | |  |  |  | |  .--.  ||  |  |  | |  |    |  |__  (_)  |  (----`|  |__  |  |    |  |__ 
-- |  |\/|  | |  |  |  | |  |  |  ||  |  |  | |  |    |  __|        \  \    |  __|  |  |    |  __| 
-- |  |  |  | |  `--'  | |  '--'  ||  `--'  | |  `----.|  |____  _.----)  |  |  |____ |  `----.|  |   
-- |__|  |__|  \______/  |_______/  \______/  |_______||_______|(_)_______/    |_______||_______||__|   
                                                                                                     
-- Written by [[user:Jarekt]]
-- Original Template:Self used the following templates and modules:
-- * Template:Dir
-- * Template:Lang
-- * Template:License migration is redundant
-- * Template:License migration is redundant multiple
-- * Template:Self/is-pd-expired
require('strict') -- used for debugging purposes as it detects cases of unintended global variables
local p = {}
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 = {
local function normalize_input_args(input_args, output_args)
'pd-us',
for name, value in pairs(input_args) do  
'pd-1923',
value = mw.text.trim(value) -- trim whitespaces from the beggining and the end of the string
'pd-old',
if value ~= '' then -- nuke empty strings
'pd-old-70',
if type(name) == 'string' then  
'pd-old-70-1923',
name = string.lower(name)
'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
output_args[name] = value
end
end
end
end
return false
return output_args
end
end


local function license_migration_redundant(migration)
-------------------------------------------------------------------------------
if migration then
local function startswith(name, str)  
return migration
-- test if strings starts with "str"
end
return string.sub(name, 1, string.len(str)) == str
for _, v in ipairs(licenses) do
if frame:expandTemplate{ title = 'License migration is redundant', args = { v } } ~= '' then
return 'redundant'
end
end
return ''
end
end


local function process_license(name, attribution, migration)
-------------------------------------------------------------------------------
return frame:preprocess(string.format('{{%s|attribution=%s|migration=%s}}', name, attribution, migration))
local function license_migration_is_redundant(name)
-- Lua version of Template:License migration is redundant
name = string.lower(name)
return (startswith(name, 'cc-by-3.0')
or startswith(name, 'cc-by-sa-3.0')
or name == 'cc-by-4.0'          or name == 'cc-by-all'
or name == 'cc-by-sa-4.0'      or name == 'cc-by-sa-all'
or name == 'cc-by-sa-any'      or name == 'cc-by-sa-1.0+'
or name == 'cc-by-sa-2.0+'      or name == 'cc-by-sa-4.0,3.0,2.5,2.0,1.0')
end
end


local function in_language_header(style)
-------------------------------------------------------------------------------
local code = lang:getCode()
local function pd_is_expired(name)
local skeleton = '<div class="description %s" style="%s" lang="%s" xml:lang="%s">'
-- Lua version of [[Template:Self/is-pd-expired]]
return string.format(skeleton, code, style, code, code)
name = string.lower(name)
return (startswith(name, 'pd-us-') or startswith(name, 'pd-old') or startswith(name, 'pd-anon')
or name == 'pd-us' or name == 'pd-1923' or name == 'pd-canada-anon' or name == 'anonymous-eu')
end
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:'
function p.main(frame)
local box = {}
-- parse inputs
table.insert(box, '{| class="licensetpl_wrapper" style="');
local args, sargs = {}, {}
table.insert(box, css);
args = normalize_input_args(frame:getParent().args, args)
table.insert(box, lang:getDir())
args = normalize_input_args(frame.args, args)
table.insert(box, ';"\n| ')
local lang = args.lang or frame:callParserFunction("int", "lang") -- get user's chosen language
if options.is_pd_expired then
local dir = mw.language.new(lang):getDir()                         -- get text direction
table.insert(box, '<div style="color:red; font-size:150%; text-align:center; font-weight:bold;">')
local page = mw.title.getCurrentTitle()
table.insert(box, mw.message.new('wm-license-self-invalid-parameter'):inLanguage(lang):plain())
local namespace = page.namespace                                  -- get page namespace
table.insert(box, '</div>[[Category:Files with invalid parameter in Self template]]')
local author = args.author
-- evaluate numbered inputs
local tag = {}
local ntag = 0 -- will count numbered inputs
local pd_expired = false -- Do we have any public domain tags due to expiration
local redundant = false
for label, value in pairs(args) do -- loop through numbered variables
if type(label) == 'number' and value ~= 'Self' then
ntag = ntag + 1
tag[ntag] = value
pd_expired = pd_expired or pd_is_expired(tag[ntag])
redundant = redundant or license_migration_is_redundant(tag[ntag])
elseif type(label) == 'string' then
sargs[label] = args[label]
end
end
end
table.insert(box, in_language_header('text-align:center; font-weight:bold;'))
table.insert(box, options.top_message)
-- One of the tags indicate public domain work not released by the author
table.insert(box, '</div>')
local output, cats = {}, {}
for _, v in ipairs(licenses) do
local msg
table.insert(box, process_license(v, options.attribution, options.migration))
if pd_expired then
msg = mw.message.new('wm-license-self-invalid-parameter'):inLanguage(lang):plain()
msg = mw.ustring.format(
'<div class="error" style="text-align:center; font-weight:bold;">%s</div>',
msg
)
table.insert(output, msg)
table.insert(cats, '[[Category:Files with invalid parameter in Self template]]')
end
end
if options.footer then
 
table.insert(box, in_language_header('text-align:center; font-style:italic;'))
-- get proper header, like ("I, the copyright holder of this work, hereby publish it under the following license:")
table.insert(box, options.footer)
if ntag > 1 and author then
table.insert(box, '</div>')
msg = mw.message.new('wm-license-self-multiple-licenses-with-author', author)
elseif ntag == 1 and author then
msg = mw.message.new('wm-license-self-one-license-with-author', author)
elseif ntag > 1 then -- no author
msg = mw.message.new('wm-license-self-multiple-licenses')
else -- ntag==1 and no author
msg = mw.message.new('wm-license-self-one-license')
end
end
table.insert(box, '\n|}')
return table.concat(box)
end


function p.main(frame)
msg = msg:inLanguage(lang):plain()
local args = getArgs(frame)
local lang_fmt = '<div lang="%s" dir="%s" class="description %s" style="display:inline;">%s</div>'
for _, v in ipairs(args) do
msg = mw.ustring.format(lang_fmt, lang, dir, lang, msg)
table.insert(licenses, v)
msg = mw.ustring.format('<div class="center" style="font-weight:bold;">%s</div>', msg)
table.insert(output, msg)
-- render license templates
sargs.attribution = args.attribution or author
if not args.migration and redundant then
sargs.migration = 'redundant'
end
end
local options = {}
 
options.is_pd_expired = is_pd_expired()
for i = 1, ntag do
local top_message
table.insert(output, frame:expandTemplate{
if args.author then
title = tag[i],
top_message = mw.message.new('wm-license-self-with-author', args.author, #licenses)
args = sargs
else
})
top_message = mw.message.new('wm-license-self', #licenses)
end
end
options.top_message = mw.getCurrentFrame():preprocess(top_message:inLanguage(lang):plain())
options.attribution = args.attribution or args.author or ''
-- extra message if multiple templates present ("You may select the license of your choice.")
options.migration = license_migration_redundant(args.migration or args.Migration)
if ntag > 1 then
if #licenses > 1 then
msg = mw.message.new('wm-license-self-multiple-licenses-select'):inLanguage(lang):plain()
options.footer = mw.message.new('wm-license-self-multiple-licenses-select'):inLanguage(lang):plain()
msg = mw.ustring.format(lang_fmt, lang, dir, lang, msg)
msg = mw.ustring.format('<div class="center">%s</div>', msg)
table.insert(output, msg)
end
end
local ret = create_box(options)
if mw.title.getCurrentTitle().namespace == 6 then
-- assemble the final template and save as string
ret = ret .. (frame.args.category or frame:getParent().args.category or '[[Category:Self-published work]]')
local results = frame:preprocess(table.concat(output, '\n'))
msg = '<div style="clear:both; margin:0.5em auto; background-color:var(--background-color-interactive,#eaecf0); color:inherit; border:2px solid var(--border-color-subtle,#c8ccd1); padding:8px; direction:%s; " class="licensetpl_wrapper">%s</div>'
results = mw.ustring.format(msg, dir, results)
-- If used in files then add categories if needed
if namespace == 6 then
table.insert(cats, '[[Category:Self-published work]]')
results = results .. table.concat(cats) -- add categories only to files
end
end
return ret
 
return results
end
end


return p
return p

Latest revision as of 03:22, 10 August 2026

Module documentation[ create · purge ]
This documentation is transcluded from Module:Self/doc.
-- .___  ___.   ______    _______   __    __   __       _______        _______. _______  __       _______ 
-- |   \/   |  /  __  \  |       \ |  |  |  | |  |     |   ____| _    /       ||   ____||  |     |   ____|
-- |  \  /  | |  |  |  | |  .--.  ||  |  |  | |  |     |  |__   (_)  |   (----`|  |__   |  |     |  |__   
-- |  |\/|  | |  |  |  | |  |  |  ||  |  |  | |  |     |   __|        \   \    |   __|  |  |     |   __|  
-- |  |  |  | |  `--'  | |  '--'  ||  `--'  | |  `----.|  |____  _.----)   |   |  |____ |  `----.|  |     
-- |__|  |__|  \______/  |_______/  \______/  |_______||_______|(_)_______/    |_______||_______||__|     
                                                                                                       
-- Written by [[user:Jarekt]]
 
-- Original Template:Self used the following templates and modules:
-- * Template:Dir
-- * Template:Lang
-- * Template:License migration is redundant
-- * Template:License migration is redundant multiple
-- * Template:Self/is-pd-expired

require('strict') -- used for debugging purposes as it detects cases of unintended global variables
local p = {}

-------------------------------------------------------------------------------
local function normalize_input_args(input_args, output_args)
	for name, value in pairs(input_args) do 
		value = mw.text.trim(value) -- trim whitespaces from the beggining and the end of the string
		if value ~= '' then -- nuke empty strings
			if type(name) == 'string' then 
				name = string.lower(name)
			end
			output_args[name] = value
		end
	end
	return output_args
end

-------------------------------------------------------------------------------
local function startswith(name, str) 
	-- test if strings starts with "str"
	return string.sub(name, 1, string.len(str)) == str
end

-------------------------------------------------------------------------------
local function license_migration_is_redundant(name)
	-- Lua version of Template:License migration is redundant
	name = string.lower(name)
	return (startswith(name, 'cc-by-3.0') 
		 or startswith(name, 'cc-by-sa-3.0') 
		 or name == 'cc-by-4.0'          or name == 'cc-by-all'
		 or name == 'cc-by-sa-4.0'       or name == 'cc-by-sa-all'
		 or name == 'cc-by-sa-any'       or name == 'cc-by-sa-1.0+'
		 or name == 'cc-by-sa-2.0+'      or name == 'cc-by-sa-4.0,3.0,2.5,2.0,1.0')
end

-------------------------------------------------------------------------------
local function pd_is_expired(name)
	-- Lua version of [[Template:Self/is-pd-expired]]
	name = string.lower(name)
	return (startswith(name, 'pd-us-') or startswith(name, 'pd-old') or startswith(name, 'pd-anon') 
		or name == 'pd-us' or name == 'pd-1923' or name == 'pd-canada-anon' or name == 'anonymous-eu')
end

-------------------------------------------------------------------------------
function p.main(frame)
	-- parse inputs
	local args, sargs = {}, {}
	args = normalize_input_args(frame:getParent().args, args)
	args = normalize_input_args(frame.args, args)
	local lang = args.lang or frame:callParserFunction("int", "lang")  -- get user's chosen language
	local dir = mw.language.new(lang):getDir()                         -- get text direction
	local page = mw.title.getCurrentTitle()
	local namespace = page.namespace                                  -- get page namespace
	local author = args.author
	
	-- evaluate numbered inputs
	local tag = {}
	local ntag = 0 -- will count numbered inputs
	local pd_expired = false -- Do we have any public domain tags due to expiration 
	local redundant = false
	for label, value in pairs(args) do -- loop through numbered variables
		if type(label) == 'number' and value ~= 'Self' then 
			ntag = ntag + 1
			tag[ntag] = value
			pd_expired = pd_expired or pd_is_expired(tag[ntag])
			redundant = redundant or license_migration_is_redundant(tag[ntag])
		elseif type(label) == 'string' then 
			sargs[label] = args[label]
		end
	end
	
	-- One of the tags indicate public domain work not released by the author
	local output, cats = {}, {}
	local msg
	if pd_expired then
		msg = mw.message.new('wm-license-self-invalid-parameter'):inLanguage(lang):plain()
		msg = mw.ustring.format(
			'<div class="error" style="text-align:center; font-weight:bold;">%s</div>',
			msg
		)
		table.insert(output, msg)
		table.insert(cats, '[[Category:Files with invalid parameter in Self template]]')
	end

	-- get proper header, like ("I, the copyright holder of this work, hereby publish it under the following license:")
	if ntag > 1 and author then
		msg = mw.message.new('wm-license-self-multiple-licenses-with-author', author)
	elseif ntag == 1 and author then
		msg = mw.message.new('wm-license-self-one-license-with-author', author)
	elseif ntag > 1 then -- no author
		msg = mw.message.new('wm-license-self-multiple-licenses')
	else -- ntag==1 and no author
		msg = mw.message.new('wm-license-self-one-license')
	end

	msg = msg:inLanguage(lang):plain()
	local lang_fmt = '<div lang="%s" dir="%s" class="description %s" style="display:inline;">%s</div>'
	msg = mw.ustring.format(lang_fmt, lang, dir, lang, msg)
	msg = mw.ustring.format('<div class="center" style="font-weight:bold;">%s</div>', msg)
	table.insert(output, msg)
	
	-- render license templates
	sargs.attribution = args.attribution or author
	if not args.migration and redundant then
		sargs.migration = 'redundant'
	end

	for i = 1, ntag do 
		table.insert(output, frame:expandTemplate{
			title = tag[i],
			args = sargs
		})
	end
	
	-- extra message if multiple templates present ("You may select the license of your choice.")
	if ntag > 1 then
		msg = mw.message.new('wm-license-self-multiple-licenses-select'):inLanguage(lang):plain()
		msg = mw.ustring.format(lang_fmt, lang, dir, lang, msg)
		msg = mw.ustring.format('<div class="center">%s</div>', msg)
		table.insert(output, msg)
	end
	
	-- assemble the final template and save as string
	local results = frame:preprocess(table.concat(output, '\n'))
	msg = '<div style="clear:both; margin:0.5em auto; background-color:var(--background-color-interactive,#eaecf0); color:inherit; border:2px solid var(--border-color-subtle,#c8ccd1); padding:8px; direction:%s; " class="licensetpl_wrapper">%s</div>'
	results = mw.ustring.format(msg, dir, results)
	
	-- If used in files then add categories if needed
	if namespace == 6 then
		table.insert(cats, '[[Category:Self-published work]]')
		results = results .. table.concat(cats) -- add categories only to files
	end

	return results
end

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