Debian Codegraph @

includes/Storage/NameTableStoreFactory.php

raw symbols SCIP
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 *
18 * @file
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 /** @var array<string,mixed> */
30 private static $info;
31 /** @var array<string,array<string,NameTableStore>> */
32 private $stores = [];
33
34 /** @var ILBFactory */
35 private $lbFactory;
36
37 /** @var WANObjectCache */
38 private $cache;
39
40 /** @var LoggerInterface */
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 * No strtolower normalization is added to the service as there are examples of
64 * extensions that do not stick to this assumption.
65 * - extensions/examples/DataPages define( 'CONTENT_MODEL_XML_DATA','XML_DATA' );
66 * - extensions/Scribunto define( 'CONTENT_MODEL_SCRIBUNTO', 'Scribunto' );
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 * Get a NameTableStore for a specific table
91 *
92 * @param string $tableName
93 * @param string|false $wiki The target wiki ID, or false for the current wiki
94 * @return NameTableStore
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 * Get a NameTableStore for the change_tag_def table
127 *
128 * @param string|bool $wiki
129 * @return NameTableStore
130 */
131 public function getChangeTagDef( $wiki = false ): NameTableStore {
132 return $this->get( 'change_tag_def', $wiki );
133 }
134
135 /**
136 * Get a NameTableStore for the content_models table
137 *
138 * @param string|bool $wiki
139 * @return NameTableStore
140 */
141 public function getContentModels( $wiki = false ): NameTableStore {
142 return $this->get( 'content_models', $wiki );
143 }
144
145 /**
146 * Get a NameTableStore for the slot_roles table
147 *
148 * @param string|bool $wiki
149 * @return NameTableStore
150 */
151 public function getSlotRoles( $wiki = false ): NameTableStore {
152 return $this->get( 'slot_roles', $wiki );
153 }
154 }