26 protected static $right =
'translate-messagereview';
27 private RevisionLookup $revisionLookup;
28 private TitleFormatter $titleFormatter;
29 private ILoadBalancer $loadBalancer;
32 public function __construct(
35 RevisionLookup $revisionLookup,
36 TitleFormatter $titleFormatter,
37 ILoadBalancer $loadBalancer,
40 parent::__construct( $main, $moduleName );
41 $this->revisionLookup = $revisionLookup;
42 $this->titleFormatter = $titleFormatter;
43 $this->loadBalancer = $loadBalancer;
44 $this->hookRunner = $hookRunner;
47 public function execute() {
48 $this->checkUserRightsAny( self::$right );
50 $params = $this->extractRequestParams();
52 $revRecord = $this->revisionLookup->getRevisionById( $params[
'revision'] );
54 $this->dieWithError( [
'apierror-nosuchrevid', $params[
'revision'] ],
'invalidrevision' );
57 $status = $this->getReviewBlockers( $this->getUser(), $revRecord );
58 if ( !$status->isGood() ) {
59 if ( $status->hasMessage(
'blocked' ) ) {
60 $this->dieBlocked( $this->getUser()->getBlock() );
62 $this->dieStatus( $status );
66 $ok = $this->doReview( $this->getUser(), $revRecord );
68 $this->addWarning(
'apiwarn-translate-alreadyreviewedbyyou' );
71 $prefixedText = $this->titleFormatter->getPrefixedText( $revRecord->getPageAsLinkTarget() );
72 $output = [
'review' => [
73 'title' => $prefixedText,
74 'pageid' => $revRecord->getPageId(),
75 'revision' => $revRecord->getId()
78 $this->getResult()->addValue(
null, $this->getModuleName(), $output );
85 private function doReview( User $user, RevisionRecord $revRecord ):
bool {
86 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
87 $table =
'translate_reviews';
89 'trr_user' => $user->getId(),
90 'trr_page' => $revRecord->getPageId(),
91 'trr_revision' => $revRecord->getId(),
93 $options = [
'IGNORE' ];
94 $dbw->insert( $table, $row, __METHOD__, $options );
96 if ( !$dbw->affectedRows() ) {
100 $title = $revRecord->getPageAsLinkTarget();
102 $entry =
new ManualLogEntry(
'translationreview',
'message' );
103 $entry->setPerformer( $user );
104 $entry->setTarget( $title );
105 $entry->setParameters( [
106 '4::revision' => $revRecord->getId(),
109 $logid = $entry->insert();
110 $entry->publish( $logid );
113 $this->hookRunner->onTranslateEventTranslationReview( $handle );
122 private function getReviewBlockers( User $user, RevisionRecord $revRecord ): Status {
123 if ( !$user->isAllowed( self::$right ) ) {
124 return Status::newFatal(
'apierror-permissiondenied-generic' );
127 if ( $user->getBlock() ) {
128 return Status::newFatal(
'blocked' );
131 $title = $revRecord->getPageAsLinkTarget();
133 if ( !$handle->isValid() ) {
134 return Status::newFatal(
'apierror-translate-unknownmessage' );
137 if ( $user->equals( $revRecord->getUser() ) ) {
138 return Status::newFatal(
'apierror-translate-owntranslation' );
141 if ( $handle->isFuzzy() ) {
142 return Status::newFatal(
'apierror-translate-fuzzymessage' );
145 return Status::newGood();
148 public function isWriteMode():
bool {
152 public function needsToken():
string {
156 protected function getAllowedParams(): array {
159 ParamValidator::PARAM_TYPE =>
'integer',
160 ParamValidator::PARAM_REQUIRED =>
true,
163 ParamValidator::PARAM_TYPE =>
'string',
164 ParamValidator::PARAM_REQUIRED =>
true,
169 protected function getExamplesMessages(): array {
171 'action=translationreview&revision=1&token=foo'
172 =>
'apihelp-translationreview-example-1',