MediaWiki  master
ApiUsageException.php
Go to the documentation of this file.
1 <?php
29 
30  protected $modulePath;
31  protected $status;
32 
38  public function __construct(
39  ?ApiBase $module, StatusValue $status, $httpCode = 0
40  ) {
41  if ( $status->isOK() ) {
42  throw new InvalidArgumentException( __METHOD__ . ' requires a fatal Status' );
43  }
44 
45  $this->modulePath = $module ? $module->getModulePath() : null;
46  $this->status = $status;
47 
48  // Bug T46111: Messages in the log files should be in English and not
49  // customized by the local wiki.
50  $enMsg = clone $this->getApiMessage();
51  $enMsg->inLanguage( 'en' )->useDatabase( false );
52  parent::__construct( ApiErrorFormatter::stripMarkup( $enMsg->text() ), $httpCode );
53  }
54 
63  public static function newWithMessage(
64  ?ApiBase $module, $msg, $code = null, $data = null, $httpCode = 0
65  ) {
66  return new static(
67  $module,
68  StatusValue::newFatal( ApiMessage::create( $msg, $code, $data ) ),
69  $httpCode
70  );
71  }
72 
76  private function getApiMessage() {
77  $errors = $this->status->getErrorsByType( 'error' );
78  if ( !$errors ) {
79  $errors = $this->status->getErrors();
80  }
81  if ( !$errors ) {
82  $msg = new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
83  } else {
84  $msg = ApiMessage::create( $errors[0] );
85  }
86  return $msg;
87  }
88 
93  public function getModulePath() {
94  return $this->modulePath;
95  }
96 
101  public function getStatusValue() {
102  return $this->status;
103  }
104 
108  public function getMessageObject() {
109  return Status::wrap( $this->status )->getMessage();
110  }
111 
115  public function __toString() {
116  $enMsg = clone $this->getApiMessage();
117  $enMsg->inLanguage( 'en' )->useDatabase( false );
118  $text = ApiErrorFormatter::stripMarkup( $enMsg->text() );
119 
120  return get_class( $this ) . ": {$enMsg->getApiCode()}: {$text} "
121  . "in {$this->getFile()}:{$this->getLine()}\n"
122  . "Stack trace:\n{$this->getTraceAsString()}";
123  }
124 
125 }
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
static stripMarkup( $text)
Turn wikitext into something resembling plaintext.
Exception used to abort API execution with an error.
getModulePath()
Fetch the responsible module name.
getMessageObject()
Return a Message object for this exception.Message
static newWithMessage(?ApiBase $module, $msg, $code=null, $data=null, $httpCode=0)
getModulePath()
Get the path to this module.
Definition: ApiBase.php:584
__construct(?ApiBase $module, StatusValue $status, $httpCode=0)
Extension of Message implementing IApiMessage.
Definition: ApiMessage.php:26
isOK()
Returns whether the operation completed.
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:55
This abstract class implements many basic API functions, and is the base of all API classes...
Definition: ApiBase.php:42
getStatusValue()
Fetch the error status.
Interface for MediaWiki-localized exceptions.
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Definition: ApiMessage.php:40