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