DBAccessBase.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. use MediaWiki\MediaWikiServices;
  3. use Wikimedia\Rdbms\IDatabase;
  4. use Wikimedia\Rdbms\ILoadBalancer;
  5. /**
  6. * Base class for objects that allow access to other wiki's databases using
  7. * the foreign database access mechanism implemented by LBFactoryMulti.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @since 1.21
  25. *
  26. * @file
  27. * @ingroup Database
  28. *
  29. * @license GPL-2.0-or-later
  30. * @author Daniel Kinzler
  31. */
  32. abstract class DBAccessBase implements IDBAccessObject {
  33. /** @var ILoadBalancer */
  34. private $lb;
  35. /** @var string|bool The target wiki's DB domain */
  36. protected $dbDomain = false;
  37. /**
  38. * @param string|bool $dbDomain The target wiki's DB domain
  39. */
  40. public function __construct( $dbDomain = false ) {
  41. $this->dbDomain = $dbDomain;
  42. $this->lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()
  43. ->getMainLB( $dbDomain );
  44. }
  45. /**
  46. * Returns a database connection.
  47. *
  48. * @see LoadBalancer::getConnection()
  49. *
  50. * @since 1.21
  51. *
  52. * @param int $id Which connection to use
  53. * @param array $groups Query groups
  54. *
  55. * @return IDatabase
  56. */
  57. protected function getConnection( $id, array $groups = [] ) {
  58. return $this->getLoadBalancer()->getConnectionRef( $id, $groups, $this->dbDomain );
  59. }
  60. /**
  61. * Releases a database connection and makes it available for recycling.
  62. *
  63. * @see LoadBalancer::reuseConnection()
  64. *
  65. * @since 1.21
  66. *
  67. * @param IDatabase $db The database connection to release.
  68. * @deprecated Since 1.34
  69. */
  70. protected function releaseConnection( IDatabase $db ) {
  71. // no-op
  72. }
  73. /**
  74. * Get the database type used for read operations.
  75. *
  76. * @see MediaWikiServices::getInstance()->getDBLoadBalancer
  77. *
  78. * @since 1.21
  79. *
  80. * @return ILoadBalancer The database load balancer object
  81. */
  82. protected function getLoadBalancer() {
  83. return $this->lb;
  84. }
  85. }