Merge "EmailNotification: Add newline before minor edit text"
[lhc/web/wiklou.git] / includes / Storage / NameTableStoreFactory.php
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 * @file
18 */
19
20 namespace MediaWiki\Storage;
21
22 use Wikimedia\Rdbms\ILBFactory;
23 use WANObjectCache;
24 use Psr\Log\LoggerInterface;
25
26 class NameTableStoreFactory {
27 private static $info;
28 private $stores = [];
29
30 /** @var ILBFactory */
31 private $lbFactory;
32
33 /** @var WANObjectCache */
34 private $cache;
35
36 /** @var LoggerInterface */
37 private $logger;
38
39 private static function getTableInfo() {
40 if ( self::$info ) {
41 return self::$info;
42 }
43 self::$info = [
44 'change_tag_def' => [
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;
51 return $insertFields;
52 }
53 ],
54
55 'content_models' => [
56 'idField' => 'model_id',
57 'nameField' => 'model_name',
58 /**
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' );
63 */
64 ],
65
66 'slot_roles' => [
67 'idField' => 'role_id',
68 'nameField' => 'role_name',
69 'normalizationCallback' => 'strtolower',
70 ],
71 ];
72 return self::$info;
73 }
74
75 public function __construct(
76 ILBFactory $lbFactory,
77 WANObjectCache $cache,
78 LoggerInterface $logger
79 ) {
80 $this->lbFactory = $lbFactory;
81 $this->cache = $cache;
82 $this->logger = $logger;
83 }
84
85 /**
86 * Get a NameTableStore for a specific table
87 *
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
91 */
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" );
96 }
97 if ( $wiki === $this->lbFactory->getLocalDomainID() ) {
98 $wiki = false;
99 }
100
101 if ( isset( $this->stores[$tableName][$wiki] ) ) {
102 return $this->stores[$tableName][$wiki];
103 }
104
105 $info = $infos[$tableName];
106 $store = new NameTableStore(
107 $this->lbFactory->getMainLB( $wiki ),
108 $this->cache,
109 $this->logger,
110 $tableName,
111 $info['idField'],
112 $info['nameField'],
113 $info['normalizationCallback'] ?? null,
114 $wiki,
115 $info['insertCallback'] ?? null
116 );
117 $this->stores[$tableName][$wiki] = $store;
118 return $store;
119 }
120
121 /**
122 * Get a NameTableStore for the change_tag_def table
123 *
124 * @param string|bool $wiki
125 * @return NameTableStore
126 */
127 public function getChangeTagDef( $wiki = false ) : NameTableStore {
128 return $this->get( 'change_tag_def', $wiki );
129 }
130
131 /**
132 * Get a NameTableStore for the content_models table
133 *
134 * @param string|bool $wiki
135 * @return NameTableStore
136 */
137 public function getContentModels( $wiki = false ) : NameTableStore {
138 return $this->get( 'content_models', $wiki );
139 }
140
141 /**
142 * Get a NameTableStore for the slot_roles table
143 *
144 * @param string|bool $wiki
145 * @return NameTableStore
146 */
147 public function getSlotRoles( $wiki = false ) : NameTableStore {
148 return $this->get( 'slot_roles', $wiki );
149 }
150 }