Skip to content

Commit

Permalink
Merge "REST: Don't assume all non-Ok status contain messages"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Sep 18, 2024
2 parents 36231a6 + 69f8ca9 commit e381c0b
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions repo/rest-api/src/Infrastructure/DataAccess/EntityUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MediaWiki\Permissions\PermissionManager;
use MediaWiki\Status\Status;
use MediaWiki\User\User;
use MessageSpecifier;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Wikibase\DataModel\Entity\EntityDocument;
Expand Down Expand Up @@ -98,8 +99,9 @@ private function createOrUpdate(
);

if ( !$status->isOK() ) {
if ( $status->getMessages()[0]->getKey() === 'wikibase-error-entity-too-big' ) {
$maxSizeInKiloBytes = $status->getMessages()[0]->getParams()[0]['size'] / 1024;
$entityTooBigError = $this->findErrorInStatus( $status, 'wikibase-error-entity-too-big' );
if ( $entityTooBigError ) {
$maxSizeInKiloBytes = $entityTooBigError->getParams()[0]['size'] / 1024;
throw new ResourceTooLargeException( $maxSizeInKiloBytes );
}

Expand All @@ -116,11 +118,20 @@ private function createOrUpdate(
}

private function isPreventedEdit( Status $status ): bool {
$errorCode = $status->getMessages()[0]->getKey();
return $this->findErrorInStatus( $status, 'actionthrottledtext' )
|| $this->findErrorInStatus( $status, 'spam-blacklisted' )
|| $this->findErrorInStatus( $status, 'abusefilter' );
}

private function findErrorInStatus( Status $status, string $errorCode ): ?MessageSpecifier {
foreach ( $status->getMessages() as $message ) {
// prefix comparison to cover different kinds of spam-blacklisted or abusefilter errors
if ( strpos( $message->getKey(), $errorCode ) === 0 ) {
return $message;
}
}

return $errorCode === 'actionthrottledtext'
|| strpos( $errorCode, 'spam-blacklisted' ) === 0
|| strpos( $errorCode, 'abusefilter' ) === 0;
return null;
}

private function checkBotRightIfProvided( User $user, bool $isBot ): void {
Expand Down

0 comments on commit e381c0b

Please sign in to comment.