My Little Pony Friendship is Magic Wiki
(added ability to retrieve media type description)
(converting ismisc to checkmisc to be less potentially confusing)
Line 27: Line 27:
 
-- to look up and return the title. No other parameters are used.
 
-- to look up and return the title. No other parameters are used.
 
-- Note: titles with no chronology will return 'N/A'. (See Module:Nameconvert/list for indices.)
 
-- Note: titles with no chronology will return 'N/A'. (See Module:Nameconvert/list for indices.)
--If named parameter "ismisc" is non-blank, return indicator of whether the given title is non-episode media.
+
--If named parameter "checkmisc" is non-blank, return indicator of whether the given title is non-episode media.
 
-- In this case, if two is defined, the false value will be 0, otherwise blank. True value is 1.
 
-- In this case, if two is defined, the false value will be 0, otherwise blank. True value is 1.
 
-- This is evaluated before any other lookup.
 
-- This is evaluated before any other lookup.
Line 41: Line 41:
 
local lastEpisode = data.lastEpisode
 
local lastEpisode = data.lastEpisode
 
local lastSeason = data.lastEpisodeSeason
 
local lastSeason = data.lastEpisodeSeason
  +
  +
if args.checkmisc then
  +
args.ismisc = args.checkmisc
  +
end
 
 
 
if not one then
 
if not one then

Revision as of 14:39, 24 June 2020

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

--Please see [[Template:Nameconvert]] for documentation. Do not use #invoke directly.
--Data for this module is located at [[Module:Nameconvert/list]].
--Run =p.testCases() in the console below to check that changes are working correctly.
--Original template and Lua conversion by Bobogoobo.
local p = {}
local eps = require('Module:Eps').call
local data = mw.loadData('Module:Nameconvert/list')

local function notBlankArg(arg)
    return mw.text.trim(arg or '') ~= ''
end

function p.main(frame)
    return p.call(frame:getParent().args)
end

function p.call(args)
    --If two is defined, then one is season number and two is episode number in season (get title).
    --If one is a number, it is the episode's overall number (get title).
    --Else, one is the episode name or other media name or alias (other media: get title, episode: get number).
    --    Note: if the title is other media and r=oa, then the media's index in its specified sequence
    --    (see Module:Nameconvert/list) will be returned. Indices start at 1.
    --pad can be: 0, 1, 2 (only applies to episodes).
    --r can be: s, sn, e, en, oa (all except oa only apply to episodes).
    --If named parameter "PonyLife" is non-blank, the above are the same but it's a Pony Life episode.
    --If named parameter "getmisc" is non-blank, the first parameter is treated as a non-episode index
    --    to look up and return the title. No other parameters are used.
    --    Note: titles with no chronology will return 'N/A'. (See Module:Nameconvert/list for indices.)
    --If named parameter "checkmisc" is non-blank, return indicator of whether the given title is non-episode media.
    --    In this case, if two is defined, the false value will be 0, otherwise blank. True value is 1.
    --    This is evaluated before any other lookup.
    --If named parameter "getdesc" is non-blank, returns the media type for the given title
    --    (as defined in Module:Nameconvert/list), e.g. Film, Shorts, Special, or Episode.
    local one = args[1]
    local two = args[2]
    local pad = tonumber(args.pad)
    local r = mw.ustring.lower(args.r or '')
    local ponyLife = false
    local ponyLifeText = ''
    local episodesKey = 'episodes'
    local lastEpisode = data.lastEpisode
    local lastSeason = data.lastEpisodeSeason
    
    if args.checkmisc then
        args.ismisc = args.checkmisc
    end
    
    if not one then
        return '<span class="error">first parameter required</span>'
    end
    if not pad or pad > 2 then
        pad = 2
    end
    if r == '' then
        r = 'all'
    end
    if notBlankArg(args.PonyLife) then
        ponyLife = true
        ponyLifeText = 'true'
        episodesKey = 'ponylife'
        lastEpisode = data.lastEpisodePonyLife
        lastSeason = data.lastEpisodeSeasonPonyLife
    end
    
    if notBlankArg(args.ismisc) then
        local name = mw.text.decode(one, true)
        if data.lookup.aliases[mw.ustring.lower(name)] or data.aliases[name] then
            return '1'
        else
            if two then
                return '0'
            else
                return ''
            end
        end
    end
    
    if notBlankArg(args.getmisc) then
        local getMisc = tonumber(one)
        
        if not getMisc then
            return '<span class="error">invalid value for getmisc</span>'
        end
        if getMisc % 100 == 0 then
            return 'N/A'
        end
        
        local result = data.lookup.aliasindex[getMisc]
        if result then
            return result
        elseif data.lookup.aliasindex[getMisc - 1] then
            return 'N/A'
        else
            return '<span class="error">invalid non-episode index</span>'
        end
    end
    
    if notBlankArg(args.getdesc) then
        local getDesc = data.aliases[one]
        if getDesc then
            return getDesc[1][2]
        else
            if data.lookup.episodes[one] or data.lookup.ponylife[one] then
                return 'Episode'
            else
                return '<span class="error">invalid title for getdesc</span>'
            end
        end
    end
    
    if two and two ~= '' then
        local season = tonumber(one)
        local episode = tonumber(two)
        if not episode or episode < 1 or episode > tonumber(eps({season, PonyLife=ponyLifeText})) then
            return '<span class="error">invalid episode number</span>'
        else
            if r == 'oa' then
                return eps({season - 1, 'total', PonyLife=ponyLifeText}) + episode
            else
                if not season or season < 1 or season > lastSeason then
                    return '<span class="error">invalid season</span>'
                else
                    return data[episodesKey][eps({season - 1, 'total', PonyLife=ponyLifeText}) + episode]
                end
            end
        end
    else
        if not tonumber(one) then
            local name = mw.text.decode(one, true)-- equivalent of #titleparts trick
            local overall
            local isMisc = false
            local clipshow = false
            local csOffset = 0
            --Add exceptions for any further clip shows, etc here
            if (overall and not ponyLife and overall > 222 and overall < 229) then
                clipshow = true
                csOffset = 222
            end
            
            if data.lookup.aliases[mw.ustring.lower(name)] then
                name = data.lookup.aliases[mw.ustring.lower(name)]
                isMisc = true
            elseif data.aliases[name] then
                isMisc = true
            else
                overall = data.lookup[episodesKey][name]
                if not overall then
                    return '<span class="error">invalid name</span>'
                end
            end
            
            if isMisc then
                if r == 'oa' then
                    return tostring(data.aliases[name][1][1])
                else
                    return name
                end
            end
            
            if r == 'oa' then
                return tostring(overall)
            else
                local snum = eps({gs=overall, PonyLife=ponyLifeText})
                local enum = overall - eps({snum - 1, 'total', PonyLife=ponyLifeText})
                if clipshow then enum = overall - csOffset end
                
                if pad == 2 then
                    snum = mw.ustring.format('%02d', snum)
                end
                if pad == 1 or pad == 2 then
                    enum = mw.ustring.format('%02d', enum)
                end
                if clipshow then enum = 'CS' .. enum end
                if r == 'all' then
                    return 'S' .. snum .. 'E' ..enum
                elseif r == 's' then
                    return 'S' .. snum
                elseif r == 'sn' then
                    return snum
                elseif r == 'e' then
                    return 'E' .. enum
                elseif r == 'en' then
                    return enum
                else
                    return '<span class="error">invalid return format</span>'
                end
            end
        else
            local overall = tonumber(one)
            if data[episodesKey][overall] then
                return data[episodesKey][overall]
            elseif overall == 0 or overall == lastEpisode + 1 then
                return 'N/A'
            else
                return '<span class="error">invalid episode number</span>'
            end
        end
    end
