22 private ILoadBalancer $loadBalancer;
23 private const TABLE_NAME =
'translate_groupreviews';
25 private ?array $priorityCache =
null;
27 public function __construct( ILoadBalancer $loadBalancer,
HookRunner $hookRunner ) {
28 $this->loadBalancer = $loadBalancer;
29 $this->hookRunner = $hookRunner;
34 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
35 return $dbw->newSelectQueryBuilder()
36 ->select(
'tgr_state' )
37 ->from( self::TABLE_NAME )
39 'tgr_group' => self::getGroupIdForDatabase( $group->
getId() ),
42 ->caller( __METHOD__ )
48 $currentState = $this->
getState( $group, $code );
49 if ( $currentState === $newState ) {
53 $index = [
'tgr_group',
'tgr_lang' ];
55 'tgr_group' => self::getGroupIdForDatabase( $group->
getId() ),
57 'tgr_state' => $newState,
59 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
60 $dbw->replace( self::TABLE_NAME, [ $index ], $row, __METHOD__ );
62 $entry =
new ManualLogEntry(
'translationreview',
'group' );
63 $entry->setPerformer( $user );
64 $entry->setTarget( SpecialPage::getTitleFor(
'Translate', $group->
getId() ) );
65 $entry->setParameters( [
66 '4::language' => $code,
67 '5::group-label' => $group->
getLabel(),
68 '6::old-state' => $currentState,
69 '7::new-state' => $newState,
74 $logId = $entry->insert();
75 $entry->publish( $logId );
77 $this->hookRunner->onTranslateEventMessageGroupStateChange( $group, $code, $currentState, $newState );
82 public function getGroupPriority(
string $group ): ?string {
83 $this->preloadGroupPriorities( __METHOD__ );
84 return $this->priorityCache[self::getGroupIdForDatabase( $group )] ??
null;
89 $dbGroupId = self::getGroupIdForDatabase( $groupId );
90 if ( isset( $this->priorityCache ) ) {
91 $this->priorityCache[$dbGroupId] = $priority;
94 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
96 'tgr_group' => $dbGroupId,
97 'tgr_lang' =>
'*priority',
98 'tgr_state' => $priority
101 if ( $priority ===
null ) {
102 unset( $row[
'tgr_state'] );
103 $dbw->delete( self::TABLE_NAME, $row, __METHOD__ );
105 $index = [
'tgr_group',
'tgr_lang' ];
106 $dbw->replace( self::TABLE_NAME, [ $index ], $row, __METHOD__ );
110 private function preloadGroupPriorities(
string $caller ): void {
111 if ( isset( $this->priorityCache ) ) {
115 $dbr = $this->loadBalancer->getConnection( DB_REPLICA );
116 $res = $dbr->newSelectQueryBuilder()
117 ->select( [
'tgr_group',
'tgr_state' ] )
118 ->from( self::TABLE_NAME )
119 ->where( [
'tgr_lang' =>
'*priority' ] )
123 $this->priorityCache = $this->result2map( $res,
'tgr_group',
'tgr_state' );
133 $result = $this->getWorkflowStates( [ $groupId ], [ $languageCode ] );
134 return $result->fetchRow()[
'tgr_state'] ??
null;
137 public function getWorkflowStatesForLanguage(
string $languageCode, array $groupIds ): array {
138 $result = $this->getWorkflowStates( $groupIds, [ $languageCode ] );
139 $states = $this->result2map( $result,
'tgr_group',
'tgr_state' );
142 foreach ( $groupIds as $groupId ) {
143 $dbGroupId = self::getGroupIdForDatabase( $groupId );
144 if ( isset( $states[ $dbGroupId ] ) ) {
145 $finalResult[ $groupId ] = $states[ $dbGroupId ];
152 public function getWorkflowStatesForGroup(
string $groupId ): array {
153 $result = $this->getWorkflowStates( [ $groupId ], null );
154 return $this->result2map( $result,
'tgr_lang',
'tgr_state' );
157 private function getWorkflowStates( ?array $groupIds, ?array $languageCodes ): IResultWrapper {
158 $dbr = $this->loadBalancer->getConnection( DB_REPLICA );
159 $conditions = array_filter(
160 [
'tgr_group' => $groupIds,
'tgr_lang' => $languageCodes ],
161 static fn ( $x ) => $x !==
null && $x !==
''
164 if ( $conditions === [] ) {
165 throw new InvalidArgumentException(
'Either the $groupId or the $languageCode should be provided' );
168 if ( isset( $conditions[
'tgr_group'] ) ) {
169 $conditions[
'tgr_group'] = array_map( [ self::class,
'getGroupIdForDatabase' ], $groupIds );
172 return $dbr->newSelectQueryBuilder()
173 ->select( [
'tgr_state',
'tgr_group',
'tgr_lang' ] )
174 ->from( self::TABLE_NAME )
175 ->where( $conditions )
176 ->caller( __METHOD__ )
180 private function result2map( IResultWrapper $result,
string $keyValue,
string $valueValue ): array {
182 foreach ( $result as $row ) {
183 $map[$row->$keyValue] = $row->$valueValue;
189 private static function getGroupIdForDatabase( $groupId ): string {
190 $groupId = strval( $groupId );
193 if ( strlen( $groupId ) <= 200 ) {
198 return mb_strcut( $groupId, 0, 160 ) .
'||' . hash(
'md5', $groupId );