Advertisement

Module:Report

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

-- <nowiki>
-- @authors Noreplyz

--------------------------------------------------------------------------
--[[ 
                                Module:Report

     Module for report templates. Proofreads user input.
     
     This module is invoked using Template:Report spam, 
     Template:Report vandalism, Template:Report profile,
     Template:Report filter and Template:Badwiki.

--]]
--------------------------------------------------------------------------

local Report = {}
local fallback = require('Dev:Fallback')
local switch = require('Module:Report/i18n')
local getArgs = require('Dev:Arguments').getArgs
local wikialang = require('Module:Report/wikialang')
local lang, i18n

local DEFAULT_DOMAIN = 'fandom'

local function init_lang(frame)
    lang = frame:preprocess('{{int:lang}}')
    i18n = fallback._langSwitch(switch, lang)
end

-- remove excess or wrongly entered content
local function trim(s)
    s = s:match( "^%s*(.-)%s*$" )
    return s
end

local function trimURL(s)
    s = s:match( "^%s*(.-)%s*$" )
    s = s:gsub("http:%/%/","")
    s = s:gsub("https:%/%/","")
    s = s:gsub("%/+$", "/")
    return s
end

local function trim_user(s, wikiurl)
    s = s:match( "^%s*(.-)%s*$" )
    if wikiurl then
        -- Make wiki url pattern-friendly
        wikiurl = wikiurl:gsub('%/', '%/')
        wikiurl = wikiurl:gsub('%.', '%.')
        wikiurl = wikiurl:gsub('%-', '%-')
        -- Remove wiki url from the parameters
        s = s:gsub("^.*%." .. wikiurl .. "%/", "")
        s = s:gsub("^.*%." .. wikiurl .. "%/wiki%/", "")
    end
    s = s:gsub("^.*%.wikia%.com%/wiki%/","")
    s = s:gsub("^.*%.wikia%.com%/","")
    s = s:gsub("^.*%.fandom%.com%/wiki%/","")
    s = s:gsub("^.*%.fandom%.com%/","")
    s = s:gsub("^.*%.gamepedia%.com%/wiki%/","")
    s = s:gsub("^.*%.gamepedia%.com%/","")
    s = s:gsub("^.*%.wikia%.org%/wiki%/","")
    s = s:gsub("^.*%.wikia%.org%/","")
    s = s:gsub("User:","")
    s = s:gsub("Special:Contributions%/","")
    s = s:gsub("Special:Contribs%/","")
    s = s:gsub(" ","_")
    s = s:gsub("&","%%26")
    s = s:gsub("?$","?_")
    s = s:gsub("!$","!_")
    s = s:gsub("%.$","._")
    return s
end

local function trim_reason(s)
    s = s:match( "^%s*(.-)%s*$" )
    s = s:gsub( "\\\\", "|" )
    return s
end

local function trim_id(s)
    s = s:match( "^%s*(.-)%s*$" )
    s = s:gsub("#","")
    s = s:gsub("%%23", "")
    return s
end

