Daimona Eaytoy has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/389778 )

Change subject: Add getmatch function
......................................................................

Add getmatch function

Added the getmatch function to store a regex match.

Bug: T179957
Change-Id: I19366ebcaa4d0f007dd675a61c91457dde57f604
---
M includes/AbuseFilter.class.php
M includes/parser/AbuseFilterParser.php
2 files changed, 37 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/AbuseFilter 
refs/changes/78/389778/1

diff --git a/includes/AbuseFilter.class.php b/includes/AbuseFilter.class.php
index 1e936b0..71a16a4 100644
--- a/includes/AbuseFilter.class.php
+++ b/includes/AbuseFilter.class.php
@@ -76,6 +76,7 @@
                        'norm(string)' => 'norm',
                        'count(needle,haystack)' => 'count',
                        'rcount(needle,haystack)' => 'rcount',
+                       'getmatch(needle,haystack)' => 'getmatch',
                        'rmwhitespace(text)' => 'rmwhitespace',
                        'rmspecials(text)' => 'rmspecials',
                        'ip_in_range(ip, range)' => 'ip_in_range',
@@ -1447,21 +1448,12 @@
                                ];
                                break;
                        case 'rangeblock':
-                               global $wgAbuseFilterRangeBlockSize, 
$wgBlockCIDRLimit;
-
-                               $ip = $wgRequest->getIP();
-                               if ( IP::isIPv6( $ip ) ) {
-                                       $CIDRsize = max( 
$wgAbuseFilterRangeBlockSize['IPv6'], $wgBlockCIDRLimit['IPv6'] );
-                               } else {
-                                       $CIDRsize = max( 
$wgAbuseFilterRangeBlockSize['IPv4'], $wgBlockCIDRLimit['IPv4'] );
-                               }
-                               $blockCIDR = $ip . '/' . $CIDRsize;
                                self::doAbuseFilterBlock(
                                        [
                                                'desc' => $rule_desc,
                                                'number' => $rule_number
                                        ],
-                                       IP::sanitizeRange( $blockCIDR ),
+                                       IP::sanitizeRange( $wgRequest->getIP() 
. '/16' ),
                                        '1 week',
                                        false
                                );
diff --git a/includes/parser/AbuseFilterParser.php 
b/includes/parser/AbuseFilterParser.php
index 1eb70f7..5da7f1a 100644
--- a/includes/parser/AbuseFilterParser.php
+++ b/includes/parser/AbuseFilterParser.php
@@ -10,7 +10,7 @@
         */
        public $mVars;
 
-       // 
length,lcase,ucase,ccnorm,rmdoubles,specialratio,rmspecials,norm,count
+       // 
length,lcase,ucase,ccnorm,rmdoubles,specialratio,rmspecials,norm,count,getmatch
        public static $mFunctions = [
                'lcase' => 'funcLc',
                'ucase' => 'funcUc',
@@ -28,6 +28,7 @@
                'rmwhitespace' => 'funcRMWhitespace',
                'count' => 'funcCount',
                'rcount' => 'funcRCount',
+               'getmatch' => 'funcGetMatch',
                'ip_in_range' => 'funcIPInRange',
                'contains_any' => 'funcContainsAny',
                'substr' => 'funcSubstr',
@@ -1025,6 +1026,39 @@
        }
 
        /**
+        * @param $args
+        * @return AFPData A list of matches.
+        * @throws AFPUserVisibleException
+        * @throws Exception
+        */
+       protected function funcGetMatch( $args ) {
+               if ( count( $args ) < 2 ) {
+                       throw new AFPUserVisibleException(
+                       'notenoughargs',
+                       $this->mCur->pos,
+                       [ 'extract', 2, count( $args )]
+                       );
+               }
+               $needle = $args[0]->toString();
+               $haystack = $args[1]->toString();
+
+               $needle = preg_replace( '!(\\\\\\\\)*(\\\\)?/!', '$1\/', 
$needle );
+               $needle = "/$needle/u";
+
+               $check = preg_match( $needle, $haystack, $matches );
+
+               if ( false === $check ) {
+                       throw new AFPUserVisibleException(
+                               'regexfailure',
+                               $this->mCur->pos,
+                               [ 'unspecified error in preg_match()', $needle]
+                       );
+               }
+
+               return AFPData::newFromPHPVar( $matches );
+       }
+       
+       /**
         * @param array $args
         * @return AFPData
         * @throws AFPUserVisibleException

-- 
To view, visit https://gerrit.wikimedia.org/r/389778
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I19366ebcaa4d0f007dd675a61c91457dde57f604
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/AbuseFilter
Gerrit-Branch: master
Gerrit-Owner: Daimona Eaytoy <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to