Jump to content

Module:Pre-Code film

From Wikisource

require('strict')

local p = {}

-- Helper function to check if a page is in a specific category
local function pageInCategory(content, category)
    return content:match('%[%[Category:' .. category .. '%]%]') ~= nil
end

function p.check()
    local mwTitle = mw.title.getCurrentTitle()
    local content = mwTitle:getContent()
    
    local isCartoon = pageInCategory(content, 'Sound cartoons')
    local hasSoundFilmCategories = isCartoon or pageInCategory(content, 'Sound film') or pageInCategory(content, 'Part%-talkies')

    if not hasSoundFilmCategories then
        return ""
    end

    local entityId = mw.wikibase.getEntityIdForCurrentPage()
    if not entityId then
        return ""
    end

    local propertyId = 'P577' -- Property ID for 'publication date'
    local releaseDate = mw.wikibase.getBestStatements(entityId, propertyId)[1]

    if releaseDate and releaseDate.mainsnak and releaseDate.mainsnak.datavalue and releaseDate.mainsnak.datavalue.value then
        local dateValue = releaseDate.mainsnak.datavalue.value.time
        -- Match the year and month in the date string
        local year, month = dateValue:match('%+(%d%d%d%d)%-(%d%d)%-')

        if year then
            local yearNum = tonumber(year)
            if yearNum and yearNum >= 1927 and yearNum <= 1933 then
                if isCartoon then
                    return "[[Category:Pre-Code cartoons]]"
                else
                    return "[[Category:Pre-Code films]]"
                end
            elseif yearNum == 1934 then
                if month then
                    local monthNum = tonumber(month)
                    if monthNum and monthNum < 7 then
                        if isCartoon then
                            return "[[Category:Pre-Code cartoons]]"
                        else
                            return "[[Category:Pre-Code films]]"
                        end
                    end
                end
            end
        end
    end

    return ""
end

return p