25 protected static $right =
'translate-messagereview';
27 private $revisionLookup;
29 private $titleFormatter;
31 private $loadBalancer;
33 public function __construct(
36 RevisionLookup $revisionLookup,
37 TitleFormatter $titleFormatter,
38 ILoadBalancer $loadBalancer
40 parent::__construct( $main, $moduleName );
41 $this->revisionLookup = $revisionLookup;
42 $this->titleFormatter = $titleFormatter;
43 $this->loadBalancer = $loadBalancer;
46 public function execute() {
47 $this->checkUserRightsAny( self::$right );
49 $params = $this->extractRequestParams();
51 $revRecord = $this->revisionLookup->getRevisionById( $params[
'revision'] );
53 $this->dieWithError( [
'apierror-nosuchrevid', $params[
'revision'] ],
'invalidrevision' );
56 $status = $this->getReviewBlockers( $this->getUser(), $revRecord );
57 if ( !$status->isGood() ) {
58 if ( $status->hasMessage(
'blocked' ) ) {
59 $this->dieBlocked( $this->getUser()->getBlock() );
61 $this->dieStatus( $status );
65 $ok = $this->doReview( $this->getUser(), $revRecord );
67 $this->addWarning(
'apiwarn-translate-alreadyreviewedbyyou' );
70 $prefixedText = $this->titleFormatter->getPrefixedText( $revRecord->getPageAsLinkTarget() );
71 $output = [
'review' => [
72 'title' => $prefixedText,
73 'pageid' => $revRecord->getPageId(),
74 'revision' => $revRecord->getId()
77 $this->getResult()->addValue(
null, $this->getModuleName(), $output );
84 private function doReview( User $user, RevisionRecord $revRecord ):
bool {
85 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
86 $table =
'translate_reviews';
88 'trr_user' => $user->getId(),
89 'trr_page' => $revRecord->getPageId(),
90 'trr_revision' => $revRecord->getId(),
92 $options = [
'IGNORE' ];
93 $dbw->insert( $table, $row, __METHOD__, $options );
95 if ( !$dbw->affectedRows() ) {
99 $title = $revRecord->getPageAsLinkTarget();
101 $entry =
new ManualLogEntry(
'translationreview',
'message' );
102 $entry->setPerformer( $user );
103 $entry->setTarget( $title );
104 $entry->setParameters( [
105 '4::revision' => $revRecord->getId(),
108 $logid = $entry->insert();
109 $entry->publish( $logid );
112 $this->getHookContainer()->run(
'TranslateEventTranslationReview', [ $handle ] );
121 private function getReviewBlockers( User $user, RevisionRecord $revRecord ): Status {
122 if ( !$user->isAllowed( self::$right ) ) {
123 return Status::newFatal(
'apierror-permissiondenied-generic' );
126 if ( $user->getBlock() ) {
127 return Status::newFatal(
'blocked' );
130 $title = $revRecord->getPageAsLinkTarget();
132 if ( !$handle->isValid() ) {
133 return Status::newFatal(
'apierror-translate-unknownmessage' );
136 if ( $user->equals( $revRecord->getUser() ) ) {
137 return Status::newFatal(
'apierror-translate-owntranslation' );
140 if ( $handle->isFuzzy() ) {
141 return Status::newFatal(
'apierror-translate-fuzzymessage' );
144 return Status::newGood();
147 public function isWriteMode():
bool {
151 public function needsToken():
string {
155 protected function getAllowedParams(): array {
158 ParamValidator::PARAM_TYPE =>
'integer',
159 ParamValidator::PARAM_REQUIRED =>
true,
162 ParamValidator::PARAM_TYPE =>
'string',
163 ParamValidator::PARAM_REQUIRED =>
true,
168 protected function getExamplesMessages(): array {
170 'action=translationreview&revision=1&token=foo'
171 =>
'apihelp-translationreview-example-1',