ForeignDBViaLBRepo.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /**
  24. * A foreign repository with a MediaWiki database accessible via the configured LBFactory
  25. *
  26. * @ingroup FileRepo
  27. */
  28. class ForeignDBViaLBRepo extends LocalRepo {
  29. /** @var string */
  30. protected $wiki;
  31. /** @var string */
  32. protected $dbName;
  33. /** @var string */
  34. protected $tablePrefix;
  35. /** @var array */
  36. protected $fileFactory = [ ForeignDBFile::class, 'newFromTitle' ];
  37. /** @var array */
  38. protected $fileFromRowFactory = [ ForeignDBFile::class, 'newFromRow' ];
  39. /** @var bool */
  40. protected $hasSharedCache;
  41. /**
  42. * @param array|null $info
  43. */
  44. function __construct( $info ) {
  45. parent::__construct( $info );
  46. $this->wiki = $info['wiki'];
  47. list( $this->dbName, $this->tablePrefix ) = wfSplitWikiID( $this->wiki );
  48. $this->hasSharedCache = $info['hasSharedCache'];
  49. }
  50. /**
  51. * @return IDatabase
  52. */
  53. function getMasterDB() {
  54. return wfGetLB( $this->wiki )->getConnectionRef( DB_MASTER, [], $this->wiki );
  55. }
  56. /**
  57. * @return IDatabase
  58. */
  59. function getReplicaDB() {
  60. return wfGetLB( $this->wiki )->getConnectionRef( DB_REPLICA, [], $this->wiki );
  61. }
  62. /**
  63. * @return Closure
  64. */
  65. protected function getDBFactory() {
  66. return function ( $index ) {
  67. return wfGetLB( $this->wiki )->getConnectionRef( $index, [], $this->wiki );
  68. };
  69. }
  70. function hasSharedCache() {
  71. return $this->hasSharedCache;
  72. }
  73. /**
  74. * Get a key on the primary cache for this repository.
  75. * Returns false if the repository's cache is not accessible at this site.
  76. * The parameters are the parts of the key, as for wfMemcKey().
  77. * @return bool|string
  78. */
  79. function getSharedCacheKey( /*...*/ ) {
  80. if ( $this->hasSharedCache() ) {
  81. $args = func_get_args();
  82. array_unshift( $args, $this->wiki );
  83. return implode( ':', $args );
  84. } else {
  85. return false;
  86. }
  87. }
  88. protected function assertWritableRepo() {
  89. throw new MWException( static::class . ': write operations are not supported.' );
  90. }
  91. public function getInfo() {
  92. return FileRepo::getInfo();
  93. }
  94. }