Modul:URLutil: Unterschied zwischen den Versionen

Inhalt gelöscht Inhalt hinzugefügt
Die Seite wurde neu angelegt: „local URLutil = { suite = "URLutil", serial = "2018-01-24", item = 10859193 } --[=[ Utilities for URL etc. on www. * get…“
 
w>XanonymusX
2022-04-05
Zeile 1:
local URLutil = { suite = "URLutil",
serial = "20182022-0104-2405",
item = 10859193 }
--[=[
Utilities for URL etc. on www.
* decode()
* encode()
* getAuthority()
* getFragment()
Zeile 24 ⟶ 26:
* isDomainInt()
* isHost()
* isHostPathResource()
* isIP()
* isIPlocal()
Zeile 37 ⟶ 40:
* isWebURL()
* wikiEscapeURL()
* failsafe()
Only [[dotted decimal]] notation for IPv4 expected.
Does not support dotted hexadecimal, dotted octal, or single-number formats.
IPv6 URL (bracketed) not yet implemented; might need Wikintax escaping anyway.
]=]
local Failsafe = URLutil
 
 
Zeile 46 ⟶ 51:
local decodeComponentProtect = { F = "\"#%<>[\]^`{|}",
P = "\"#%<>[\]^`{|}/?",
Q = "\"#%<>[\]^`{|}&=+;,",
X = "\"#%<>[\]^`{|}&=+;,/?" }
 
 
Zeile 123 ⟶ 128:
end
n = ( ask:byte( j, j ) - 48 ) * 16 + n
if decodeComponentEscape(n averse,== n39 ) thenand
ask:sub( i + 3, i + 5 ) == "%27" then
j = i + 6
while ( ask:sub( j, j + 2 ) == "%27" ) do
j = j + 3
end -- while "%27"
elseif decodeComponentEscape( averse, n ) then
if m then
ask = string.format( "%s%c%s",
Zeile 189 ⟶ 200:
return r
end -- getHash()
 
 
 
URLutil.decode = function ( url, enctype )
local r, s
if type( enctype ) == "string" then
s = mw.text.trim( enctype )
if s == "" then
s = false
else
s = s:upper()
end
end
r = mw.text.encode( mw.uri.decode( url, s ) )
if r:find( "[%[|%]]" ) then
local k
r, k = r:gsub( "%[", "&#91;" )
:gsub( "|", "&#124;" )
:gsub( "%]", "&#93;" )
end
return r
end -- URLutil.decode()
 
 
 
URLutil.encode = function ( url, enctype )
local k, r, s
if type( enctype ) == "string" then
s = mw.text.trim( enctype )
if s == "" then
s = false
else
s = s:upper()
end
end
r = mw.uri.encode( url, s )
k = r:byte( 1, 1 )
if -- k == 35 or -- #
k == 42 or -- *
k == 58 or -- :
k == 59 then -- ;
r = string.format( "%%%X%s", k, r:sub( 2 ) )
end
if r:find( "[%[|%]]" ) then
r, k = r:gsub( "%[", "%5B" )
:gsub( "|", "%7C" )
:gsub( "%]", "%5D" )
end
return r
end -- URLutil.encode()
 
 
Zeile 601 ⟶ 662:
local r
if type( s ) == "string" then
local scan = "^%s*([%w%.%%_-]+*%w)%.(%a[%w-]*%a)%s*$"
local scope
s, scope = mw.ustring.match( s, scan )
Zeile 663 ⟶ 724:
return URLutil.isDomain( s ) or URLutil.isIP( s )
end -- URLutil.isHost()
 
 
 
URLutil.isHostPathResource = function ( s )
local r = URLutil.isResourceURL( s )
if not r and s then
r = URLutil.isResourceURL( "//" .. mw.text.trim( s ) )
end
return r
end -- URLutil.isHostPathResource()
 
 
Zeile 842 ⟶ 913:
local s = URLutil.getAuthority( url )
local pat = "[%[|%]" ..
mw.ustring.char( 8201, 45, 820734,
8201, 45, 8207,
8234, 45, 8239,
8288 )
Zeile 875 ⟶ 947:
URLutil.isWebURL = function ( url )
if URLutil.getScheme( url ) and URLutil.getAuthority( url ) then
if not url:matchfind( "%S%s+%S" ) then and
not url:find( "''", 1, true ) then
return true
end
Zeile 896 ⟶ 969:
 
 
function URLutilFailsafe.failsafe = function ( assertatleast )
-- Retrieve versioning and check for compliance
-- Precondition:
-- assertatleast -- string, with required version or "wikidata",
-- or wikidata|item|~|@ or false
-- Postcondition:
-- Returns string -- with appropriatequeried version/item, oralso if falseproblem
-- false -- if appropriate
local since = assert
-- 2020-08-17
local since = atleast
local last = ( since == "~" )
local linked = ( since == "@" )
local link = ( since == "item" )
local r
if last or link or linked or since == "wikidata" then
local item = URLutilFailsafe.item
since = false
if type( item ) == "number" and item > 0 then
local entsuited = mw.wikibase.getEnt( string.format( "Q%d", item )
if link then
item ) )
if type( ent ) r == "table" thensuited
else
local vsn = ent:formatPropertyValues( "P348" )
iflocal type( vsn )entity == "table"mw.wikibase.getEntity( suited and)
if type( vsn.valueentity ) == "stringtable" andthen
vsn.value ~local seek = Failsafe.serialProperty or "P348" then
rlocal vsn = vsn.valueentity:formatPropertyValues( seek )
if type( vsn ) == "table" and
type( vsn.value ) == "string" and
vsn.value ~= "" then
if last and vsn.value == Failsafe.serial then
r = false
elseif linked then
if mw.title.getCurrentTitle().prefixedText
== mw.wikibase.getSitelink( suited ) then
r = false
else
r = suited
end
else
r = vsn.value
end
end
end
end
end
end
if nottype( r ) == "nil" then
if not since or since <= URLutilFailsafe.serial then
r = URLutilFailsafe.serial
else
r = false
Zeile 929 ⟶ 1.023:
end
return r
end -- URLutilFailsafe.failsafe()
 
 
 
local function Template( frame, action, amount )
-- Provide template access and expose URLutil table to require
-- Run actual code from template transclusion
-- Precondition:
-- frame -- object
-- action -- string, with function name
-- amount -- number, of args if > 1
-- Postcondition:
-- Return string or not
local n = amount or 1
local v = { }
local r, s
for i = 1, n do
s = frame.args[ i ]
if s then
s = mw.text.trim( s )
if s ~= "" then
v[ i ] = s
end
end
end -- for i
if v[ 1 ] then
r = URLutil[ action ]( v[ 1 ], v[ 2 ], v[ 3 ] )
end
return r
end -- Template()
 
 
 
local p = {}
 
function p.decode( frame )
return Template( frame, "decode", 2 ) or ""
end
function p.encode( frame )
return Template( frame, "encode", 2 ) or ""
end
function p.getAuthority( frame )
return URLutil.getAuthorityTemplate( frame.args[, 1 ]"getAuthority" ) or ""
end
function p.getFragment( frame )
local r = URLutil.getFragmentTemplate( frame.args[, 1 ]"getFragment", frame.args[ 2 ] )
if r then
r = "#" .. r
Zeile 950 ⟶ 1.076:
end
function p.getHost( frame )
return URLutil.getHostTemplate( frame.args[, 1 ]"getHost" ) or ""
end
function p.getLocation( frame )
return URLutil.getLocationTemplate( frame.args[, 1 ]"getLocation" ) or ""
end
function p.getNormalized( frame )
return URLutil.getNormalizedTemplate( frame.args[, 1 ]"getNormalized" ) or ""
end
function p.getPath( frame )
return URLutil.getPathTemplate( frame.args[, 1 ]"getPath" ) or ""
end
function p.getPort( frame )
return URLutil.getPortTemplate( frame.args[, 1 ]"getPort" ) or ""
end
function p.getQuery( frame )
local r = Template( frame, "getQuery", 3 )
if r then
local key = frame.args[ 2 ]
if local key then= frame.args[ 2 ]
key = mw.text.trim(if key )then
if key == ""mw.text.trim( key then)
if key == "" nilthen
key = nil
end
end
end
r = URLutil.getQuery( frame.args[ 1 ], key, frame.args[ 3 ] )
if r then
if not key then
r = "?" .. r
Zeile 984 ⟶ 1.109:
end
function p.getRelativePath( frame )
return URLutil.getRelativePathTemplate( frame.args[, 1 ]"getRelativePath" ) or ""
end
function p.getScheme( frame )
return URLutil.getSchemeTemplate( frame.args[, 1 ]"getScheme" ) or ""
end
function p.getSortkey( frame )
return URLutil.getSortkeyTemplate( frame.args[, 1 ]"getSortkey" ) or ""
end
function p.getTLD( frame )
return URLutil.getTLDTemplate( frame.args[, 1 ]"getTLD" ) or ""
end
function p.getTop2domain( frame )
return URLutil.getTop2domainTemplate( frame.args[, 1 ]"getTop2domain" ) or ""
end
function p.getTop3domain( frame )
return URLutil.getTop3domainTemplate( frame.args[, 1 ]"getTop3domain" ) or ""
end
function p.isAuthority( frame )
return URLutil.isAuthorityTemplate( frame.args[, 1 ]"isAuthority" ) and "1" or ""
end
function p.isDomain( frame )
return URLutil.isDomainTemplate( frame.args[, 1 ]"isDomain" ) and "1" or ""
end
function p.isDomainExample( frame )
return URLutil.isDomainExampleTemplate( frame.args[, 1 ]"isDomainExample" ) and "1" or ""
end
function p.isDomainInt( frame )
return URLutil.isDomainIntTemplate( frame.args[, 1 ]"isDomainInt" ) and "1" or ""
end
function p.isHost( frame )
return URLutil.isHostTemplate( frame.args[, 1 ]"isHost" ) and "1" or ""
end
function p.isHostPathResource( frame )
return Template( frame, "isHostPathResource" ) and "1" or ""
end
function p.isIP( frame )
return URLutil.isIPTemplate( frame.args[, 1 ]"isIP" ) or ""
end
function p.isIPlocal( frame )
return URLutil.isIPlocalTemplate( frame.args[, 1 ]"isIPlocal" ) and "1" or ""
end
function p.isIPv4( frame )
return URLutil.isIPv4Template( frame.args[, 1 ]"isIPv4" ) and "1" or ""
end
function p.isIPv6( frame )
return URLutil.isIPv6Template( frame.args[, 1 ]"isIPv6" ) and "1" or ""
end
function p.isMailAddress( frame )
return URLutil.isMailAddressTemplate( frame.args[, 1 ]"isMailAddress" ) and "1" or ""
end
function p.isMailLink( frame )
return URLutil.isMailLinkTemplate( frame.args[, 1 ]"isMailLink" ) and "1" or ""
end
function p.isProtocolDialog( frame )
return URLutil.isProtocolDialogTemplate( frame.args[, 1 ]"isProtocolDialog" ) and "1" or ""
end
function p.isProtocolWiki( frame )
return URLutil.isProtocolWikiTemplate( frame.args[, 1 ]"isProtocolWiki" ) and "1" or ""
end
function p.isResourceURL( frame )
return URLutil.isResourceURLTemplate( frame.args[, 1 ]"isResourceURL" ) and "1" or ""
end
function p.isSuspiciousURL( frame )
return URLutil.isSuspiciousURLTemplate( frame.args[, 1 ]"isSuspiciousURL" ) and "1" or ""
end
function p.isUnescapedURL( frame )
return URLutil.isUnescapedURLTemplate( frame.args[, 1 ]"isUnescapedURL", frame.args[ 2 ] ) and "1" or ""
end
function p.isWebURL( frame )
return URLutil.isWebURLTemplate( frame.args[, 1 ]"isWebURL" ) and "1" or ""
end
function p.wikiEscapeURL( frame )
return URLutil.wikiEscapeURLTemplate( frame.args[, 1 ]"wikiEscapeURL" )
end
p.failsafe = function ( frame )
Zeile 1.069 ⟶ 1.197:
end
end
return URLutilFailsafe.failsafe( since ) or ""
end
function p.URLutil()