Module:Self: Difference between revisions
From Neodyland Wiki
More actions
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 = {} | ||
------------------------------------------------------------------------------- | |||
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 | 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 | end | ||
------------------------------------------------------------------------------- | |||
function p.main(frame) | |||
-- parse inputs | |||
local args = core.getArgs(frame) | |||
local | local lang = args.lang or frame:callParserFunction("int","lang") -- get user's chosen language | ||
local | 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 | local ntag = 0 | ||
local | 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 | end | ||
if | |||
-- One of the tags indicate public domain work not released by the author | |||
table.insert( | local output, cats = {}, {} | ||
table.insert( | 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 | ||
-- 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 | 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 | 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 | end | ||
-- extra message if multiple templates present ("You may select the license of your choice.") | |||
if (ntag>1) then | |||
if | 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 | end | ||
local | |||
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 | end | ||
return | results = results .. table.concat(cats) | ||
return results | |||
end | end | ||
return p | return p | ||
Revision as of 04:50, 29 January 2024
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