-- @return true if is IP, false otherwise
local function is_ip(ip)
    if ip == nil or type(ip) ~= "string" then
        return false
    end

    -- Check IPv4
    local chunks = {ip:match("(%d+)%.(%d+)%.(%d+)%.(%d+)")}
    if (#chunks == 4) then
        for _,v in pairs(chunks) do
            if (tonumber(v) < 0 or tonumber(v) > 255) then
                return false
            end
        end
        return true
    end

    -- Check IPv6
    local _, chunks = ip:gsub("%:", "")
    if chunks == 7 then
        return true
    end

    -- Otherwise
    return false
end

local function crosswikilinks(wikiurl, user)
    local cwret = {}
    cwret[#cwret + 1] = '<span style="font-size:11px;margin-left:10px;">'
    if (is_ip(user)) then
        cwret[#cwret + 1] = '([https://community.fandom.com/wiki/Special:MultiLookup?wptarget='
        cwret[#cwret + 1] = user 
        cwret[#cwret + 1] = ' ML] &bull; '
    else
        cwret[#cwret + 1] = '([https://community.fandom.com/wiki/Special:LookupContribs?target='
        cwret[#cwret + 1] = user
        cwret[#cwret + 1] = ' LC] &bull; '
    end
    cwret[#cwret + 1] = '[' .. wikiurl .. '/wiki/Special:CheckUser?user=' .. user
    cwret[#cwret + 1] = ' CU] &bull; '
    cwret[#cwret + 1] = '[https://community.fandom.com/wiki/Special:Phalanx?type=8&target=' 
    cwret[#cwret + 1] = user
    cwret[#cwret + 1] = '&wpPhalanxCheckBlocker='
    cwret[#cwret + 1] = user
    cwret[#cwret + 1] = ' Phalanx])'
    cwret[#cwret + 1] = '</span>'
    return table.concat(cwret)
end


-- Splits wiki, domain and language from a full URL
-- example: http://community.fandom.com/es/
function Report._splitWikiFromURL(url)
    url = trimURL(url) .. '/'

    -- try wikia.com parse
    local domain = 'wikia'
    local res, _, lang, wikiname = string.find(url, '([^%.]-)%.*([^%.]*)%.wikia%.com.*')
    -- try fandom.com parse
    if not res then
        domain = 'fandom'
        res, _, wikiname, lang = string.find(url, '([^%.]-)%.fandom%.com%/*([^%/]*)%/*.*')
    end
     -- try gamepedia.com parse
    if not res then
        domain = 'gamepedia'
        res, _, wikiname, lang = string.find(url, '([^%.]-)%.gamepedia%.com%/*([^%/]*)%/*.*')
    end
    -- try wikia.org parse
    if not res then
        domain = 'org'
        res, _, wikiname, lang = string.find(url, '([^%.]-)%.wikia%.org%/*([^%/]*)%/*.*')
    end
    -- revert to the default: wikia
    if not res then
        domain = DEFAULT_DOMAIN
        wikiname = url
    end
    if lang ~= '' and not wikialang[lang] then
        -- deal with situations like naruto.answers.wikia.com
        if lang ~= 'wiki' and domain == 'wikia' then
            wikiname = lang .. '.' .. wikiname
        end
        lang = ''
    end
    return {wikiname, domain, lang}
end

-- Splits wiki, domain and language from a standard entry
-- example: es.community, fandom
function Report._splitWikiFromPartial(wiki)
    local res, _, lang, wikiname = string.find(wiki, '([^%.]-)%.*([^%.]-)$')
    if not res then
        lang = ''
        wikiname = wiki
    end
    if lang ~= '' and not wikialang[lang] then
        -- deal with situations like naruto.answers.wikia.com
        if lang ~= 'wiki' then
            wikiname = lang .. '.' .. wikiname
        end
        lang = ''
    end
    return {wikiname, lang}
end

-- Returns a wiki URL based on domain and language
function Report._getWikiURL(wiki, domain, lang)
    if domain == 'fandom' then
        if lang == '' then
            return 'https://' .. wiki .. '.fandom.com'
        else
            return 'https://' .. wiki .. '.fandom.com/' .. lang
        end
    elseif domain == 'org' then
        if lang == '' then
            return 'https://' .. wiki .. '.wikia.org'
        else
            return 'https://' .. wiki .. '.wikia.org/' .. lang
        end
        elseif domain == 'gamepedia' then
        if lang == '' then
            return 'https://' .. wiki .. '.gamepedia.com'
        else
            return 'https://' .. wiki .. '-' .. lang .. '.gamepedia.com'
        end
    else
        if lang == '' then
            if string.find(wiki, '%.') then
                return 'http://' .. wiki .. '.wikia.com'
            end
            return 'https://' .. wiki .. '.wikia.com'
        else
            return 'http://' .. lang .. '.' .. wiki .. '.wikia.com'
        end
    end
end

-- Domain is one of [wikia, fandom, org]
function Report._getWikiComponents(wiki, domain)
    local lang
    wiki = wiki or 'WIKI'
    -- es.community.wikia.com or community.fandom.com/es
    if string.match(wiki, 'wikia%.com') or string.match(wiki, 'fandom%.com') or string.match(wiki, 'gamepedia%.com') or string.match(wiki, 'wikia%.org') then
        if domain then
            wiki, _, lang = unpack(Report._splitWikiFromURL(wiki))
        else
            wiki, domain, lang = unpack(Report._splitWikiFromURL(wiki))
        end
    -- es.community
    elseif domain then
        if not domain == 'wikia' and not domain == 'fandom' and not domain == 'gamepedia' and not domain == 'org' then
            domain = DEFAULT_DOMAIN
        end
        wiki, lang = unpack(Report._splitWikiFromPartial(wiki))
            
        -- deal with situations like naruto.answers.wikia.com
        local res, _, originalwiki = string.find(wiki, '%.([^%.]-)$')
        if domain ~= 'wikia' and res then
            wiki = originalwiki
        end
    else
        wiki, lang = unpack(Report._splitWikiFromPartial(wiki))
        domain = DEFAULT_DOMAIN
    end
    
    local wikiurl = Report._getWikiURL(wiki, domain, lang)
    local partial = wiki
    if lang ~= '' then
        partial = lang .. '.' .. wiki
    end
    return {wikiurl, partial, wiki, domain, lang}
end

-- creates the general format of the report
-- reportType: string of the type of user (Spammer, Vandal, Profile)
function Report._createReport(args, reportType)
    local ret = {}
    -- check total users
    local num_args = 0
    for key, user in ipairs(args) do
        num_args = num_args + 1 
    end
    
    local reason = trim_reason(args[2] or '')
    local crosswiki = args.crosswiki or ''
    local socks = args.socks or ''
    local reporter = args[num_args - 1] or ''
    local time = args[num_args] or ''
    
    local wikiurl, partial = unpack(Report._getWikiComponents(args[1], args.domain))
    
    -- start report creation
    ret[#ret + 1] = '<div class="reports">\n'
    ret[#ret + 1] = '*' .. i18n["wiki"] .. ': '
    ret[#ret + 1] = '<span class="plainlinks">' .. wikiurl .. '</span>'
    ret[#ret + 1] = '<span style="font-size:11px;margin-left:10px;">'
        ret[#ret + 1] = '([[w:c:' .. partial .. ':Special:ListAdmins|admins]])'
    ret[#ret + 1] = '</span>\n'

    if crosswiki == "yes" then
        ret[#ret + 1] = "*''" .. i18n["cwmsg"] .. "''\n"
    end
    -- Spam, Vandalism or Profile report
    if num_args >= 20 then
        ret[#ret + 1] = '<div class="reports-scrollbox">\n'
    end
    for key, user in ipairs(args) do 
        if (key >= 3 and key <= (num_args - 2)) then
            user = trim_user(user, wikiurl)
            ret[#ret + 1] = '*' .. i18n[reportType] .. ': '
            ret[#ret + 1] = '<span class="plainlinks">' .. wikiurl .. '/wiki/Special:Contributions/' .. user
            ret[#ret + 1] = '</span>'
            if (reportType == "spammer") then
                ret[#ret + 1] = ' <span style="font-size:11px;">'
                ret[#ret + 1] = '([[w:c:' .. partial .. ':Special:DeletedContributions/' .. user .. '|DelContribs]])'
                ret[#ret + 1] = '</span>'
            end
            -- add c/w links
            if (crosswiki == "yes") then
                ret[#ret + 1] = crosswikilinks(wikiurl, user)
            end
            ret[#ret + 1] = '\n'
        end
    end
    if num_args >= 20 then
        ret[#ret + 1] = '</div>\n'
    end
    if socks ~= "" then
        ret[#ret + 1] = "*''".. i18n["spmsg"] .. "''\n"
        for sock in string.gmatch(socks, '([^\n]+)') do
            sock = trim_user(sock or "", wikiurl)
            ret[#ret + 1] = '**'
            ret[#ret + 1] = i18n["sock"]
            ret[#ret + 1] = ': [[w:c:' .. partial .. ':Special:Contributions/' .. sock
            ret[#ret + 1] = '|' .. sock .. ']]'
            ret[#ret + 1] = crosswikilinks(wikiurl, sock)
            ret[#ret + 1] = '\n'
        end
    end

    ret[#ret + 1] = '*' .. i18n["reason"] .. ': '
    ret[#ret + 1] = reason ..'\n'
    ret[#ret + 1] = '*' .. i18n["sig"] .. ': '
    if string.find(reporter, "#") then
        ret[#ret + 1] = '-- https://vignette.wikia.nocookie.net/central/images/d/dd/Discord.png/revision/latest/scale-to-width-down/18#.png ' .. reporter .. ' '
    else
        ret[#ret + 1] = '-- [[Special:Contributions/'.. reporter ..'|' .. reporter .. ']] '
        ret[#ret + 1] = '([[User talk:'.. reporter .. '|' .. i18n["talk"] .. ']]) '
    end
    ret[#ret + 1] = time .. '\n'
    ret[#ret + 1] = '</div>'

    return table.concat(ret)
    
end

function Report._filterproblem(args)
    local ret = {}
    local page = trim_user(args[2] or "")
    local blockId = trim_id(args[3] or "")
    local reason = args[4] or ""
    local reporter = args[5] or ""
    local time = args[6] or ""

    local wikiurl, partial = unpack(Report._getWikiComponents(args[1], args.domain))
    
    ret[#ret + 1] = '*' .. i18n["wiki"] .. ': '
    ret[#ret + 1] = '<span class="plainlinks">' .. wikiurl
    ret[#ret + 1] = '</span>\n'

    ret[#ret + 1] = '*' .. i18n["page"] .. ': '
    ret[#ret + 1] = '<span class="plainlinks">' .. wikiurl ..'/'.. page
    ret[#ret + 1] = '</span>\n'

    ret[#ret + 1] = '*'
    ret[#ret + 1] = i18n["blockid"] 
    ret[#ret + 1] = ': <span class="plainlinks">https://community.fandom.com/wiki/Special:PhalanxStats/' 
    ret[#ret + 1] = blockId
    ret[#ret + 1] = '</span>\n'

    ret[#ret + 1] = '*' .. i18n["reason"] .. ': '
    ret[#ret + 1] = reason ..'\n'

    ret[#ret + 1] = '*' .. i18n["sig"] .. ': '
    ret[#ret + 1] = '-- [[Special:Contributions/'.. reporter ..'|' .. reporter .. ']] '
    ret[#ret + 1] = '([[User talk:'.. reporter .. '|' .. i18n["talk"] .. ']]) '
    ret[#ret + 1] = time .. '\n'

    return table.concat(ret)
end

function Report._badwiki(args)
    local reason = args[2] or 'spam'
    local wikiurl, partial = unpack(Report._getWikiComponents(args[1], args[3]))

    local ret = mw.html.create('span'):attr('class', 'plainlinks')
        :attr('data-wiki', args[1])
        :tag('span'):attr('class', 'closeme')
            :wikitext('[https://community.fandom.com/wiki/Special:WikiFactory/' .. partial .. '/close <small>close me</small>] ')
            :done()
        :wikitext('[' .. wikiurl .. ' ' .. partial .. ' Wiki] ')
        :tag('code')
            :wikitext('reason: ')
            :tag('span'):attr('class', 'QuickReason')
                :wikitext(reason)
                :done()
            :done()
        :done()

    return tostring(ret)
end

function Report.spam(frame)
    init_lang(frame)
    local args = getArgs(frame, {removeBlanks = false})
    return Report._createReport(args, "spammer")
end

function Report.vandalism(frame)
    init_lang(frame)
    local args = getArgs(frame, {removeBlanks = false})
    return Report._createReport(args, "vandal")
end

function Report.profile(frame)
    init_lang(frame)
    local args = getArgs(frame, {removeBlanks = false})
    return Report._createReport(args, "profile")
end

function Report.filter_problem(frame)
    init_lang(frame)
    local args = getArgs(frame, {removeBlanks = false})
    return Report._filterproblem(args)
end

function Report.badWiki(frame)
    local args = getArgs(frame)
    return Report._badwiki(args)
end

function Report.translate(frame)
    init_lang(frame)
    local args = getArgs(frame, {removeBlanks = false})
    return i18n[args[1]] or args[1]
end

return Report
-- </nowiki>