Remove Revision::getRevisionText from ApiQueryDeletedrevs
[lhc/web/wiklou.git] / includes / api / ApiErrorFormatter_BackCompat.php
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 /**
22 * Format errors and warnings in the old style, for backwards compatibility.
23 * @since 1.25
24 * @deprecated Only for backwards compatibility, do not use
25 * @ingroup API
26 */
27 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
28 class ApiErrorFormatter_BackCompat extends ApiErrorFormatter {
29
30 /**
31 * @param ApiResult $result Into which data will be added
32 */
33 public function __construct( ApiResult $result ) {
34 parent::__construct( $result, Language::factory( 'en' ), 'none', false );
35 }
36
37 public function getFormat() {
38 return 'bc';
39 }
40
41 public function arrayFromStatus( StatusValue $status, $type = 'error', $format = null ) {
42 if ( $status->isGood() || !$status->getErrors() ) {
43 return [];
44 }
45
46 $result = [];
47 foreach ( $status->getErrorsByType( $type ) as $error ) {
48 $msg = ApiMessage::create( $error );
49 $error = [
50 'message' => $msg->getKey(),
51 'params' => $msg->getParams(),
52 'code' => $msg->getApiCode(),
53 ] + $error;
54 ApiResult::setIndexedTagName( $error['params'], 'param' );
55 $result[] = $error;
56 }
57 ApiResult::setIndexedTagName( $result, $type );
58
59 return $result;
60 }
61
62 protected function formatMessageInternal( $msg, $format ) {
63 return [
64 'code' => $msg->getApiCode(),
65 'info' => $msg->text(),
66 ] + $msg->getApiData();
67 }
68
69 /**
70 * Format an exception as an array
71 * @since 1.29
72 * @param Exception|Throwable $exception
73 * @param array $options See parent::formatException(), plus
74 * - bc: (bool) Return only the string, not an array
75 * @return array|string
76 */
77 public function formatException( $exception, array $options = [] ) {
78 $ret = parent::formatException( $exception, $options );
79 return empty( $options['bc'] ) ? $ret : $ret['info'];
80 }
81
82 protected function addWarningOrError( $tag, $modulePath, $msg ) {
83 $value = self::stripMarkup( $msg->text() );
84
85 if ( $tag === 'error' ) {
86 // In BC mode, only one error
87 $existingError = $this->result->getResultData( [ 'error' ] );
88 if ( !is_array( $existingError ) ||
89 !isset( $existingError['code'] ) || !isset( $existingError['info'] )
90 ) {
91 $value = [
92 'code' => $msg->getApiCode(),
93 'info' => $value,
94 ] + $msg->getApiData();
95 $this->result->addValue( null, 'error', $value,
96 ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK );
97 }
98 } else {
99 if ( $modulePath === null ) {
100 $moduleName = 'unknown';
101 } else {
102 $i = strrpos( $modulePath, '+' );
103 $moduleName = $i === false ? $modulePath : substr( $modulePath, $i + 1 );
104 }
105
106 // Don't add duplicate warnings
107 $tag .= 's';
108 $path = [ $tag, $moduleName ];
109 $oldWarning = $this->result->getResultData( [ $tag, $moduleName, $tag ] );
110 if ( $oldWarning !== null ) {
111 $warnPos = strpos( $oldWarning, $value );
112 // If $value was found in $oldWarning, check if it starts at 0 or after "\n"
113 if ( $warnPos !== false && ( $warnPos === 0 || $oldWarning[$warnPos - 1] === "\n" ) ) {
114 // Check if $value is followed by "\n" or the end of the $oldWarning
115 $warnPos += strlen( $value );
116 if ( strlen( $oldWarning ) <= $warnPos || $oldWarning[$warnPos] === "\n" ) {
117 return;
118 }
119 }
120 // If there is a warning already, append it to the existing one
121 $value = "$oldWarning\n$value";
122 }
123 $this->result->addContentValue( $path, $tag, $value,
124 ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK );
125 }
126 }
127 }