MediaWiki REL1_30
ApiMessage.php
Go to the documentation of this file.
1<?php
35interface IApiMessage extends MessageSpecifier {
45 public function getApiCode();
46
51 public function getApiData();
52
58 public function setApiCode( $code, array $data = null );
59
64 public function setApiData( array $data );
65}
66
73
78 protected static $messageMap = [
79 'actionthrottledtext' => 'ratelimited',
80 'autoblockedtext' => 'autoblocked',
81 'badaccess-group0' => 'permissiondenied',
82 'badaccess-groups' => 'permissiondenied',
83 'badipaddress' => 'invalidip',
84 'blankpage' => 'emptypage',
85 'blockedtext' => 'blocked',
86 'cannotdelete' => 'cantdelete',
87 'cannotundelete' => 'cantundelete',
88 'cantmove-titleprotected' => 'protectedtitle',
89 'cantrollback' => 'onlyauthor',
90 'confirmedittext' => 'confirmemail',
91 'content-not-allowed-here' => 'contentnotallowedhere',
92 'deleteprotected' => 'cantedit',
93 'delete-toobig' => 'bigdelete',
94 'edit-conflict' => 'editconflict',
95 'imagenocrossnamespace' => 'nonfilenamespace',
96 'imagetypemismatch' => 'filetypemismatch',
97 'importbadinterwiki' => 'badinterwiki',
98 'importcantopen' => 'cantopenfile',
99 'import-noarticle' => 'badinterwiki',
100 'importnofile' => 'nofile',
101 'importuploaderrorpartial' => 'partialupload',
102 'importuploaderrorsize' => 'filetoobig',
103 'importuploaderrortemp' => 'notempdir',
104 'ipb_already_blocked' => 'alreadyblocked',
105 'ipb_blocked_as_range' => 'blockedasrange',
106 'ipb_cant_unblock' => 'cantunblock',
107 'ipb_expiry_invalid' => 'invalidexpiry',
108 'ip_range_invalid' => 'invalidrange',
109 'mailnologin' => 'cantsend',
110 'markedaspatrollederror-noautopatrol' => 'noautopatrol',
111 'movenologintext' => 'cantmove-anon',
112 'movenotallowed' => 'cantmove',
113 'movenotallowedfile' => 'cantmovefile',
114 'namespaceprotected' => 'protectednamespace',
115 'nocreate-loggedin' => 'cantcreate',
116 'nocreatetext' => 'cantcreate-anon',
117 'noname' => 'invaliduser',
118 'nosuchusershort' => 'nosuchuser',
119 'notanarticle' => 'missingtitle',
120 'nouserspecified' => 'invaliduser',
121 'ns-specialprotected' => 'unsupportednamespace',
122 'protect-cantedit' => 'cantedit',
123 'protectedinterface' => 'protectednamespace-interface',
124 'protectedpagetext' => 'protectedpage',
125 'range_block_disabled' => 'rangedisabled',
126 'rcpatroldisabled' => 'patroldisabled',
127 'readonlytext' => 'readonly',
128 'sessionfailure' => 'badtoken',
129 'systemblockedtext' => 'blocked',
130 'titleprotected' => 'protectedtitle',
131 'undo-failure' => 'undofailure',
132 'userrights-nodatabase' => 'nosuchdatabase',
133 'userrights-no-interwiki' => 'nointerwikiuserrights',
134 ];
135
136 protected $apiCode = null;
137 protected $apiData = [];
138
139 public function getApiCode() {
140 if ( $this->apiCode === null ) {
141 $key = $this->getKey();
142 if ( isset( self::$messageMap[$key] ) ) {
143 $this->apiCode = self::$messageMap[$key];
144 } elseif ( $key === 'apierror-missingparam' ) {
146 $this->apiCode = 'no' . $this->getParams()[0];
147 } elseif ( substr( $key, 0, 8 ) === 'apiwarn-' ) {
148 $this->apiCode = substr( $key, 8 );
149 } elseif ( substr( $key, 0, 9 ) === 'apierror-' ) {
150 $this->apiCode = substr( $key, 9 );
151 } else {
152 $this->apiCode = $key;
153 }
154 }
155 return $this->apiCode;
156 }
157
158 public function setApiCode( $code, array $data = null ) {
159 if ( $code !== null && !( is_string( $code ) && $code !== '' ) ) {
160 throw new InvalidArgumentException( "Invalid code \"$code\"" );
161 }
162
163 $this->apiCode = $code;
164 if ( $data !== null ) {
165 $this->setApiData( $data );
166 }
167 }
168
169 public function getApiData() {
170 return $this->apiData;
171 }
172
173 public function setApiData( array $data ) {
174 $this->apiData = $data;
175 }
176
177 public function serialize() {
178 return serialize( [
179 'parent' => parent::serialize(),
180 'apiCode' => $this->apiCode,
181 'apiData' => $this->apiData,
182 ] );
183 }
184
185 public function unserialize( $serialized ) {
186 $data = unserialize( $serialized );
187 parent::unserialize( $data['parent'] );
188 $this->apiCode = $data['apiCode'];
189 $this->apiData = $data['apiData'];
190 }
191}
192
198class ApiMessage extends Message implements IApiMessage {
199 use ApiMessageTrait;
200
212 public static function create( $msg, $code = null, array $data = null ) {
213 if ( is_array( $msg ) ) {
214 // From StatusValue
215 if ( isset( $msg['message'] ) ) {
216 if ( isset( $msg['params'] ) ) {
217 $msg = array_merge( [ $msg['message'] ], $msg['params'] );
218 } else {
219 $msg = [ $msg['message'] ];
220 }
221 }
222
223 // Weirdness that comes in sometimes, including the above
224 if ( $msg[0] instanceof MessageSpecifier ) {
225 $msg = $msg[0];
226 }
227 }
228
229 if ( $msg instanceof IApiMessage ) {
230 return $msg;
231 } elseif ( $msg instanceof RawMessage ) {
232 return new ApiRawMessage( $msg, $code, $data );
233 } else {
234 return new ApiMessage( $msg, $code, $data );
235 }
236 }
237
246 public function __construct( $msg, $code = null, array $data = null ) {
247 if ( $msg instanceof Message ) {
248 foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
249 if ( isset( $msg->$key ) ) {
250 $this->$key = $msg->$key;
251 }
252 }
253 } elseif ( is_array( $msg ) ) {
254 $key = array_shift( $msg );
255 parent::__construct( $key, $msg );
256 } else {
257 parent::__construct( $msg );
258 }
259 $this->setApiCode( $code, $data );
260 }
261}
262
268class ApiRawMessage extends RawMessage implements IApiMessage {
269 use ApiMessageTrait;
270
279 public function __construct( $msg, $code = null, array $data = null ) {
280 if ( $msg instanceof RawMessage ) {
281 foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
282 if ( isset( $msg->$key ) ) {
283 $this->$key = $msg->$key;
284 }
285 }
286 } elseif ( is_array( $msg ) ) {
287 $key = array_shift( $msg );
288 parent::__construct( $key, $msg );
289 } else {
290 parent::__construct( $msg );
291 }
292 $this->setApiCode( $code, $data );
293 }
294}
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:863
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