Jump to content
Wikipedia The Free Encyclopedia

Module:IPAddress/sandbox

From Wikipedia, the free encyclopedia
This is the module sandbox page for Module:IPAddress (diff).
See also the companion subpage for test cases (run).
Module documentation[view] [edit] [history] [purge]
[画像:Warning] This Lua module is used in MediaWiki:Newarticletext and MediaWiki:Blockedtext , and on approximately 205,000 pages.
Changes to it can cause immediate changes to the Wikipedia user interface.
To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them.
This module can only be edited by administrators because it is transcluded onto one or more cascade-protected pages.

Functions are not "local", so other modules can require this module and call them directly. We return an object with 3 small stub functions to call the real ones so that the functions can be called from templates also.

Only dotted decimal notation for IPv4 supported. Does not support dotted hexadecimal, dotted octal, or single-number formats (see IPv4#Address_representations).

Unit tests at Module:IPAddress/testcases

The above documentation is transcluded from Module:IPAddress/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Add categories to the /doc subpage. Subpages of this module.
 localp={}

 functionp._isIpV6(s)
 localdcolon,groups
 iftype(s)~="string"
 ors:len()==0
 ors:find("[^:%x]")-- only colon and hex digits are legal chars
 ors:find("^:[^:]")-- can begin or end with :: but not with single :
 ors:find("[^:]:$")
 ors:find(":::")
 then
 returnfalse
 end
 s,dcolon=s:gsub("::",":")
 ifdcolon>1thenreturnfalseend-- at most one ::
 s=s:gsub("^:?",":")-- prepend : if needed, upper
 s,groups=s:gsub(":%x%x?%x?%x?","")-- remove valid groups, and count them
 return((dcolon==1andgroups<8)or(dcolon==0andgroups==8))
 and(s:len()==0or(dcolon==1ands==":"))-- might be one dangling : if original ended with ::
 end

 functionp._isIpV4(s)
 localfunctionlegal(n)return(tonumber(n)or256)<256andnotn:match("^0%d")end
 iftype(s)~="string"thenreturnfalseend
 localp1,p2,p3,p4=s:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$")
 returnlegal(p1)andlegal(p2)andlegal(p3)andlegal(p4)
 end

 functionp._isIp(s)
 returnp._isIpV4(s)and"4"orp._isIpV6(s)and"6"
 end

 functionp._isIpV4Range(s)
 returnp._isIpV4(s:gsub("/%d+$",""))and(p._isIpOrRange(s)=='range')
 end

 functionp._isIpV6Range(s)
 returnp._isIpV6(s:gsub("/%d+$",""))and(p._isIpOrRange(s)=='range')
 end

 functionp._isIpOrRange(s)
 localmodip=require('Module:IP')
 localsuccess,ip=pcall(modip.IPAddress.new,s)
 ifsuccessthen
 return'ip'
 end
 success,ip=pcall(modip.Subnet.new,s)
 ifsuccessthen
 return'range'
 end
 return''
 end

 functionp._isUser(s)
 ifp._isIpOrRange(s)~=''thenreturn''end
 localisTempUser=p._isTempUser(s)
 ifisTempUserthenreturn'temp'
 elsereturn'perm'end
 end

 functionp._isTempUser(s)
 ifp._isIpOrRange(s)~=''thenreturnfalseend
 returns:match('^~%d+%-%d+%-%d+$')
 end

 functionp._isPermUser(s)
 ifp._isIpOrRange(s)~=''thenreturnfalseend
 returnnotp._isTempUser(s)
 end

 functionp._main(s)
 localisIpOrRange=p._isIpOrRange(s)
 localisUser=p._isUser(s)
 returnisIpOrRange~=''andisIpOrRangeorisUser
 end

 localfunctioninput(frame)
 -- Return input parameter after replacing any of following directional markers.
 -- LRM : LEFT-TO-RIGHT MARK (U+200E) : hex e2 80 8e = 226 128 142
 -- LRE : LEFT-TO-RIGHT EMBEDDING (U+202A) : hex e2 80 aa = 226 128 170
 -- PDF : POP DIRECTIONAL FORMATTING (U+202C) : hex e2 80 ac = 226 128 172
 -- This is required for MediaWiki:Blockedtext message.
 return(frame.args[1]or''):gsub('226円128円[142円170円172円]',' ')-- replace LRM, LRE, PDF with space delimiter
 end

 functionp.isIpV6(frame)returnp._isIpV6(input(frame))and"1"or"0"end
 functionp.isIpV4(frame)returnp._isIpV4(input(frame))and"1"or"0"end
 functionp.isIpV6Range(frame)returnp._isIpV6Range(input(frame))and"1"or"0"end
 functionp.isIpV4Range(frame)returnp._isIpV4Range(input(frame))and"1"or"0"end
 functionp.isIp(frame)returnp._isIp(input(frame))or""end

 functionp.isIpOrRange(frame)
 -- {{#invoke:IPAddress|isIpOrRange|x}} → 'ip' (IPv4/IPv6) or 'range' (CIDR IPv4/IPv6) or '' (invalid)
 returnp._isIpOrRange(input(frame))
 end

 functionp.isTempUser(frame)returnp._isTempUser(input(frame))and"1"or"0"end
 functionp.isPermUser(frame)returnp._isPermUser(input(frame))and"1"or"0"end
 functionp.isUser(frame)returnp._isUser(input(frame))or""end

 functionp.main(frame)
 localargs=require("Module:Arguments").getArgs(frame)
 localsanitizeInput=input(frame)
 localresult=p._main(sanitizeInput)
 ifargs[result]then
 returnargs[result]
 else
 returnresult
 end
 end

 returnp

AltStyle によって変換されたページ (->オリジナル) /