Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageGroupMetadata.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageProcessing;
5
8use Wikimedia\Rdbms\ILoadBalancer;
9
21 private const MAX_ITEMS_PER_QUERY = 2000;
23 private array $cache = [];
24 private ?array $priorityCache = null;
25 private ILoadBalancer $loadBalancer;
26
27 public function __construct( ILoadBalancer $loadBalancer ) {
28 $this->loadBalancer = $loadBalancer;
29 }
30
31 public function preloadGroups( array $groups, string $caller ): void {
32 $dbGroupIds = array_map( [ $this, 'getGroupIdForDatabase' ], $groups );
33 $missing = array_keys( array_diff_key( array_flip( $dbGroupIds ), $this->cache ) );
34 if ( !$missing ) {
35 return;
36 }
37
38 $functionName = __METHOD__ . " (for $caller)";
39
40 $this->cache += array_fill_keys( $missing, null ); // cache negatives
41
42 // TODO: Ideally, this should use the injected ILoadBalancer to make it mockable.
43 $dbr = Utilities::getSafeReadDB();
44 $chunks = array_chunk( $missing, self::MAX_ITEMS_PER_QUERY );
45 foreach ( $chunks as $chunk ) {
46 $res = $dbr->newSelectQueryBuilder()
47 ->select( [ 'tmd_group', 'tmd_key', 'tmd_value' ] )
48 ->from( 'translate_metadata' )
49 ->where( [ 'tmd_group' => array_map( 'strval', $chunk ) ] )
50 ->caller( $functionName )
51 ->fetchResultSet();
52 foreach ( $res as $row ) {
53 $this->cache[$row->tmd_group][$row->tmd_key] = $row->tmd_value;
54 }
55 }
56 }
57
64 public function get( string $group, string $key ) {
65 $this->preloadGroups( [ $group ], __METHOD__ );
66 return $this->cache[$this->getGroupIdForDatabase( $group )][$key] ?? false;
67 }
68
73 public function getWithDefaultValue( string $group, string $key, ?string $defaultValue ): ?string {
74 $value = $this->get( $group, $key );
75 return $value === false ? $defaultValue : $value;
76 }
77
85 public function set( string $groupId, string $key, $value ): void {
86 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
87 $dbGroupId = $this->getGroupIdForDatabase( $groupId );
88 $data = [ 'tmd_group' => $dbGroupId, 'tmd_key' => $key, 'tmd_value' => $value ];
89 if ( $value === false ) {
90 unset( $data['tmd_value'] );
91 $dbw->delete( 'translate_metadata', $data, __METHOD__ );
92 unset( $this->cache[$dbGroupId][$key] );
93 } else {
94 $dbw->replace(
95 'translate_metadata',
96 [ [ 'tmd_group', 'tmd_key' ] ],
97 $data,
98 __METHOD__
99 );
100 $this->cache[$dbGroupId][$key] = $value;
101 }
102
103 $this->priorityCache = null;
104 }
105
110 public function getSubgroups( string $groupId ): ?array {
111 $groups = $this->get( $groupId, 'subgroups' );
112 if ( is_string( $groups ) ) {
113 if ( str_contains( $groups, '|' ) ) {
114 $groups = explode( '|', $groups );
115 } else {
116 $groups = array_map( 'trim', explode( ',', $groups ) );
117 }
118
119 foreach ( $groups as $index => $id ) {
120 if ( trim( $id ) === '' ) {
121 unset( $groups[$index] );
122 }
123 }
124 } else {
125 $groups = null;
126 }
127
128 return $groups;
129 }
130
132 public function setSubgroups( string $groupId, array $subgroupIds ): void {
133 $subgroups = implode( '|', $subgroupIds );
134 $this->set( $groupId, 'subgroups', $subgroups );
135 }
136
138 public function deleteGroup( string $groupId ): void {
139 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
140
141 $dbGroupId = $this->getGroupIdForDatabase( $groupId );
142 $conditions = [ 'tmd_group' => $dbGroupId ];
143 $dbw->delete( 'translate_metadata', $conditions, __METHOD__ );
144 $this->cache[ $dbGroupId ] = null;
145 unset( $this->priorityCache[ $dbGroupId ] );
146 }
147
148 public function isExcluded( string $groupId, string $code ): bool {
149 if ( $this->priorityCache === null ) {
150 // TODO: Ideally, this should use the injected ILoadBalancer to make it mockable.
151 $db = Utilities::getSafeReadDB();
152 $res = $db->newSelectQueryBuilder()
153 ->select( [
154 'group' => 'a.tmd_group',
155 'langs' => 'b.tmd_value',
156 ] )
157 ->from( 'translate_metadata', 'a' )
158 ->leftJoin( 'translate_metadata', 'b', [
159 'a.tmd_group = b.tmd_group',
160 'b.tmd_key' => 'prioritylangs',
161 ] )
162 ->where( [
163 'a.tmd_key' => 'priorityforce',
164 'a.tmd_value' => 'on'
165 ] )
166 ->caller( __METHOD__ )
167 ->fetchResultSet();
168
169 $this->priorityCache = [];
170 foreach ( $res as $row ) {
171 if ( isset( $row->langs ) ) {
172 $this->priorityCache[ $row->group ] = array_flip( explode( ',', $row->langs ) );
173 } else {
174 $this->priorityCache[ $row->group ] = [];
175 }
176 }
177 }
178
179 $dbGroupId = $this->getGroupIdForDatabase( $groupId );
180 $isDiscouraged = MessageGroups::getPriority( $groupId ) === 'discouraged';
181 $hasLimitedLanguages = isset( $this->priorityCache[$dbGroupId] );
182 $isLanguageIncluded = isset( $this->priorityCache[$dbGroupId][$code] );
183
184 return $isDiscouraged || ( $hasLimitedLanguages && !$isLanguageIncluded );
185 }
186
193 public function loadBasicMetadataForTranslatablePages( array $groupIds, array $keys ): array {
194 // TODO: Ideally, this should use the injected ILoadBalancer to make it mockable.
195 $db = Utilities::getSafeReadDB();
196 $dbGroupIdMap = [];
197
198 foreach ( $groupIds as $groupId ) {
199 $dbGroupIdMap[ $this->getGroupIdForDatabase( $groupId ) ] = $groupId;
200 }
201
202 $res = $db->newSelectQueryBuilder()
203 ->select( [ 'tmd_group', 'tmd_key', 'tmd_value' ] )
204 ->from( 'translate_metadata' )
205 ->where( [
206 'tmd_group' => array_keys( $dbGroupIdMap ),
207 'tmd_key' => $keys,
208 ] )
209 ->caller( __METHOD__ )
210 ->fetchResultSet();
211
212 $ret = [];
213 foreach ( $res as $row ) {
214 $groupId = $row->tmd_group;
215 // Remap the db group ids to group id in the response
216 $ret[ $dbGroupIdMap[ $groupId ] ][ $row->tmd_key ] = $row->tmd_value;
217 }
218
219 return $ret;
220 }
221
222 public function moveMetadata(
223 string $oldGroupId,
224 string $newGroupId,
225 array $metadataKeysToMove
226 ): void {
227 $this->preloadGroups( [ $oldGroupId, $newGroupId ], __METHOD__ );
228 foreach ( $metadataKeysToMove as $type ) {
229 $value = $this->get( $oldGroupId, $type );
230 if ( $value !== false ) {
231 $this->set( $oldGroupId, $type, false );
232 $this->set( $newGroupId, $type, $value );
233 }
234 }
235 }
236
241 public function clearMetadata( string $groupId, array $metadataKeys ): void {
242 // remove the entries from metadata table.
243 foreach ( $metadataKeys as $type ) {
244 $this->set( $groupId, $type, false );
245 }
246 }
247
249 public function getGroupsWithSubgroups(): array {
250 // TODO: Ideally, this should use the injected ILoadBalancer to make it mockable.
251 $db = Utilities::getSafeReadDB();
252 // There is no need to de-hash the group id from the database as
253 // AggregateGroupsActionApi::generateAggregateGroupId already ensures that the length
254 // is appropriate
255 return $db->newSelectQueryBuilder()
256 ->select( 'tmd_group' )
257 ->from( 'translate_metadata' )
258 ->where( [ 'tmd_key' => 'subgroups' ] )
259 ->caller( __METHOD__ )
260 ->fetchFieldValues();
261 }
262
263 private function getGroupIdForDatabase( string $groupId ): string {
264 // Check if length is more than 200 bytes
265 if ( strlen( $groupId ) <= 200 ) {
266 return $groupId;
267 }
268
269 $hash = hash( 'md5', $groupId );
270 // We take 160 bytes of the original string and append the md5 hash (32 bytes)
271 return mb_strcut( $groupId, 0, 160 ) . '||' . $hash;
272 }
273}
Factory class for accessing message groups individually by id or all of them as a list.
Offers functionality for reading and updating Translate group related metadata.
getWithDefaultValue(string $group, string $key, ?string $defaultValue)
Get a metadata value for the given group and key.
loadBasicMetadataForTranslatablePages(array $groupIds, array $keys)
Do a query optimized for page list in Special:PageTranslation.
setSubgroups(string $groupId, array $subgroupIds)
Wrapper for setting subgroups.
get(string $group, string $key)
Get a metadata value for the given group and key.
deleteGroup(string $groupId)
Wrapper for deleting one wiki aggregate group at once.
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:31