diff --git a/ParserFunctions.i18n.magic.php b/ParserFunctions.i18n.magic.php index 0dda091..029451e 100644 --- a/ParserFunctions.i18n.magic.php +++ b/ParserFunctions.i18n.magic.php @@ -38,6 +38,7 @@ $magicWords['en'] = array( 'replace' => array( 0, 'replace' ), 'explode' => array( 0, 'explode' ), 'urldecode' => array( 0, 'urldecode' ), + 'if_ip' => array( 0, 'if_ip' ), ); /** Arabic (العربية) */ diff --git a/ParserFunctions.php b/ParserFunctions.php index fc28ece..c18acc9 100644 --- a/ParserFunctions.php +++ b/ParserFunctions.php @@ -100,6 +100,7 @@ function wfRegisterParserFunctions( $parser ) { $parser->setFunctionHook( 'replace', 'ExtParserFunctions::runReplace' ); $parser->setFunctionHook( 'explode', 'ExtParserFunctions::runExplode' ); $parser->setFunctionHook( 'urldecode', 'ExtParserFunctions::runUrlDecode' ); + $parser->setFunctionHook( 'if_ip', 'ExtParserFunctions::runIfIp', SFH_OBJECT_ARGS ); } if( $wgPFEnableConvert ) { diff --git a/ParserFunctions_body.php b/ParserFunctions_body.php index d68ed8b..afc07eb 100644 --- a/ParserFunctions_body.php +++ b/ParserFunctions_body.php @@ -888,4 +888,21 @@ class ExtParserFunctions { wfProfileOut( __METHOD__ ); return $result; } + + /** + * {{#if_ip:string|then|else}} + * + * @param $parser Parser + * @param $frame PPFrame + * @param $args array + * @return string + */ + public static function runIfIp( $parser, $frame, $args ) { + $test = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; + if ( $test !== '' && IP::isIPAddress( $test ) ) { + return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; + } else { + return isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : ''; + } + } } diff --git a/stringFunctionTests.txt b/stringFunctionTests.txt index 064dece..5f70921 100644 --- a/stringFunctionTests.txt +++ b/stringFunctionTests.txt @@ -30,3 +30,11 @@ some unescaped string
!! end +!! test +#if_ip +!!input +{{#if_ip:127.0.0.1|pass|fail}}-{{#if_ip:1.2.3.4.5|fail|pass}}-{{#if_ip:foo|fail|pass}}-{{#if_ip:fe80:0000:0000:0000:0202:b3ff:fe1e:8329|pass|fail}} +!! result +pass-pass-pass-pass +
+!!end \ No newline at end of file