X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fapi%2FApiErrorFormatter.php;h=8049cd84b983dd202dbcfcdee3e114cac6e14169;hb=ab9436fb9a20b5a335574e36fdd476becabe2dd1;hp=a37ecc2df993dfe6c22c1977577538446bf34b07;hpb=93350da7f1ab397b87c3ac3073ddfc1c857b272e;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/api/ApiErrorFormatter.php b/includes/api/ApiErrorFormatter.php index a37ecc2df9..8049cd84b9 100644 --- a/includes/api/ApiErrorFormatter.php +++ b/includes/api/ApiErrorFormatter.php @@ -153,9 +153,10 @@ class ApiErrorFormatter { * @param string|null $modulePath * @param StatusValue $status * @param string[]|string $types 'warning' and/or 'error' + * @param string[] $filter Messages to filter out (since 1.33) */ public function addMessagesFromStatus( - $modulePath, StatusValue $status, $types = [ 'warning', 'error' ] + $modulePath, StatusValue $status, $types = [ 'warning', 'error' ], array $filter = [] ) { if ( $status->isGood() || !$status->getErrors() ) { return; @@ -178,7 +179,9 @@ class ApiErrorFormatter { ->inLanguage( $this->lang ) ->title( $this->getDummyTitle() ) ->useDatabase( $this->useDB ); - $this->addWarningOrError( $tag, $modulePath, $msg ); + if ( !in_array( $msg->getKey(), $filter, true ) ) { + $this->addWarningOrError( $tag, $modulePath, $msg ); + } } } @@ -385,111 +388,3 @@ class ApiErrorFormatter { } } } - -/** - * Format errors and warnings in the old style, for backwards compatibility. - * @since 1.25 - * @deprecated Only for backwards compatibility, do not use - * @ingroup API - */ -// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps -class ApiErrorFormatter_BackCompat extends ApiErrorFormatter { - - /** - * @param ApiResult $result Into which data will be added - */ - public function __construct( ApiResult $result ) { - parent::__construct( $result, Language::factory( 'en' ), 'none', false ); - } - - public function getFormat() { - return 'bc'; - } - - public function arrayFromStatus( StatusValue $status, $type = 'error', $format = null ) { - if ( $status->isGood() || !$status->getErrors() ) { - return []; - } - - $result = []; - foreach ( $status->getErrorsByType( $type ) as $error ) { - $msg = ApiMessage::create( $error ); - $error = [ - 'message' => $msg->getKey(), - 'params' => $msg->getParams(), - 'code' => $msg->getApiCode(), - ] + $error; - ApiResult::setIndexedTagName( $error['params'], 'param' ); - $result[] = $error; - } - ApiResult::setIndexedTagName( $result, $type ); - - return $result; - } - - protected function formatMessageInternal( $msg, $format ) { - return [ - 'code' => $msg->getApiCode(), - 'info' => $msg->text(), - ] + $msg->getApiData(); - } - - /** - * Format an exception as an array - * @since 1.29 - * @param Exception|Throwable $exception - * @param array $options See parent::formatException(), plus - * - bc: (bool) Return only the string, not an array - * @return array|string - */ - public function formatException( $exception, array $options = [] ) { - $ret = parent::formatException( $exception, $options ); - return empty( $options['bc'] ) ? $ret : $ret['info']; - } - - protected function addWarningOrError( $tag, $modulePath, $msg ) { - $value = self::stripMarkup( $msg->text() ); - - if ( $tag === 'error' ) { - // In BC mode, only one error - $existingError = $this->result->getResultData( [ 'error' ] ); - if ( !is_array( $existingError ) || - !isset( $existingError['code'] ) || !isset( $existingError['info'] ) - ) { - $value = [ - 'code' => $msg->getApiCode(), - 'info' => $value, - ] + $msg->getApiData(); - $this->result->addValue( null, 'error', $value, - ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK ); - } - } else { - if ( $modulePath === null ) { - $moduleName = 'unknown'; - } else { - $i = strrpos( $modulePath, '+' ); - $moduleName = $i === false ? $modulePath : substr( $modulePath, $i + 1 ); - } - - // Don't add duplicate warnings - $tag .= 's'; - $path = [ $tag, $moduleName ]; - $oldWarning = $this->result->getResultData( [ $tag, $moduleName, $tag ] ); - if ( $oldWarning !== null ) { - $warnPos = strpos( $oldWarning, $value ); - // If $value was found in $oldWarning, check if it starts at 0 or after "\n" - if ( $warnPos !== false && ( $warnPos === 0 || $oldWarning[$warnPos - 1] === "\n" ) ) { - // Check if $value is followed by "\n" or the end of the $oldWarning - $warnPos += strlen( $value ); - if ( strlen( $oldWarning ) <= $warnPos || $oldWarning[$warnPos] === "\n" ) { - return; - } - } - // If there is a warning already, append it to the existing one - $value = "$oldWarning\n$value"; - } - $this->result->addContentValue( $path, $tag, $value, - ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK ); - } - } -}