00001 <?php
00015 class ApiGroupReview extends ApiBase {
00016 protected static $right = 'translate-groupreview';
00017 protected static $salt = 'translate-groupreview';
00018
00019 public function execute() {
00020 $user = $this->getUser();
00021 $requestParams = $this->extractRequestParams();
00022
00023 $group = MessageGroups::getGroup( $requestParams['group'] );
00024 $code = $requestParams['language'];
00025
00026 if ( !$group || MessageGroups::isDynamic( $group ) ) {
00027 $this->dieUsageMsg( array( 'missingparam', 'group' ) );
00028 }
00029 $stateConfig = $group->getMessageGroupStates()->getStates();
00030 if ( !$stateConfig ) {
00031 $this->dieUsage( 'Message group review not in use', 'disabled' );
00032 }
00033
00034 if ( !$user->isAllowed( self::$right ) ) {
00035 $this->dieUsage( 'Permission denied', 'permissiondenied' );
00036 }
00037
00038 $requestParams = $this->extractRequestParams();
00039
00040 $languages = Language::getLanguageNames( false );
00041 if ( !isset( $languages[$code] ) ) {
00042 $this->dieUsageMsg( array( 'missingparam', 'language' ) );
00043 }
00044
00045 $targetState = $requestParams['state'];
00046 if ( !isset( $stateConfig[$targetState] ) ) {
00047 $this->dieUsage( 'The requested state is invalid', 'invalidstate' );
00048 }
00049
00050 if ( is_array( $stateConfig[$targetState] )
00051 && isset( $stateConfig[$targetState]['right'] )
00052 && !$user->isAllowed( $stateConfig[$targetState]['right'] )
00053 ) {
00054 $this->dieUsage( 'Permission denied', 'permissiondenied' );
00055 }
00056
00057 self::changeState( $group, $code, $targetState, $user );
00058
00059 $output = array( 'review' => array(
00060 'group' => $group->getId(),
00061 'language' => $code,
00062 'state' => $targetState,
00063 ) );
00064
00065 $this->getResult()->addValue( null, $this->getModuleName(), $output );
00066 }
00067
00068 public static function getState( MessageGroup $group, $code ) {
00069 $dbw = wfGetDB( DB_MASTER );
00070 $table = 'translate_groupreviews';
00071
00072 $field = 'tgr_state';
00073 $conds = array(
00074 'tgr_group' => $group->getId(),
00075 'tgr_lang' => $code
00076 );
00077
00078 return $dbw->selectField( $table, $field, $conds, __METHOD__ );
00079 }
00080
00081 public static function changeState( MessageGroup $group, $code, $newState, User $user ) {
00082 $currentState = self::getState( $group, $code );
00083 if ( $currentState === $newState ) {
00084 return false;
00085 }
00086
00087 $table = 'translate_groupreviews';
00088 $index = array( 'tgr_group', 'tgr_language' );
00089 $row = array(
00090 'tgr_group' => $group->getId(),
00091 'tgr_lang' => $code,
00092 'tgr_state' => $newState,
00093 );
00094
00095 $dbw = wfGetDB( DB_MASTER );
00096 $dbw->replace( $table, array( $index ), $row, __METHOD__ );
00097
00098 $entry = new ManualLogEntry( 'translationreview', 'group' );
00099 $entry->setPerformer( $user );
00100 $entry->setTarget( SpecialPage::getTitleFor( 'Translate', $group->getId() ) );
00101
00102
00103 $entry->setParameters( array(
00104 '4::language' => $code,
00105 '5::group-label' => $group->getLabel(),
00106 '6::old-state' => $currentState,
00107 '7::new-state' => $newState,
00108 ) );
00109
00110 $logid = $entry->insert();
00111 $entry->publish( $logid );
00112
00113 wfRunHooks( 'TranslateEventMessageGroupStateChange',
00114 array( $group, $code, $currentState, $newState ) );
00115
00116 return true;
00117 }
00118
00119 public function isWriteMode() {
00120 return true;
00121 }
00122
00123 public function needsToken() {
00124 return true;
00125 }
00126
00127 public function getTokenSalt() {
00128 return self::$salt;
00129 }
00130
00131 public function getAllowedParams() {
00132 return array(
00133 'group' => array(
00134 ApiBase::PARAM_TYPE => 'string',
00135 ApiBase::PARAM_REQUIRED => true,
00136 ),
00137 'language' => array(
00138 ApiBase::PARAM_TYPE => 'string',
00139 ApiBase::PARAM_DFLT => 'en',
00140 ),
00141 'state' => array(
00142 ApiBase::PARAM_TYPE => 'string',
00143 ApiBase::PARAM_REQUIRED => true,
00144 ),
00145 'token' => array(
00146 ApiBase::PARAM_TYPE => 'string',
00147 ApiBase::PARAM_REQUIRED => true,
00148 ),
00149 );
00150 }
00151
00152 public function getParamDescription() {
00153 $action = TranslateUtils::getTokenAction( 'groupreview' );
00154
00155 return array(
00156 'group' => 'Message group',
00157 'language' => 'Language code',
00158 'state' => 'The new state for the group',
00159 'token' => "A token previously acquired with $action",
00160 );
00161 }
00162
00163 public function getDescription() {
00164 return 'Set message group workflow states';
00165 }
00166
00167 public function getPossibleErrors() {
00168 $right = self::$right;
00169
00170 return array_merge( parent::getPossibleErrors(), array(
00171 array( 'code' => 'permissiondenied', 'info' => "You must have $right right" ),
00172 array( 'code' => 'disabled', 'info' => 'Message group workflows are not in use' ),
00173 array(
00174 'code' => 'sameworkflowstate',
00175 'info' => 'The requested state is identical to the current state'
00176 ),
00177 array( 'code' => 'invalidstate', 'info' => 'The requested state is invalid' ),
00178 ) );
00179 }
00180
00181 public function getExamples() {
00182 $group = 'page-Example';
00183
00184 return array(
00185 "api.php?action=groupreview&group=$group&language=de&state=ready&token=foo",
00186 );
00187 }
00188
00189 public function getVersion() {
00190 return __CLASS__ . ': ' . TRANSLATE_VERSION;
00191 }
00192
00193 public static function getToken() {
00194 $user = RequestContext::getMain()->getUser();
00195 if ( !$user->isAllowed( self::$right ) ) {
00196 return false;
00197 }
00198
00199 return $user->getEditToken( self::$salt );
00200 }
00201
00202 public static function injectTokenFunction( &$list ) {
00203 $list['groupreview'] = array( __CLASS__, 'getToken' );
00204
00205 return true;
00206 }
00207
00208 public static function getRight() {
00209 return self::$right;
00210 }
00211 }