19 private static $cache = [];
21 private static $priorityCache;
28 $missing = array_keys( array_diff_key( array_flip( $groups ), self::$cache ) );
33 $fname = __METHOD__ .
" (for $caller)";
35 self::$cache += array_fill_keys( $missing,
null );
37 $dbr = Utilities::getSafeReadDB();
38 $conds = count( $missing ) <= 500 ? [
'tmd_group' => array_map(
'strval', $missing ) ] : [];
41 [
'tmd_group',
'tmd_key',
'tmd_value' ],
45 foreach ( $res as $row ) {
46 self::$cache[$row->tmd_group][$row->tmd_key] = $row->tmd_value;
56 public static function get( $group, $key ) {
57 self::preloadGroups( [ $group ], __METHOD__ );
59 return self::$cache[$group][$key] ??
false;
71 string $group,
string $key,
string $defaultValue
73 $value = self::
get( $group, $key );
74 return $value ===
false ? $defaultValue : $value;
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] );
94 [ [
'tmd_group',
'tmd_key' ] ],
98 self::$cache[$group][$key] = $value;
101 self::$priorityCache =
null;
111 $groups = self::get( $groupId,
'subgroups' );
112 if ( is_string( $groups ) ) {
113 if ( strpos( $groups,
'|' ) !==
false ) {
114 $groups = explode(
'|', $groups );
116 $groups = array_map(
'trim', explode(
',', $groups ) );
119 foreach ( $groups as $index => $id ) {
120 if ( trim( $id ) ===
'' ) {
121 unset( $groups[$index] );
138 $subgroups = implode(
'|', $subgroupIds );
139 self::set( $groupId,
'subgroups', $subgroups );
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 ] );
155 public static function isExcluded(
string $groupId,
string $code ): bool {
156 if ( self::$priorityCache === null ) {
157 $db = Utilities::getSafeReadDB();
160 'a' =>
'translate_metadata',
161 'b' =>
'translate_metadata'
164 'group' =>
'b.tmd_group',
165 'langs' =>
'b.tmd_value',
174 'a.tmd_group = b.tmd_group',
175 'a.tmd_key' =>
'priorityforce',
176 'a.tmd_value' =>
'on',
177 'b.tmd_key' =>
'prioritylangs',
183 self::$priorityCache = [];
184 foreach ( $res as $row ) {
185 self::$priorityCache[$row->group] =
186 array_flip( explode(
',', $row->langs ) );
190 $isDiscouraged = MessageGroups::getPriority( $groupId ) ===
'discouraged';
191 $hasLimitedLanguages = isset( self::$priorityCache[$groupId] );
192 $isLanguageIncluded = isset( self::$priorityCache[$groupId][$code] );
194 return $isDiscouraged || ( $hasLimitedLanguages && !$isLanguageIncluded );
206 'translate_metadata',
207 [
'tmd_group',
'tmd_key',
'tmd_value' ],
209 'tmd_group' => $groupIds,
216 foreach ( $res as $row ) {
217 $ret[$row->tmd_group][$row->tmd_key] = $row->tmd_value;
232 array $metadataKeysToMove
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 );
249 public static function clearMetadata(
string $groupId, array $metadataKeys ): void {
251 foreach ( $metadataKeys as $type ) {
252 self::set( $groupId, $type,
false );