Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
26.32% |
5 / 19 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
MWException | |
27.78% |
5 / 18 |
|
33.33% |
2 / 6 |
66.25 | |
0.00% |
0 / 1 |
isLoggable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
useMessageCache | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
4.25 | |||
msg | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
report | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
hasOverriddenHandler | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isCommandLine | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
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 | namespace MediaWiki\Exception; |
22 | |
23 | use Exception; |
24 | use LocalisationCache; |
25 | use MediaWiki\Debug\MWDebug; |
26 | use Wikimedia\Message\MessageParam; |
27 | use Wikimedia\Message\MessageSpecifier; |
28 | |
29 | /** |
30 | * MediaWiki exception |
31 | * |
32 | * @newable |
33 | * @stable to extend |
34 | * |
35 | * @ingroup Exception |
36 | * @deprecated since 1.40, use native exceptions instead (either directly, or defining subclasses when appropriate) |
37 | */ |
38 | class MWException extends Exception { |
39 | |
40 | /** |
41 | * Whether to log this exception in the exception debug log. |
42 | * |
43 | * @stable to override |
44 | * |
45 | * @since 1.23 |
46 | * @return bool |
47 | */ |
48 | public function isLoggable() { |
49 | return true; |
50 | } |
51 | |
52 | /** |
53 | * Can the extension use the Message class/wfMessage to get i18n-ed messages? |
54 | * |
55 | * @stable to override |
56 | * |
57 | * @return bool |
58 | */ |
59 | public function useMessageCache() { |
60 | foreach ( $this->getTrace() as $frame ) { |
61 | if ( isset( $frame['class'] ) && $frame['class'] === LocalisationCache::class ) { |
62 | return false; |
63 | } |
64 | } |
65 | return true; |
66 | } |
67 | |
68 | /** |
69 | * Get a message from i18n |
70 | * |
71 | * @param string $key Message name |
72 | * @param string $fallback Default message if the message cache can't be |
73 | * called by the exception |
74 | * @phpcs:ignore Generic.Files.LineLength |
75 | * @param MessageParam|MessageSpecifier|string|int|float|list<MessageParam|MessageSpecifier|string|int|float> ...$params |
76 | * See Message::params() |
77 | * @return string Message with arguments replaced |
78 | */ |
79 | public function msg( $key, $fallback, ...$params ) { |
80 | // NOTE: Keep logic in sync with MWExceptionRenderer::msg. |
81 | $res = false; |
82 | if ( $this->useMessageCache() ) { |
83 | try { |
84 | $res = wfMessage( $key, ...$params )->text(); |
85 | } catch ( Exception $e ) { |
86 | } |
87 | } |
88 | if ( $res === false ) { |
89 | // Fallback to static message text and generic sitename. |
90 | // Avoid live config as this must work before Setup/MediaWikiServices finish. |
91 | $res = wfMsgReplaceArgs( $fallback, $params ); |
92 | $res = strtr( $res, [ |
93 | '{{SITENAME}}' => 'MediaWiki', |
94 | ] ); |
95 | } |
96 | return $res; |
97 | } |
98 | |
99 | /** |
100 | * Output a report about the exception and takes care of formatting. |
101 | * It will be either HTML or plain text based on isCommandLine(). |
102 | * |
103 | * @stable to override |
104 | */ |
105 | public function report() { |
106 | // May be overridden by subclasses to replace the whole error page |
107 | MWExceptionRenderer::output( $this, MWExceptionRenderer::AS_PRETTY ); |
108 | } |
109 | |
110 | /** |
111 | * @internal |
112 | */ |
113 | final public function hasOverriddenHandler(): bool { |
114 | return MWDebug::detectDeprecatedOverride( $this, __CLASS__, 'report' ); |
115 | } |
116 | |
117 | /** |
118 | * Check whether we are in command line mode or not to report the exception |
119 | * in the correct format. |
120 | * |
121 | * @return bool |
122 | */ |
123 | public static function isCommandLine() { |
124 | return MW_ENTRY_POINT === 'cli'; |
125 | } |
126 | |
127 | } |
128 | |
129 | /** @deprecated class alias since 1.44 */ |
130 | class_alias( MWException::class, 'MWException' ); |