From 92357f040f89e172ed23f1486e73f749faa3cdd2 Mon Sep 17 00:00:00 2001 From: Tphamtranba <119475427+Bapham12@users.noreply.github.com> Date: Thu, 5 Feb 2026 18:34:28 +0700 Subject: [PATCH] Add Lua module for self-published work licenses This file implements a Lua module for handling self-published work licenses, including functions for normalizing input arguments, checking license redundancy, and determining if public domain status has expired. It processes input arguments, evaluates conditions, and generates appropriate output messages and categories based on the input. --- Other/Self.lua | 146 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 Other/Self.lua diff --git a/Other/Self.lua b/Other/Self.lua new file mode 100644 index 0000000..6f5a901 --- /dev/null +++ b/Other/Self.lua @@ -0,0 +1,146 @@ +-- .___ ___. ______ _______ __ __ __ _______ _______. _______ __ _______ +-- | \/ | / __ \ | \ | | | | | | | ____| _ / || ____|| | | ____| +-- | \ / | | | | | | .--. || | | | | | | |__ (_) | (----`| |__ | | | |__ +-- | |\/| | | | | | | | | || | | | | | | __| \ \ | __| | | | __| +-- | | | | | `--' | | '--' || `--' | | `----.| |____ _.----) | | |____ | `----.| | +-- |__| |__| \______/ |_______/ \______/ |_______||_______|(_)_______/ |_______||_______||__| + +-- Written by [[c: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 +-- * Template:SDC statement exist +-- * Module:SDC tracking + +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('