27 protected static $right =
'translate-messagereview';
28 private RevisionLookup $revisionLookup;
29 private TitleFormatter $titleFormatter;
30 private ILoadBalancer $loadBalancer;
33 public function __construct(
36 RevisionLookup $revisionLookup,
37 TitleFormatter $titleFormatter,
38 ILoadBalancer $loadBalancer,
41 parent::__construct( $main, $moduleName );
42 $this->revisionLookup = $revisionLookup;
43 $this->titleFormatter = $titleFormatter;
44 $this->loadBalancer = $loadBalancer;
45 $this->hookRunner = $hookRunner;
48 public function execute() {
49 $this->checkUserRightsAny( self::$right );
51 $params = $this->extractRequestParams();
53 $revRecord = $this->revisionLookup->getRevisionById( $params[
'revision'] );
55 $this->dieWithError( [
'apierror-nosuchrevid', $params[
'revision'] ],
'invalidrevision' );
58 $status = $this->getReviewBlockers( $this->getUser(), $revRecord );
59 if ( !$status->isGood() ) {
60 if ( $status->hasMessage(
'blocked' ) ) {
61 $this->dieBlocked( $this->getUser()->getBlock() );
63 $this->dieStatus( $status );
67 $ok = $this->doReview( $this->getUser(), $revRecord );
69 $this->addWarning(
'apiwarn-translate-alreadyreviewedbyyou' );
72 $prefixedText = $this->titleFormatter->getPrefixedText( $revRecord->getPageAsLinkTarget() );
73 $output = [
'review' => [
74 'title' => $prefixedText,
75 'pageid' => $revRecord->getPageId(),
76 'revision' => $revRecord->getId()
79 $this->getResult()->addValue(
null, $this->getModuleName(), $output );
86 private function doReview( User $user, RevisionRecord $revRecord ):
bool {
87 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
88 $dbw->newInsertQueryBuilder()
89 ->insertInto(
'translate_reviews' )
92 'trr_user' => $user->getId(),
93 'trr_page' => $revRecord->getPageId(),
94 'trr_revision' => $revRecord->getId(),
96 ->caller( __METHOD__ )
99 if ( !$dbw->affectedRows() ) {
103 $title = $revRecord->getPageAsLinkTarget();
105 $entry =
new ManualLogEntry(
'translationreview',
'message' );
106 $entry->setPerformer( $user );
107 $entry->setTarget( $title );
108 $entry->setParameters( [
109 '4::revision' => $revRecord->getId(),
112 $logid = $entry->insert();
113 $entry->publish( $logid );
116 $this->hookRunner->onTranslateEventTranslationReview( $handle );
125 private function getReviewBlockers( User $user, RevisionRecord $revRecord ): Status {
126 if ( !$user->isAllowed( self::$right ) ) {
127 return Status::newFatal(
'apierror-permissiondenied-generic' );
130 if ( $user->getBlock() ) {
131 return Status::newFatal(
'blocked' );
134 $title = $revRecord->getPageAsLinkTarget();
136 if ( !$handle->isValid() ) {
137 return Status::newFatal(
'apierror-translate-unknownmessage' );
140 if ( $user->equals( $revRecord->getUser() ) ) {
141 return Status::newFatal(
'apierror-translate-owntranslation' );
144 if ( $handle->isFuzzy() ) {
145 return Status::newFatal(
'apierror-translate-fuzzymessage' );
148 return Status::newGood();
151 public function isWriteMode():
bool {
155 public function needsToken():
string {
159 protected function getAllowedParams(): array {
162 ParamValidator::PARAM_TYPE =>
'integer',
163 ParamValidator::PARAM_REQUIRED =>
true,
168 protected function getExamplesMessages(): array {
170 'action=translationreview&revision=1&token=foo'
171 =>
'apihelp-translationreview-example-1',