end

function p.testCases()
    --Add tests here: {args}, 'expected result',
    local tests = {
        {'1', '2'}, 'Friendship is Magic, part 2',
        {'2', '26'}, 'A Canterlot Wedding - Part 2',
        {'Baby Cakes'}, 'S02E13',
        {'Look Before You Sleep', pad='0'}, 'S1E8',
        {'Magic Duel', r='e'}, 'E05',
        {'Magic Duel', r='oa'}, '57',
        {p.call({'1', '25'})}, 'S01E25',
        {p.call({'46'})}, 'S02E20',
        {'127'}, 'Applejack\'s "Day" Off',
        {'1', '3', PonyLife='x'}, 'Cute-pocalypse Meow',
        {'4', PonyLife='x'}, 'How Applejack Got Her Hat Back',
        {'Princess Probz', pad='0', PonyLife='x'}, 'S1E1',
        {p.call({'2', PonyLife='x'}), PonyLife='x'}, 'S01E02',
        {'My Little Pony Equestria Girls', ismisc='x'}, '1',
        {'Friendship is Magic, part 1', ismisc='x'}, '',
        {'Friendship is Magic, part 1', 'x', ismisc='x'}, '0',
        {'mlptm'}, 'My Little Pony The Movie',
        {p.call({'ssshorts'}), r='oa'}, '10',
        {'My Little Pony The Movie', r='oa'}, '0',
        {'0', getmisc='x'}, 'N/A',
        {'17', getmisc='x'}, 'My Little Pony Equestria Girls: Spring Breakdown',
        {'101', getmisc='x'}, 'My Little Pony Best Gift Ever',
        {'My Little Pony Equestria Girls', getdesc='x'}, 'Film',
        {'My Little Pony Equestria Girls: Summertime Shorts', getdesc='x'}, 'Shorts',
        {'Triple Threat', getdesc='x'}, 'Episode',
    }
    local passedTests = 0
    
    for test = 1, #tests do
        if (test + 1) % 2 == 0 then
            local result = p.call(tests[test])
            local expect = tests[test+1]
            if result ~= expect then
                return 'Test #' .. (test + 1) / 2 .. ' failed!' ..
                    '\n    Received: ' .. result ..
                    '\n    Expected: ' .. expect ..
                    '\nFurther tests skipped.'
            else
                passedTests = passedTests + 1
            end
        end
    end
    
    return passedTests .. ' tests passed.'
end

return p