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