jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/392577 )

Change subject: Add multi-wiki support
......................................................................


Add multi-wiki support

Add a $wgThrottleOverrideCentralWiki configuration setting which can be
used to designate a single wiki in a wiki farm as the location for
managing ThrottleOverride exemptions.

Bug: T147364
Change-Id: I56fa0866133668a2151ce04510e0021f0ae3cbdd
---
M SpecialOverrideThrottle.php
M SpecialThrottleOverrideList.php
M ThrottleOverride.hooks.php
A ThrottleOverrideUtils.php
M extension.json
5 files changed, 104 insertions(+), 34 deletions(-)

Approvals:
  Aaron Schulz: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/SpecialOverrideThrottle.php b/SpecialOverrideThrottle.php
index a9e1f7b..20109a4 100644
--- a/SpecialOverrideThrottle.php
+++ b/SpecialOverrideThrottle.php
@@ -125,7 +125,7 @@
                        $data['Target']['default'] = $this->par;
 
                        // We need the most recent data here, we're about to 
change the throttle.
-                       $dbw = wfGetDB( DB_MASTER );
+                       $dbw = ThrottleOverrideUtils::getCentralDB( DB_MASTER );
                        $row = $dbw->selectRow(
                                'throttle_override',
                                [ 'thr_expiry', 'thr_reason', 'thr_type' ],
@@ -184,7 +184,7 @@
                list( $rangeStart, $rangeEnd ) = $parsedRange;
 
                // Save the new exemption
-               $dbw = wfGetDB( DB_MASTER );
+               $dbw = ThrottleOverrideUtils::getCentralDB( DB_MASTER );
                $row = [
                        'thr_target' => $this->target,
                        'thr_expiry' => $dbw->encodeExpiry( $data['Expiry'] ),
@@ -207,7 +207,7 @@
 
                // Purge the cache
                $cache = 
MediaWikiServices::getInstance()->getMainWANObjectCache();
-               $cache->touchCheckKey( ThrottleOverrideHooks::getBucketKey( 
$cache, $rangeStart ) );
+               $cache->touchCheckKey( ThrottleOverrideUtils::getBucketKey( 
$cache, $rangeStart ) );
 
                return true;
        }
@@ -221,7 +221,7 @@
         * @return int
         */
        public static function getThrottleOverrideId( $ip, $dbtype = DB_REPLICA 
) {
-               $db = wfGetDB( $dbtype );
+               $db = ThrottleOverrideUtils::getCentralDB( $dbtype );
                $field = $db->selectField(
                        'throttle_override',
                        'thr_id',
diff --git a/SpecialThrottleOverrideList.php b/SpecialThrottleOverrideList.php
index 7113ce3..72e6819 100644
--- a/SpecialThrottleOverrideList.php
+++ b/SpecialThrottleOverrideList.php
@@ -67,7 +67,7 @@
        function onSubmit( array $data, HTMLForm $form = null ) {
                if ( !wfReadOnly() && !mt_rand( 0, 10 ) ) {
                        // Purge expired entries on one in every 10 queries
-                       $dbw = wfGetDB( DB_MASTER );
+                       $dbw = ThrottleOverrideUtils::getCentralDB( DB_MASTER );
                        $method = __METHOD__;
                        $dbw->onTransactionIdle( function () use ( $dbw, 
$method ) {
                                $dbw->delete(
diff --git a/ThrottleOverride.hooks.php b/ThrottleOverride.hooks.php
index a08e8ce..40dc2cd 100644
--- a/ThrottleOverride.hooks.php
+++ b/ThrottleOverride.hooks.php
@@ -45,7 +45,7 @@
         * @return bool
         */
        public static function onPingLimiter( User &$user, $action, &$result, 
$ip = null ) {
-               global $wgRateLimits;
+               global $wgRateLimits, $wgThrottleOverrideCentralWiki;
 
                if ( $action !== 'actcreate' && !isset( $wgRateLimits[$action] 
) ) {
                        return true;
@@ -60,10 +60,15 @@
 
                $cache = 
MediaWikiServices::getInstance()->getMainWANObjectCache();
                $expiry = $cache->getWithSetCallback(
-                       $cache->makeKey( 'throttle_override', $action, $hexIp ),
+                       $cache->makeGlobalKey(
+                               'throttle_override',
+                               $wgThrottleOverrideCentralWiki,
+                               $action,
+                               $hexIp
+                       ),
                        $cache::TTL_HOUR,
                        function ( $cValue, &$ttl, &$setOpts, $asOf ) use ( 
$ip, $hexIp, $action ) {
-                               $dbr = wfGetDB( DB_REPLICA );
+                               $dbr = ThrottleOverrideUtils::getCentralDB( 
DB_REPLICA );
                                $setOpts += Database::getCacheSetOptions( $dbr 
);
 
                                $expiry = $dbr->selectField(
@@ -95,7 +100,7 @@
                                return ( $expiry === false ) ? 
self::NO_OVERRIDE : $expiry;
                        },
                        [
-                               'checkKeys' => [ self::getBucketKey( $cache, 
$ip ) ]
+                               'checkKeys' => [ 
ThrottleOverrideUtils::getBucketKey( $cache, $ip ) ]
                        ]
                );
 
@@ -115,7 +120,7 @@
                        return false;
                } elseif ( $expiry !== false ) {
                        // Expired exemption. Delete it from the DB.
-                       $dbw = wfGetDB( DB_MASTER );
+                       $dbw = ThrottleOverrideUtils::getCentralDB( DB_MASTER );
                        $dbw->delete(
                                'throttle_override',
                                self::makeConds( $dbw, $hexIp, $action ),
@@ -124,25 +129,6 @@
                }
 
                return true;
-       }
-
-       /**
-        * Get the cache bucket key for either:
-        *   - a) The IP address of a user
-        *   - b) A throttle override that happens to include the given IP 
address
-        *
-        * @param WANObjectCache $cache
-        * @param string $ip A valid IP address (with no pointless CIDR)
-        * @return string
-        */
-       public static function getBucketKey( WANObjectCache $cache, $ip ) {
-               global $wgThrottleOverrideCIDRLimit;
-               // Split the address space into buckets such that any given 
user IP address
-               // or throttle override's IP range will fall into exactly one 
bucket.
-               $proto = IP::isIPv6( $ip ) ? 'IPv6' : 'IPv4';
-               $bucket = IP::sanitizeRange( 
"$ip/{$wgThrottleOverrideCIDRLimit[$proto]}" );
-               // Purge all cache for all IPs in this bucket
-               return $cache->makeKey( 'throttle-override', $bucket );
        }
 
        /**
@@ -185,4 +171,18 @@
 
                return true;
        }
+
+       public static function onSetupAfterCache() {
+               global $wgThrottleOverrideCentralWiki;
+               if ( $wgThrottleOverrideCentralWiki === false ) {
+                       $wgThrottleOverrideCentralWiki = wfWikiId();
+               }
+       }
+
+       public static function onSpecialPageInitList( array &$specialPages ) {
+               if ( ThrottleOverrideUtils::isCentralWiki() ) {
+                       $specialPages['OverrideThrottle'] = 
SpecialOverrideThrottle::class;
+                       $specialPages['ThrottleOverrideList'] = 
SpecialThrottleOverrideList::class;
+               }
+       }
 }
diff --git a/ThrottleOverrideUtils.php b/ThrottleOverrideUtils.php
new file mode 100644
index 0000000..375c341
--- /dev/null
+++ b/ThrottleOverrideUtils.php
@@ -0,0 +1,64 @@
+<?php
+/**
+ * @section LICENSE
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @copyright © 2017 Wikimedia Foundation and contributors
+ */
+
+class ThrottleOverrideUtils {
+       /**
+        * @return bool
+        */
+       public static function isCentralWiki() {
+               global $wgThrottleOverrideCentralWiki;
+               return wfWikiId() === $wgThrottleOverrideCentralWiki;
+       }
+
+       /**
+        * @param int $index DB_MASTER/DB_REPLICA
+        * @return DBConnRef
+        */
+       public static function getCentralDB( $index ) {
+               global $wgThrottleOverrideCentralWiki;
+               return wfGetLB( $wgThrottleOverrideCentralWiki 
)->getLazyConnectionRef(
+                       $index, [], $wgThrottleOverrideCentralWiki );
+       }
+
+       /**
+        * Get the cache bucket key for either:
+        *   - a) The IP address of a user
+        *   - b) A throttle override that happens to include the given IP 
address
+        *
+        * @param WANObjectCache $cache
+        * @param string $ip A valid IP address (with no pointless CIDR)
+        * @return string
+        */
+       public static function getBucketKey( WANObjectCache $cache, $ip ) {
+               global $wgThrottleOverrideCentralWiki, 
$wgThrottleOverrideCIDRLimit;
+               // Split the address space into buckets such that any given 
user IP address
+               // or throttle override's IP address range will fall into 
exactly one bucket.
+               $proto = IP::isIPv6( $ip ) ? 'IPv6' : 'IPv4';
+               $bucket = IP::sanitizeRange( 
"$ip/{$wgThrottleOverrideCIDRLimit[$proto]}" );
+               // Purge all cache for all IPs in this bucket
+               return $cache->makeGlobalKey(
+                       'throttle-override',
+                       $wgThrottleOverrideCentralWiki,
+                       $bucket
+               );
+       }
+}
diff --git a/extension.json b/extension.json
index 877be67..75c26da 100644
--- a/extension.json
+++ b/extension.json
@@ -9,10 +9,6 @@
        "AvailableRights": [
                "throttleoverride"
        ],
-       "SpecialPages": {
-               "OverrideThrottle": "SpecialOverrideThrottle",
-               "ThrottleOverrideList": "SpecialThrottleOverrideList"
-       },
        "LogTypes": [
                "throttleoverride"
        ],
@@ -28,6 +24,12 @@
                ],
                "LoadExtensionSchemaUpdates": [
                        "ThrottleOverrideHooks::onLoadExtensionSchemaUpdates"
+               ],
+               "SetupAfterCache": [
+                       "ThrottleOverrideHooks::onSetupAfterCache"
+               ],
+               "SpecialPage_initList": [
+                       "ThrottleOverrideHooks::onSpecialPageInitList"
                ]
        },
        "MessagesDirs": {
@@ -43,9 +45,13 @@
                "SpecialOverrideThrottle": "SpecialOverrideThrottle.php",
                "SpecialThrottleOverrideList": 
"SpecialThrottleOverrideList.php",
                "ThrottleOverridePager": "ThrottleOverridePager.php",
-               "ThrottleOverrideLogFormatter": 
"ThrottleOverrideLogFormatter.php"
+               "ThrottleOverrideLogFormatter": 
"ThrottleOverrideLogFormatter.php",
+               "ThrottleOverrideUtils": "ThrottleOverrideUtils.php"
        },
        "config": {
+               "ThrottleOverrideCentralWiki": {
+                       "value": false
+               },
                "ThrottleOverrideCIDRLimit": {
                        "value": {
                                "IPv4": 16,

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I56fa0866133668a2151ce04510e0021f0ae3cbdd
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/ThrottleOverride
Gerrit-Branch: master
Gerrit-Owner: BryanDavis <[email protected]>
Gerrit-Reviewer: Aaron Schulz <[email protected]>
Gerrit-Reviewer: BryanDavis <[email protected]>
Gerrit-Reviewer: EddieGP <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Reedy <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to