21 private const MAX_ITEMS_PER_QUERY = 2000;
23 private array $cache = [];
24 private ?array $priorityCache =
null;
26 public function __construct(
private readonly IConnectionProvider $dbProvider ) {
29 public function preloadGroups( array $groups,
string $caller ):
void {
30 $dbGroupIds = array_map( $this->getGroupIdForDatabase( ... ), $groups );
31 $missing = array_keys( array_diff_key( array_flip( $dbGroupIds ), $this->cache ) );
36 $functionName = __METHOD__ .
" (for $caller)";
38 $this->cache += array_fill_keys( $missing,
null );
41 $dbr = Utilities::getSafeReadDB();
42 $chunks = array_chunk( $missing, self::MAX_ITEMS_PER_QUERY );
43 foreach ( $chunks as $chunk ) {
44 $res = $dbr->newSelectQueryBuilder()
45 ->select( [
'tmd_group',
'tmd_key',
'tmd_value' ] )
46 ->from(
'translate_metadata' )
47 ->where( [
'tmd_group' => array_map( strval( ... ), $chunk ) ] )
48 ->caller( $functionName )
50 foreach ( $res as $row ) {
51 $this->cache[$row->tmd_group][$row->tmd_key] = $row->tmd_value;
61 public function get(
string $group,
string $key ): string|false {
62 $this->preloadGroups( [ $group ], __METHOD__ );
63 return $this->cache[$this->getGroupIdForDatabase( $group )][$key] ??
false;
71 $value = $this->get( $group, $key );
72 return $value ===
false ? $defaultValue : $value;
82 public function set(
string $groupId,
string $key,
string|
false $value ): void {
83 $dbw = $this->dbProvider->getPrimaryDatabase();
84 $dbGroupId = $this->getGroupIdForDatabase( $groupId );
85 $data = [
'tmd_group' => $dbGroupId,
'tmd_key' => $key,
'tmd_value' => $value ];
86 if ( $value ===
false ) {
87 unset( $data[
'tmd_value'] );
88 $dbw->newDeleteQueryBuilder()
89 ->deleteFrom(
'translate_metadata' )
91 ->caller( __METHOD__ )
93 unset( $this->cache[$dbGroupId][$key] );
95 $dbw->newReplaceQueryBuilder()
96 ->replaceInto(
'translate_metadata' )
97 ->uniqueIndexFields( [
'tmd_group',
'tmd_key' ] )
99 ->caller( __METHOD__ )
101 $this->cache[$dbGroupId][$key] = $value;
104 $this->priorityCache =
null;
112 $groups = $this->get( $groupId,
'subgroups' );
113 if ( is_string( $groups ) ) {
114 if ( str_contains( $groups,
'|' ) ) {
115 $groups = explode(
'|', $groups );
117 $groups = array_map(
'trim', explode(
',', $groups ) );
120 foreach ( $groups as $index => $id ) {
121 if ( trim( $id ) ===
'' ) {
122 unset( $groups[$index] );
133 public function setSubgroups(
string $groupId, array $subgroupIds ): void {
134 $subgroups = implode(
'|', $subgroupIds );
135 $this->
set( $groupId,
'subgroups', $subgroups );
140 $dbw = $this->dbProvider->getPrimaryDatabase();
142 $dbGroupId = $this->getGroupIdForDatabase( $groupId );
143 $dbw->newDeleteQueryBuilder()
144 ->deleteFrom(
'translate_metadata' )
145 ->where( [
'tmd_group' => $dbGroupId ] )
146 ->caller( __METHOD__ )
148 $this->cache[ $dbGroupId ] =
null;
149 unset( $this->priorityCache[ $dbGroupId ] );
152 public function isExcluded(
string $groupId,
string $code ): bool {
153 if ( $this->priorityCache === null ) {
155 $db = Utilities::getSafeReadDB();
156 $res = $db->newSelectQueryBuilder()
158 'group' =>
'a.tmd_group',
159 'langs' =>
'b.tmd_value',
161 ->from(
'translate_metadata',
'a' )
162 ->leftJoin(
'translate_metadata',
'b', [
163 'a.tmd_group = b.tmd_group',
164 'b.tmd_key' =>
'prioritylangs',
167 'a.tmd_key' =>
'priorityforce',
168 'a.tmd_value' =>
'on'
170 ->caller( __METHOD__ )
173 $this->priorityCache = [];
174 foreach ( $res as $row ) {
175 $this->priorityCache[$row->group] = isset( $row->langs )
176 ? array_flip( explode(
',', $row->langs ) )
181 $dbGroupId = $this->getGroupIdForDatabase( $groupId );
182 $isDiscouraged = MessageGroups::getPriority( $groupId ) ===
'discouraged';
183 $hasLimitedLanguages = isset( $this->priorityCache[$dbGroupId] );
184 $isLanguageIncluded = isset( $this->priorityCache[$dbGroupId][$code] );
186 return $isDiscouraged || ( $hasLimitedLanguages && !$isLanguageIncluded );
200 foreach ( $groupIds as $groupId ) {
201 $dbGroupIdMap[ $this->getGroupIdForDatabase( $groupId ) ] = $groupId;
204 $res = $db->newSelectQueryBuilder()
205 ->select( [
'tmd_group',
'tmd_key',
'tmd_value' ] )
206 ->from(
'translate_metadata' )
208 'tmd_group' => array_keys( $dbGroupIdMap ),
211 ->caller( __METHOD__ )
215 foreach ( $res as $row ) {
216 $groupId = $row->tmd_group;
218 $ret[ $dbGroupIdMap[ $groupId ] ][ $row->tmd_key ] = $row->tmd_value;
224 public function moveMetadata(
227 array $metadataKeysToMove
229 $this->preloadGroups( [ $oldGroupId, $newGroupId ], __METHOD__ );
230 foreach ( $metadataKeysToMove as $type ) {
231 $value = $this->
get( $oldGroupId, $type );
232 if ( $value !==
false ) {
233 $this->
set( $oldGroupId, $type, false );
234 $this->
set( $newGroupId, $type, $value );
243 public function clearMetadata(
string $groupId, array $metadataKeys ): void {
245 foreach ( $metadataKeys as $type ) {
246 $this->
set( $groupId, $type, false );
257 return $db->newSelectQueryBuilder()
258 ->select(
'tmd_group' )
259 ->from(
'translate_metadata' )
260 ->where( [
'tmd_key' =>
'subgroups' ] )
261 ->caller( __METHOD__ )
262 ->fetchFieldValues();
265 private function getGroupIdForDatabase(
string $groupId ): string {
267 if ( strlen( $groupId ) <= 200 ) {
271 $hash = hash(
'md5', $groupId );
273 return mb_strcut( $groupId, 0, 160 ) .
'||' . $hash;