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.
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.
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
20 namespace MediaWiki\Storage
;
22 use Wikimedia\Rdbms\ILBFactory
;
24 use Psr\Log\LoggerInterface
;
26 class NameTableStoreFactory
{
30 /** @var ILBFactory */
33 /** @var WANObjectCache */
36 /** @var LoggerInterface */
39 private static function getTableInfo() {
45 'idField' => 'ctd_id',
46 'nameField' => 'ctd_name',
47 'normalizationCallback' => null,
48 'insertCallback' => function ( $insertFields ) {
49 $insertFields['ctd_user_defined'] = 0;
50 $insertFields['ctd_count'] = 0;
56 'idField' => 'model_id',
57 'nameField' => 'model_name',
59 * No strtolower normalization is added to the service as there are examples of
60 * extensions that do not stick to this assumption.
61 * - extensions/examples/DataPages define( 'CONTENT_MODEL_XML_DATA','XML_DATA' );
62 * - extensions/Scribunto define( 'CONTENT_MODEL_SCRIBUNTO', 'Scribunto' );
67 'idField' => 'role_id',
68 'nameField' => 'role_name',
69 'normalizationCallback' => 'strtolower',
75 public function __construct(
76 ILBFactory
$lbFactory,
77 WANObjectCache
$cache,
78 LoggerInterface
$logger
80 $this->lbFactory
= $lbFactory;
81 $this->cache
= $cache;
82 $this->logger
= $logger;
86 * Get a NameTableStore for a specific table
88 * @param string $tableName The table name
89 * @param string|false $wiki The target wiki ID, or false for the current wiki
90 * @return NameTableStore
92 public function get( $tableName, $wiki = false ) : NameTableStore
{
93 $infos = self
::getTableInfo();
94 if ( !isset( $infos[$tableName] ) ) {
95 throw new \
InvalidArgumentException( "Invalid table name \$tableName" );
97 if ( $wiki === $this->lbFactory
->getLocalDomainID() ) {
101 if ( isset( $this->stores
[$tableName][$wiki] ) ) {
102 return $this->stores
[$tableName][$wiki];
105 $info = $infos[$tableName];
106 $store = new NameTableStore(
107 $this->lbFactory
->getMainLB( $wiki ),
113 $info['normalizationCallback'] ??
null,
115 $info['insertCallback'] ??
null
117 $this->stores
[$tableName][$wiki] = $store;
122 * Get a NameTableStore for the change_tag_def table
124 * @param string|bool $wiki
125 * @return NameTableStore
127 public function getChangeTagDef( $wiki = false ) : NameTableStore
{
128 return $this->get( 'change_tag_def', $wiki );
132 * Get a NameTableStore for the content_models table
134 * @param string|bool $wiki
135 * @return NameTableStore
137 public function getContentModels( $wiki = false ) : NameTableStore
{
138 return $this->get( 'content_models', $wiki );
142 * Get a NameTableStore for the slot_roles table
144 * @param string|bool $wiki
145 * @return NameTableStore
147 public function getSlotRoles( $wiki = false ) : NameTableStore
{
148 return $this->get( 'slot_roles', $wiki );