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
add support for “lang” parameter – would be useful for testing, although license templates don’t support it
Did not know there was already a version written, so I rewrote it from scratch
Line 1: Line 1:
-- .___  ___.  ______    _______  __    __  __      _______        _______. _______  __      _______
-- |  \/  |  /  __  \  |      \ |  |  |  | |  |    |  ____| _    /      ||  ____||  |    |  ____|
-- |  \  /  | |  |  |  | |  .--.  ||  |  |  | |  |    |  |__  (_)  |  (----`|  |__  |  |    |  |__ 
-- |  |\/|  | |  |  |  | |  |  |  ||  |  |  | |  |    |  __|        \  \    |  __|  |  |    |  __| 
-- |  |  |  | |  `--'  | |  '--'  ||  `--'  | |  `----.|  |____  _.----)  |  |  |____ |  `----.|  |   
-- |__|  |__|  \______/  |_______/  \______/  |_______||_______|(_)_______/    |_______||_______||__|   
                                                                                                     
-- Written by [[user:jarekt]]
require('strict') -- used for debugging purposes as it detects cases of unintended global variables
local core    = require('Module:Core')
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 = {
function p.license_migration_is_redundant(name)
'pd-us',
--  
'pd-1923',
return (string.gmatch( name, '^cc-by-3.0' )
'pd-old',
or string.gmatch( name, '^cc-by-sa-3.0')
'pd-old-70',
or name=='cc-by-4.0' or name=='cc-by-all'
'pd-old-70-1923',
or name=='cc-by-sa-4.0' or name=='cc-by-sa-all'
'pd-old-80',
or name=='cc-by-sa-any' or name=='cc-by-sa-1.0+'
'pd-old-80-1923',
or name=='cc-by-sa-2.0+' or name=='cc-by-sa-4.0,3.0,2.5,2.0,1.0')
'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
end


local function license_migration_redundant(migration)
-------------------------------------------------------------------------------
if migration then
function p.pd_is_expired(name)
return migration
-- Lua version of [[Template:Self/is-pd-expired]]
end
return (string.gmatch( name, '^pd-us') or string.gmatch( name, '^pd-old') or string.gmatch( name, '^pd-anon')  
for _, v in ipairs(licenses) do
or name=='pd-1923' or name=='pd-canada-anon')
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|lang=%s}}', name, attribution, migration, lang:getCode()))
function p.main(frame)
end
    -- parse inputs
 
local args = core.getArgs(frame)
local function in_language_header(style)
local lang = args.lang or frame:callParserFunction("int","lang")  -- get user's chosen language
local code = lang:getCode()
local dir  = mw.language.new( lang ):getDir()                     -- get text direction
local skeleton = '<div class="description %s" style="%s" lang="%s" xml:lang="%s">'
local namespace = page.namespace                                  -- get page namespace
return string.format(skeleton, code, style, code, code)
local author = args.author
end
 
-- evaluate numbered inputs
local function create_box(options)
local tag = {}
local css = 'clear:both; margin:0.5em auto; background-color:#f0f0f0; border:2px solid #e0e0e0; padding:8px; border-spacing:0; direction:'
local ntag = 0
local box = {}
local pd_expired = false -- Do we have any public domain tags due to expiration
table.insert(box, '{| class="licensetpl_wrapper" style="');
local redundant  = false
table.insert(box, css);
for i=1,10 do
table.insert(box, lang:getDir())
if (frame.args[i] and frame.args[i]~='self') then
table.insert(box, ';"\n| ')
ntag = ntag+1
if options.is_pd_expired then
tag[ntag] = string.lower(frame.args[i])
table.insert(box, '<div style="color:red; font-size:150%; text-align:center; font-weight:bold;">')
pd_expired = pd_expired or p.pd_is_expired(tag[ntag])
table.insert(box, mw.message.new('wm-license-self-invalid-parameter'):inLanguage(lang):plain())
redundant  = redundant  or p.license_migration_is_redundant(tag[ntag])
table.insert(box, '</div>[[Category:Files with invalid parameter in Self template]]')
end
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
end
if options.footer then
table.insert(box, in_language_header('text-align:center; font-style:italic;'))
-- One of the tags indicate public domain work not released by the author
table.insert(box, options.footer)
local output, cats = {}, {}
table.insert(box, '</div>')
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
end
table.insert(box, '\n|}')
return table.concat(box)
end


