22 private const TABLE_NAME =
'translate_groupreviews';
24 private ?array $priorityCache =
null;
26 public function __construct(
27 private readonly IConnectionProvider $dbProvider,
34 $dbw = $this->dbProvider->getPrimaryDatabase();
35 return $dbw->newSelectQueryBuilder()
36 ->select(
'tgr_state' )
37 ->from( self::TABLE_NAME )
39 'tgr_group' => MessageGroupSubscriptionStore::getGroupIdForDatabase( $group ),
42 ->caller( __METHOD__ )
48 $currentState = $this->
getState( $group, $code );
49 if ( $currentState === $newState ) {
54 'tgr_group' => MessageGroupSubscriptionStore::getGroupIdForDatabase( $group ),
56 'tgr_state' => $newState,
58 $dbw = $this->dbProvider->getPrimaryDatabase();
59 $dbw->newReplaceQueryBuilder()
60 ->replaceInto( self::TABLE_NAME )
61 ->uniqueIndexFields( [
'tgr_group',
'tgr_lang' ] )
63 ->caller( __METHOD__ )
66 $entry =
new ManualLogEntry(
'translationreview',
'group' );
67 $entry->setPerformer( $user );
68 $entry->setTarget( SpecialPage::getTitleFor(
'Translate', $group->
getId() ) );
69 $entry->setParameters( [
70 '4::language' => $code,
71 '5::group-label' => $group->
getLabel(),
72 '6::old-state' => $currentState,
73 '7::new-state' => $newState,
78 $logId = $entry->insert();
79 $entry->publish( $logId );
81 $this->hookRunner->onTranslateEventMessageGroupStateChange( $group, $code, $currentState, $newState );
86 public function getGroupPriority(
string $group ): ?string {
87 $this->preloadGroupPriorities( __METHOD__ );
88 $dbGroupId = MessageGroupSubscriptionStore::getGroupIdForDatabase( $group );
89 return $this->priorityCache[$dbGroupId] ??
null;
95 if ( $this->priorityCache !==
null ) {
96 $this->priorityCache[$dbGroupId] = $priority;
99 $dbw = $this->dbProvider->getPrimaryDatabase();
101 'tgr_group' => $dbGroupId,
102 'tgr_lang' =>
'*priority',
103 'tgr_state' => $priority
106 if ( $priority ===
null ) {
107 unset( $row[
'tgr_state'] );
108 $dbw->newDeleteQueryBuilder()
109 ->deleteFrom( self::TABLE_NAME )
111 ->caller( __METHOD__ )
114 $dbw->newReplaceQueryBuilder()
115 ->replaceInto( self::TABLE_NAME )
116 ->uniqueIndexFields( [
'tgr_group',
'tgr_lang' ] )
118 ->caller( __METHOD__ )
123 private function preloadGroupPriorities(
string $caller ): void {
124 if ( $this->priorityCache !== null ) {
128 $dbr = $this->dbProvider->getReplicaDatabase();
129 $res = $dbr->newSelectQueryBuilder()
130 ->select( [
'tgr_group',
'tgr_state' ] )
131 ->from( self::TABLE_NAME )
132 ->where( [
'tgr_lang' =>
'*priority' ] )
136 $this->priorityCache = $this->result2map( $res,
'tgr_group',
'tgr_state' );
146 $result = $this->getWorkflowStates( [ $group ], [ $languageCode ] );
147 return $result->fetchRow()[
'tgr_state'] ??
null;
155 $result = $this->getWorkflowStates( $groupIds, [ $languageCode ] );
156 $states = $this->result2map( $result,
'tgr_group',
'tgr_state' );
159 foreach ( $groupIds as $groupId ) {
160 $dbGroupId = MessageGroupSubscriptionStore::getGroupIdForDatabase( $groupId );
161 if ( isset( $states[ $dbGroupId ] ) ) {
162 $finalResult[ $groupId ] = $states[ $dbGroupId ];
169 public function getWorkflowStatesForGroup(
string $groupId ): array {
170 $result = $this->getWorkflowStates( [ $groupId ], null );
171 return $this->result2map( $result,
'tgr_lang',
'tgr_state' );
178 private function getWorkflowStates( ?array $groups, ?array $languageCodes ): IResultWrapper {
179 if ( $groups === null && $languageCodes === null ) {
180 throw new InvalidArgumentException(
'Either the $groupId or the $languageCode should be provided' );
185 $conditions[
'tgr_group'] = array_map(
186 MessageGroupSubscriptionStore::getGroupIdForDatabase( ... ),
190 if ( $languageCodes ) {
191 $conditions[
'tgr_lang'] = $languageCodes;
194 return $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
195 ->select( [
'tgr_state',
'tgr_group',
'tgr_lang' ] )
196 ->from( self::TABLE_NAME )
197 ->where( $conditions )
198 ->caller( __METHOD__ )
202 private function result2map( IResultWrapper $result,
string $keyValue,
string $valueValue ): array {
204 foreach ( $result as $row ) {
205 $map[$row->$keyValue] = $row->$valueValue;