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\ILoadBalancer;
13use Wikimedia\Rdbms\IResultWrapper;
14
21 private HookRunner $hookRunner;
22 private ILoadBalancer $loadBalancer;
23 private const TABLE_NAME = 'translate_groupreviews';
25 private ?array $priorityCache = null;
26
27 public function __construct( ILoadBalancer $loadBalancer, HookRunner $hookRunner ) {
28 $this->loadBalancer = $loadBalancer;
29 $this->hookRunner = $hookRunner;
30 }
31
33 public function getState( MessageGroup $group, string $code ) {
34 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
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 $index = [ 'tgr_group', 'tgr_lang' ];
54 $row = [
55 'tgr_group' => self::getGroupIdForDatabase( $group->getId() ),
56 'tgr_lang' => $code,
57 'tgr_state' => $newState,
58 ];
59 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
60 $dbw->replace( self::TABLE_NAME, [ $index ], $row, __METHOD__ );
61
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,
70 ] );
71 // @todo
72 // $entry->setComment( $comment );
73
74 $logId = $entry->insert();
75 $entry->publish( $logId );
76
77 $this->hookRunner->onTranslateEventMessageGroupStateChange( $group, $code, $currentState, $newState );
78
79 return true;
80 }
81
82 public function getGroupPriority( string $group ): ?string {
83 $this->preloadGroupPriorities( __METHOD__ );
84 return $this->priorityCache[self::getGroupIdForDatabase( $group )] ?? null;
85 }
86
88 public function setGroupPriority( string $groupId, ?string $priority ): void {
89 $dbGroupId = self::getGroupIdForDatabase( $groupId );
90 if ( isset( $this->priorityCache ) ) {
91 $this->priorityCache[$dbGroupId] = $priority;
92 }
93
94 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
95 $row = [
96 'tgr_group' => $dbGroupId,
97 'tgr_lang' => '*priority',
98 'tgr_state' => $priority
99 ];
100
101 if ( $priority === null ) {
102 unset( $row['tgr_state'] );
103 $dbw->delete( self::TABLE_NAME, $row, __METHOD__ );
104 } else {
105 $index = [ 'tgr_group', 'tgr_lang' ];
106 $dbw->replace( self::TABLE_NAME, [ $index ], $row, __METHOD__ );
107 }
108 }
109
110 private function preloadGroupPriorities( string $caller ): void {
111 if ( isset( $this->priorityCache ) ) {
112 return;
113 }
114
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' ] )
120 ->caller( $caller )
121 ->fetchResultSet();
122
123 $this->priorityCache = $this->result2map( $res, 'tgr_group', 'tgr_state' );
124 }
125
132 public function getWorkflowState( string $groupId, string $languageCode ): ?string {
133 $result = $this->getWorkflowStates( [ $groupId ], [ $languageCode ] );
134 return $result->fetchRow()['tgr_state'] ?? null;
135 }
136
137 public function getWorkflowStatesForLanguage( string $languageCode, array $groupIds ): array {
138 $result = $this->getWorkflowStates( $groupIds, [ $languageCode ] );
139 $states = $this->result2map( $result, 'tgr_group', 'tgr_state' );
140
141 $finalResult = [];
142 foreach ( $groupIds as $groupId ) {
143 $dbGroupId = self::getGroupIdForDatabase( $groupId );
144 if ( isset( $states[ $dbGroupId ] ) ) {
145 $finalResult[ $groupId ] = $states[ $dbGroupId ];
146 }
147 }
148
149 return $finalResult;
150 }
151
152 public function getWorkflowStatesForGroup( string $groupId ): array {
153 $result = $this->getWorkflowStates( [ $groupId ], null );
154 return $this->result2map( $result, 'tgr_lang', 'tgr_state' );
155 }
156
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 !== ''
162 );
163
164 if ( $conditions === [] ) {
165 throw new InvalidArgumentException( 'Either the $groupId or the $languageCode should be provided' );
166 }
167
168 if ( isset( $conditions['tgr_group'] ) ) {
169 $conditions['tgr_group'] = array_map( [ self::class, 'getGroupIdForDatabase' ], $groupIds );
170 }
171
172 return $dbr->newSelectQueryBuilder()
173 ->select( [ 'tgr_state', 'tgr_group', 'tgr_lang' ] )
174 ->from( self::TABLE_NAME )
175 ->where( $conditions )
176 ->caller( __METHOD__ )
177 ->fetchResultSet();
178 }
179
180 private function result2map( IResultWrapper $result, string $keyValue, string $valueValue ): array {
181 $map = [];
182 foreach ( $result as $row ) {
183 $map[$row->$keyValue] = $row->$valueValue;
184 }
185
186 return $map;
187 }
188
189 private static function getGroupIdForDatabase( $groupId ): string {
190 $groupId = strval( $groupId );
191
192 // Check if length is more than 200 bytes
193 if ( strlen( $groupId ) <= 200 ) {
194 return $groupId;
195 }
196
197 // We take 160 bytes of the original string and append the md5 hash (32 bytes)
198 return mb_strcut( $groupId, 0, 160 ) . '||' . hash( 'md5', $groupId );
199 }
200}
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).