Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageGroupStatesUpdaterJob.php
Go to the documentation of this file.
1<?php
15use MediaWiki\MediaWikiServices;
16
27 public function __construct( $title, $params = [] ) {
28 parent::__construct( __CLASS__, $title, $params );
29 $this->removeDuplicates = true;
30 }
31
38 public static function onChange( MessageHandle $handle ) {
39 $job = self::newJob( $handle->getTitle() );
40 MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job );
41
42 return true;
43 }
44
49 public static function newJob( $title ) {
50 $job = new self( $title );
51
52 return $job;
53 }
54
55 public function run() {
56 $lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
57 if ( !$lb->waitForReplication() ) {
58 $this->logWarning( 'Continuing despite replication lag' );
59 }
60
61 $title = $this->title;
62 $handle = new MessageHandle( $title );
63 $code = $handle->getCode();
64
65 if ( !$code && !$handle->isValid() ) {
66 return true;
67 }
68
69 $groups = self::getGroupsWithTransitions( $handle );
70 $messageGroupReviewStore = Services::getInstance()->getMessageGroupReviewStore();
71 foreach ( $groups as $id => $transitions ) {
72 $group = MessageGroups::getGroup( $id );
73 $stats = MessageGroupStats::forItem( $id, $code, MessageGroupStats::FLAG_IMMEDIATE_WRITES );
74 $state = self::getNewState( $stats, $transitions );
75 if ( $state ) {
76 $messageGroupReviewStore->changeState( $group, $code, $state, FuzzyBot::getUser() );
77 }
78 }
79
80 return true;
81 }
82
83 public static function getGroupsWithTransitions( MessageHandle $handle ) {
84 $listeners = [];
85 foreach ( $handle->getGroupIds() as $id ) {
86 $group = MessageGroups::getGroup( $id );
87
88 // No longer exists?
89 if ( !$group ) {
90 continue;
91 }
92
93 $conds = $group->getMessageGroupStates()->getConditions();
94 if ( $conds ) {
95 $listeners[$id] = $conds;
96 }
97 }
98
99 return $listeners;
100 }
101
102 public static function getStatValue( $stats, $type ) {
103 $total = $stats[MessageGroupStats::TOTAL];
104 $translated = $stats[MessageGroupStats::TRANSLATED];
105 $outdated = $stats[MessageGroupStats::FUZZY];
106 $proofread = $stats[MessageGroupStats::PROOFREAD];
107
108 switch ( $type ) {
109 case 'UNTRANSLATED':
110 return $total - $translated - $outdated;
111 case 'OUTDATED':
112 return $outdated;
113 case 'TRANSLATED':
114 return $translated;
115 case 'PROOFREAD':
116 return $proofread;
117 default:
118 throw new InvalidArgumentException( "Unknown condition $type" );
119 }
120 }
121
122 public static function matchCondition( $value, $condition, $max ) {
123 switch ( $condition ) {
124 case 'ZERO':
125 return $value === 0;
126 case 'NONZERO':
127 return $value > 0;
128 case 'MAX':
129 return $value === $max;
130 default:
131 throw new InvalidArgumentException( "Unknown condition value $condition" );
132 }
133 }
134
141 public static function getNewState( $stats, $transitions ) {
142 foreach ( $transitions as $transition ) {
143 [ $newState, $conds ] = $transition;
144 $match = true;
145
146 foreach ( $conds as $type => $cond ) {
147 $statValue = self::getStatValue( $stats, $type );
148 $max = $stats[MessageGroupStats::TOTAL];
149 $match = $match && self::matchCondition( $statValue, $cond, $max );
150 // Conditions are AND, so no point trying more if no match
151 if ( !$match ) {
152 break;
153 }
154 }
155
156 if ( $match ) {
157 return $newState;
158 }
159 }
160
161 return false;
162 }
163}
Factory class for accessing message groups individually by id or all of them as a list.
Minimal service container.
Definition Services.php:44
FuzzyBot - the misunderstood workhorse.
Definition FuzzyBot.php:15
Logic for handling automatic message group state changes.
static getNewState( $stats, $transitions)
static onChange(MessageHandle $handle)
Hook: TranslateEventTranslationReview and also on translation changes.
const PROOFREAD
Array index.
const TOTAL
Array index.
const FUZZY
Array index.
const TRANSLATED
Array index.
static forItem( $id, $code, $flags=0)
Returns stats for given group in given language.
Class for pointing to messages, like Title class is for titles.
getGroupIds()
Returns all message group ids this message belongs to.
getTitle()
Get the original title.