function p.main(frame)
-- get proper header, like ("I, the copyright holder of this work, hereby publish it under the following license:")
local args = getArgs(frame)
if (ntag>1 and author) then
for _, v in ipairs(args) do
msg = mw.message.new( 'wm-license-self-multiple-licenses-with-author', author)
table.insert(licenses, v)
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
if args.lang and mw.language.isSupportedLanguage(args.lang) then
msg = msg:inLanguage(lang):plain()
lang = mw.getLanguage(args.lang)
msg = mw.ustring.format('<div lang="%s" dir="%s" class="description %s" style="display:inline;">%s</div>', 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
local sargs = {}
sargs.attribution = args.attribution or author
if not args.migration and redundant then
sargs.migration='redundant'
end
end
local options = {}
for i=1,ntag do
options.is_pd_expired = is_pd_expired()
if mw.title.new(tag[i]).exists then
local top_message
table.insert(output, frame:expandTemplate{ title = tag[i], args=sargs } )
if args.author then
end
top_message = mw.message.new('wm-license-self-with-author', args.author, #licenses)
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('<div lang="%s" dir="%s" class="description %s" style="display:inline;">%s</div>', 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
local result = table.concat(output)
ret = ret .. (frame.args.category or frame:getParent().args.category or '[[Category:Self-published work]]')
msg = '<div style="clear:both; margin:0.5em auto; background-color:#eee; border:2px solid #ddd; padding:8px; direction:%s; " class="licensetpl_wrapper">%s</div>'
result = mw.ustring.format(msg, dir, result)
if namespace==6 then
table.insert(cats, '[[Category:Self-published work]]')
local entity = mw.wikibase.getEntity()
-- check if SDC statement exist
if not(entity and entity.statements and entity.statements['P275']) then
table.insert(cats, '[[Category:Self-published work missing SDC copyright license]]')
end
end
end
return ret
results = results .. table.concat(cats)
return results
end
end


return p
return p

Revision as of 04:50, 29 January 2024

Module documentation[ create · purge ]
This documentation is transcluded from Module:Self/doc.
-- .___  ___.   ______    _______   __    __   __       _______        _______. _______  __       _______ 
-- |   \/   |  /  __  \  |       \ |  |  |  | |  |     |   ____| _    /       ||   ____||  |     |   ____|
-- |  \  /  | |  |  |  | |  .--.  ||  |  |  | |  |     |  |__   (_)  |   (----`|  |__   |  |     |  |__   
-- |  |\/|  | |  |  |  | |  |  |  ||  |  |  | |  |     |   __|        \   \    |   __|  |  |     |   __|  
-- |  |  |  | |  `--'  | |  '--'  ||  `--'  | |  `----.|  |____  _.----)   |   |  |____ |  `----.|  |     
-- |__|  |__|  \______/  |_______/  \______/  |_______||_______|(_)_______/    |_______||_______||__|     
                                                                                                       
 -- Written by [[user:jarekt]]
 
require('strict') -- used for debugging purposes as it detects cases of unintended global variables
local core    = require('Module:Core')
local p = {}

-------------------------------------------------------------------------------
function p.license_migration_is_redundant(name)
	-- 
	return (string.gmatch( name, '^cc-by-3.0' ) 
		 or string.gmatch( 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

-------------------------------------------------------------------------------
function p.pd_is_expired(name)
	-- Lua version of [[Template:Self/is-pd-expired]]
	return (string.gmatch( name, '^pd-us') or string.gmatch( name, '^pd-old') or string.gmatch( name, '^pd-anon') 
		or name=='pd-1923' or name=='pd-canada-anon')
end

-------------------------------------------------------------------------------
function p.main(frame)
    -- parse inputs
	local args = core.getArgs(frame)
	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 namespace = page.namespace                                  -- get page namespace
	local author = args.author
	
	-- evaluate numbered inputs
	local tag = {}
	local ntag = 0
	local pd_expired = false -- Do we have any public domain tags due to expiration 
	local redundant  = false
	for i=1,10 do 
		if (frame.args[i] and frame.args[i]~='self') then
			ntag = ntag+1
			tag[ntag] = string.lower(frame.args[i])
			pd_expired = pd_expired or p.pd_is_expired(tag[ntag])
			redundant  = redundant  or p.license_migration_is_redundant(tag[ntag])
		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()
	msg = mw.ustring.format('<div lang="%s" dir="%s" class="description %s" style="display:inline;">%s</div>', 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
	local sargs = {}
	sargs.attribution = args.attribution or author
	if not args.migration and redundant then
		sargs.migration='redundant'
	end
	for i=1,ntag do 
		if mw.title.new(tag[i]).exists then
			table.insert(output, frame:expandTemplate{ title = tag[i], args=sargs } )
		end
	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('<div lang="%s" dir="%s" class="description %s" style="display:inline;">%s</div>', lang, dir, lang, msg)
		msg = mw.ustring.format('<div class="center">%s</div>', msg)
		table.insert(output, msg)
	end
	
	local result = table.concat(output)
	msg = '<div style="clear:both; margin:0.5em auto; background-color:#eee; border:2px solid #ddd; padding:8px; direction:%s; " class="licensetpl_wrapper">%s</div>'
	result = mw.ustring.format(msg, dir, result)
	
	if namespace==6 then
		table.insert(cats, '[[Category:Self-published work]]')
		local entity = mw.wikibase.getEntity()
		-- check if SDC statement exist
		if not(entity and entity.statements and entity.statements['P275']) then
			table.insert(cats, '[[Category:Self-published work missing SDC copyright license]]')
		end
	end
	results = results .. table.concat(cats)
	return results
end

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