Aaron Schulz has uploaded a new change for review.

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


Change subject: Use self-releasing (for reuse) DB connections
......................................................................

Use self-releasing (for reuse) DB connections

* Also use the correct DB for the Pager classes

Change-Id: Ia2020924822d9548f100383e6b11d3c9fc4884e3
---
M backend/MWOAuthConsumer.php
M backend/MWOAuthConsumerAcceptance.php
M backend/MWOAuthDAO.php
M backend/MWOAuthUtils.php
M control/MWOAuthConsumerAcceptanceSubmitControl.php
M control/MWOAuthConsumerSubmitControl.php
M frontend/specialpages/actions/MWOAuthConsumerRegistration.php
M frontend/specialpages/actions/MWOAuthManageConsumers.php
M frontend/specialpages/actions/MWOAuthManageMyGrants.php
9 files changed, 49 insertions(+), 45 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/OAuth 
refs/changes/89/73989/1

diff --git a/backend/MWOAuthConsumer.php b/backend/MWOAuthConsumer.php
index d255f93..e36b288 100644
--- a/backend/MWOAuthConsumer.php
+++ b/backend/MWOAuthConsumer.php
@@ -105,12 +105,12 @@
        }
 
        /**
-        * @param DatabaseBase $db
+        * @param DBConnRef $db
         * @param string $key
         * @param integer $flags MWOAuthConsumer::READ_* bitfield
         * @return MWOAuthConsumer|bool
         */
