Bsitu has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/58428


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, 66 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Echo 
refs/changes/28/58428/1

diff --git a/Echo.php b/Echo.php
index b3c68ca..db7dfed 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';
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..c9993d8
--- /dev/null
+++ b/includes/EchoDbFactory.php
@@ -0,0 +1,54 @@
+<?php
+
+/**
+ * Database factory class
+ */
+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 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
+        */
+       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: newchange
Gerrit-Change-Id: I84b0d904795d858d88d8e52c22f00d81c0e81303
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Bsitu <[email protected]>

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

Reply via email to