MediaWiki  1.30.0
ApiMessage.php
Go to the documentation of this file.
1 <?php
35 interface 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 
198 class ApiMessage extends Message implements IApiMessage {
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 
268 class ApiRawMessage extends RawMessage implements IApiMessage {
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 }
IApiMessage\getApiData
getApiData()
Returns additional machine-readable data about the error condition.
$apiCode
$apiCode
Definition: ApiMessage.php:136
MessageSpecifier
Definition: MessageSpecifier.php:21
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$serialized
foreach( $res as $row) $serialized
Definition: testCompression.php:79
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:185
serialize
serialize()
Definition: ApiMessage.php:177
IApiMessage\getApiCode
getApiCode()
Returns a machine-readable code for use by the API.
ApiMessage\__construct
__construct( $msg, $code=null, array $data=null)
Definition: ApiMessage.php:246
getApiCode
getApiCode()
Definition: ApiMessage.php:139
IApiMessage
Interface for messages with machine-readable data for use by the API.
Definition: ApiMessage.php:35
setApiData
setApiData(array $data)
Definition: ApiMessage.php:173
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ApiRawMessage
Extension of RawMessage implementing IApiMessage.
Definition: ApiMessage.php:268
$apiData
$apiData
Definition: ApiMessage.php:137
ApiMessage
Extension of Message implementing IApiMessage.
Definition: ApiMessage.php:198
getApiData
getApiData()
Definition: ApiMessage.php:169
ApiRawMessage\__construct
__construct( $msg, $code=null, array $data=null)
Definition: ApiMessage.php:279
IApiMessage\setApiData
setApiData(array $data)
Sets additional machine-readable data about the error condition.
ApiMessage\create
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Definition: ApiMessage.php:212
IApiMessage\setApiCode
setApiCode( $code, array $data=null)
Sets the machine-readable code for use by the API.
$value
$value
Definition: styleTest.css.php:45
setApiCode
setApiCode( $code, array $data=null)
Definition: ApiMessage.php:158
ApiMessageTrait
trait ApiMessageTrait
Trait to implement the IApiMessage interface for Message subclasses.
Definition: ApiMessage.php:72
$code
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:781
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
RawMessage
Variant of the Message class.
Definition: RawMessage.php:34
array
the array() calling protocol came about after MediaWiki 1.4rc1.