jenkins-bot has submitted this change and it was merged.

Change subject: Add external db support to Echo
......................................................................


Add external db support to Echo

Change-Id: I84b0d904795d858d88d8e52c22f00d81c0e81303
---
M Echo.php
M includes/DbEchoBackend.php
M includes/DbEmailBatch.php
A includes/EchoDbFactory.php
M maintenance/removeInvalidNotification.php
M processEchoEmailBatch.php
6 files changed, 72 insertions(+), 11 deletions(-)

Approvals:
  Matthias Mullie: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/Echo.php b/Echo.php
index b3c68ca..ae1b690 100644
--- a/Echo.php
+++ b/Echo.php
@@ -79,6 +79,7 @@
 // Backend support
 $wgAutoloadClasses['MWEchoBackend'] = $dir . 'includes/EchoBackend.php';
 $wgAutoloadClasses['MWDbEchoBackend'] = $dir . 'includes/DbEchoBackend.php';
+$wgAutoloadClasses['MWEchoDbFactory'] = $dir . 'includes/EchoDbFactory.php';
 
 // Housekeeping hooks
 $wgHooks['LoadExtensionSchemaUpdates'][] = 'EchoHooks::getSchemaUpdates';
@@ -208,6 +209,10 @@
 // The organization address, the value should be defined in LocalSettings.php
 $wgEchoEmailFooterAddress = '';
 
+// Use the main db if this is set to false, to use a specific external db, just
+// use any key defined in $wgExternalServers
+$wgEchoCluster = false;
+
 // The max notification count showed in badge
 // The max number showed in bundled message, eg, <user> and 99+ others <action>
 $wgEchoMaxNotificationCount = 99;
diff --git a/includes/DbEchoBackend.php b/includes/DbEchoBackend.php
index 77ada76..ec6fc30 100644
--- a/includes/DbEchoBackend.php
+++ b/includes/DbEchoBackend.php
@@ -12,8 +12,8 @@
        private $dbw;
 
        protected function __construct() {
-               $this->dbr = wfGetDB( DB_SLAVE );
-               $this->dbw = wfGetDB( DB_MASTER );
+               $this->dbr = MWEchoDbFactory::getDB( DB_SLAVE );
+               $this->dbw = MWEchoDbFactory::getDB( DB_MASTER );
        }
 
        /**
@@ -224,7 +224,7 @@
 
                global $wgEchoMaxNotificationCount;
 
-               $db = wfGetDB( $dbSource );
+               $db = MWEchoDbFactory::getDB( $dbSource );
                $res = $db->select(
                        array( 'echo_notification', 'echo_event' ),
                        array( 'notification_event' ),
diff --git a/includes/DbEmailBatch.php b/includes/DbEmailBatch.php
index dde1615..b39106d 100644
--- a/includes/DbEmailBatch.php
+++ b/includes/DbEmailBatch.php
@@ -12,7 +12,7 @@
         * @return bool true if event exists false otherwise
         */
        protected function setLastEvent() {
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = MWEchoDbFactory::getDB( DB_SLAVE );
                $res = $dbr->selectField(
                        array( 'echo_email_batch' ),
                        array( 'MAX( eeb_event_id )' ),
@@ -40,7 +40,7 @@
                $validEvents = array_keys( $wgEchoNotifications );
 
                if ( $validEvents ) {
-                       $dbr = wfGetDB( DB_SLAVE );
+                       $dbr = MWEchoDbFactory::getDB( DB_SLAVE );
 
                        $conds = array(
                                'eeb_user_id' => $this->mUser->getId(),
@@ -79,7 +79,7 @@
                        $conds[] = 'eeb_event_id <= ' . intval( 
$this->lastEvent );
                }
 
-               $dbw = wfGetDB( DB_MASTER );
+               $dbw = MWEchoDbFactory::getDB( DB_MASTER );
                $dbw->delete(
                        'echo_email_batch',
                        $conds,
@@ -99,7 +99,7 @@
                        return;
                }
 
-               $dbw = wfGetDB( DB_MASTER );
+               $dbw = MWEchoDbFactory::getDB( DB_MASTER );
                $dbw->insert(
                        'echo_email_batch',
                        array(
@@ -118,7 +118,7 @@
         * @param $batchSize int
         */
        public static function actuallyGetUsersToNotify( $startUserId, 
$batchSize ) {
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = MWEchoDbFactory::getDB( DB_SLAVE );
                $res = $dbr->select(
                        array( 'echo_email_batch' ),
                        array( 'eeb_user_id' ),
diff --git a/includes/EchoDbFactory.php b/includes/EchoDbFactory.php
new file mode 100644
index 0000000..eee62cf
--- /dev/null
+++ b/includes/EchoDbFactory.php
@@ -0,0 +1,56 @@
+<?php
+
+/**
+ * Database factory class, this will determine whether to use the main database
+ * or an external database defined in configuration file
+ */
+class MWEchoDbFactory {
+
+       /**
+        * Database loadbalancer
+        */
+       private static $lb;
+
+       /**
+        * Database cache
+        */
+       private static $cache = array();
+
+       /**
+        * Internal function for getting database loadbalancer, Echo database
+        * can reside on extension1 db
+        *
+        * @param $wiki string The wiki ID, or false for the current wiki
+        */
+       private static function initLB( $wiki ) {
+               if ( self::$lb === null ) {
+                       global $wgEchoCluster;
+
+                       // Use the external db defined for Echo
+                       if ( $wgEchoCluster ) {
+                               self::$lb = wfGetLBFactory()->getExternalLB( 
$wgEchoCluster );
+                       } else {
+                               self::$lb = wfGetLB( $wiki );
+                       }
+               }
+       }
+
+       /**
+        * Wrapper function for wfGetDB
+        *
+        * @param $db int Index of the connection to get
+        * @param $groups mixed Query groups.
+        * @param $wiki string The wiki ID, or false for the current wiki
+        * @return DatabaseBase
+        */
+       public static function getDB( $db, $groups = array(), $wiki = false ) {
+               if ( !isset( self::$cache[$db] ) ) {
+                       self::initLB( $wiki );
+
+                       self::$cache[$db] = self::$lb->getConnection( $db, 
$groups, $wiki );
+               }
+
+               return self::$cache[$db];
+       }
+
+}
diff --git a/maintenance/removeInvalidNotification.php 
b/maintenance/removeInvalidNotification.php
index b16e9c9..43ec701 100644
--- a/maintenance/removeInvalidNotification.php
+++ b/maintenance/removeInvalidNotification.php
@@ -24,8 +24,8 @@
                        return;
                }
 
-               $dbw = wfGetDB( DB_MASTER );
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbw = MWEchoDbFactory::getDB( DB_MASTER );
+               $dbr = MWEchoDbFactory::getDB( DB_SLAVE );
 
                $count = $this->batchSize;
 
diff --git a/processEchoEmailBatch.php b/processEchoEmailBatch.php
index fdeb1aa..ba7701e 100644
--- a/processEchoEmailBatch.php
+++ b/processEchoEmailBatch.php
@@ -28,7 +28,7 @@
        }
 
        protected function init() {
-               $this->dbr = wfGetDB( DB_SLAVE );
+               $this->dbr = MWEchoDbFactory::getDB( DB_SLAVE );
        }
 
        public function execute() {

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I84b0d904795d858d88d8e52c22f00d81c0e81303
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Bsitu <[email protected]>
Gerrit-Reviewer: Kaldari <[email protected]>
Gerrit-Reviewer: Lwelling <[email protected]>
Gerrit-Reviewer: Matthias Mullie <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to