NameTableStoreFactory.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. * @file
  18. */
  19. namespace MediaWiki\Storage;
  20. use Wikimedia\Rdbms\ILBFactory;
  21. use WANObjectCache;
  22. use Psr\Log\LoggerInterface;
  23. class NameTableStoreFactory {
  24. private static $info;
  25. private $stores = [];
  26. /** @var ILBFactory */
  27. private $lbFactory;
  28. /** @var WANObjectCache */
  29. private $cache;
  30. /** @var LoggerInterface */
  31. private $logger;
  32. private static function getTableInfo() {
  33. if ( self::$info ) {
  34. return self::$info;
  35. }
  36. self::$info = [
  37. 'change_tag_def' => [
  38. 'idField' => 'ctd_id',
  39. 'nameField' => 'ctd_name',
  40. 'normalizationCallback' => null,
  41. 'insertCallback' => function ( $insertFields ) {
  42. $insertFields['ctd_user_defined'] = 0;
  43. $insertFields['ctd_count'] = 0;
  44. return $insertFields;
  45. }
  46. ],
  47. 'content_models' => [
  48. 'idField' => 'model_id',
  49. 'nameField' => 'model_name',
  50. /**
  51. * No strtolower normalization is added to the service as there are examples of
  52. * extensions that do not stick to this assumption.
  53. * - extensions/examples/DataPages define( 'CONTENT_MODEL_XML_DATA','XML_DATA' );
  54. * - extensions/Scribunto define( 'CONTENT_MODEL_SCRIBUNTO', 'Scribunto' );
  55. */
  56. ],
  57. 'slot_roles' => [
  58. 'idField' => 'role_id',
  59. 'nameField' => 'role_name',
  60. 'normalizationCallback' => 'strtolower',
  61. ],
  62. ];
  63. return self::$info;
  64. }
  65. public function __construct(
  66. ILBFactory $lbFactory,
  67. WANObjectCache $cache,
  68. LoggerInterface $logger
  69. ) {
  70. $this->lbFactory = $lbFactory;
  71. $this->cache = $cache;
  72. $this->logger = $logger;
  73. }
  74. /**
  75. * Get a NameTableStore for a specific table
  76. *
  77. * @param string $tableName The table name
  78. * @param string|false $wiki The target wiki ID, or false for the current wiki
  79. * @return NameTableStore
  80. */
  81. public function get( $tableName, $wiki = false ) : NameTableStore {
  82. $infos = self::getTableInfo();
  83. if ( !isset( $infos[$tableName] ) ) {
  84. throw new \InvalidArgumentException( "Invalid table name \$tableName" );
  85. }
  86. if ( $wiki === $this->lbFactory->getLocalDomainID() ) {
  87. $wiki = false;
  88. }
  89. if ( isset( $this->stores[$tableName][$wiki] ) ) {
  90. return $this->stores[$tableName][$wiki];
  91. }
  92. $info = $infos[$tableName];
  93. $store = new NameTableStore(
  94. $this->lbFactory->getMainLB( $wiki ),
  95. $this->cache,
  96. $this->logger,
  97. $tableName,
  98. $info['idField'],
  99. $info['nameField'],
  100. $info['normalizationCallback'] ?? null,
  101. $wiki,
  102. $info['insertCallback'] ?? null
  103. );
  104. $this->stores[$tableName][$wiki] = $store;
  105. return $store;
  106. }
  107. /**
  108. * Get a NameTableStore for the change_tag_def table
  109. *
  110. * @param string|bool $wiki
  111. * @return NameTableStore
  112. */
  113. public function getChangeTagDef( $wiki = false ) : NameTableStore {
  114. return $this->get( 'change_tag_def', $wiki );
  115. }
  116. /**
  117. * Get a NameTableStore for the content_models table
  118. *
  119. * @param string|bool $wiki
  120. * @return NameTableStore
  121. */
  122. public function getContentModels( $wiki = false ) : NameTableStore {
  123. return $this->get( 'content_models', $wiki );
  124. }
  125. /**
  126. * Get a NameTableStore for the slot_roles table
  127. *
  128. * @param string|bool $wiki
  129. * @return NameTableStore
  130. */
  131. public function getSlotRoles( $wiki = false ) : NameTableStore {
  132. return $this->get( 'slot_roles', $wiki );
  133. }
  134. }