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