Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslateMetadata.php
Go to the documentation of this file.
1<?php
2
5
19 private static $cache = [];
21 private static $priorityCache;
22
27 public static function preloadGroups( array $groups, string $caller ) {
28 $missing = array_keys( array_diff_key( array_flip( $groups ), self::$cache ) );
29 if ( !$missing ) {
30 return;
31 }
32
33 $fname = __METHOD__ . " (for $caller)";
34
35 self::$cache += array_fill_keys( $missing, null ); // cache negatives
36
37 $dbr = Utilities::getSafeReadDB();
38 $conds = count( $missing ) <= 500 ? [ 'tmd_group' => array_map( 'strval', $missing ) ] : [];
39 $res = $dbr->select(
40 'translate_metadata',
41 [ 'tmd_group', 'tmd_key', 'tmd_value' ],
42 $conds,
43 $fname
44 );
45 foreach ( $res as $row ) {
46 self::$cache[$row->tmd_group][$row->tmd_key] = $row->tmd_value;
47 }
48 }
49
56 public static function get( $group, $key ) {
57 self::preloadGroups( [ $group ], __METHOD__ );
58
59 return self::$cache[$group][$key] ?? false;
60 }
61
70 public static function getWithDefaultValue(
71 string $group, string $key, string $defaultValue
72 ): string {
73 $value = self::get( $group, $key );
74 return $value === false ? $defaultValue : $value;
75 }
76
84 public static function set( $group, $key, $value ) {
85 $dbw = wfGetDB( DB_PRIMARY );
86 $data = [ 'tmd_group' => $group, 'tmd_key' => $key, 'tmd_value' => $value ];
87 if ( $value === false ) {
88 unset( $data['tmd_value'] );
89 $dbw->delete( 'translate_metadata', $data, __METHOD__ );
90 unset( self::$cache[$group][$key] );
91 } else {
92 $dbw->replace(
93 'translate_metadata',
94 [ [ 'tmd_group', 'tmd_key' ] ],
95 $data,
96 __METHOD__
97 );
98 self::$cache[$group][$key] = $value;
99 }
100
101 self::$priorityCache = null;
102 }
103
110 public static function getSubgroups( string $groupId ): ?array {
111 $groups = self::get( $groupId, 'subgroups' );
112 if ( is_string( $groups ) ) {
113 if ( strpos( $groups, '|' ) !== false ) {
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
137 public static function setSubgroups( $groupId, $subgroupIds ) {
138 $subgroups = implode( '|', $subgroupIds );
139 self::set( $groupId, 'subgroups', $subgroups );
140 }
141
147 public static function deleteGroup( $groupId ) {
148 $dbw = wfGetDB( DB_PRIMARY );
149 $conds = [ 'tmd_group' => $groupId ];
150 $dbw->delete( 'translate_metadata', $conds, __METHOD__ );
151 self::$cache[$groupId] = null;
152 unset( self::$priorityCache[ $groupId ] );
153 }
154
155 public static function isExcluded( string $groupId, string $code ): bool {
156 if ( self::$priorityCache === null ) {
157 $db = Utilities::getSafeReadDB();
158 $res = $db->select(
159 [
160 'a' => 'translate_metadata',
161 'b' => 'translate_metadata'
162 ],
163 [
164 'group' => 'b.tmd_group',
165 'langs' => 'b.tmd_value',
166 ],
167 [],
168 __METHOD__,
169 [],
170 [
171 'b' => [
172 'INNER JOIN',
173 [
174 'a.tmd_group = b.tmd_group',
175 'a.tmd_key' => 'priorityforce',
176 'a.tmd_value' => 'on',
177 'b.tmd_key' => 'prioritylangs',
178 ]
179 ]
180 ]
181 );
182
183 self::$priorityCache = [];
184 foreach ( $res as $row ) {
185 self::$priorityCache[$row->group] =
186 array_flip( explode( ',', $row->langs ) );
187 }
188 }
189
190 $isDiscouraged = MessageGroups::getPriority( $groupId ) === 'discouraged';
191 $hasLimitedLanguages = isset( self::$priorityCache[$groupId] );
192 $isLanguageIncluded = isset( self::$priorityCache[$groupId][$code] );
193
194 return $isDiscouraged || ( $hasLimitedLanguages && !$isLanguageIncluded );
195 }
196
203 public static function loadBasicMetadataForTranslatablePages( array $groupIds, array $keys ): array {
204 $db = Utilities::getSafeReadDB();
205 $res = $db->select(
206 'translate_metadata',
207 [ 'tmd_group', 'tmd_key', 'tmd_value' ],
208 [
209 'tmd_group' => $groupIds,
210 'tmd_key' => $keys,
211 ],
212 __METHOD__
213 );
214
215 $ret = [];
216 foreach ( $res as $row ) {
217 $ret[$row->tmd_group][$row->tmd_key] = $row->tmd_value;
218 }
219
220 return $ret;
221 }
222
229 public static function moveMetadata(
230 string $oldGroupId,
231 string $newGroupId,
232 array $metadataKeysToMove
233 ): void {
234 self::preloadGroups( [ $oldGroupId, $newGroupId ], __METHOD__ );
235 foreach ( $metadataKeysToMove as $type ) {
236 $value = self::get( $oldGroupId, $type );
237 if ( $value !== false ) {
238 self::set( $oldGroupId, $type, false );
239 self::set( $newGroupId, $type, $value );
240 }
241 }
242 }
243
249 public static function clearMetadata( string $groupId, array $metadataKeys ): void {
250 // remove the entries from metadata table.
251 foreach ( $metadataKeys as $type ) {
252 self::set( $groupId, $type, false );
253 }
254 }
255}
Factory class for accessing message groups individually by id or all of them as a list.
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:30
static getWithDefaultValue(string $group, string $key, string $defaultValue)
Get a metadata value for the given group and key.
static getSubgroups(string $groupId)
Wrapper for getting subgroups.
static moveMetadata(string $oldGroupId, string $newGroupId, array $metadataKeysToMove)
static get( $group, $key)
Get a metadata value for the given group and key.
static deleteGroup( $groupId)
Wrapper for deleting one wiki aggregate group at once.
static preloadGroups(array $groups, string $caller)
static loadBasicMetadataForTranslatablePages(array $groupIds, array $keys)
Do a query optimized for page list in Special:PageTranslation.
static clearMetadata(string $groupId, array $metadataKeys)
static setSubgroups( $groupId, $subgroupIds)
Wrapper for setting subgroups.