Modul:URLutil: Unterschied zwischen den Versionen

update
w>PerfektesChaos
(update)
w>PerfektesChaos
(update)
Zeile 1: Zeile 1:
--[=[ URLutil 2014-05-01
--[=[ URLutil 2014-09-08
Utilities for URL etc. on www.
Utilities for URL etc. on www.
* getURIScheme()
* getAuthority()
* getAuthority()
* getFragment()
* getHost()
* getHost()
* getLocation()
* getPath()
* getPort()
* getPort()
* getQuery()
* getQueryTable()
* getRelativePath()
* getScheme()
* getScheme()
* getTLD()
* getTLD()
Zeile 19: Zeile 24:
* isMailAddress()
* isMailAddress()
* isMailLink()
* isMailLink()
* isProtocolMW()
* isProtocolDialog()
* isProtocolDialog()
* isProtocolWiki()
* isProtocolWiki()
Zeile 94: Zeile 98:
     return false
     return false
end -- URLutil.getAuthority()
end -- URLutil.getAuthority()
URLutil.getFragment = function ( url, decode )
    local r
    if type( url ) == "string" then
        local i = url:find( "#", 1, true )
        if i then
            r = mw.text.trim( url:sub( i ) ):sub( 2 )
            if type( decode ) == "string" then
                local encoding = mw.text.trim( decode )
                local launch
                if encoding == "%" then
                    launch = true
                elseif encoding == "WIKI" then
                    r = r:gsub( "%.(%x%x)", "%%%1" )
                        :gsub( "_", " " )
                    launch = true
                end
                if launch then
                    r = mw.uri.decode( r, "PATH" )
                end
            end
        else
            r = false
        end
    else
        r = nil
    end
    return r
end -- URLutil.getFragment()




Zeile 104: Zeile 139:
     return false
     return false
end -- URLutil.getHost()
end -- URLutil.getHost()
URLutil.getLocation = function ( url )
    local r
    if type( url ) == "string" then
        r = mw.text.trim( url )
        if r == "" then
            r = false
        else
            local i = r:find( "#", 1, true )
            if i then
                if i == 1 then
                    r = false
                else
                    r = r:sub( 1,  i - 1 )
                end
            end
        end
    else
        r = nil
    end
    return r
end -- URLutil.getLocation()
URLutil.getPath = function ( url )
    local r = URLutil.getRelativePath( url )
    if r then
        local s = r:match( "^([^%?]*)%?" )
        if s then
            r = s
        end
        s = r:match( "^([^#]*)#" )
        if s then
            r = s
        end
    end
    return r
end -- URLutil.getPath()






URLutil.getPort = function ( url )
URLutil.getPort = function ( url )
     url = URLutil.getAuthority( url )
     local r = URLutil.getAuthority( url )
     if url then
     if r then
         url = url:match( ":([1-9][0-9]*)$" )
         r = r:match( ":([1-9][0-9]*)$" )
         if type( url ) == "string" then
         if r then
             return tonumber( url )
             r = tonumber( r )
        else
            r = false
         end
         end
     end
     end
     return false
     return r
end -- URLutil.getPort()
end -- URLutil.getPort()
URLutil.getQuery = function ( url, key, separator )
    local r = URLutil.getLocation( url )
    if r then
        r = r:match( "^[^%?]*%?(.+)$" )
        if r then
            if type( key ) == "string" then
                local single = mw.text.trim( key )
                local sep = "&"
                local s, scan
                if type( separator ) == "string" then
                    s = mw.text.trim( separator )
                    if s:match( "^[&;,/]$" ) then
                        sep = s
                    end
                end
                s = string.format( "%s%s%s", sep, r, sep )
                scan = string.format( "%s%s=([^%s]*)%s",
                                      sep, key, sep, sep )
                r = s:match( scan )
            end
        end
        if not r then
            r = false
        end
    end
    return r
end -- URLutil.getQuery()
URLutil.getQueryTable = function ( url, separator )
    local r = URLutil.getQuery( url )
    if r then
        local sep = "&"
        local n, pairs, s, set
        if type( separator ) == "string" then
            s = mw.text.trim( separator )
            if s:match( "^[&;,/]$" ) then
                sep = s
            end
        end
        pairs = mw.text.split( r, sep, true )
        n = #pairs
        r = { }
        for i = 1, n do
            s = pairs[ i ]
            if s:find( "=", 2, true ) then
                s, set = s:match( "^([^=]+)=(.*)$" )
                if s then
                    r[ s ] = set
                end
            else
                r[ s ] = false
            end
        end -- for i
    end
    return r
end -- URLutil.getQueryTable()
URLutil.getRelativePath = function ( url )
    local r
    if type( url ) == "string" then
        local s = url:match( "^%s*[a-zA-Z]*://(.*)$" )
        if s then
            s = s:match( "[^/]+(/.*)$" )
        else
            local x
            x, s = url:match( "^%s*(/?)(/.*)$" )
            if x == "/" then
                s = s:match( "/[^/]+(/.*)$" )
            end
        end
        if s then
            r = mw.text.trim( s )
        elseif URLutil.isResourceURL( url ) then
            r = "/"
        else
            r = false
        end
    else
        r = nil
    end
    return r
end -- URLutil.getRelativePath()






URLutil.getScheme = function ( url )
URLutil.getScheme = function ( url )
    local r
     if type( url ) == "string" then
     if type( url ) == "string" then
         local prot, colon, slashes = url:match( "^%s*([a-zA-Z]*)(:?)(//)" )
         local prot, colon, slashes = url:match( "^%s*([a-zA-Z]*)(:?)(//)" )
        r = false
         if slashes == "//" then
         if slashes == "//" then
             if colon == ":" then
             if colon == ":" then
                 if #prot > 2 then
                 if #prot > 2 then
                     return prot:lower() .. "://"
                     r = prot:lower() .. "://"
                 end
                 end
             elseif #prot == 0 then
             elseif #prot == 0 then
                 return "//"
                 r = "//"
             end
             end
         end
         end
    else
        r = nil
     end
     end
     return false
     return r
end -- URLutil.getScheme()
end -- URLutil.getScheme()


Zeile 378: Zeile 549:
     local scheme = URLutil.getScheme( url )
     local scheme = URLutil.getScheme( url )
     if scheme then
     if scheme then
         local s = " // http:// https:// ftp:// "
         local s = " // http:// https:// ftp:// sftp:// "
         s = s:find( " " .. scheme .. " " )
         s = s:find( string.format( " %s ", scheme ) )
         if s then
         if s then
             if URLutil.getAuthority( url ) then
             if URLutil.getAuthority( url ) then
Zeile 458: Zeile 629:
function p.getAuthority( frame )
function p.getAuthority( frame )
     return URLutil.getAuthority( frame.args[ 1 ] ) or ""
     return URLutil.getAuthority( frame.args[ 1 ] ) or ""
end
function p.getFragment( frame )
    local r = URLutil.getFragment( frame.args[ 1 ], frame.args[ 2 ] )
    if r then
        r = "#" .. r
    else
        r = ""
    end
    return r
end
end
function p.getHost( frame )
function p.getHost( frame )
     return URLutil.getHost( frame.args[ 1 ] ) or ""
     return URLutil.getHost( frame.args[ 1 ] ) or ""
end
function p.getLocation( frame )
    return URLutil.getLocation( frame.args[ 1 ] ) or ""
end
function p.getPath( frame )
    return URLutil.getPath( frame.args[ 1 ] ) or ""
end
end
function p.getPort( frame )
function p.getPort( frame )
     return URLutil.getPort( frame.args[ 1 ] ) or ""
     return URLutil.getPort( frame.args[ 1 ] ) or ""
end
function p.getQuery( frame )
    local r
    local key = frame.args[ 2 ]
    if key then
        key = mw.text.trim( key )
        if key == "" then
            key = nil
        end
    end
    r = URLutil.getQuery( frame.args[ 1 ], key, frame.args[ 3 ] )
    if r then
        if not key then
            r = "?" .. r
        end
    else
        r = ""
    end
    return r
end
function p.getRelativePath( frame )
    return URLutil.getRelativePath( frame.args[ 1 ] ) or ""
end
end
function p.getScheme( frame )
function p.getScheme( frame )
Anonymer Benutzer