ForeignDBViaLBRepo.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * A foreign repository with a MediaWiki database accessible via the configured LBFactory.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup FileRepo
  22. */
  23. use MediaWiki\MediaWikiServices;
  24. use Wikimedia\Rdbms\IDatabase;
  25. use Wikimedia\Rdbms\LoadBalancer;
  26. /**
  27. * A foreign repository with a MediaWiki database accessible via the configured LBFactory
  28. *
  29. * @ingroup FileRepo
  30. */
  31. class ForeignDBViaLBRepo extends LocalRepo {
  32. /** @var string */
  33. protected $wiki;
  34. /** @var string */
  35. protected $dbName;
  36. /** @var string */
  37. protected $tablePrefix;
  38. /** @var array */
  39. protected $fileFactory = [ ForeignDBFile::class, 'newFromTitle' ];
  40. /** @var array */
  41. protected $fileFromRowFactory = [ ForeignDBFile::class, 'newFromRow' ];
  42. /** @var bool */
  43. protected $hasSharedCache;
  44. /**
  45. * @param array|null $info
  46. */
  47. function __construct( $info ) {
  48. parent::__construct( $info );
  49. $this->wiki = $info['wiki'];
  50. list( $this->dbName, $this->tablePrefix ) = wfSplitWikiID( $this->wiki );
  51. $this->hasSharedCache = $info['hasSharedCache'];
  52. }
  53. /**
  54. * @return IDatabase
  55. */
  56. function getMasterDB() {
  57. return $this->getDBLoadBalancer()->getConnectionRef( DB_MASTER, [], $this->wiki );
  58. }
  59. /**
  60. * @return IDatabase
  61. */
  62. function getReplicaDB() {
  63. return $this->getDBLoadBalancer()->getConnectionRef( DB_REPLICA, [], $this->wiki );
  64. }
  65. /**
  66. * @return Closure
  67. */
  68. protected function getDBFactory() {
  69. return function ( $index ) {
  70. return $this->getDBLoadBalancer()->getConnectionRef( $index, [], $this->wiki );
  71. };
  72. }
  73. /**
  74. * @return LoadBalancer
  75. */
  76. protected function getDBLoadBalancer() {
  77. $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
  78. return $lbFactory->getMainLB( $this->wiki );
  79. }
  80. function hasSharedCache() {
  81. return $this->hasSharedCache;
  82. }
  83. /**
  84. * Get a key on the primary cache for this repository.
  85. * Returns false if the repository's cache is not accessible at this site.
  86. * The parameters are the parts of the key, as for wfMemcKey().
  87. * @return bool|string
  88. */
  89. function getSharedCacheKey( /*...*/ ) {
  90. if ( $this->hasSharedCache() ) {
  91. $args = func_get_args();
  92. array_unshift( $args, $this->wiki );
  93. return implode( ':', $args );
  94. } else {
  95. return false;
  96. }
  97. }
  98. protected function assertWritableRepo() {
  99. throw new MWException( static::class . ': write operations are not supported.' );
  100. }
  101. public function getInfo() {
  102. return FileRepo::getInfo();
  103. }
  104. }