Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageGroupReviewStore.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use InvalidArgumentException;
7use ManualLogEntry;
9use MediaWiki\SpecialPage\SpecialPage;
10use MediaWiki\User\User;
11use MessageGroup;
12use Wikimedia\Rdbms\IConnectionProvider;
13use Wikimedia\Rdbms\IResultWrapper;
14
21 private HookRunner $hookRunner;
22 private IConnectionProvider $dbProvider;
23 private const TABLE_NAME = 'translate_groupreviews';
25 private ?array $priorityCache = null;
26
27 public function __construct( IConnectionProvider $dbProvider, HookRunner $hookRunner ) {
28 $this->dbProvider = $dbProvider;
29 $this->hookRunner = $hookRunner;
30 }
31
33 public function getState( MessageGroup $group, string $code ) {
34 $dbw = $this->dbProvider->getPrimaryDatabase();
35 return $dbw->newSelectQueryBuilder()
36 ->select( 'tgr_state' )
37 ->from( self::TABLE_NAME )
38 ->where( [
39 'tgr_group' => self::getGroupIdForDatabase( $group->getId() ),
40 'tgr_lang' => $code
41 ] )
42 ->caller( __METHOD__ )
43 ->fetchField();
44 }
45
47 public function changeState( MessageGroup $group, string $code, string $newState, User $user ): bool {
48 $currentState = $this->getState( $group, $code );
49 if ( $currentState === $newState ) {
50 return false;
51 }
52
53 $row = [
54 'tgr_group' => self::getGroupIdForDatabase( $group->getId() ),
55 'tgr_lang' => $code,
56 'tgr_state' => $newState,
57 ];
58 $dbw = $this->dbProvider->getPrimaryDatabase();
59 $dbw->newReplaceQueryBuilder()
60 ->replaceInto( self::TABLE_NAME )
61 ->uniqueIndexFields( [ 'tgr_group', 'tgr_lang' ] )
62 ->row( $row )
63 ->caller( __METHOD__ )
64 ->execute();
65
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,
74 ] );
75 // @todo
76 // $entry->setComment( $comment );
77
78 $logId = $entry->insert();
79 $entry->publish( $logId );
80
81 $this->hookRunner->onTranslateEventMessageGroupStateChange( $group, $code, $currentState, $newState );
82
83 return true;
84 }
85
86 public function getGroupPriority( string $group ): ?string {
87 $this->preloadGroupPriorities( __METHOD__ );
88 return $this->priorityCache[self::getGroupIdForDatabase( $group )] ?? null;
89 }
90
92 public function setGroupPriority( string $groupId, ?string $priority ): void {
93 $dbGroupId = self::getGroupIdForDatabase( $groupId );
94 if ( $this->priorityCache !== null ) {
95 $this->priorityCache[$dbGroupId] = $priority;
96 }
97
98 $dbw = $this->dbProvider->getPrimaryDatabase();
99 $row = [
100 'tgr_group' => $dbGroupId,
101 'tgr_lang' => '*priority',
102 'tgr_state' => $priority
103 ];
104
105 if ( $priority === null ) {
106 unset( $row['tgr_state'] );
107 $dbw->newDeleteQueryBuilder()
108 ->deleteFrom( self::TABLE_NAME )
109 ->where( $row )
110 ->caller( __METHOD__ )
111 ->execute();
112 } else {
113 $dbw->newReplaceQueryBuilder()
114 ->replaceInto( self::TABLE_NAME )
115 ->uniqueIndexFields( [ 'tgr_group', 'tgr_lang' ] )
116 ->row( $row )
117 ->caller( __METHOD__ )
118 ->execute();
119 }
120 }
121
122 private function preloadGroupPriorities( string $caller ): void {
123 if ( $this->priorityCache !== null ) {
124 return;
125 }
126
127 $dbr = $this->dbProvider->getReplicaDatabase();
128 $res = $dbr->newSelectQueryBuilder()
129 ->select( [ 'tgr_group', 'tgr_state' ] )
130 ->from( self::TABLE_NAME )
131 ->where( [ 'tgr_lang' => '*priority' ] )
132 ->caller( $caller )
133 ->fetchResultSet();
134
135 $this->priorityCache = $this->result2map( $res, 'tgr_group', 'tgr_state' );
136 }
137
144 public function getWorkflowState( string $groupId, string $languageCode ): ?string {
145 $result = $this->getWorkflowStates( [ $groupId ], [ $languageCode ] );
146 return $result->fetchRow()['tgr_state'] ?? null;
147 }
148
153 public function getWorkflowStatesForLanguage( string $languageCode, array $groupIds ): array {
154 $result = $this->getWorkflowStates( $groupIds, [ $languageCode ] );
155 $states = $this->result2map( $result, 'tgr_group', 'tgr_state' );
156
157 $finalResult = [];
158 foreach ( $groupIds as $groupId ) {
159 $dbGroupId = self::getGroupIdForDatabase( $groupId );
160 if ( isset( $states[ $dbGroupId ] ) ) {
161 $finalResult[ $groupId ] = $states[ $dbGroupId ];
162 }
163 }
164
165 return $finalResult;
166 }
167
168 public function getWorkflowStatesForGroup( string $groupId ): array {
169 $result = $this->getWorkflowStates( [ $groupId ], null );
170 return $this->result2map( $result, 'tgr_lang', 'tgr_state' );
171 }
172
177 private function getWorkflowStates( ?array $groupIds, ?array $languageCodes ): IResultWrapper {
178 $dbr = $this->dbProvider->getReplicaDatabase();
179 $conditions = array_filter(
180 [ 'tgr_group' => $groupIds, 'tgr_lang' => $languageCodes ],
181 static fn ( $x ) => $x !== null && $x !== ''
182 );
183
184 if ( $conditions === [] ) {
185 throw new InvalidArgumentException( 'Either the $groupId or the $languageCode should be provided' );
186 }
187
188 if ( isset( $conditions['tgr_group'] ) ) {
189 $conditions['tgr_group'] = array_map( [ self::class, 'getGroupIdForDatabase' ], $groupIds );
190 }
191
192 return $dbr->newSelectQueryBuilder()
193 ->select( [ 'tgr_state', 'tgr_group', 'tgr_lang' ] )
194 ->from( self::TABLE_NAME )
195 ->where( $conditions )
196 ->caller( __METHOD__ )
197 ->fetchResultSet();
198 }
199
200 private function result2map( IResultWrapper $result, string $keyValue, string $valueValue ): array {
201 $map = [];
202 foreach ( $result as $row ) {
203 $map[$row->$keyValue] = $row->$valueValue;
204 }
205
206 return $map;
207 }
208
209 private static function getGroupIdForDatabase( string $groupId ): string {
210 // Check if length is more than 200 bytes
211 if ( strlen( $groupId ) <= 200 ) {
212 return $groupId;
213 }
214
215 // We take 160 bytes of the original string and append the md5 hash (32 bytes)
216 return mb_strcut( $groupId, 0, 160 ) . '||' . hash( 'md5', $groupId );
217 }
218}
Hook runner for the Translate extension.
Provides methods to get and change the state of a message group.
changeState(MessageGroup $group, string $code, string $newState, User $user)
getWorkflowState(string $groupId, string $languageCode)
Get the current workflow state for the given message group for the given language.
setGroupPriority(string $groupId, ?string $priority)
Store priority for message group.
Interface for message groups.
getId()
Returns the unique identifier for this group.
getLabel(?IContextSource $context=null)
Returns the human readable label (as plain text).