jenkins-bot has submitted this change and it was merged.
Change subject: Clean up database switching
......................................................................
Clean up database switching
Seems like there was unused complexity around database handling.
Bug: T92000
Bug: T91763
Change-Id: I983cb3b5c882bb4884c76a94d4ebba39763fd299
---
M api/ApiCentralNoticeBannerChoiceData.php
M includes/BannerChoiceDataProvider.php
M includes/BannerMessageGroup.php
M includes/CNBannerChoiceDataResourceLoaderModule.php
M includes/CNDatabase.php
5 files changed, 31 insertions(+), 60 deletions(-)
Approvals:
Awight: Looks good to me, approved
jenkins-bot: Verified
diff --git a/api/ApiCentralNoticeBannerChoiceData.php
b/api/ApiCentralNoticeBannerChoiceData.php
index b73d1c5..261b9bf 100644
--- a/api/ApiCentralNoticeBannerChoiceData.php
+++ b/api/ApiCentralNoticeBannerChoiceData.php
@@ -29,8 +29,7 @@
parent::LANG_FILTER
);
- $choicesProvider = new BannerChoiceDataProvider(
- $project, $lang,
BannerChoiceDataProvider::USE_DEFAULT_DB );
+ $choicesProvider = new BannerChoiceDataProvider( $project,
$lang );
$choices = $choicesProvider->getChoices();
diff --git a/includes/BannerChoiceDataProvider.php
b/includes/BannerChoiceDataProvider.php
index 9ec10d1..e71d451 100644
--- a/includes/BannerChoiceDataProvider.php
+++ b/includes/BannerChoiceDataProvider.php
@@ -6,31 +6,17 @@
*/
class BannerChoiceDataProvider {
- /**
- * Query the default DB.
- */
- const USE_DEFAULT_DB = 0;
-
- /**
- * Query the infrastructure DB using the wiki ID in
- * $wgCentralDBname
- */
- const USE_INFRASTRUCTURE_DB = 1;
-
protected $project;
protected $language;
- protected $whichDb;
/**
* @param string $project The project to get choices for
* @param string $language The language to get choices for
*/
- public function __construct( $project, $language,
- $whichDb=self::USE_DEFAULT_DB ) {
+ public function __construct( $project, $language ) {
$this->project = $project;
$this->language = $language;
- $this->whichDb = $whichDb;
}
/**
@@ -43,27 +29,10 @@
* are provided.
*/
public function getChoices() {
- global $wgCentralDBname;
-
// For speed, we'll do our own queries instead of using methods
in
// Campaign and Banner.
- switch ( $this->whichDb ) {
- case self::USE_DEFAULT_DB:
- $wikiId = false;
- break;
-
- case self::USE_INFRASTRUCTURE_DB:
- $wikiId = $wgCentralDBname;
- break;
-
- default:
- throw new MWException( $this->whichDb . 'is not
a valid constant '
- . 'for selecting a DB for
BannerChoiceDataProvider.' );
- }
-
- // Note: CNDatabase can't guarantee that we get the slave
connection
- $dbr = wfGetDB( DB_SLAVE, array(), $wikiId );
+ $dbr = CNDatabase::getDb( DB_SLAVE );
// Set up conditions
$quotedNow = $dbr->addQuotes( $dbr->timestamp() );
diff --git a/includes/BannerMessageGroup.php b/includes/BannerMessageGroup.php
index f4dd166..6266fdc 100644
--- a/includes/BannerMessageGroup.php
+++ b/includes/BannerMessageGroup.php
@@ -223,7 +223,8 @@
* @return bool
*/
public static function registerGroupHook( &$list ) {
- $dbr = CNDatabase::getDb( DB_MASTER ); // Must be explicitly
master for runs under a jobqueue
+ // Must be explicitly master for runs under a jobqueue
+ $dbr = CNDatabase::getDb( DB_MASTER );
// Create the base aggregate group
$conf = array();
diff --git a/includes/CNBannerChoiceDataResourceLoaderModule.php
b/includes/CNBannerChoiceDataResourceLoaderModule.php
index d32906f..dc11128 100644
--- a/includes/CNBannerChoiceDataResourceLoaderModule.php
+++ b/includes/CNBannerChoiceDataResourceLoaderModule.php
@@ -62,17 +62,14 @@
}
/**
- * Get the banner choices data via a direct DB call using
- * $wgCentralDBname.
+ * Get the banner choices data via a direct DB call to the
infrastructure wiki
*
* @param string $project
* @param string $language
*/
protected function getFromDb( $project, $language ) {
- $choicesProvider = new BannerChoiceDataProvider(
- $project, $language,
- BannerChoiceDataProvider::USE_INFRASTRUCTURE_DB );
+ $choicesProvider = new BannerChoiceDataProvider( $project,
$language );
return $choicesProvider->getChoices();
}
diff --git a/includes/CNDatabase.php b/includes/CNDatabase.php
index 0b8292d..17b5b7f 100644
--- a/includes/CNDatabase.php
+++ b/includes/CNDatabase.php
@@ -1,37 +1,42 @@
<?php
/**
- * Utility functions for CentralNotice that don't belong elsewhere
+ * Fetches the CentralNotice infrastructure database.
*/
class CNDatabase {
/**
* Gets a database object. Will be the DB_MASTER if the user has the
* centralnotice-admin right. NOTE: $force is ignored for such users.
*
- * @param int|bool $force If false will return a DB master/slave
based
- * on users permissions. Set to DB_MASTER or
- * DB_SLAVE to force that type for users
that
- * don't have the centralnotice-admin right.
+ * We will either connect to the primary database, or a separate
CentralNotice
+ * infrastructure DB specified by $wgCentralDBname. This is metawiki
for
+ * WMF sister projects. Note that the infrastructure DB does not
support
+ * table prefixes if running in multi-database mode.
*
- * @param string|bool $wiki Wiki database to connect to, if false
will be
- * the infrastructure DB.
+ * @param int $target Set to DB_MASTER or DB_SLAVE to force a connection
+ * to that database. If no parameter is given, this will defaults to
+ * master for CentralNotice admin users, and the slave connection
+ * otherwise.
*
* @return DatabaseBase
*/
- public static function getDb( $force = false, $wiki = false ) {
- global $wgCentralDBname;
- global $wgUser;
+ public static function getDb( $target = null ) {
+ global $wgCentralDBname,
+ $wgDBname,
+ $wgUser;
- if ( $wgUser->isAllowed( 'centralnotice-admin' ) ) {
- $dbmode = DB_MASTER;
- } elseif ( $force === false ) {
- $dbmode = DB_SLAVE;
- } else {
- $dbmode = $force;
+ if ( $target === null ) {
+ if ( $wgUser->isAllowed( 'centralnotice-admin' ) ) {
+ $target = DB_SLAVE;
+ } else {
+ $target = DB_MASTER;
+ }
}
- $db = ( $wiki === false ) ? $wgCentralDBname : $wiki;
-
- return wfGetDB( $dbmode, array(), $db );
+ if ( $wgCentralDBname === false || $wgCentralDBname ===
$wgDBname ) {
+ return wfGetDB( $target );
+ } else {
+ return wfGetDB( $target, array(), $wgCentralDBname );
+ }
}
}
--
To view, visit https://gerrit.wikimedia.org/r/181238
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I983cb3b5c882bb4884c76a94d4ebba39763fd299
Gerrit-PatchSet: 12
Gerrit-Project: mediawiki/extensions/CentralNotice
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>
Gerrit-Reviewer: AndyRussG <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: Katie Horn <[email protected]>
Gerrit-Reviewer: Mwalker <[email protected]>
Gerrit-Reviewer: Ssmith <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits