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() ),
46 $currentState = $this->
getState( $group, $code );
47 if ( $currentState === $newState ) {
51 $index = [
'tgr_group',
'tgr_lang' ];
53 'tgr_group' => self::getGroupIdForDatabase( $group->
getId() ),
55 'tgr_state' => $newState,
57 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
58 $dbw->replace( self::TABLE_NAME, [ $index ], $row, __METHOD__ );
60 $entry =
new ManualLogEntry(
'translationreview',
'group' );
61 $entry->setPerformer( $user );
62 $entry->setTarget( SpecialPage::getTitleFor(
'Translate', $group->
getId() ) );
63 $entry->setParameters( [
64 '4::language' => $code,
65 '5::group-label' => $group->
getLabel(),
66 '6::old-state' => $currentState,
67 '7::new-state' => $newState,
72 $logId = $entry->insert();
73 $entry->publish( $logId );
75 $this->hookRunner->onTranslateEventMessageGroupStateChange( $group, $code, $currentState, $newState );
80 public function getGroupPriority(
string $group ): ?string {
81 $this->preloadGroupPriorities( __METHOD__ );
82 return $this->priorityCache[self::getGroupIdForDatabase( $group )] ??
null;
87 $dbGroupId = self::getGroupIdForDatabase( $groupId );
88 if ( isset( $this->priorityCache ) ) {
89 $this->priorityCache[$dbGroupId] = $priority;
92 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
94 'tgr_group' => $dbGroupId,
95 'tgr_lang' =>
'*priority',
96 'tgr_state' => $priority
99 if ( $priority ===
null ) {
100 unset( $row[
'tgr_state'] );
101 $dbw->delete( self::TABLE_NAME, $row, __METHOD__ );
103 $index = [
'tgr_group',
'tgr_lang' ];
104 $dbw->replace( self::TABLE_NAME, [ $index ], $row, __METHOD__ );
108 private function preloadGroupPriorities(
string $caller ): void {
109 if ( isset( $this->priorityCache ) ) {
113 $dbr = $this->loadBalancer->getConnection( DB_REPLICA );
114 $res = $dbr->newSelectQueryBuilder()
115 ->select( [
'tgr_group',
'tgr_state' ] )
116 ->from( self::TABLE_NAME )
117 ->where( [
'tgr_lang' =>
'*priority' ] )
121 $this->priorityCache = $this->result2map( $res,
'tgr_group',
'tgr_state' );
131 $result = $this->getWorkflowStates( [ $groupId ], [ $languageCode ] );
132 return $result->fetchRow()[
'tgr_state'] ??
null;
135 public function getWorkflowStatesForLanguage(
string $languageCode, array $groupIds ): array {
136 $result = $this->getWorkflowStates( $groupIds, [ $languageCode ] );
137 $states = $this->result2map( $result,
'tgr_group',
'tgr_state' );
140 foreach ( $groupIds as $groupId ) {
141 $dbGroupId = self::getGroupIdForDatabase( $groupId );
142 if ( isset( $states[ $dbGroupId ] ) ) {
143 $finalResult[ $groupId ] = $states[ $dbGroupId ];
150 public function getWorkflowStatesForGroup(
string $groupId ): array {
151 $result = $this->getWorkflowStates( [ $groupId ], null );
152 return $this->result2map( $result,
'tgr_lang',
'tgr_state' );
155 private function getWorkflowStates( ?array $groupIds, ?array $languageCodes ): IResultWrapper {
156 $dbr = $this->loadBalancer->getConnection( DB_REPLICA );
157 $conditions = array_filter(
158 [
'tgr_group' => $groupIds,
'tgr_lang' => $languageCodes ],
159 static fn ( $x ) => $x !==
null && $x !==
''
162 if ( $conditions === [] ) {
163 throw new InvalidArgumentException(
'Either the $groupId or the $languageCode should be provided' );
166 if ( isset( $conditions[
'tgr_group'] ) ) {
167 $conditions[
'tgr_group'] = array_map( [ self::class,
'getGroupIdForDatabase' ], $groupIds );
170 return $dbr->newSelectQueryBuilder()
171 ->select( [
'tgr_state',
'tgr_group',
'tgr_lang' ] )
172 ->from( self::TABLE_NAME )
173 ->where( $conditions )
174 ->caller( __METHOD__ )
178 private function result2map( IResultWrapper $result,
string $keyValue,
string $valueValue ): array {
180 foreach ( $result as $row ) {
181 $map[$row->$keyValue] = $row->$valueValue;
187 private static function getGroupIdForDatabase( $groupId ): string {
188 $groupId = strval( $groupId );
191 if ( strlen( $groupId ) <= 200 ) {
196 return mb_strcut( $groupId, 0, 160 ) .
'||' . hash(
'md5', $groupId );