From: Zoranzoki21 Date: Sat, 20 Apr 2019 22:38:11 +0000 (+0200) Subject: Split ApiErrorFormatter.php to have one class in one file X-Git-Tag: 1.34.0-rc.0~1886^2 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22articles%22%2C%22id_article=%24id_article%22%29%20.%20%22?a=commitdiff_plain;h=2d4eacf50b4c86797dd5cf70d3d1c84db5627e33;p=lhc%2Fweb%2Fwiklou.git Split ApiErrorFormatter.php to have one class in one file Change-Id: I5c6904e32f9bc7fe3aac511c56b03757bfde15a9 --- diff --git a/.phpcs.xml b/.phpcs.xml index fef07e6204..b60a3aff66 100644 --- a/.phpcs.xml +++ b/.phpcs.xml @@ -189,7 +189,6 @@ Whitelist existing violations, but enable the sniff to prevent any new occurrences. --> - */includes/api/ApiErrorFormatter\.php */includes/parser/Preprocessor_DOM\.php */includes/parser/Preprocessor_Hash\.php */includes/parser/Preprocessor\.php diff --git a/autoload.php b/autoload.php index 4f41c8af91..35137ab943 100644 --- a/autoload.php +++ b/autoload.php @@ -39,7 +39,7 @@ $wgAutoloadLocalClasses = [ 'ApiEditPage' => __DIR__ . '/includes/api/ApiEditPage.php', 'ApiEmailUser' => __DIR__ . '/includes/api/ApiEmailUser.php', 'ApiErrorFormatter' => __DIR__ . '/includes/api/ApiErrorFormatter.php', - 'ApiErrorFormatter_BackCompat' => __DIR__ . '/includes/api/ApiErrorFormatter.php', + 'ApiErrorFormatter_BackCompat' => __DIR__ . '/includes/api/ApiErrorFormatter_BackCompat.php', 'ApiExpandTemplates' => __DIR__ . '/includes/api/ApiExpandTemplates.php', 'ApiFeedContributions' => __DIR__ . '/includes/api/ApiFeedContributions.php', 'ApiFeedRecentChanges' => __DIR__ . '/includes/api/ApiFeedRecentChanges.php', diff --git a/includes/api/ApiErrorFormatter.php b/includes/api/ApiErrorFormatter.php index 9669464733..8049cd84b9 100644 --- a/includes/api/ApiErrorFormatter.php +++ b/includes/api/ApiErrorFormatter.php @@ -388,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 ); - } - } -} diff --git a/includes/api/ApiErrorFormatter_BackCompat.php b/includes/api/ApiErrorFormatter_BackCompat.php new file mode 100644 index 0000000000..6aee4cb216 --- /dev/null +++ b/includes/api/ApiErrorFormatter_BackCompat.php @@ -0,0 +1,127 @@ +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 ); + } + } +}