21 private const MAX_ITEMS_PER_QUERY = 2000;
23 private array $cache = [];
24 private ?array $priorityCache =
null;
25 private ILoadBalancer $loadBalancer;
27 public function __construct( ILoadBalancer $loadBalancer ) {
28 $this->loadBalancer = $loadBalancer;
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 ) );
38 $functionName = __METHOD__ .
" (for $caller)";
40 $this->cache += array_fill_keys( $missing,
null );
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 )
52 foreach ( $res as $row ) {
53 $this->cache[$row->tmd_group][$row->tmd_key] = $row->tmd_value;
64 public function get(
string $group,
string $key ) {
65 $this->preloadGroups( [ $group ], __METHOD__ );
66 return $this->cache[$this->getGroupIdForDatabase( $group )][$key] ??
false;
74 $value = $this->
get( $group, $key );
75 return $value ===
false ? $defaultValue : $value;
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] );
96 [ [
'tmd_group',
'tmd_key' ] ],
100 $this->cache[$dbGroupId][$key] = $value;
103 $this->priorityCache =
null;
111 $groups = $this->get( $groupId,
'subgroups' );
112 if ( is_string( $groups ) ) {
113 if ( str_contains( $groups,
'|' ) ) {
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] );
132 public function setSubgroups(
string $groupId, array $subgroupIds ): void {
133 $subgroups = implode(
'|', $subgroupIds );
134 $this->
set( $groupId,
'subgroups', $subgroups );
139 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
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 ] );
148 public function isExcluded(
string $groupId,
string $code ): bool {
149 if ( $this->priorityCache === null ) {
151 $db = Utilities::getSafeReadDB();
152 $res = $db->newSelectQueryBuilder()
154 'group' =>
'a.tmd_group',
155 'langs' =>
'b.tmd_value',
157 ->from(
'translate_metadata',
'a' )
158 ->leftJoin(
'translate_metadata',
'b', [
159 'a.tmd_group = b.tmd_group',
160 'b.tmd_key' =>
'prioritylangs',
163 'a.tmd_key' =>
'priorityforce',
164 'a.tmd_value' =>
'on'
166 ->caller( __METHOD__ )
169 $this->priorityCache = [];
170 foreach ( $res as $row ) {
171 if ( isset( $row->langs ) ) {
172 $this->priorityCache[ $row->group ] = array_flip( explode(
',', $row->langs ) );
174 $this->priorityCache[ $row->group ] = [];
179 $dbGroupId = $this->getGroupIdForDatabase( $groupId );
180 $isDiscouraged = MessageGroups::getPriority( $groupId ) ===
'discouraged';
181 $hasLimitedLanguages = isset( $this->priorityCache[$dbGroupId] );
182 $isLanguageIncluded = isset( $this->priorityCache[$dbGroupId][$code] );
184 return $isDiscouraged || ( $hasLimitedLanguages && !$isLanguageIncluded );
198 foreach ( $groupIds as $groupId ) {
199 $dbGroupIdMap[ $this->getGroupIdForDatabase( $groupId ) ] = $groupId;
202 $res = $db->newSelectQueryBuilder()
203 ->select( [
'tmd_group',
'tmd_key',
'tmd_value' ] )
204 ->from(
'translate_metadata' )
206 'tmd_group' => array_keys( $dbGroupIdMap ),
209 ->caller( __METHOD__ )
213 foreach ( $res as $row ) {
214 $groupId = $row->tmd_group;
216 $ret[ $dbGroupIdMap[ $groupId ] ][ $row->tmd_key ] = $row->tmd_value;
222 public function moveMetadata(
225 array $metadataKeysToMove
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 );
241 public function clearMetadata(
string $groupId, array $metadataKeys ): void {
243 foreach ( $metadataKeys as $type ) {
244 $this->
set( $groupId, $type, false );
255 return $db->newSelectQueryBuilder()
256 ->select(
'tmd_group' )
257 ->from(
'translate_metadata' )
258 ->where( [
'tmd_key' =>
'subgroups' ] )
259 ->caller( __METHOD__ )
260 ->fetchFieldValues();
263 private function getGroupIdForDatabase(
string $groupId ): string {
265 if ( strlen( $groupId ) <= 200 ) {
269 $hash = hash(
'md5', $groupId );
271 return mb_strcut( $groupId, 0, 160 ) .
'||' . $hash;