Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
47.27% |
26 / 55 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| NameTableStoreFactory | |
47.27% |
26 / 55 |
|
66.67% |
4 / 6 |
28.74 | |
0.00% |
0 / 1 |
| getTableInfo | |
6.67% |
2 / 30 |
|
0.00% |
0 / 1 |
5.25 | |||
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
95.24% |
20 / 21 |
|
0.00% |
0 / 1 |
5 | |||
| getChangeTagDef | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getContentModels | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSlotRoles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Storage; |
| 8 | |
| 9 | use InvalidArgumentException; |
| 10 | use Psr\Log\LoggerInterface; |
| 11 | use Wikimedia\ObjectCache\WANObjectCache; |
| 12 | use Wikimedia\Rdbms\ILBFactory; |
| 13 | |
| 14 | class NameTableStoreFactory { |
| 15 | /** @var array<string,mixed> */ |
| 16 | private static $info; |
| 17 | /** @var array<string,array<string,NameTableStore>> */ |
| 18 | private $stores = []; |
| 19 | |
| 20 | private static function getTableInfo(): array { |
| 21 | if ( self::$info ) { |
| 22 | return self::$info; |
| 23 | } |
| 24 | self::$info = [ |
| 25 | 'change_tag_def' => [ |
| 26 | 'idField' => 'ctd_id', |
| 27 | 'nameField' => 'ctd_name', |
| 28 | 'normalizationCallback' => null, |
| 29 | 'insertCallback' => static function ( $insertFields ) { |
| 30 | $insertFields['ctd_user_defined'] = 0; |
| 31 | $insertFields['ctd_count'] = 0; |
| 32 | return $insertFields; |
| 33 | } |
| 34 | ], |
| 35 | |
| 36 | 'content_models' => [ |
| 37 | 'idField' => 'model_id', |
| 38 | 'nameField' => 'model_name', |
| 39 | /** |
| 40 | * No strtolower normalization is added to the service as there are examples of |
| 41 | * extensions that do not stick to this assumption. |
| 42 | * - extensions/examples/DataPages define( 'CONTENT_MODEL_XML_DATA','XML_DATA' ); |
| 43 | * - extensions/Scribunto define( 'CONTENT_MODEL_SCRIBUNTO', 'Scribunto' ); |
| 44 | */ |
| 45 | ], |
| 46 | |
| 47 | 'slot_roles' => [ |
| 48 | 'idField' => 'role_id', |
| 49 | 'nameField' => 'role_name', |
| 50 | 'normalizationCallback' => 'strtolower', |
| 51 | ], |
| 52 | ]; |
| 53 | return self::$info; |
| 54 | } |
| 55 | |
| 56 | public function __construct( |
| 57 | private readonly ILBFactory $lbFactory, |
| 58 | private readonly WANObjectCache $cache, |
| 59 | private readonly LoggerInterface $logger, |
| 60 | ) { |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get a NameTableStore for a specific table |
| 65 | * |
| 66 | * @param string $tableName |
| 67 | * @param string|false $wiki The target wiki ID, or false for the current wiki |
| 68 | * @return NameTableStore |
| 69 | */ |
| 70 | public function get( $tableName, $wiki = false ): NameTableStore { |
| 71 | $infos = self::getTableInfo(); |
| 72 | if ( !isset( $infos[$tableName] ) ) { |
| 73 | throw new InvalidArgumentException( "Invalid table name \$tableName" ); |
| 74 | } |
| 75 | if ( $wiki !== false && $wiki === $this->lbFactory->getLocalDomainID() ) { |
| 76 | $wiki = false; |
| 77 | } |
| 78 | |
| 79 | if ( isset( $this->stores[$tableName][$wiki] ) ) { |
| 80 | return $this->stores[$tableName][$wiki]; |
| 81 | } |
| 82 | |
| 83 | $info = $infos[$tableName]; |
| 84 | $store = new NameTableStore( |
| 85 | $this->lbFactory->getMainLB( $wiki ), |
| 86 | $this->cache, |
| 87 | $this->logger, |
| 88 | $tableName, |
| 89 | $info['idField'], |
| 90 | $info['nameField'], |
| 91 | $info['normalizationCallback'] ?? null, |
| 92 | $wiki, |
| 93 | $info['insertCallback'] ?? null |
| 94 | ); |
| 95 | $this->stores[$tableName][$wiki] = $store; |
| 96 | return $store; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get a NameTableStore for the change_tag_def table |
| 101 | * |
| 102 | * @param string|bool $wiki |
| 103 | * @return NameTableStore |
| 104 | */ |
| 105 | public function getChangeTagDef( $wiki = false ): NameTableStore { |
| 106 | return $this->get( 'change_tag_def', $wiki ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get a NameTableStore for the content_models table |
| 111 | * |
| 112 | * @param string|bool $wiki |
| 113 | * @return NameTableStore |
| 114 | */ |
| 115 | public function getContentModels( $wiki = false ): NameTableStore { |
| 116 | return $this->get( 'content_models', $wiki ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get a NameTableStore for the slot_roles table |
| 121 | * |
| 122 | * @param string|bool $wiki |
| 123 | * @return NameTableStore |
| 124 | */ |
| 125 | public function getSlotRoles( $wiki = false ): NameTableStore { |
| 126 | return $this->get( 'slot_roles', $wiki ); |
| 127 | } |
| 128 | } |