Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
49.12% |
28 / 57 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| NameTableStoreFactory | |
49.12% |
28 / 57 |
|
66.67% |
4 / 6 |
26.94 | |
0.00% |
0 / 1 |
| getTableInfo | |
6.67% |
2 / 30 |
|
0.00% |
0 / 1 |
5.25 | |||
| __construct | |
100.00% |
3 / 3 |
|
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 | /** @var ILBFactory */ |
| 21 | private $lbFactory; |
| 22 | |
| 23 | /** @var WANObjectCache */ |
| 24 | private $cache; |
| 25 | |
| 26 | /** @var LoggerInterface */ |
| 27 | private $logger; |
| 28 | |
| 29 | private static function getTableInfo(): array { |
| 30 | if ( self::$info ) { |
| 31 | return self::$info; |
| 32 | } |
| 33 | self::$info = [ |
| 34 | 'change_tag_def' => [ |
| 35 | 'idField' => 'ctd_id', |
| 36 | 'nameField' => 'ctd_name', |
| 37 | 'normalizationCallback' => null, |
| 38 | 'insertCallback' => static function ( $insertFields ) { |
| 39 | $insertFields['ctd_user_defined'] = 0; |
| 40 | $insertFields['ctd_count'] = 0; |
| 41 | return $insertFields; |
| 42 | } |
| 43 | ], |
| 44 | |
| 45 | 'content_models' => [ |
| 46 | 'idField' => 'model_id', |
| 47 | 'nameField' => 'model_name', |
| 48 | /** |
| 49 | * No strtolower normalization is added to the service as there are examples of |
| 50 | * extensions that do not stick to this assumption. |
| 51 | * - extensions/examples/DataPages define( 'CONTENT_MODEL_XML_DATA','XML_DATA' ); |
| 52 | * - extensions/Scribunto define( 'CONTENT_MODEL_SCRIBUNTO', 'Scribunto' ); |
| 53 | */ |
| 54 | ], |
| 55 | |
| 56 | 'slot_roles' => [ |
| 57 | 'idField' => 'role_id', |
| 58 | 'nameField' => 'role_name', |
| 59 | 'normalizationCallback' => 'strtolower', |
| 60 | ], |
| 61 | ]; |
| 62 | return self::$info; |
| 63 | } |
| 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 | /** |
| 76 | * Get a NameTableStore for a specific table |
| 77 | * |
| 78 | * @param string $tableName |
| 79 | * @param string|false $wiki The target wiki ID, or false for the current wiki |
| 80 | * @return NameTableStore |
| 81 | */ |
| 82 | public function get( $tableName, $wiki = false ): NameTableStore { |
| 83 | $infos = self::getTableInfo(); |
| 84 | if ( !isset( $infos[$tableName] ) ) { |
| 85 | throw new InvalidArgumentException( "Invalid table name \$tableName" ); |
| 86 | } |
| 87 | if ( $wiki !== false && $wiki === $this->lbFactory->getLocalDomainID() ) { |
| 88 | $wiki = false; |
| 89 | } |
| 90 | |
| 91 | if ( isset( $this->stores[$tableName][$wiki] ) ) { |
| 92 | return $this->stores[$tableName][$wiki]; |
| 93 | } |
| 94 | |
| 95 | $info = $infos[$tableName]; |
| 96 | $store = new NameTableStore( |
| 97 | $this->lbFactory->getMainLB( $wiki ), |
| 98 | $this->cache, |
| 99 | $this->logger, |
| 100 | $tableName, |
| 101 | $info['idField'], |
| 102 | $info['nameField'], |
| 103 | $info['normalizationCallback'] ?? null, |
| 104 | $wiki, |
| 105 | $info['insertCallback'] ?? null |
| 106 | ); |
| 107 | $this->stores[$tableName][$wiki] = $store; |
| 108 | return $store; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get a NameTableStore for the change_tag_def table |
| 113 | * |
| 114 | * @param string|bool $wiki |
| 115 | * @return NameTableStore |
| 116 | */ |
| 117 | public function getChangeTagDef( $wiki = false ): NameTableStore { |
| 118 | return $this->get( 'change_tag_def', $wiki ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get a NameTableStore for the content_models table |
| 123 | * |
| 124 | * @param string|bool $wiki |
| 125 | * @return NameTableStore |
| 126 | */ |
| 127 | public function getContentModels( $wiki = false ): NameTableStore { |
| 128 | return $this->get( 'content_models', $wiki ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get a NameTableStore for the slot_roles table |
| 133 | * |
| 134 | * @param string|bool $wiki |
| 135 | * @return NameTableStore |
| 136 | */ |
| 137 | public function getSlotRoles( $wiki = false ): NameTableStore { |
| 138 | return $this->get( 'slot_roles', $wiki ); |
| 139 | } |
| 140 | } |