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