ApiTranslationReview.php

Go to the documentation of this file.
00001 <?php
00015 class ApiTranslationReview extends ApiBase {
00016     protected static $right = 'translate-messagereview';
00017     protected static $salt = 'translate-messagereview';
00018 
00019     public function execute() {
00020         if ( !$this->getUser()->isAllowed( self::$right ) ) {
00021             $this->dieUsage( 'Permission denied', 'permissiondenied' );
00022         }
00023 
00024         $params = $this->extractRequestParams();
00025 
00026         $revision = Revision::newFromId( $params['revision'] );
00027         if ( !$revision ) {
00028             $this->dieUsage( 'Invalid revision', 'invalidrevision' );
00029         }
00030 
00031         $error = self::getReviewBlockers( $this->getUser(), $revision );
00032         switch ( $error ) {
00033             case '':
00034                 // Everything is okay
00035                 break;
00036             case 'permissiondenied':
00037                 $this->dieUsage( 'Permission denied', $error );
00038                 break; // Unreachable, but throws off code analyzer.
00039             case 'unknownmessage':
00040                 $this->dieUsage( 'Unknown message', $error );
00041                 break; // Unreachable, but throws off code analyzer.
00042             case 'owntranslation':
00043                 $this->dieUsage( 'Cannot review own translations', $error );
00044                 break; // Unreachable, but throws off code analyzer.
00045             case 'fuzzymessage':
00046                 $this->dieUsage( 'Cannot review fuzzy translations', $error );
00047                 break; // Unreachable, but throws off code analyzer.
00048             default:
00049                 $this->dieUsage( 'Unknown error', $error );
00050         }
00051 
00052         $ok = self::doReview( $this->getUser(), $revision );
00053         if ( !$ok ) {
00054             $this->setWarning( 'Already marked as reviewed by you' );
00055         }
00056 
00057         $output = array( 'review' => array(
00058             'title' => $revision->getTitle()->getPrefixedText(),
00059             'pageid' => $revision->getPage(),
00060             'revision' => $revision->getId()
00061         ) );
00062 
00063         $this->getResult()->addValue( null, $this->getModuleName(), $output );
00064     }
00065 
00073     public static function doReview( User $user, Revision $revision, $comment = null ) {
00074         $dbw = wfGetDB( DB_MASTER );
00075         $table = 'translate_reviews';
00076         $row = array(
00077             'trr_user' => $user->getId(),
00078             'trr_page' => $revision->getPage(),
00079             'trr_revision' => $revision->getId(),
00080         );
00081         $options = array( 'IGNORE' );
00082         $dbw->insert( $table, $row, __METHOD__, $options );
00083 
00084         if ( !$dbw->affectedRows() ) {
00085             return false;
00086         }
00087 
00088         $title = $revision->getTitle();
00089 
00090         $entry = new ManualLogEntry( 'translationreview', 'message' );
00091         $entry->setPerformer( $user );
00092         $entry->setTarget( $title );
00093         $entry->setComment( $comment );
00094         $entry->setParameters( array(
00095             '4::revision' => $revision->getId(),
00096         ) );
00097 
00098         $logid = $entry->insert();
00099         $entry->publish( $logid );
00100 
00101         $handle = new MessageHandle( $title );
00102         wfRunHooks( 'TranslateEventTranslationReview', array( $handle ) );
00103 
00104         return true;
00105     }
00106 
00114     public static function getReviewBlockers( User $user, Revision $revision ) {
00115         if ( !$user->isAllowed( self::$right ) ) {
00116             return 'permissiondenied';
00117         }
00118 
00119         $title = $revision->getTitle();
00120         $handle = new MessageHandle( $title );
00121         if ( !$handle->isValid() ) {
00122             return 'unknownmessage';
00123         }
00124 
00125         if ( $revision->getUser() == $user->getId() ) {
00126             return 'owntranslation';
00127         }
00128 
00129         if ( $handle->isFuzzy() ) {
00130             return 'fuzzymessage';
00131         }
00132 
00133         return '';
00134     }
00135 
00136     public function isWriteMode() {
00137         return true;
00138     }
00139 
00140     public function needsToken() {
00141         return true;
00142     }
00143 
00144     public function getTokenSalt() {
00145         return self::$salt;
00146     }
00147 
00148     public function getAllowedParams() {
00149         return array(
00150             'revision' => array(
00151                 ApiBase::PARAM_TYPE => 'integer',
00152                 ApiBase::PARAM_REQUIRED => true,
00153             ),
00154             'token' => array(
00155                 ApiBase::PARAM_TYPE => 'string',
00156                 ApiBase::PARAM_REQUIRED => true,
00157             ),
00158         );
00159     }
00160 
00161     public function getParamDescription() {
00162         $action = TranslateUtils::getTokenAction( 'translationreview' );
00163 
00164         return array(
00165             'revision' => 'The revision number to review',
00166             'token' => "A token previously acquired with $action",
00167         );
00168     }
00169 
00170     public function getDescription() {
00171         return 'Mark translations reviewed';
00172     }
00173 
00174     public function getPossibleErrors() {
00175         $right = self::$right;
00176 
00177         return array_merge( parent::getPossibleErrors(), array(
00178             array( 'code' => 'permissiondenied', 'info' => "You must have $right right" ),
00179             array( 'code' => 'unknownmessage', 'info' => 'Title $1 does not belong to a message group' ),
00180             array( 'code' => 'fuzzymessage', 'info' => 'Cannot review fuzzy translations' ),
00181             array( 'code' => 'owntranslation', 'info' => 'Cannot review own translations' ),
00182             array( 'code' => 'invalidrevision', 'info' => 'Revision $1 is invalid' ),
00183         ) );
00184     }
00185 
00186     public function getExamples() {
00187         return array(
00188             'api.php?action=translationreview&revision=1&token=foo',
00189         );
00190     }
00191 
00192     public function getVersion() {
00193         return __CLASS__ . ': ' . TRANSLATE_VERSION;
00194     }
00195 
00196     public static function getToken() {
00197         $user = RequestContext::getMain()->getUser();
00198         if ( !$user->isAllowed( self::$right ) ) {
00199             return false;
00200         }
00201 
00202         return $user->getEditToken( self::$salt );
00203     }
00204 
00205     public static function injectTokenFunction( &$list ) {
00206         $list['translationreview'] = array( __CLASS__, 'getToken' );
00207 
00208         return true; // Hooks must return bool
00209     }
00210 
00211     public static function getRight() {
00212         return self::$right;
00213     }
00214 }
Generated on Tue Oct 29 00:00:23 2013 for MediaWiki Translate Extension by  doxygen 1.6.3