Jump to content
Search

The new Ubuntu Wiki is live. Legacy content may be unavailable and page links may have changed. Read the announcement on Discourse.

Module:Sandbox/kkuo

From Ubuntu Wiki

Documentation for this module may be created at Module:Sandbox/kkuo/doc

local p = {}
local yesno = require('Module:Yesno')

function p.main(frame)
    local args = frame:getParent().args
    if not args.version and not args.by and not args.zero and not args.cc0 and not args.sa and not args.nc and not args.nd then
        args = frame.args
    end

    -- Extract flags
    local is_zero = yesno(args.zero or args.cc0, false)
    
    -- Attribution defaults to true for all standard CC licenses unless explicitly disabled or CC0
    local by = yesno(args.by, not is_zero)
    local nc = yesno(args.nc, false)
    local sa = yesno(args.sa, false)
    local nd = yesno(args.nd, false)
    local version = args.version or "4.0"
    local jurisdiction = (args.jurisdiction or args.port or ""):lower():match("^%s*(.-)%s*$")
    local nocat = yesno(args.nocat, false)
    local attribution = (args.attribution or args.author or ""):match("^%s*(.-)%s*$")

    -- Namespace check: Category only applies in File namespace (NS 6) and non-doc pages
    local title_obj = mw.title.getCurrentTitle()
    local is_file_ns = (title_obj and title_obj.namespace == 6)
    local is_doc = (title_obj and title_obj.isSubpage and title_obj.subpageText:lower() == "doc")
    local should_cat = is_file_ns and not is_doc and not nocat

    -- Build license code, title, and icons (32px)
    local code_parts = {}
    local name_parts = {}
    local icons = {'[[File:Cc.logo.circle.svg|32px|link=]]'}

    if is_zero then
        code_parts = {"zero"}
        name_parts = {"CC0 1.0 Universal Public Domain Dedication"}
        table.insert(icons, '[[File:Cc-zero.svg|32px|link=]]')
    else
        if by then
            table.insert(code_parts, "by")
            table.insert(name_parts, "Attribution")
            table.insert(icons, '[[File:Cc-by.svg|32px|link=]]')
        end
        if nc then
            table.insert(code_parts, "nc")
            table.insert(name_parts, "NonCommercial")
            table.insert(icons, '[[File:Cc-nc.svg|32px|link=]]')
        end
        if nd then
            table.insert(code_parts, "nd")
            table.insert(name_parts, "NoDerivatives")
            table.insert(icons, '[[File:Cc-nd.svg|32px|link=]]')
        elseif sa then
            table.insert(code_parts, "sa")
            table.insert(name_parts, "ShareAlike")
            table.insert(icons, '[[File:Cc-sa.svg|32px|link=]]')
        end
    end

    local code = table.concat(code_parts, "-"):lower()
    local code_upper = table.concat(code_parts, "-"):upper()

    -- Generate License URL
    local url = ""
    if is_zero then
        url = "https://creativecommons.org/publicdomain/zero/1.0/"
    else
        local jur_path = (jurisdiction ~= "" and jurisdiction ~= "international" and jurisdiction ~= "unported") 
            and ("/" .. jurisdiction) or ""
        url = string.format("https://creativecommons.org/licenses/%s/%s%s/", code, version, jur_path)
    end

    -- Construct Readable Names
    local full_name = ""
    local short_name = ""
    if is_zero then
        full_name = "CC0 1.0 Universal"
        short_name = "CC0 1.0"
    else
        local jur_text = (jurisdiction ~= "" and jurisdiction ~= "unported") 
            and (" " .. jurisdiction:gsub("^%l", string.upper)) 
            or (version == "4.0" and " International" or " Unported")
        full_name = "Creative Commons " .. table.concat(name_parts, "-") .. " " .. version .. jur_text
        
        short_name = "CC " .. code_upper .. " " .. version
        if jurisdiction ~= "" and jurisdiction ~= "international" and jurisdiction ~= "unported" then
            short_name = short_name .. " " .. jurisdiction:upper()
        end
    end

    -- Title Parameter for License_card
    local title_text = string.format("This file is licensed under the [%s %s] license.", url, full_name)

    -- Construct Detailed Description (Permissions and Restrictions)
    local desc_parts = {}
    if is_zero then
        table.insert(desc_parts, "You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.")
    else
        table.insert(desc_parts, "You are free:")
        local freedoms = {"* '''to share''' – to copy, distribute and transmit the work"}
        if not nd then
            table.insert(freedoms, "* '''to remix''' – to adapt the work")
        end
        table.insert(desc_parts, table.concat(freedoms, "\n"))

        table.insert(desc_parts, "Under the following conditions:")
        local conditions = {}
        if by then
            table.insert(conditions, "* '''attribution''' – You must give appropriate credit, provide a link to the license, and indicate if changes were made.")
        end
        if nc then
            table.insert(conditions, "* '''noncommercial''' – You may not use the material for commercial purposes.")
        end
        if sa and not nd then
            table.insert(conditions, "* '''share alike''' – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.")
        end
        if nd then
            table.insert(conditions, "* '''no derivatives''' – If you remix, transform, or build upon the material, you may not distribute the modified material.")
        end
        table.insert(desc_parts, table.concat(conditions, "\n"))
    end

    local description_text = table.concat(desc_parts, "\n")

    -- Build Category Output
    local category_markup = ""
    if should_cat then
        category_markup = string.format("\n[[Category:Creative Commons %s %s files]]", code_upper, version)
    end

    -- Delegate rendering to Template:License_card
    return frame:expandTemplate{
        title = 'License card',
        args = {
            title = title_text,
            description = description_text .. category_markup,
            icons = table.concat(icons, " "),
            shortname = short_name,
            longname = full_name,
            link = url,
            linkreq = is_zero and "false" or "true",
            attrreq = by and "true" or "false",
            nonfree = (nc or nd) and "true" or "false",
            attribution = attribution ~= "" and attribution or nil
        }
    }
end

return p