-       public static function newFromKey( DatabaseBase $db, $key, $flags = 0 ) 
{
+       public static function newFromKey( DBConnRef $db, $key, $flags = 0 ) {
                $row = $db->selectRow( static::getTable(),
                        array_values( static::getFieldColumnMap() ),
                        array( 'oarc_consumer_key' => $key ),
@@ -128,7 +128,7 @@
        }
 
        /**
-        * @param DatabaseBase $db
+        * @param DBConnRef $db
         * @param string $name
         * @param string $version
         * @param User|integer $user User or user ID
@@ -136,7 +136,7 @@
         * @return MWOAuthConsumer|bool
         */
        public static function newFromNameVersionUser(
-               DatabaseBase $db, $name, $version, $user, $flags = 0
+               DBConnRef $db, $name, $version, $user, $flags = 0
        ) {
                $uid = ( $user instanceof User ) ? $user->getId() : $user;
 
@@ -194,7 +194,7 @@
                $this->deleted = (int)$this->deleted;
        }
 
-       protected function encodeRow( DatabaseBase $db, $row ) {
+       protected function encodeRow( DBConnRef $db, $row ) {
                $row['oarc_registration'] = $db->timestamp( 
$row['oarc_registration'] );
                $row['oarc_stage_timestamp'] = $db->timestamp( 
$row['oarc_stage_timestamp'] );
                $row['oarc_restrictions'] = FormatJSON::encode( 
$row['oarc_restrictions'] );
@@ -204,7 +204,7 @@
                return $row;
        }
 
-       protected function decodeRow( DatabaseBase $db, $row ) {
+       protected function decodeRow( DBConnRef $db, $row ) {
                $row['oarc_registration'] = wfTimestamp( TS_MW, 
$row['oarc_registration'] );
                $row['oarc_stage_timestamp'] = wfTimestamp( TS_MW, 
$row['oarc_stage_timestamp'] );
                $row['oarc_restrictions'] = FormatJSON::decode( 
$row['oarc_restrictions'], true );
diff --git a/backend/MWOAuthConsumerAcceptance.php 
b/backend/MWOAuthConsumerAcceptance.php
index 383fab3..f370ef0 100644
--- a/backend/MWOAuthConsumerAcceptance.php
+++ b/backend/MWOAuthConsumerAcceptance.php
@@ -60,12 +60,12 @@
        }
 
        /**
-        * @param DatabaseBase $db
+        * @param DBConnRef $db
         * @param string $token Access token
         * @param integer $flags MWOAuthConsumerAcceptance::READ_* bitfield
         * @return MWOAuthConsumerAcceptance|bool
         */
-       public static function newFromToken( DatabaseBase $db, $token, $flags = 
0 ) {
+       public static function newFromToken( DBConnRef $db, $token, $flags = 0 
) {
                $row = $db->selectRow( static::getTable(),
                        array_values( static::getFieldColumnMap() ),
                        array( 'oaac_access_token' => $token ),
@@ -88,13 +88,13 @@
                $this->accepted = wfTimestamp( TS_MW, $this->accepted );
        }
 
-       protected function encodeRow( DatabaseBase $db, $row ) {
+       protected function encodeRow( DBConnRef $db, $row ) {
                $row['oaac_grants'] = FormatJSON::encode( $row['oaac_grants'] );
                $row['oaac_accepted'] = $db->timestamp( $row['oaac_accepted'] );
                return $row;
        }
 
-       protected function decodeRow( DatabaseBase $db, $row ) {;
+       protected function decodeRow( DBConnRef $db, $row ) {;
                $row['oaac_grants'] = FormatJSON::decode( $row['oaac_grants'], 
true );
                $row['oaac_accepted'] = wfTimestamp( TS_MW, 
$row['oaac_accepted'] );
                return $row;
diff --git a/backend/MWOAuthDAO.php b/backend/MWOAuthDAO.php
index ebc81a1..7e1298f 100644
--- a/backend/MWOAuthDAO.php
+++ b/backend/MWOAuthDAO.php
@@ -46,24 +46,24 @@
        }
 
        /**
-        * @param DatabaseBase $db
+        * @param DBConnRef $db
         * @param array|stdClass $row
         * @return MWOAuthDAO
         */
-       final public static function newFromRow( DatabaseBase $db, $row ) {
+       final public static function newFromRow( DBConnRef $db, $row ) {
                $consumer = new static();
                $consumer->loadFromRow( $db, $row );
                return $consumer;
        }
 
        /**
-        * @param DatabaseBase $db
+        * @param DBConnRef $db
         * @param integer $id
         * @param integer $flags MWOAuthDAO::READ_* bitfield
         * @return MWOAuthDAO|bool Returns false if not found
         * @throws DBError
         */
-       final public static function newFromId( DatabaseBase $db, $id, $flags = 
0 ) {
+       final public static function newFromId( DBConnRef $db, $id, $flags = 0 
) {
                $row = $db->selectRow( static::getTable(),
                        array_values( static::getFieldColumnMap() ),
                        array( static::getIdColumn() => $id ),
@@ -137,11 +137,11 @@
        }
 
        /**
-        * @param DatabaseBase $dbw
+        * @param DBConnRef $dbw
         * @return bool
         * @throws DBError
         */
-       public function save( DatabaseBase $dbw ) {
+       public function save( DBConnRef $dbw ) {
                $uniqueId = $this->getIdValue();
                $idColumn = static::getIdColumn();
                if ( $this->daoOrigin === 'db' ) {
@@ -176,11 +176,11 @@
        }
 
        /**
-        * @param DatabaseBase $dbw
+        * @param DBConnRef $dbw
         * @return bool
         * @throws DBError
         */
-       public function delete( DatabaseBase $dbw ) {
+       public function delete( DBConnRef $dbw ) {
                $uniqueId = $this->getIdValue();
                $idColumn = static::getIdColumn();
                if ( $this->daoOrigin === 'db' ) {
@@ -334,11 +334,11 @@
        abstract protected function normalizeValues();
 
        /**
-        * @param DatabaseBase $db
+        * @param DBConnRef $db
         * @param stdClass|array $row
         * @return void
         */
-       final protected function loadFromRow( DatabaseBase $db, $row ) {
+       final protected function loadFromRow( DBConnRef $db, $row ) {
                $row = $this->decodeRow( $db, (array)$row );
                $values = array();
                foreach ( static::getFieldColumnMap() as $field => $column ) {
@@ -353,28 +353,28 @@
         * Subclasses should make this to encode DB fields (e.g. timestamps).
         * This must also flatten any PHP data structures into flat values.
         *
-        * @param DatabaseBase $db
+        * @param DBConnRef $db
         * @param array $row
         * @return array
         */
-       abstract protected function encodeRow( DatabaseBase $db, $row );
+       abstract protected function encodeRow( DBConnRef $db, $row );
 
        /**
         * Subclasses should make this to decode DB fields (e.g. timestamps).
         * This can also expand some flat values (e.g. JSON) into PHP data 
structures.
         * Note: this does not need to handle what normalizeValues() already 
does.
         *
-        * @param DatabaseBase $db
+        * @param DBConnRef $db
         * @param array $row
         * @return array
         */
-       abstract protected function decodeRow( DatabaseBase $db, $row );
+       abstract protected function decodeRow( DBConnRef $db, $row );
 
        /**
-        * @param DatabaseBase $db
+        * @param DBConnRef $db
         * @return array
         */
-       final protected function getRowArray( DatabaseBase $db ) {
+       final protected function getRowArray( DBConnRef $db ) {
                $row = array();
                foreach ( static::getFieldColumnMap() as $field => $column ) {
                        $row[$column] = $this->$field;
diff --git a/backend/MWOAuthUtils.php b/backend/MWOAuthUtils.php
index ef8e3d5..9c8c366 100644
--- a/backend/MWOAuthUtils.php
+++ b/backend/MWOAuthUtils.php
@@ -16,20 +16,21 @@
        }
 
        /**
-        * @TODO: reference count/release DB
         * @param integer $index DB_MASTER/DB_SLAVE
+        * @return DBConnRef
         */
        public static function getCentralDB( $index ) {
                global $wgMWOAuthCentralWiki;
 
-               return wfGetDB( $index, array(), $wgMWOAuthCentralWiki );
+               return wfGetLB( $wgMWOAuthCentralWiki )->getConnectionRef(
+                       $index, array(), $wgMWOAuthCentralWiki );
        }
 
        /**
-        * @param DatabaseBase $db
+        * @param DBConnRef $db
         * @return array
         */
-       public static function getConsumerStateCounts( DatabaseBase $db ) {
+       public static function getConsumerStateCounts( DBConnRef $db ) {
                $res = $db->select( 'oauth_registered_consumer',
                        array( 'oarc_stage', 'count' => 'COUNT(*)' ),
                        array(),
@@ -98,10 +99,10 @@
 
 
        /**
-        * @param DatabaseBase $dbw
+        * @param DBConnRef $dbw
         * @return void
         */
-       public static function runAutoMaintenance( DatabaseBase $dbw ) {
+       public static function runAutoMaintenance( DBConnRef $dbw ) {
                global $wgMWOAuthRequestExpirationAge;
 
                if ( $wgMWOAuthRequestExpirationAge <= 0 ) {
diff --git a/control/MWOAuthConsumerAcceptanceSubmitControl.php 
b/control/MWOAuthConsumerAcceptanceSubmitControl.php
index ab19ccc..435acd9 100644
--- a/control/MWOAuthConsumerAcceptanceSubmitControl.php
+++ b/control/MWOAuthConsumerAcceptanceSubmitControl.php
@@ -135,11 +135,11 @@
        }
 
        /**
-        * @param DatabaseBase $db
+        * @param DBConnRef $db
         * @param int $userId
         * @return Title
         */
-       protected function getLogTitle( DatabaseBase $db, $userId ) {
+       protected function getLogTitle( DBConnRef $db, $userId ) {
                $name = $db->selectField( 'user', 'user_name', array( 'user_id' 
=> $userId ) );
                return Title::makeTitleSafe( NS_USER, $name );
        }
diff --git a/control/MWOAuthConsumerSubmitControl.php 
b/control/MWOAuthConsumerSubmitControl.php
index 379e758..3cf6365 100644
--- a/control/MWOAuthConsumerSubmitControl.php
+++ b/control/MWOAuthConsumerSubmitControl.php
@@ -334,11 +334,11 @@
        }
 
        /**
-        * @param DatabaseBase $db
+        * @param DBConnRef $db
         * @param int $userId
         * @return Title
         */
-       protected function getLogTitle( DatabaseBase $db, $userId ) {
+       protected function getLogTitle( DBConnRef $db, $userId ) {
                $name = $db->selectField( 'user', 'user_name', array( 'user_id' 
=> $userId ) );
                return Title::makeTitleSafe( NS_USER, $name );
        }
diff --git a/frontend/specialpages/actions/MWOAuthConsumerRegistration.php 
b/frontend/specialpages/actions/MWOAuthConsumerRegistration.php
index b7c75fc..a9eae76 100644
--- a/frontend/specialpages/actions/MWOAuthConsumerRegistration.php
+++ b/frontend/specialpages/actions/MWOAuthConsumerRegistration.php
@@ -307,11 +307,11 @@
        }
 
        /**
-        * @param DatabaseBase $db
+        * @param DBConnRef $db
         * @param sdtclass $row
         * @return string
         */
-       public function formatRow( $db, $row ) {
+       public function formatRow( DBConnRef $db, $row ) {
                $cmr = MWOAuthDAOAccessControl::wrap(
                        MWOAuthConsumer::newFromRow( $db, $row ), 
$this->getContext() );
 
@@ -385,13 +385,14 @@
        function __construct( $form, $conds, $user ) {
                $this->mForm = $form;
                $this->mConds = $conds;
-
                $this->mConds['oarc_user_id'] = $user instanceof $user ? 
$user->getId() : $user;
                if ( !$this->getUser()->isAllowed( 'mwoauthviewsuppressed' ) ) {
                        $this->mConds['oarc_deleted'] = 0;
                }
 
+               $this->mDB = MWOAuthUtils::getCentralDB( DB_SLAVE );
                parent::__construct();
+
                # Treat 20 as the default limit, since each entry takes up 5 
rows.
                $urlLimit = $this->mRequest->getInt( 'limit' );
                $this->mLimit = $urlLimit ? $urlLimit : 20;
diff --git a/frontend/specialpages/actions/MWOAuthManageConsumers.php 
b/frontend/specialpages/actions/MWOAuthManageConsumers.php
index 249d12f..a4d0dbb 100644
--- a/frontend/specialpages/actions/MWOAuthManageConsumers.php
+++ b/frontend/specialpages/actions/MWOAuthManageConsumers.php
@@ -399,11 +399,11 @@
        }
 
        /**
-        * @param DatabaseBase $db
+        * @param DBConnRef $db
         * @param sdtclass $row
         * @return string
         */
-       public function formatRow( $db, $row ) {
+       public function formatRow( DBConnRef $db, $row ) {
                global $wgMemc;
 
                static $stageActionMap = array(
@@ -499,13 +499,14 @@
        function __construct( $form, $conds, $stage ) {
                $this->mForm = $form;
                $this->mConds = $conds;
-
                $this->mConds['oarc_stage'] = $stage;
                if ( !$this->getUser()->isAllowed( 'mwoauthviewsuppressed' ) ) {
                        $this->mConds['oarc_deleted'] = 0;
                }
 
+               $this->mDB = MWOAuthUtils::getCentralDB( DB_SLAVE );
                parent::__construct();
+
                # Treat 20 as the default limit, since each entry takes up 5 
rows.
                $urlLimit = $this->mRequest->getInt( 'limit' );
                $this->mLimit = $urlLimit ? $urlLimit : 20;
diff --git a/frontend/specialpages/actions/MWOAuthManageMyGrants.php 
b/frontend/specialpages/actions/MWOAuthManageMyGrants.php
index 8c6387a..21f8b93 100644
--- a/frontend/specialpages/actions/MWOAuthManageMyGrants.php
+++ b/frontend/specialpages/actions/MWOAuthManageMyGrants.php
@@ -231,11 +231,11 @@
        }
 
        /**
-        * @param DatabaseBase $db
+        * @param DBConnRef $db
         * @param sdtclass $row
         * @return string
         */
-       public function formatRow( $db, $row ) {
+       public function formatRow( DBConnRef $db, $row ) {
                $cmr = MWOAuthDAOAccessControl::wrap(
                        MWOAuthConsumer::newFromRow( $db, $row ), 
$this->getContext() );
                $cmra = MWOAuthDAOAccessControl::wrap(
@@ -299,14 +299,15 @@
        function __construct( $form, $conds, $user ) {
                $this->mForm = $form;
                $this->mConds = $conds;
-
                $this->mConds[] = 'oaac_consumer_id = oarc_id';
                $this->mConds['oaac_user_id'] = $user;
                if ( !$this->getUser()->isAllowed( 'mwoauthviewsuppressed' ) ) {
                        $this->mConds['oarc_deleted'] = 0;
                }
 
+               $this->mDB = MWOAuthUtils::getCentralDB( DB_SLAVE );
                parent::__construct();
+
                # Treat 20 as the default limit, since each entry takes up 5 
rows.
                $urlLimit = $this->mRequest->getInt( 'limit' );
                $this->mLimit = $urlLimit ? $urlLimit : 20;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia2020924822d9548f100383e6b11d3c9fc4884e3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/OAuth
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <[email protected]>

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

Reply via email to