| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Storage; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use Psr\Log\LoggerInterface; |
| 25 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 26 |
use Wikimedia\Rdbms\ILBFactory; |
| 27 |
|
| 28 |
class NameTableStoreFactory { |
| 29 |
|
| 30 |
private static $info; |
| 31 |
|
| 32 |
private $stores = []; |
| 33 |
|
| 34 |
|
| 35 |
private $lbFactory; |
| 36 |
|
| 37 |
|
| 38 |
private $cache; |
| 39 |
|
| 40 |
|
| 41 |
private $logger; |
| 42 |
|
| 43 |
private static function getTableInfo() { |
| 44 |
if ( self::$info ) { |
| 45 |
return self::$info; |
| 46 |
} |
| 47 |
self::$info = [ |
| 48 |
'change_tag_def' => [ |
| 49 |
'idField' => 'ctd_id', |
| 50 |
'nameField' => 'ctd_name', |
| 51 |
'normalizationCallback' => null, |
| 52 |
'insertCallback' => static function ( $insertFields ) { |
| 53 |
$insertFields['ctd_user_defined'] = 0; |
| 54 |
$insertFields['ctd_count'] = 0; |
| 55 |
return $insertFields; |
| 56 |
} |
| 57 |
], |
| 58 |
|
| 59 |
'content_models' => [ |
| 60 |
'idField' => 'model_id', |
| 61 |
'nameField' => 'model_name', |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
], |
| 69 |
|
| 70 |
'slot_roles' => [ |
| 71 |
'idField' => 'role_id', |
| 72 |
'nameField' => 'role_name', |
| 73 |
'normalizationCallback' => 'strtolower', |
| 74 |
], |
| 75 |
]; |
| 76 |
return self::$info; |
| 77 |
} |
| 78 |
|
| 79 |
public function __construct( |
| 80 |
ILBFactory $lbFactory, |
| 81 |
WANObjectCache $cache, |
| 82 |
LoggerInterface $logger |
| 83 |
) { |
| 84 |
$this->lbFactory = $lbFactory; |
| 85 |
$this->cache = $cache; |
| 86 |
$this->logger = $logger; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function get( $tableName, $wiki = false ): NameTableStore { |
| 97 |
$infos = self::getTableInfo(); |
| 98 |
if ( !isset( $infos[$tableName] ) ) { |
| 99 |
throw new InvalidArgumentException( "Invalid table name \$tableName" ); |
| 100 |
} |
| 101 |
if ( $wiki !== false && $wiki === $this->lbFactory->getLocalDomainID() ) { |
| 102 |
$wiki = false; |
| 103 |
} |
| 104 |
|
| 105 |
if ( isset( $this->stores[$tableName][$wiki] ) ) { |
| 106 |
return $this->stores[$tableName][$wiki]; |
| 107 |
} |
| 108 |
|
| 109 |
$info = $infos[$tableName]; |
| 110 |
$store = new NameTableStore( |
| 111 |
$this->lbFactory->getMainLB( $wiki ), |
| 112 |
$this->cache, |
| 113 |
$this->logger, |
| 114 |
$tableName, |
| 115 |
$info['idField'], |
| 116 |
$info['nameField'], |
| 117 |
$info['normalizationCallback'] ?? null, |
| 118 |
$wiki, |
| 119 |
$info['insertCallback'] ?? null |
| 120 |
); |
| 121 |
$this->stores[$tableName][$wiki] = $store; |
| 122 |
return $store; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function getChangeTagDef( $wiki = false ): NameTableStore { |
| 132 |
return $this->get( 'change_tag_def', $wiki ); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function getContentModels( $wiki = false ): NameTableStore { |
| 142 |
return $this->get( 'content_models', $wiki ); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public function getSlotRoles( $wiki = false ): NameTableStore { |
| 152 |
return $this->get( 'slot_roles', $wiki ); |
| 153 |
} |
| 154 |
} |