Remove Revision::getRevisionText from ApiQueryDeletedrevs
[lhc/web/wiklou.git] / includes / api / ApiUsageException.php
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 /**
22 * Exception used to abort API execution with an error
23 *
24 * If possible, use ApiBase::dieWithError() instead of throwing this directly.
25 *
26 * @ingroup API
27 */
28 class ApiUsageException extends MWException implements ILocalizedException {
29
30 protected $modulePath;
31 protected $status;
32
33 /**
34 * @param ApiBase|null $module API module responsible for the error, if known
35 * @param StatusValue $status Status holding errors
36 * @param int $httpCode HTTP error code to use
37 */
38 public function __construct(
39 ApiBase $module = null, 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
55 /**
56 * @param ApiBase|null $module API module responsible for the error, if known
57 * @param string|array|Message $msg See ApiMessage::create()
58 * @param string|null $code See ApiMessage::create()
59 * @param array|null $data See ApiMessage::create()
60 * @param int $httpCode HTTP error code to use
61 * @return static
62 */
63 public static function newWithMessage(
64 ApiBase $module = null, $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
73 /**
74 * @return ApiMessage
75 */
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
89 /**
90 * Fetch the responsible module name
91 * @return string|null
92 */
93 public function getModulePath() {
94 return $this->modulePath;
95 }
96
97 /**
98 * Fetch the error status
99 * @return StatusValue
100 */
101 public function getStatusValue() {
102 return $this->status;
103 }
104
105 /**
106 * @inheritDoc
107 */
108 public function getMessageObject() {
109 return Status::wrap( $this->status )->getMessage();
110 }
111
112 /**
113 * @return string
114 */
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 }