MediaWiki REL1_31
ApiMessage.php
Go to the documentation of this file.
1<?php
42interface IApiMessage extends MessageSpecifier {
52 public function getApiCode();
53
58 public function getApiData();
59
65 public function setApiCode( $code, array $data = null );
66
71 public function setApiData( array $data );
72}
73
80
85 protected static $messageMap = [
86 'actionthrottledtext' => 'ratelimited',
87 'autoblockedtext' => 'autoblocked',
88 'badaccess-group0' => 'permissiondenied',
89 'badaccess-groups' => 'permissiondenied',
90 'badipaddress' => 'invalidip',
91 'blankpage' => 'emptypage',
92 'blockedtext' => 'blocked',
93 'cannotdelete' => 'cantdelete',
94 'cannotundelete' => 'cantundelete',
95 'cantmove-titleprotected' => 'protectedtitle',
96 'cantrollback' => 'onlyauthor',
97 'confirmedittext' => 'confirmemail',
98 'content-not-allowed-here' => 'contentnotallowedhere',
99 'deleteprotected' => 'cantedit',
100 'delete-toobig' => 'bigdelete',
101 'edit-conflict' => 'editconflict',
102 'imagenocrossnamespace' => 'nonfilenamespace',
103 'imagetypemismatch' => 'filetypemismatch',
104 'importbadinterwiki' => 'badinterwiki',
105 'importcantopen' => 'cantopenfile',
106 'import-noarticle' => 'badinterwiki',
107 'importnofile' => 'nofile',
108 'importuploaderrorpartial' => 'partialupload',
109 'importuploaderrorsize' => 'filetoobig',
110 'importuploaderrortemp' => 'notempdir',
111 'ipb_already_blocked' => 'alreadyblocked',
112 'ipb_blocked_as_range' => 'blockedasrange',
113 'ipb_cant_unblock' => 'cantunblock',
114 'ipb_expiry_invalid' => 'invalidexpiry',
115 'ip_range_invalid' => 'invalidrange',
116 'mailnologin' => 'cantsend',
117 'markedaspatrollederror-noautopatrol' => 'noautopatrol',
118 'movenologintext' => 'cantmove-anon',
119 'movenotallowed' => 'cantmove',
120 'movenotallowedfile' => 'cantmovefile',
121 'namespaceprotected' => 'protectednamespace',
122 'nocreate-loggedin' => 'cantcreate',
123 'nocreatetext' => 'cantcreate-anon',
124 'noname' => 'invaliduser',
125 'nosuchusershort' => 'nosuchuser',
126 'notanarticle' => 'missingtitle',
127 'nouserspecified' => 'invaliduser',
128 'ns-specialprotected' => 'unsupportednamespace',
129 'protect-cantedit' => 'cantedit',
130 'protectedinterface' => 'protectednamespace-interface',
131 'protectedpagetext' => 'protectedpage',
132 'range_block_disabled' => 'rangedisabled',
133 'rcpatroldisabled' => 'patroldisabled',
134 'readonlytext' => 'readonly',
135 'sessionfailure' => 'badtoken',
136 'systemblockedtext' => 'blocked',
137 'titleprotected' => 'protectedtitle',
138 'undo-failure' => 'undofailure',
139 'userrights-nodatabase' => 'nosuchdatabase',
140 'userrights-no-interwiki' => 'nointerwikiuserrights',
141 ];
142
143 protected $apiCode = null;
144 protected $apiData = [];
145
146 public function getApiCode() {
147 if ( $this->apiCode === null ) {
148 $key = $this->getKey();
149 if ( isset( self::$messageMap[$key] ) ) {
150 $this->apiCode = self::$messageMap[$key];
151 } elseif ( $key === 'apierror-missingparam' ) {
153 $this->apiCode = 'no' . $this->getParams()[0];
154 } elseif ( substr( $key, 0, 8 ) === 'apiwarn-' ) {
155 $this->apiCode = substr( $key, 8 );
156 } elseif ( substr( $key, 0, 9 ) === 'apierror-' ) {
157 $this->apiCode = substr( $key, 9 );
158 } else {
159 $this->apiCode = $key;
160 }
161 }
162 return $this->apiCode;
163 }
164
165 public function setApiCode( $code, array $data = null ) {
166 if ( $code !== null && !( is_string( $code ) && $code !== '' ) ) {
167 throw new InvalidArgumentException( "Invalid code \"$code\"" );
168 }
169
170 $this->apiCode = $code;
171 if ( $data !== null ) {
172 $this->setApiData( $data );
173 }
174 }
175
176 public function getApiData() {
177 return $this->apiData;
178 }
179
180 public function setApiData( array $data ) {
181 $this->apiData = $data;
182 }
183
184 public function serialize() {
185 return serialize( [
186 'parent' => parent::serialize(),
187 'apiCode' => $this->apiCode,
188 'apiData' => $this->apiData,
189 ] );
190 }
191
192 public function unserialize( $serialized ) {
193 $data = unserialize( $serialized );
194 parent::unserialize( $data['parent'] );
195 $this->apiCode = $data['apiCode'];
196 $this->apiData = $data['apiData'];
197 }
198}
199
205class ApiMessage extends Message implements IApiMessage {
206 use ApiMessageTrait;
207
219 public static function create( $msg, $code = null, array $data = null ) {
220 if ( is_array( $msg ) ) {
221 // From StatusValue
222 if ( isset( $msg['message'] ) ) {
223 if ( isset( $msg['params'] ) ) {
224 $msg = array_merge( [ $msg['message'] ], $msg['params'] );
225 } else {
226 $msg = [ $msg['message'] ];
227 }
228 }
229
230 // Weirdness that comes in sometimes, including the above
231 if ( $msg[0] instanceof MessageSpecifier ) {
232 $msg = $msg[0];
233 }
234 }
235
236 if ( $msg instanceof IApiMessage ) {
237 return $msg;
238 } elseif ( $msg instanceof RawMessage ) {
239 return new ApiRawMessage( $msg, $code, $data );
240 } else {
241 return new ApiMessage( $msg, $code, $data );
242 }
243 }
244
253 public function __construct( $msg, $code = null, array $data = null ) {
254 if ( $msg instanceof Message ) {
255 foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
256 if ( isset( $msg->$key ) ) {
257 $this->$key = $msg->$key;
258 }
259 }
260 } elseif ( is_array( $msg ) ) {
261 $key = array_shift( $msg );
262 parent::__construct( $key, $msg );
263 } else {
264 parent::__construct( $msg );
265 }
266 $this->setApiCode( $code, $data );
267 }
268}
269
275class ApiRawMessage extends RawMessage implements IApiMessage {
276 use ApiMessageTrait;
277
286 public function __construct( $msg, $code = null, array $data = null ) {
287 if ( $msg instanceof RawMessage ) {
288 foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
289 if ( isset( $msg->$key ) ) {
290 $this->$key = $msg->$key;
291 }
292 }
293 } elseif ( is_array( $msg ) ) {
294 $key = array_shift( $msg );
295 parent::__construct( $key, $msg );
296 } else {
297 parent::__construct( $msg );
298 }
299 $this->setApiCode( $code, $data );
300 }
301}
getApiCode()
serialize()
setApiData(array $data)
$apiCode
unserialize( $serialized)
$apiData
getApiData()
setApiCode( $code, array $data=null)
Extension of Message implementing IApiMessage.
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
__construct( $msg, $code=null, array $data=null)
Extension of RawMessage implementing IApiMessage.
__construct( $msg, $code=null, array $data=null)
The Message class provides methods which fulfil two basic services:
Definition Message.php:159
string $key
The message key.
Definition Message.php:201
Variant of the Message class.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition hooks.txt:865
trait ApiMessageTrait
Trait to implement the IApiMessage interface for Message subclasses.
Interface for messages with machine-readable data for use by the API.
setApiCode( $code, array $data=null)
Sets the machine-readable code for use by the API.
getApiCode()
Returns a machine-readable code for use by the API.
setApiData(array $data)
Sets additional machine-readable data about the error condition.
getApiData()
Returns additional machine-readable data about the error condition.
foreach( $res as $row) $serialized