Marvel Database
Advertisement

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

local p = {}
local monthData = mw.loadData( 'Module:Month/data' )
local h = require("Module:HF")
local getArgs = require('Dev:Arguments').getArgs
local language = mw.language.new('en')
local monthAliases = monthData[1]
local monthNumbers = monthData[2]


-- return full month name
function p.month(frame)
	local args = getArgs(frame)
	local name = mw.text.trim(args[1])
	local output = monthAliases[ string.lower(name) ]
	return output or name 
end


-- return month number
function p.monthNumber(frame)
    local args = getArgs(frame)
	local number = mw.text.trim(args[1])
	local output = monthNumbers[ string.lower(number) ]
	return output or number
end


--takes a date and formats it according to the syntax - https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##time
function p.lua_date_converter(s_date, s_format)
    local s = {} --1 = year, 2 = month, 3 = day
    local i = 0
    local j = 0
    local output = ''
    
    --'To comma' = 'F j, Y', "dash" = 'm-d-Y'
    s_format = s_format or 'Ymd'

    if h.isempty(s_date)
        then output = language:formatDate(s_format)
        else
            i = string.find(s_date, '-',1,true)
            j = string.find(s_date, ', ',1,true)
            if i ~= nil 
                then
                    j = string.find(s_date, '-',i+1,true)
                    s[2] = p.month({ string.sub(s_date, 1, i-1) })
                    s[3] = string.sub(s_date, i+1, j-1)
                    if #s[3] == 1 then s[3] = '0'..s[3] end
                    s[1] = string.sub(s_date, j+1, #s_date)
                    output = language:formatDate(s_format, s[2]..' '..s[3]..' '..s[1])
                elseif j ~= nil then
                    s[1] = string.sub(s_date, j+2, #s_date)
                    i = string.find(s_date, ' ',1,true)
                    s[2] = p.month({ string.sub(s_date, 1, i-1) })
                    s[3] = string.sub(s_date, i+1, j-1)
                    if #s[3] == 1 then s[3] = '0'..s[3] end
                    output = language:formatDate(s_format, s[2]..' '..s[3]..' '..s[1])
                else
                   output = language:formatDate(s_format, s_date)
            end
    end
    return output
end

--compares two dates. Return "true" if 1st date is before the 2nd date, and "false" otherwise.
function p.lua_date_comparison(date1, date2)
    date1 = p.lua_date_converter(date1, 'Ymd')
    date2 = p.lua_date_converter(date2 or 'now', 'Ymd') --If 2nd date is ommited, then current date is used for comparison.
    return date1<=date2
end


--returns era based on year
function p.getEra(frame)
    local args = getArgs(frame)
    local year = tonumber(args[1])
    local key = args[2] or ''
    local output = ''

    if not h.isempty(year)
        then
            if not h.isempty(key) then key = ' '..key end
            if year<1955 then output = 'Golden-Age'..key end
            if year>=1955 and year<1970 then output = 'Silver-Age'..key end
            if year>=1970 and year<1983 then output = 'Bronze-Age'..key end
            if year>=1983 and year<1992 then output = 'Copper-Age'..key end
            if year>=1992 then output = 'Modern-Age'..key end
    end
    
    return output
end


--returns link to date of publication category
function p.lua_link_to_publication_category(year, month)
    local output = ''

    if not h.isempty(year)
        then
            if not h.isempty(month)
                then
                    month = p.month({month})
                    output = h.lua_link_to_category(year..', '..month, month)..h.lua_link_to_category(year)
                else
                    output = h.lua_link_to_category(year)
            end
    end
    
    return output
end


-- returns information about release date
function p.lua_get_release_date_info(release_date)
    local released = true
    local recent = false
    local releaseweek
    local text = ''
    local link = ''
    local category = ''
    local output = ''
    
    if not h.isempty(release_date)
        then 
            output = p.lua_date_converter(release_date, 'F j, Y')
            released = p.lua_date_comparison(output)
            text = 'Week '..p.lua_date_converter(output, 'W')..', '..p.lua_date_converter(output, 'Y')
            link = h.lua_link_to_category(text, output)
            recent = p.lua_is_recently_released(output)
    end

    output = {
        date = output, 
        week = {
            text = text, 
            link = link
        },
        released = released, 
        recent = recent,
    }

    return output
end


-- returns information about publication date
function p.lua_get_publication_date_info(year, month, canceled)
    local published = true
    local recent = false
    local month2
    local categories = {}
    local link = ''
    local text = ''
    local sortdate = ''
    local output = ''
    
    if h.isempty(month) 
        then
            month = ''
            month2 = p.month({'1'})
        else
            month = p.month({month})
            month2 = month
    end
    
    if not h.isempty(year)
        then
            sortdate = p.lua_date_converter(month2..' '..year, 'Ymd')
            published = p.lua_date_comparison(sortdate)
            sortdate = '&nbsp;'..sortdate
            if month ~= ''
                then
                    link = year..', '..month
                    text = month..', '..year
                else
                    link = year
                    text = year
            end
            recent = p.lua_is_recently_published(year, month)
            table.insert(categories, year)
            table.insert(categories, link)
            table.insert(categories, p.getEra({year}))
            link = h.lua_link_to_category(link, text)
        elseif not h.isempty(canceled) and not canceled
            then table.insert(categories, 'Need Comic Dates')
    end
    output = {
        link = link, 
        text = text, 
        year = year, 
        month = month, 
        sortdate = sortdate,
        published = published,
        recent = recent,
    }
    
    return output, categories
end

-----------------------------------------------------------------------
--check if comics, episode, movie, etc. was recently released/published
function p.isRecent(frame)
    local args = getArgs(frame)
    local releasedate = args['releasedate']
    local year = args['year']
    local month = args['month']
    local canceled = args['canceled'] or false
    local output = false

    if not canceled and (p.lua_is_recently_published(year, month) or p.lua_is_recently_released(releasedate))
        then output = true
    end
    
    return output
end


function p.lua_is_recently_published(year, month)
    local current_date = p.lua_date_converter()
    local s
    local publication_date
    local output = false
    
    if not h.isempty(year)
        then
            if h.isempty(month)
                then month = p.month({'1'})
                else month = p.month({month})
            end
            publication_date = p.lua_date_converter(month..' 01 '..year, 'Ymd')
            s = language:formatDate('Ym', '-2 months')..'01'
            if publication_date>=s and publication_date<=current_date
                then output = true
            end
    end

    return output
end


function p.lua_is_recently_released(releasedate)
    local current_date = p.lua_date_converter()
    local s
    local publication_date
    local output = false
    
    if not h.isempty(releasedate)
        then
            releasedate = p.lua_date_converter(releasedate, 'Ymd')
            s = language:formatDate('Ymd', '-4 months')
            if releasedate>=s and releasedate<=current_date
                then output = true
            end
    end

    return output
end


return p
Advertisement