FANDOM


--<pre> Creates links and changes wikitext when it detects external links
-- Syntax
-- {{#invoke:links|main|page/url|label|type=ext/local}}
local p = {}
local interwikiPrefix = {wikia={"w:c",domain="wikia"},wikipedia={"wikipedia",domain="wikipedia"}}
 
function p.link(args)
    if not args then
        return
    end
 
    local typ = args["type"]
    local url = args[1] or args["page"] or ""
    -- url = createLocalLink(url)
    local lenUrl = mw.ustring.len(url)
    local uMatch = mw.ustring.match
 
    if typ == "ext" and (not uMatch(url, "%.") or  lenUrl < 3)
            or lenUrl < 1 then
        return
    end
 
    local prefix = "[["
    local suffix = "]]"
    local separator = "|"
 
    if url:match("^[Cc]ategory:") and typ ~= "ext" then
        prefix = "[[:"
    end
 
    -- Adapt if url is external
    if typ ~= "local" and (uMatch(url, "www%.") or uMatch(url, "^http"))
            or typ == "ext" then
        prefix = "[//"
 
        if uMatch(url, "^http") then
            prefix = "["
        end
 
        suffix = "]"
        separator = " "
    end
    local desc = args[2] or args["desc"] or ""
 
    if mw.ustring.len(desc) < 1 then
        separator = ""
    end
 
    return prefix .. url .. separator .. desc .. suffix
end
 
function createLocalLink(url)
    local link = mw.uri.new(url)
    local serverSubDomainName,serverName = mw.ustring.match (mw.site.server,"%:%/%/(.*)%.(.*)%..*")
    local newLink 
    local separator = ":"
    local foundServer 
 
    for server,v in pairs(interwikiPrefix) do
        serverSubDomainName,foundServer = mw.ustring.match (url,"(.*)%.("..v.domain ..")%..*")
 
        if foundServer  then
            serverName = foundServer
            break;
        end
    end
 
    if not link.host and url:match("%."..serverName) then
        link = mw.uri.new("//"..url)
    else 
        return url
    end
 
    local iwPrefix = interwikiPrefix[serverName][1]
    local subDomainName,linkServerName = mw.ustring.match (link.host,"(.*)%.(.*)%..*")
    local linkDomain = mw.ustring.match(link.host,"(.-)%."..serverName)
 
    if (linkDomain) then
        local linkDetails =  mw.text.split(linkDomain,"%.")
        local wiki,parentWiki = linkDetails[1] or "",linkDetails[2]
 
        newLink = wiki 
 
        if ( wiki and parentWiki) then
            newLink = wiki .. ".".. parentWiki 
        end
        local pagename = mw.ustring.match (link.path,"%/wiki%/(.*)") 
        if (pagename) then
            newLink = newLink ..":".. pagename 
        end
 
        newLink = iwPrefix..separator..newLink
    end
 
    local server = mw.uri.new(mw.site.server)
    if link.host ==server.host and mw.ustring.match (link.path,"%/wiki%/(.*)") then
        newLink = mw.ustring.match (link.path,"%/wiki%/(.*)")  
    end
    return newLink 
end
 
function p.pagelinks(frame)
    -- local page = frame.args["page"] or frame.args[1] or error("Invalid page")
    local page = tostring(mw.title.getCurrentTitle() )
    local contentPage = mw.title.new(page):getContent()
    local links = contentPage:gmatch("%[%[.-%]%]")
    local count =0
 
    for i,v in links do
        count = count +1
    end
    return count
end
 
function p.main(frame)
    local args = require("Dev:Utility").getArgs(frame)
 
    return p.link(args)
end
 
return p