FANDOM


local p = {}
local getArgs = require('Dev:Arguments').getArgs
 
-- returns content of the page
function p.lua_getContent(pagename)
    local output = ''
 
    if not p.isempty(pagename)
        then output = mw.title.new(p.lua_breaklink(pagename,1)):getContent() or ''
    end
 
    return output
end
 
 
-- returns value of 'field' from 'page_content'
function p.lua_getFieldValue(page_content, field)
    local output = ''
 
    if not p.isempty(page_content) and not p.isempty(field)
        then 
            output = string.match(page_content, '|%s-'..field..'%s-=%s-(.-)\n')
            if not p.isempty(output)
                then output = p.trim(output)
            end
    end
 
    return output
end
 
 
-- returns namespace of the page
function p.lua_getNamespace(pagename)
    local title = mw.title.new(pagename)
    local output = ''
 
    if not p.isempty(pagename)
        then
            if title.namespace == 0
                then 
                    if title.exists
                        then return "Main"
                    end
                else return title.nsText
            end
            if title.exists 
                then return "Main"
                elseif p.lua_isexists('File:'..pagename)
                    then return "File"
                    elseif p.lua_isexists('Category:'..pagename)
                        then return "Category"
                        else return ''
            end
    end
 
    return output
end
 
 
-- returns type of page based on the name of its main template (workd only for templates in 'Marvel Database' namespace.
function p.lua_pagetype(pagename)
    local namespace = p.lua_getNamespace(pagename)
    local content = ''
    local output = namespace
 
    if namespace == 'Main'
        then
            content = p.lua_getContent(pagename)
            if not p.isempty(content)
                then
                    output = string.match(content, '{{Marvel Database:(.-) Template\n') or 'Unknown'
            end
    end
 
    return output
end
 
 
-- returns 'true' if page is a disambiguation
function p.lua_isDisambiguation(pagename)
    local content = ''
    local i
    local j
    local k
    local output = false
    local n = 0
 
    if p.lua_isexists(pagename)
        then
            content = p.lua_getContent(pagename)
            i = string.find(content, 'DisambiguationFull')
            j = string.find(content, 'Disambiguation')
            k = string.find(content, 'Disambig')
            if i ~= nil
                then 
                    output = true
                    n = 1
                elseif j ~= nil
                    then output = true
                        n = 2
                    elseif k ~= nil
                        then 
                            output = true
                            n = 3
            end
    end
 
    return output, n
end
 
 
-- returns main page of a disambiguation
function p.lua_getDisambiguationMain(pagename)
    local content = ''
    local i
    local j
    local output = ''
 
    i, j = p.lua_isDisambiguation(pagename)
 
    if i
        then
            content = p.lua_getContent(pagename)
            if j == 1 -- Template:DisambiguationFull
                then output = p.lua_getFieldValue(content, 'main')
                elseif j == 2 -- Template:Disambiguation
                    then output = p.lua_getFieldValue(content, 'Main Character')
                    elseif j == 3 -- Template:Disambig
                        then output = p.lua_getFieldValue(content, 'MainPage')
            end
            output = p.lua_breaklink(output, 1)
    end
 
    return output
end
 
 
-- Check if 'text' is part of the 'list' or not
function p.inlist(list, text)
    local output = ''
 
    if p.isempty(list) == true or p.isempty(text) == true
        then output = false 
        else
            for i, v in ipairs( list ) do
                if v == text
                    then 
                        output = true 
                        break
                    else output = false 
                end
            end
    end
 
    return output
end
 
 
--checks if 'page' exists or not
function p.lua_isexists(page)
    if not p.isempty(page) and mw.title.new(page).exists
        then return true
        else return false
    end
end
 
 
--returns true if text is a wikilink or false otherwise
function p.lua_isLink(link)
    local i
    local j
    local output = false
 
    if not p.isempty(link)
        then
            i = string.find(link, "[[",1,true)
            j = string.find(link, "]]",1,true)
            if i ~= nil and j ~= nil
                then output = true
            end
    end
 
    return output
end
 
 
--Check if 'link' is a wikilink. If yes, then check if it has | inside. If yes, then return 'part' part of it. If 'link' is not a wikilink return 'link'.
function p.lua_breaklink (link, part)
    local i
    local j
    local k
    local output = ''
 
    if not p.isempty(link)
        then
            if p.lua_isLink(link)
                then
                    i = string.find(link, "[[",1,true)
                    j = string.find(link, "|",1,true)
                    k = string.find(link, "]]",1,true)
                    if j == nil 
                        then output = string.sub(link, i+2, k-1) 
                        elseif part == 2 
                            then output = string.sub(link, j+1, k-1)
                            else output = string.sub(link, i+2, j-1)
                    end
                else output = link
            end
    end
 
    return output
end
function p.breaklink (frame)
    return p.lua_breaklink(frame.args[1], tonumber(frame.args[2]))
end
 
 
-- return number of pages in the "category", "pagetype" can be one of '*', 'all', 'pages', 'subcats', or 'files' 
function p.lua_PagesInCategory(category, pagetype)
    local output = 0
    pagetype = pagetype or 'pages'
 
    if not p.isempty(category)
        then
            category = string.gsub(category,''',"'")
            output = mw.site.stats.pagesInCategory( category, pagetype )
    end
 
    return output
end
 
 
 
--Check if "page" is redirect or not
function p.lua_isRedirect(page)
    local output = false
 
    if not p.isempty(page)
        then output = mw.title.new(page).isRedirect
    end
 
    return output
end
 
 
--return target of redirect
function p.lua_redirectTarget(page)
    local content = ''
    local output = ''
 
    if not p.isempty(page) and p.lua_isRedirect(page)
        then 
            content = p.lua_getContent(page)
            output = mw.ustring.gsub(content, "^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)%]%]", "%1") or
                mw.ustring.gsub(content,"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)|[^%[%]]-%]%]", "%1")
    end
 
    return output
end
 
 
 
function p.explode( sep, text )
  local sep, fields = sep or "::", {}
  local pattern = string.format("([^%s]+)", sep)
  text:gsub(pattern, function(c) fields[#fields+1] = c end)
  return fields
end
 
function p.trim(s)
  if type(s) == "string" then
    return (s:gsub("^%s*(.-)%s*$", "%1"))
  else
    return false
  end
end
 
function p.isempty(s)
  local result = false
  if type(s) == "nil" then
    result = true
  elseif type(s) == "string" then
    if s == "" then
      result = true
    end
  elseif type(s) == "table" then
    if next(s) == nil then
      result = true
    end
  end
  return result
end
 
function p.print_r( t, name, indent )
  local cart     -- a container
  local autoref  -- for self references
 
  --[[ counts the number of elements in a table
  local function tablecount(t)
     local n = 0
     for _, _ in pairs(t) do n = n+1 end
     return n
  end
  ]]
  -- (RiciLake) returns true if the table is empty
  local function isemptytable(t) return next(t) == nil end
 
  local function basicSerialize (o)
     local so = tostring(o)
     if type(o) == "function" then
        local info = debug.getinfo(o, "S")
        -- info.name is nil because o is not a calling level
        if info.what == "C" then
           return string.format("%q", so .. ", C function")
        else 
           -- the information is defined through lines
           return string.format("%q", so .. ", defined in (" ..
               info.linedefined .. "-" .. info.lastlinedefined ..
               ")" .. info.source)
        end
     elseif type(o) == "number" or type(o) == "boolean" then
        return so
     else
        return string.format("%q", so)
     end
  end
 
  local function addtocart (value, name, indent, saved, field)
     indent = indent or ""
     saved = saved or {}
     field = field or name
 
     cart = cart .. indent .. field
 
     if type(value) ~= "table" then
        cart = cart .. " = " .. basicSerialize(value) .. ";\n"
     else
        if saved[value] then
           cart = cart .. " = {}; -- " .. saved[value] 
                       .. " (self reference)\n"
           autoref = autoref ..  name .. " = " .. saved[value] .. ";\n"
        else
           saved[value] = name
           --if tablecount(value) == 0 then
           if isemptytable(value) then
              cart = cart .. " = {};\n"
           else
              cart = cart .. " = {\n"
              for k, v in pairs(value) do
                 k = basicSerialize(k)
                 local fname = string.format("%s[%s]", name, k)
                 field = string.format("[%s]", k)
                 -- three spaces between levels
                 addtocart(v, fname, indent .. "   ", saved, field)
              end
              cart = cart .. indent .. "};\n"
           end
        end
     end
  end
 
  name = name or "__unnamed__"
  if type(t) ~= "table" then
     return name .. " = " .. basicSerialize(t)
  end
  cart, autoref = "", ""
  addtocart(t, name, indent)
  return cart .. autoref
end
 
function p.firstToUpper( str )
return (str:gsub("^%l", string.upper))
end
 
function p.round(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end
 
function p.AddZeros( s, len )
  local output = ""
 
  local sLength = string.len( tostring( s ) )
  local diff = tonumber( len ) - tonumber( sLength )
 
  if diff > 0 then
    for i = 1, diff do
      output = output .. "0"
    end
  end
 
  output = output .. s
 
  return output
end
 
function p.ExternalLink( link, text, plain )
  local output = "[" .. link .. " " .. text .. "]"
 
  if plain == true then
    output = "<span class=\"plainlinks\">" .. output .. "</span>"
  end  
 
  return output
end
 
function p.CategoryLink( category, sort, text )
  local output = ""
  if not p.isempty( text ) then
    output = "[[:Category:" .. category .. "|" .. text .. "]][[Category:" .. category .. "|" .. sort .. "]]"
  else 
    output = "[[Category:" .. category .. "|" .. sort .. "]]"
  end
  return output
end
 
function p.Link( link, text )
  local output = "[[" .. link
 
  if not p.isempty( text ) then
    output = output .. "|" .. text
  end
 
  output = output .. "]]"
 
  return output
end
 
function p.HelpButton( frame )
	local args = getArgs( frame )
	return p._HelpButton( args )
end
 
function p._HelpButton( args )
  if p.isempty( args.buttonsize ) then args.buttonsize = "10px" end
  local target = args.ArticleTarget or "Click here for help with this field"
	local link = string.format(
		"[[File:Information-silk.png|%s|link=Click here for help with this field#%s]] %s",
		args.buttonsize,
		args.Section or args.Label or '',
    args.Label or ''
	)
  return link
end
 
return p
Community content is available under CC-BY-SA unless otherwise noted.

Bring Your Marvel Movies Together