ApiErrorFormatter_BackCompat.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. /**
  21. * Format errors and warnings in the old style, for backwards compatibility.
  22. * @since 1.25
  23. * @deprecated Only for backwards compatibility, do not use
  24. * @ingroup API
  25. */
  26. // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
  27. class ApiErrorFormatter_BackCompat extends ApiErrorFormatter {
  28. /**
  29. * @param ApiResult $result Into which data will be added
  30. */
  31. public function __construct( ApiResult $result ) {
  32. parent::__construct( $result, Language::factory( 'en' ), 'none', false );
  33. }
  34. public function getFormat() {
  35. return 'bc';
  36. }
  37. public function arrayFromStatus( StatusValue $status, $type = 'error', $format = null ) {
  38. if ( $status->isGood() || !$status->getErrors() ) {
  39. return [];
  40. }
  41. $result = [];
  42. foreach ( $status->getErrorsByType( $type ) as $error ) {
  43. $msg = ApiMessage::create( $error );
  44. $error = [
  45. 'message' => $msg->getKey(),
  46. 'params' => $msg->getParams(),
  47. 'code' => $msg->getApiCode(),
  48. ] + $error;
  49. ApiResult::setIndexedTagName( $error['params'], 'param' );
  50. $result[] = $error;
  51. }
  52. ApiResult::setIndexedTagName( $result, $type );
  53. return $result;
  54. }
  55. protected function formatMessageInternal( $msg, $format ) {
  56. return [
  57. 'code' => $msg->getApiCode(),
  58. 'info' => $msg->text(),
  59. ] + $msg->getApiData();
  60. }
  61. /**
  62. * Format an exception as an array
  63. * @since 1.29
  64. * @param Exception|Throwable $exception
  65. * @param array $options See parent::formatException(), plus
  66. * - bc: (bool) Return only the string, not an array
  67. * @return array|string
  68. */
  69. public function formatException( $exception, array $options = [] ) {
  70. $ret = parent::formatException( $exception, $options );
  71. return empty( $options['bc'] ) ? $ret : $ret['info'];
  72. }
  73. protected function addWarningOrError( $tag, $modulePath, $msg ) {
  74. $value = self::stripMarkup( $msg->text() );
  75. if ( $tag === 'error' ) {
  76. // In BC mode, only one error
  77. $existingError = $this->result->getResultData( [ 'error' ] );
  78. if ( !is_array( $existingError ) ||
  79. !isset( $existingError['code'] ) || !isset( $existingError['info'] )
  80. ) {
  81. $value = [
  82. 'code' => $msg->getApiCode(),
  83. 'info' => $value,
  84. ] + $msg->getApiData();
  85. $this->result->addValue( null, 'error', $value,
  86. ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK );
  87. }
  88. } else {
  89. if ( $modulePath === null ) {
  90. $moduleName = 'unknown';
  91. } else {
  92. $i = strrpos( $modulePath, '+' );
  93. $moduleName = $i === false ? $modulePath : substr( $modulePath, $i + 1 );
  94. }
  95. // Don't add duplicate warnings
  96. $tag .= 's';
  97. $path = [ $tag, $moduleName ];
  98. $oldWarning = $this->result->getResultData( [ $tag, $moduleName, $tag ] );
  99. if ( $oldWarning !== null ) {
  100. $warnPos = strpos( $oldWarning, $value );
  101. // If $value was found in $oldWarning, check if it starts at 0 or after "\n"
  102. if ( $warnPos !== false && ( $warnPos === 0 || $oldWarning[$warnPos - 1] === "\n" ) ) {
  103. // Check if $value is followed by "\n" or the end of the $oldWarning
  104. $warnPos += strlen( $value );
  105. if ( strlen( $oldWarning ) <= $warnPos || $oldWarning[$warnPos] === "\n" ) {
  106. return;
  107. }
  108. }
  109. // If there is a warning already, append it to the existing one
  110. $value = "$oldWarning\n$value";
  111. }
  112. $this->result->addContentValue( $path, $tag, $value,
  113. ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK );
  114. }
  115. }
  116. }