Merge "Add MessagesBi.php"
[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 === wfWikiID() ) {
98 $wiki = false;
99 }
100 if ( isset( $this->stores[$tableName][$wiki] ) ) {
101 return $this->stores[$tableName][$wiki];
102 }
103
104 $info = $infos[$tableName];
105 $store = new NameTableStore(
106 $this->lbFactory->getMainLB( $wiki ),
107 $this->cache,
108 $this->logger,
109 $tableName,
110 $info['idField'],
111 $info['nameField'],
112 $info['normalizationCallback'] ?? null,
113 $wiki,
114 $info['insertCallback'] ?? null
115 );
116 $this->stores[$tableName][$wiki] = $store;
117 return $store;
118 }
119
120 /**
121 * Get a NameTableStore for the change_tag_def table
122 *
123 * @param string|bool $wiki
124 * @return NameTableStore
125 */
126 public function getChangeTagDef( $wiki = false ) : NameTableStore {
127 return $this->get( 'change_tag_def', $wiki );
128 }
129
130 /**
131 * Get a NameTableStore for the content_models table
132 *
133 * @param string|bool $wiki
134 * @return NameTableStore
135 */
136 public function getContentModels( $wiki = false ) : NameTableStore {
137 return $this->get( 'content_models', $wiki );
138 }
139
140 /**
141 * Get a NameTableStore for the slot_roles table
142 *
143 * @param string|bool $wiki
144 * @return NameTableStore
145 */
146 public function getSlotRoles( $wiki = false ) : NameTableStore {
147 return $this->get( 'slot_roles', $wiki );
148 }
149 }