| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Api; |
| 22 |
|
| 23 |
use ILocalizedException; |
| 24 |
use InvalidArgumentException; |
| 25 |
use MediaWiki\Status\Status; |
| 26 |
use MWException; |
| 27 |
use StatusValue; |
| 28 |
use Stringable; |
| 29 |
use Throwable; |
| 30 |
use Wikimedia\Message\MessageSpecifier; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class ApiUsageException extends MWException implements Stringable, ILocalizedException { |
| 41 |
|
| 42 |
|
| 43 |
protected $modulePath; |
| 44 |
|
| 45 |
protected $status; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function __construct( |
| 56 |
?ApiBase $module, StatusValue $status, $httpCode = 0, ?Throwable $previous = null |
| 57 |
) { |
| 58 |
if ( $status->isOK() ) { |
| 59 |
throw new InvalidArgumentException( __METHOD__ . ' requires a fatal Status' ); |
| 60 |
} |
| 61 |
|
| 62 |
$this->modulePath = $module ? $module->getModulePath() : null; |
| 63 |
$this->status = $status; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
$enMsg = clone $this->getApiMessage(); |
| 68 |
$enMsg->inLanguage( 'en' )->useDatabase( false ); |
| 69 |
parent::__construct( ApiErrorFormatter::stripMarkup( $enMsg->text() ), $httpCode, $previous ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public static function newWithMessage( |
| 82 |
?ApiBase $module, $msg, $code = null, $data = null, $httpCode = 0, ?Throwable $previous = null |
| 83 |
) { |
| 84 |
return new static( |
| 85 |
$module, |
| 86 |
StatusValue::newFatal( ApiMessage::create( $msg, $code, $data ) ), |
| 87 |
$httpCode, |
| 88 |
$previous |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
private function getApiMessage() { |
| 96 |
|
| 97 |
foreach ( $this->status->getMessages( 'error' ) as $msg ) { |
| 98 |
|
| 99 |
return ApiMessage::create( $msg ); |
| 100 |
} |
| 101 |
foreach ( $this->status->getMessages( 'warning' ) as $msg ) { |
| 102 |
|
| 103 |
return ApiMessage::create( $msg ); |
| 104 |
} |
| 105 |
return new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' ); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function getModulePath() { |
| 113 |
return $this->modulePath; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function getStatusValue() { |
| 121 |
return $this->status; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function getMessageObject() { |
| 128 |
return Status::wrap( $this->status )->getMessage(); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public function __toString() { |
| 135 |
$enMsg = clone $this->getApiMessage(); |
| 136 |
$enMsg->inLanguage( 'en' )->useDatabase( false ); |
| 137 |
$text = ApiErrorFormatter::stripMarkup( $enMsg->text() ); |
| 138 |
|
| 139 |
return get_class( $this ) . ": {$enMsg->getApiCode()}: {$text} " |
| 140 |
. "in {$this->getFile()}:{$this->getLine()}\n" |
| 141 |
. "Stack trace:\n{$this->getTraceAsString()}" |
| 142 |
. ( $this->getPrevious() ? "\n\nNext {$this->getPrevious()}" : "" ); |
| 143 |
} |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
class_alias( ApiUsageException::class, 'ApiUsageException' ); |