ForeignDBRepo.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * A foreign repository with an accessible MediaWiki database.
  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 Wikimedia\Rdbms\Database;
  24. use Wikimedia\Rdbms\IDatabase;
  25. /**
  26. * A foreign repository with an accessible MediaWiki database
  27. *
  28. * @ingroup FileRepo
  29. */
  30. class ForeignDBRepo extends LocalRepo {
  31. /** @var string */
  32. protected $dbType;
  33. /** @var string */
  34. protected $dbServer;
  35. /** @var string */
  36. protected $dbUser;
  37. /** @var string */
  38. protected $dbPassword;
  39. /** @var string */
  40. protected $dbName;
  41. /** @var string */
  42. protected $dbFlags;
  43. /** @var string */
  44. protected $tablePrefix;
  45. /** @var bool */
  46. protected $hasSharedCache;
  47. /** @var IDatabase */
  48. protected $dbConn;
  49. /** @var callable */
  50. protected $fileFactory = [ ForeignDBFile::class, 'newFromTitle' ];
  51. /** @var callable */
  52. protected $fileFromRowFactory = [ ForeignDBFile::class, 'newFromRow' ];
  53. /**
  54. * @param array|null $info
  55. */
  56. function __construct( $info ) {
  57. parent::__construct( $info );
  58. $this->dbType = $info['dbType'];
  59. $this->dbServer = $info['dbServer'];
  60. $this->dbUser = $info['dbUser'];
  61. $this->dbPassword = $info['dbPassword'];
  62. $this->dbName = $info['dbName'];
  63. $this->dbFlags = $info['dbFlags'];
  64. $this->tablePrefix = $info['tablePrefix'];
  65. $this->hasSharedCache = $info['hasSharedCache'];
  66. }
  67. /**
  68. * @return IDatabase
  69. */
  70. function getMasterDB() {
  71. if ( !isset( $this->dbConn ) ) {
  72. $func = $this->getDBFactory();
  73. $this->dbConn = $func( DB_MASTER );
  74. }
  75. return $this->dbConn;
  76. }
  77. /**
  78. * @return IDatabase
  79. */
  80. function getReplicaDB() {
  81. return $this->getMasterDB();
  82. }
  83. /**
  84. * @return Closure
  85. */
  86. protected function getDBFactory() {
  87. $type = $this->dbType;
  88. $params = [
  89. 'host' => $this->dbServer,
  90. 'user' => $this->dbUser,
  91. 'password' => $this->dbPassword,
  92. 'dbname' => $this->dbName,
  93. 'flags' => $this->dbFlags,
  94. 'tablePrefix' => $this->tablePrefix,
  95. 'foreign' => true,
  96. ];
  97. return function ( $index ) use ( $type, $params ) {
  98. return Database::factory( $type, $params );
  99. };
  100. }
  101. /**
  102. * @return bool
  103. */
  104. function hasSharedCache() {
  105. return $this->hasSharedCache;
  106. }
  107. /**
  108. * Get a key on the primary cache for this repository.
  109. * Returns false if the repository's cache is not accessible at this site.
  110. * The parameters are the parts of the key, as for wfMemcKey().
  111. * @return bool|mixed
  112. */
  113. function getSharedCacheKey( /*...*/ ) {
  114. if ( $this->hasSharedCache() ) {
  115. $args = func_get_args();
  116. array_unshift( $args, $this->dbName, $this->tablePrefix );
  117. return call_user_func_array( 'wfForeignMemcKey', $args );
  118. } else {
  119. return false;
  120. }
  121. }
  122. protected function assertWritableRepo() {
  123. throw new MWException( static::class . ': write operations are not supported.' );
  124. }
  125. /**
  126. * Return information about the repository.
  127. *
  128. * @return array
  129. * @since 1.22
  130. */
  131. function getInfo() {
  132. return FileRepo::getInfo();
  133. }
  134. }