MessageGroupStatesUpdaterJob.php
Go to the documentation of this file.00001 <?php
00016 class MessageGroupStatesUpdaterJob extends Job {
00017
00022 public static function onChange( MessageHandle $handle ) {
00023 $job = self::newJob( $handle->getTitle() );
00024 $job->insert();
00025
00026 return true;
00027 }
00028
00033 public static function newJob( $title ) {
00034 $job = new self( $title );
00035
00036 return $job;
00037 }
00038
00039 public function __construct( $title, $params = array(), $id = 0 ) {
00040 parent::__construct( __CLASS__, $title, $params, $id );
00041 }
00042
00043 public function run() {
00044 $title = $this->title;
00045 $handle = new MessageHandle( $title );
00046 $code = $handle->getCode();
00047
00048 if ( !$handle->isValid() && !$code ) {
00049 return true;
00050 }
00051
00052 $groups = self::getGroupsWithTransitions( $handle );
00053 foreach ( $groups as $id => $transitions ) {
00054 $group = MessageGroups::getGroup( $id );
00055 $stats = MessageGroupStats::forItem( $id, $code );
00056 $state = self::getNewState( $stats, $transitions );
00057 if ( $state ) {
00058 ApiGroupReview::changeState( $group, $code, $state, FuzzyBot::getUser() );
00059 }
00060 }
00061
00062 return true;
00063 }
00064
00065 public static function getGroupsWithTransitions( MessageHandle $handle ) {
00066 $listeners = array();
00067 foreach ( $handle->getGroupIds() as $id ) {
00068 $group = MessageGroups::getGroup( $id );
00069
00070
00071 if ( !$group ) {
00072 continue;
00073 }
00074
00075 $conds = $group->getMessageGroupStates()->getConditions();
00076 if ( $conds ) {
00077 $listeners[$id] = $conds;
00078 }
00079 }
00080
00081 return $listeners;
00082 }
00083
00084 public static function getStatValue( $stats, $type ) {
00085 $total = $stats[MessageGroupStats::TOTAL];
00086 $translated = $stats[MessageGroupStats::TRANSLATED];
00087 $outdated = $stats[MessageGroupStats::FUZZY];
00088 $proofread = $stats[MessageGroupStats::PROOFREAD];
00089
00090 switch ( $type ) {
00091 case 'UNTRANSLATED':
00092 return $total - $translated - $outdated;
00093 case 'OUTDATED':
00094 return $outdated;
00095 case 'TRANSLATED':
00096 return $translated;
00097 case 'PROOFREAD':
00098 return $proofread;
00099 default:
00100 throw new MWException( "Unknown condition $type" );
00101 }
00102 }
00103
00104 public static function matchCondition( $value, $condition, $max ) {
00105 switch ( $condition ) {
00106 case 'ZERO':
00107 return $value === 0;
00108 case 'NONZERO':
00109 return $value > 0;
00110 case 'MAX':
00111 return $value === $max;
00112 default:
00113 throw new MWException( "Unknown condition value $condition" );
00114 }
00115 }
00116
00117 public static function getNewState( $stats, $transitions ) {
00118 foreach ( $transitions as $transition ) {
00119 list( $newState, $conds ) = $transition;
00120 $match = true;
00121
00122 foreach ( $conds as $type => $cond ) {
00123 $statValue = self::getStatValue( $stats, $type );
00124 $max = $stats[MessageGroupStats::TOTAL];
00125 $match = $match && self::matchCondition( $statValue, $cond, $max );
00126
00127 if ( !$match ) {
00128 break;
00129 }
00130 }
00131
00132 if ( $match ) {
00133 return $newState;
00134 }
00135 }
00136
00137 return false;
00138 }
00139 }